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.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-08-15 22:23:57 +02:00
|
|
|
for (const auto &pattern : keywordPatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
|
|
|
rule.pattern.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
|
2015-04-22 19:33:53 +02:00
|
|
|
rule.format = m_keywordFormat;
|
|
|
|
m_highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
2018-08-15 22:23:57 +02:00
|
|
|
m_singleLineCommentFormat.setForeground(Qt::darkGray);
|
|
|
|
rule.pattern = QRegularExpression(QStringLiteral("//[^\n]*"));
|
2015-04-22 19:33:53 +02:00
|
|
|
rule.format = m_singleLineCommentFormat;
|
|
|
|
m_highlightingRules.append(rule);
|
|
|
|
|
2018-08-15 22:23:57 +02:00
|
|
|
m_multiLineCommentFormat.setForeground(Qt::darkGray);
|
2015-04-22 19:33:53 +02:00
|
|
|
|
2018-08-15 22:23:57 +02:00
|
|
|
m_regexFormat.setForeground(Qt::darkMagenta);
|
|
|
|
rule.pattern = QRegularExpression(QStringLiteral("/.*/"));
|
|
|
|
rule.pattern.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
|
|
|
|
rule.format = m_regexFormat;
|
2015-04-22 19:33:53 +02:00
|
|
|
m_highlightingRules.append(rule);
|
|
|
|
|
2018-08-15 22:23:57 +02:00
|
|
|
m_stringFormat.setForeground(Qt::darkGreen);
|
|
|
|
rule.pattern = QRegularExpression(QStringLiteral("\".*\""));
|
|
|
|
rule.pattern.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
|
|
|
|
rule.format = m_stringFormat;
|
|
|
|
m_highlightingRules.append(rule);
|
|
|
|
|
|
|
|
m_functionFormat.setForeground(Qt::darkBlue);
|
|
|
|
rule.pattern = QRegularExpression(QStringLiteral("(?!if)\\b[A-Za-z0-9_]+(?=\\()"));
|
|
|
|
rule.pattern.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
|
2015-04-22 19:33:53 +02:00
|
|
|
rule.format = m_functionFormat;
|
|
|
|
m_highlightingRules.append(rule);
|
|
|
|
|
2018-08-15 22:23:57 +02:00
|
|
|
m_commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
|
|
|
|
m_commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JavaScriptHighlighter::highlightBlock(const QString &text)
|
|
|
|
{
|
2018-08-15 22:23:57 +02:00
|
|
|
for (const auto &rule : m_highlightingRules) {
|
|
|
|
const auto &expression(rule.pattern);
|
|
|
|
auto match = expression.match(text);
|
|
|
|
while (match.hasMatch()) {
|
|
|
|
const auto index = match.capturedStart();
|
|
|
|
const auto length = match.capturedLength();
|
2015-04-22 19:33:53 +02:00
|
|
|
setFormat(index, length, rule.format);
|
2018-08-15 22:23:57 +02:00
|
|
|
match = expression.match(text, index + length);
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setCurrentBlockState(0);
|
|
|
|
|
2018-08-15 22:23:57 +02:00
|
|
|
auto startIndex = 0;
|
2016-08-05 01:48:36 +02:00
|
|
|
if (previousBlockState() != 1) {
|
2018-08-15 22:23:57 +02:00
|
|
|
startIndex = m_commentStartExpression.match(text).capturedStart();
|
2016-08-05 01:48:36 +02:00
|
|
|
}
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
while (startIndex >= 0) {
|
2018-08-15 22:23:57 +02:00
|
|
|
const auto endMatch = m_commentEndExpression.match(text, startIndex);
|
|
|
|
const auto endIndex = endMatch.capturedStart();
|
|
|
|
const auto commentLength = [&] {
|
|
|
|
if (endIndex >= 0) {
|
|
|
|
return endIndex - startIndex + endMatch.capturedLength();
|
|
|
|
}
|
2015-04-22 19:33:53 +02:00
|
|
|
setCurrentBlockState(1);
|
2018-08-15 22:23:57 +02:00
|
|
|
return text.length() - startIndex;
|
|
|
|
}();
|
2015-04-22 19:33:53 +02:00
|
|
|
setFormat(startIndex, commentLength, m_multiLineCommentFormat);
|
2018-08-15 22:23:57 +02:00
|
|
|
startIndex = m_commentStartExpression.match(text, startIndex + commentLength).capturedStart();
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:18:01 +01:00
|
|
|
} // namespace QtGui
|