-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXML_highlighter.cpp
45 lines (33 loc) · 1.38 KB
/
XML_highlighter.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
45
#include "XML_highlighter.h"
XML_Highlighter::XML_Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(Qt::black);
keywordFormat.setFontWeight(QFont::Bold);
attributeFormat.setForeground(Qt::darkGreen);
valueFormat.setForeground(Qt::red);
commentFormat.setForeground(Qt::gray);
rule.pattern = QRegularExpression(QStringLiteral("<[?\\s]*[/]?[\\s]*([^\\n][^>]*)(?=[\\s/>])"));
rule.format = keywordFormat;
highlightingRules.append(rule);
rule.pattern = QRegularExpression(QStringLiteral("\\w+(?=\\=)"));
rule.format = attributeFormat;
highlightingRules.append(rule);
rule.pattern = QRegularExpression(QStringLiteral("\"[^\\n\"]+\"(?=[?\\s/>])"));
rule.format = valueFormat;
highlightingRules.append(rule);
rule.pattern = QRegularExpression(QStringLiteral("<!--[^\\n]*-->"));
rule.format = commentFormat;
highlightingRules.append(rule);
}
void XML_Highlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule &rule : qAsConst(highlightingRules)) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
}