-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodesyntaxhighlighter.cpp
More file actions
86 lines (75 loc) · 2.69 KB
/
Copy pathcodesyntaxhighlighter.cpp
File metadata and controls
86 lines (75 loc) · 2.69 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "codesyntaxhighlighter.h"
CodeSyntaxHighlighter::CodeSyntaxHighlighter(QTextDocument *parent)
: QSyntaxHighlighter{parent}
{
// Gruvbox theme
// keyword: blue
QTextCharFormat keywordFormat;
keywordFormat.setForeground(QColor("#83a598"));
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
keywordPatterns << "\\bif\\b"
<< "\\belse\\b"
<< "\\bfor\\b"
<< "\\bwhile\\b"
<< "\\bdo\\b"
<< "\\bswitch\\b"
<< "\\bcase\\b"
<< "\\bbreak\\b"
<< "\\bcontinue\\b"
<< "\\breturn\\b"
<< "\\bclass\\b"
<< "\\bstruct\\b"
<< "\\benum\\b"
<< "\\bpublic\\b"
<< "\\bprivate\\b"
<< "\\bprotected\\b"
<< "\\btemplate\\b"
<< "\\btypename\\b"
<< "\\btry\\b"
<< "\\bcatch\\b"
<< "\\bnamespace\\b";
for (const QString &pattern : keywordPatterns)
{
HighlightingRule rule;
rule.pattern = QRegularExpression(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
// String: green
QTextCharFormat stringFormat;
stringFormat.setForeground(QColor("#b8bb26"));
HighlightingRule stringRule;
stringRule.pattern = QRegularExpression("\".*?\"");
stringRule.format = stringFormat;
highlightingRules.append(stringRule);
// Comment: gray
QTextCharFormat commentFormat;
commentFormat.setForeground(QColor("#928374"));
HighlightingRule commentRule;
commentRule.pattern = QRegularExpression("#[^\n]*");
commentRule.format = commentFormat;
highlightingRules.append(commentRule);
// Number: purple
QTextCharFormat numberFormat;
numberFormat.setForeground(QColor("#d3869b"));
HighlightingRule numberRule;
numberRule.pattern = QRegularExpression("\\b[0-9]+(\\.[0-9]+)?\\b");
numberRule.format = numberFormat;
highlightingRules.append(numberRule);
}
CodeSyntaxHighlighter::~CodeSyntaxHighlighter()
{
}
void CodeSyntaxHighlighter::highlightBlock(const QString &text){
// Trying to match all of rules
for (const HighlightingRule &rule : std::as_const(highlightingRules))
{
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext())
{
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
}