2015-09-06 20:20:00 +02:00
|
|
|
#include "./javascripthighlighter.h"
|
2015-04-22 19:33:53 +02:00
|
|
|
|
2023-07-23 22:17:47 +02:00
|
|
|
#include "../misc/utility.h"
|
|
|
|
|
2015-04-22 19:33:53 +02:00
|
|
|
namespace QtGui {
|
|
|
|
|
|
|
|
JavaScriptHighlighter::JavaScriptHighlighter(QTextDocument *parent)
|
|
|
|
: QSyntaxHighlighter(parent)
|
|
|
|
{
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
|
|
|
m_keywordFormat.setFontWeight(QFont::Bold);
|
2022-05-26 21:41:48 +02:00
|
|
|
static const QStringList keywordPatterns{ QStringLiteral("\\bvar\\b"), QStringLiteral("\\blet\\b"), QStringLiteral("\\bconst\\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("\\bnull\\b"), QStringLiteral("\\bcontinue\\b"), QStringLiteral("\\bfalse\\b"), QStringLiteral("\\btrue\\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()) {
|
2023-07-23 22:17:47 +02:00
|
|
|
const auto index = Utility::containerSizeToInt(match.capturedStart());
|
|
|
|
const auto length = Utility::containerSizeToInt(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) {
|
2023-07-23 22:17:47 +02:00
|
|
|
startIndex = Utility::containerSizeToInt(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);
|
2023-07-23 22:17:47 +02:00
|
|
|
const auto endIndex = Utility::containerSizeToInt(endMatch.capturedStart());
|
2018-08-15 22:23:57 +02:00
|
|
|
const auto commentLength = [&] {
|
|
|
|
if (endIndex >= 0) {
|
2023-07-23 22:17:47 +02:00
|
|
|
return endIndex - startIndex + Utility::containerSizeToInt(endMatch.capturedLength());
|
2018-08-15 22:23:57 +02:00
|
|
|
}
|
2015-04-22 19:33:53 +02:00
|
|
|
setCurrentBlockState(1);
|
2023-07-23 22:17:47 +02:00
|
|
|
return Utility::containerSizeToInt(text.length()) - startIndex;
|
2018-08-15 22:23:57 +02:00
|
|
|
}();
|
2015-04-22 19:33:53 +02:00
|
|
|
setFormat(startIndex, commentLength, m_multiLineCommentFormat);
|
2023-07-23 22:17:47 +02:00
|
|
|
startIndex = Utility::containerSizeToInt(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
|