rdr2view/uimod/JSHighlighter.cpp

100 lines
3.3 KiB
C++
Raw Normal View History

2019-11-10 00:12:44 +01:00
/*****************************************************************************
* gta5view Grand Theft Auto V Profile Viewer
2020-10-21 07:40:18 +02:00
* Copyright (C) 2017-2020 Syping
2019-11-10 00:12:44 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "JSHighlighter.h"
JSHighlighter::JSHighlighter(QTextDocument *parent) :
QSyntaxHighlighter(parent)
{
HighlightingRule rule;
QBrush keywordBrush(QColor::fromRgb(66, 137, 244));
keywordFormat.setForeground(keywordBrush);
keywordFormat.setFontItalic(true);
QStringList keywordPatterns;
keywordPatterns << "\\btrue\\b" << "\\bfalse\\b";
for (QString pattern : keywordPatterns)
{
2020-10-21 07:40:18 +02:00
#if QT_VERSION >= 0x050000
rule.pattern = QRegularExpression(pattern);
#else
2019-11-10 00:12:44 +01:00
rule.pattern = QRegExp(pattern);
2020-10-21 07:40:18 +02:00
#endif
2019-11-10 00:12:44 +01:00
rule.format = keywordFormat;
highlightingRules.append(rule);
}
QBrush doubleBrush(QColor::fromRgb(66, 137, 244));
doubleFormat.setForeground(doubleBrush);
2020-10-21 07:40:18 +02:00
#if QT_VERSION >= 0x050000
rule.pattern = QRegularExpression("[+-]?\\d*\\.?\\d+");
#else
2019-11-10 00:12:44 +01:00
rule.pattern = QRegExp("[+-]?\\d*\\.?\\d+");
2020-10-21 07:40:18 +02:00
#endif
2019-11-10 00:12:44 +01:00
rule.format = doubleFormat;
highlightingRules.append(rule);
QBrush quotationBrush(QColor::fromRgb(66, 244, 104));
quotationFormat.setForeground(quotationBrush);
2020-10-21 07:40:18 +02:00
#if QT_VERSION >= 0x050000
rule.pattern = QRegularExpression("\"[^\"]*\"");
#else
2019-11-10 00:12:44 +01:00
rule.pattern = QRegExp("\"[^\"]*\"");
2020-10-21 07:40:18 +02:00
#endif
2019-11-10 00:12:44 +01:00
rule.format = quotationFormat;
highlightingRules.append(rule);
QBrush objectBrush(QColor::fromRgb(255, 80, 80));
objectFormat.setForeground(objectBrush);
2020-10-21 07:40:18 +02:00
#if QT_VERSION >= 0x050000
rule.pattern = QRegularExpression("\"[^\"]*\"(?=:)");
#else
2019-11-10 00:12:44 +01:00
rule.pattern = QRegExp("\"[^\"]*\"(?=:)");
2020-10-21 07:40:18 +02:00
#endif
2019-11-10 00:12:44 +01:00
rule.format = objectFormat;
highlightingRules.append(rule);
}
void JSHighlighter::highlightBlock(const QString &text)
{
2020-10-21 07:40:18 +02:00
#if QT_VERSION >= 0x050000
for (const HighlightingRule &rule : qAsConst(highlightingRules))
#else
for (const HighlightingRule &rule : highlightingRules)
#endif
2019-11-10 00:12:44 +01:00
{
2020-10-21 07:40:18 +02:00
#if QT_VERSION >= 0x050000
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
#else
2019-11-10 00:12:44 +01: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);
}
2020-10-21 07:40:18 +02:00
#endif
2019-11-10 00:12:44 +01:00
}
setCurrentBlockState(0);
}