tageditor/gui/javascripthighlighter.cpp

77 lines
2.9 KiB
C++
Raw Normal View History

2015-09-06 20:20:00 +02:00
#include "./javascripthighlighter.h"
2015-04-22 19:33:53 +02:00
namespace QtGui {
JavaScriptHighlighter::JavaScriptHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
m_keywordFormat.setForeground(Qt::darkBlue);
m_keywordFormat.setFontWeight(QFont::Bold);
2018-03-18 18:34:28 +01:00
static const QStringList keywordPatterns{ QStringLiteral("\\bvar\\b"), QStringLiteral("\\bArray\\b"), QStringLiteral("\\bfunction\\b"),
QStringLiteral("\\breturn\\b"), QStringLiteral("\\barguments\\b"), QStringLiteral("\\bif\\b"), QStringLiteral("\\belse\\b"),
QStringLiteral("\\bfor\\b"), QStringLiteral("\\bswitch\\b"), QStringLiteral("\\bcase\\b"), QStringLiteral("\\bbreak\\b"),
QStringLiteral("\\bwhile\\b"), QStringLiteral("\\bundefined\\b"), QStringLiteral("\\continue\\b") };
2018-03-07 01:18:01 +01:00
for (const QString &pattern : keywordPatterns) {
2015-04-22 19:33:53 +02:00
rule.pattern = QRegExp(pattern);
rule.format = m_keywordFormat;
m_highlightingRules.append(rule);
}
m_singleLineCommentFormat.setForeground(Qt::red);
2016-08-05 01:48:36 +02:00
rule.pattern = QRegExp(QStringLiteral("//[^\n]*"));
2015-04-22 19:33:53 +02:00
rule.format = m_singleLineCommentFormat;
m_highlightingRules.append(rule);
m_multiLineCommentFormat.setForeground(Qt::red);
m_quotationFormat.setForeground(Qt::darkGreen);
2016-08-05 01:48:36 +02:00
rule.pattern = QRegExp(QStringLiteral("\".*\""));
2015-04-22 19:33:53 +02:00
rule.format = m_quotationFormat;
m_highlightingRules.append(rule);
m_functionFormat.setFontItalic(true);
m_functionFormat.setForeground(Qt::blue);
2016-08-05 01:48:36 +02:00
rule.pattern = QRegExp(QStringLiteral("(?!if)\\b[A-Za-z0-9_]+(?=\\()"));
2015-04-22 19:33:53 +02:00
rule.format = m_functionFormat;
m_highlightingRules.append(rule);
2016-08-05 01:48:36 +02:00
m_commentStartExpression = QRegExp(QStringLiteral("/\\*"));
m_commentEndExpression = QRegExp(QStringLiteral("\\*/"));
2015-04-22 19:33:53 +02:00
}
void JavaScriptHighlighter::highlightBlock(const QString &text)
{
2018-03-07 01:18:01 +01:00
for (const HighlightingRule &rule : m_highlightingRules) {
2015-04-22 19:33:53 +02:00
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
int startIndex = 0;
2016-08-05 01:48:36 +02:00
if (previousBlockState() != 1) {
2015-04-22 19:33:53 +02:00
startIndex = m_commentStartExpression.indexIn(text);
2016-08-05 01:48:36 +02:00
}
2015-04-22 19:33:53 +02:00
while (startIndex >= 0) {
int endIndex = m_commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
2016-08-05 01:48:36 +02:00
commentLength = endIndex - startIndex + m_commentEndExpression.matchedLength();
2015-04-22 19:33:53 +02:00
}
setFormat(startIndex, commentLength, m_multiLineCommentFormat);
startIndex = m_commentStartExpression.indexIn(text, startIndex + commentLength);
}
}
2018-03-07 01:18:01 +01:00
} // namespace QtGui