forked from AngryFender/Remini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlighter.cpp
More file actions
184 lines (158 loc) · 6.54 KB
/
Copy pathhighlighter.cpp
File metadata and controls
184 lines (158 loc) · 6.54 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "highlighter.h"
#include <utility> // std::as_const
Highlighter::Highlighter(QObject *parent)
: QSyntaxHighlighter(parent), initialised(false)
{
}
void Highlighter::highlightBlock(const QString &text)
{
if (!initialised)
return;
for (const HighlightingRule &rule : std::as_const(highlightingRules))
{
QRegularExpressionMatchIterator matchIterator =
rule.pattern.globalMatch(text);
while (matchIterator.hasNext())
{
const QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(),
rule.format);
}
}
setCurrentBlockState(0);
// Multi-line /* ... */ comments.
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = text.indexOf(commentStartExpression);
while (startIndex >= 0)
{
const QRegularExpressionMatch match =
commentEndExpression.match(text, startIndex);
const int endIndex = match.capturedStart();
int commentLength = 0;
if (endIndex == -1)
{
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
}
else
{
commentLength = endIndex - startIndex + match.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex =
text.indexOf(commentStartExpression, startIndex + commentLength);
}
// Search highlight. Skip when the pattern is empty / invalid, otherwise
// an empty regex would match every position and paint the whole document.
if (!searchMatchExpression.pattern().isEmpty() &&
searchMatchExpression.isValid())
{
QRegularExpressionMatchIterator searchMatchIterator =
searchMatchExpression.globalMatch(text);
while (searchMatchIterator.hasNext())
{
const QRegularExpressionMatch match = searchMatchIterator.next();
if (match.capturedLength() == 0)
continue;
setFormat(match.capturedStart(), match.capturedLength(),
searchMatchFormat);
}
}
}
void Highlighter::initColors()
{
// Clear previous rules so re-initialisation (theme change) does not
// keep appending duplicate rules.
highlightingRules.clear();
HighlightingRule rule;
keywordFormat.setForeground(syntaxColor.keyword);
keywordFormat.setFontWeight(QFont::Bold);
const QString keywordPatterns[] = {
QStringLiteral("\\bchar\\b"), QStringLiteral("\\bclass\\b"),
QStringLiteral("\\bconst\\b"), QStringLiteral("\\bdouble\\b"),
QStringLiteral("\\benum\\b"), QStringLiteral("\\bexplicit\\b"),
QStringLiteral("\\bfriend\\b"), QStringLiteral("\\binline\\b"),
QStringLiteral("\\bint\\b"), QStringLiteral("\\blong\\b"),
QStringLiteral("\\bnamespace\\b"), QStringLiteral("\\boperator\\b"),
QStringLiteral("\\bprivate\\b"), QStringLiteral("\\bprotected\\b"),
QStringLiteral("\\bpublic\\b"), QStringLiteral("\\bshort\\b"),
QStringLiteral("\\bsignals\\b"), QStringLiteral("\\bsigned\\b"),
QStringLiteral("\\bslots\\b"), QStringLiteral("\\bstatic\\b"),
QStringLiteral("\\bstruct\\b"), QStringLiteral("\\btemplate\\b"),
QStringLiteral("\\btypedef\\b"), QStringLiteral("\\btypename\\b"),
QStringLiteral("\\bunion\\b"), QStringLiteral("\\bunsigned\\b"),
QStringLiteral("\\bvirtual\\b"), QStringLiteral("\\bvoid\\b"),
QStringLiteral("\\bvolatile\\b"), QStringLiteral("\\bbool\\b"),
QStringLiteral("\\bcmake\\b")};
for (const QString &pattern : keywordPatterns)
{
rule.pattern = QRegularExpression(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
classFormat.setFontWeight(QFont::Bold);
classFormat.setForeground(syntaxColor.argument);
rule.pattern =
QRegularExpression(QStringLiteral("(?<=\\s)--?[a-zA-Z0-9_]+"));
rule.format = classFormat;
highlightingRules.append(rule);
tickMarkFormat.setForeground(QColor(60, 179, 113));
tickMarkFormat.setFontWeight(QFont::ExtraBold);
rule.pattern = QRegularExpression(QStringLiteral("(☑|-\\s\\[x]\\s)"));
rule.format = tickMarkFormat;
highlightingRules.append(rule);
unTickMarkFormat.setForeground(QColor(219, 112, 147));
unTickMarkFormat.setFontWeight(QFont::ExtraBold);
rule.pattern = QRegularExpression(QStringLiteral("(☐|-\\s\\[\\s\\]\\s)"));
rule.format = unTickMarkFormat;
highlightingRules.append(rule);
singleLineCommentFormat.setForeground(syntaxColor.comment);
rule.pattern = QRegularExpression(QStringLiteral("//[^\n]*"));
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(syntaxColor.comment);
quotationFormat.setForeground(syntaxColor.quote);
rule.pattern = QRegularExpression(QStringLiteral("\"(.*?)\""));
rule.format = quotationFormat;
highlightingRules.append(rule);
functionFormat.setFontItalic(true);
functionFormat.setForeground(syntaxColor.method);
rule.pattern =
QRegularExpression(QStringLiteral("\\b[A-Za-z0-9_]+(?=\\()"));
rule.format = functionFormat;
highlightingRules.append(rule);
commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
regexCodeBlock.setPattern(QStringLiteral("^```+.*"));
searchMatchFormat.setBackground(syntaxColor.searchMatch);
initialised = true;
}
void Highlighter::syntaxColorUpdateHandler(HighlightColor &colors)
{
syntaxColor.argument = colors.argument;
syntaxColor.keyword = colors.keyword;
syntaxColor.method = colors.method;
syntaxColor.comment = colors.comment;
syntaxColor.quote = colors.quote;
syntaxColor.type = colors.type;
syntaxColor.searchMatch = colors.searchMatch;
// initColors() clears highlightingRules before re-populating,
// so repeated theme changes no longer duplicate rules.
initColors();
rehighlight();
}
void Highlighter::updateSearchText(const QString &text)
{
if (text.isEmpty())
{
// Clearing the search box must remove all search highlighting.
searchMatchExpression.setPattern(QString());
rehighlight();
return;
}
QRegularExpression re(QRegularExpression::escape(text),
QRegularExpression::CaseInsensitiveOption);
searchMatchExpression = re;
rehighlight();
}