-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathQYAMLHighlighter.cpp
More file actions
97 lines (83 loc) · 2.26 KB
/
QYAMLHighlighter.cpp
File metadata and controls
97 lines (83 loc) · 2.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <QYAMLHighlighter>
#include <QSyntaxStyle>
QYAMLHighlighter::QYAMLHighlighter(QTextDocument *document)
: QStyleSyntaxHighlighter(document),
m_yamlKeywordRegexes(),
m_yamlValuesRegex(R"(:\s.+$)"),
m_yamlBooleansRegex(R"( (y|yes|n|no|true|false|on|off)$)"),
m_yamlNumbersRegex(R"( [[:digit:]]+(\.[[:digit:]]+)?)"),
m_yamlArraysRegex(R"(\[" "\]" ":\s+[|>]" "^\s*- )"),
m_yamlReservedRegex(R"((^| )!!(binary|bool|float|int|map|null|omap|seq|set|str) )"),
m_yamlCommentRegex(R"(#.*$)"),
m_yamlStringRegex(R"(["'][^'"]*["'])"),
m_yamlEqualSignRegex(R"(:( |$))")
{
m_yamlKeywordRegexes
<< QRegularExpression(R"(^\s*[\$A-Za-z0-9_-]+\:)")
<< QRegularExpression(R"(^\s*@[\$A-Za-z0-9_-]+\:)");
}
void QYAMLHighlighter::highlightBlock(const QString &text)
{
for (const auto& regex : m_yamlKeywordRegexes)
{
highlightByRegex(
syntaxStyle()->getFormat("Keyword"),
regex,
text
);
}
highlightByRegex(
syntaxStyle()->getFormat("Text"),
m_yamlValuesRegex,
text
);
highlightByRegex(
syntaxStyle()->getFormat("Text"),
m_yamlBooleansRegex,
text
);
highlightByRegex(
syntaxStyle()->getFormat("Number"),
m_yamlNumbersRegex,
text
);
highlightByRegex(
syntaxStyle()->getFormat("Text"),
m_yamlArraysRegex,
text
);
highlightByRegex(
syntaxStyle()->getFormat("Keyword"),
m_yamlReservedRegex,
text
);
highlightByRegex(
syntaxStyle()->getFormat("String"),
m_yamlStringRegex,
text
);
highlightByRegex(
syntaxStyle()->getFormat("Text"),
m_yamlEqualSignRegex,
text
);
highlightByRegex(
syntaxStyle()->getFormat("Comment"),
m_yamlCommentRegex,
text
);
}
void
QYAMLHighlighter::highlightByRegex(const QTextCharFormat& format, const QRegularExpression& regex, const QString& text)
{
auto matchIterator = regex.globalMatch(text);
while (matchIterator.hasNext())
{
auto match = matchIterator.next();
setFormat(
match.capturedStart(),
match.capturedLength(),
format
);
}
}