-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCHighlighter.cpp
More file actions
83 lines (69 loc) · 3.26 KB
/
CHighlighter.cpp
File metadata and controls
83 lines (69 loc) · 3.26 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
#include "CHighlighter.h"
CHighlighter::CHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent) {
// 关键词
keywordFormat.setForeground(Qt::cyan);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywords = {
"auto", "bool", "break", "case", "catch", "char", "class", "const", "constexpr", "continue",
"default", "delete", "do", "double", "else", "enum", "explicit", "extern", "false", "float",
"for", "friend", "goto", "if", "inline", "int", "long", "mutable", "namespace", "new",
"nullptr", "operator", "private", "protected", "public", "register", "reinterpret_cast",
"return", "short", "signed", "sizeof", "static", "static_cast", "struct", "switch", "template",
"this", "throw", "true", "try", "typedef", "typename", "union", "unsigned", "using", "virtual",
"void", "volatile", "while"
};
for (const QString &word : keywords) {
rules.append({QRegularExpression("\\b" + word + "\\b"), keywordFormat});
}
// 单行注释
singleLineCommentFormat.setForeground(QColor("#6AA84F")); // 绿色
rules.append({QRegularExpression("//[^\n]*"), singleLineCommentFormat});
// 字符串
stringFormat.setForeground(QColor("#FFFF99")); // 黄色
rules.append({QRegularExpression(R"("([^"\\]|\\.)*")"), stringFormat});
// 字符常量
charFormat.setForeground(QColor("#FFCC66")); // 橙色
rules.append({QRegularExpression(R"('([^'\\]|\\.)')"), charFormat});
// 数字
numberFormat.setForeground(QColor("#FF9966")); // 橙色
rules.append({QRegularExpression(R"(\b\d+(\.\d+)?\b)"), numberFormat});
// include 语句
includeFormat.setForeground(QColor("#9999FF")); // 蓝紫
rules.append({QRegularExpression(R"(^\s*#\s*include\s*[<"].*[>"])"), includeFormat});
// 多行注释格式
multiLineCommentFormat.setForeground(QColor("#6AA84F")); // 绿色
multiLineCommentStart = QRegularExpression(R"(/\*)");
multiLineCommentEnd = QRegularExpression(R"(\*/)");
}
void CHighlighter::highlightBlock(const QString &text) {
// 应用正则规则
for (const HighlightRule &rule : qAsConst(rules)) {
QRegularExpressionMatchIterator it = rule.pattern.globalMatch(text);
while (it.hasNext()) {
auto match = it.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
// 多行注释处理
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1) {
QRegularExpressionMatch match = multiLineCommentStart.match(text);
startIndex = match.hasMatch() ? match.capturedStart() : -1;
}
while (startIndex >= 0) {
QRegularExpressionMatch endMatch = multiLineCommentEnd.match(text, startIndex);
int endIndex = endMatch.hasMatch() ? endMatch.capturedEnd() : -1;
int commentLength = 0;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex;
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
// 查找下一段注释
startIndex = multiLineCommentStart.match(text, startIndex + commentLength).capturedStart();
}
}