-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighlighter.cpp
More file actions
75 lines (66 loc) · 2.98 KB
/
Highlighter.cpp
File metadata and controls
75 lines (66 loc) · 2.98 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
#include "Highlighter.h"
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(Qt::magenta);
keywordFormat.setFontWeight(QFont::Bold);
const QString keywordPatterns[] = {
QStringLiteral("\\bchar\\b"), QStringLiteral("\\bconst\\b"),
QStringLiteral("\\bint\\b"), QStringLiteral("\\bsignal\\b"),
QStringLiteral("\\btypedef\\b"), QStringLiteral("\\btype\\b"),
QStringLiteral("\\bbool\\b"), QStringLiteral("\\bblock\\b"),
QStringLiteral("\\bbreak\\b"), QStringLiteral("\\bdup\\b"),
QStringLiteral("\\bdatalist\\b"), QStringLiteral("\\bdelaylist\\b"),
QStringLiteral("\\belse\\b"), QStringLiteral("\\berror\\b"),
QStringLiteral("\\bfalse\\b"), QStringLiteral("\\bfloat\\b"),
QStringLiteral("\\bfunc\\b"), QStringLiteral("\\bfuncdef\\b"),
QStringLiteral("\\bnil\\b"), QStringLiteral("\\bparlist\\b"),
QStringLiteral("\\bprefunc\\b"), QStringLiteral("\\breturn\\b"),
QStringLiteral("\\btrue\\b")
};
for (const QString &pattern : keywordPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
quotationFormat.setForeground(Qt::red);
rule.pattern = QRegularExpression(QStringLiteral("\".*\""));
rule.format = quotationFormat;
highlightingRules.append(rule);
singleLineCommentFormat.setForeground(Qt::green);
rule.pattern = QRegularExpression(QStringLiteral("//[^\n]*"));
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::darkGreen);
commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
}
void Highlighter::highlightBlock(const QString &text)
{
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);
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = text.indexOf(commentStartExpression);
while (startIndex >= 0) {
QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
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);
}
}