-
Notifications
You must be signed in to change notification settings - Fork 11
/
jsonsyntaxhighlighter.cpp
44 lines (34 loc) · 1.36 KB
/
jsonsyntaxhighlighter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "jsonsyntaxhighlighter.h"
#include "preferences/preferences.h"
JsonSyntaxHighlighter::JsonSyntaxHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
//keywordFormat.setForeground(Qt::white);
QStringList keywordPatterns;
keywordPatterns << "\"" << "\'";
Q_FOREACH (const QString &pattern, keywordPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
jsonKeyFormat.setForeground(P->syntaxKeywordColor);
jsonKeyFormat.setFontWeight(QFont::Bold);
rule.pattern = QRegularExpression("\".*\"(?=:)");
rule.format = jsonKeyFormat;
highlightingRules.append(rule);
jsonValueKeywordFormat.setForeground(P->syntaxValueColor);
rule.pattern = QRegularExpression("(?= ?)true|false|null(?= +|,)");
rule.format = jsonValueKeywordFormat;
highlightingRules.append(rule);
}
void JsonSyntaxHighlighter::highlightBlock(const QString &text)
{
Q_FOREACH (const HighlightingRule &rule, highlightingRules) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
}