forked from radareorg/iaito
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxHighlighter.cpp
More file actions
113 lines (91 loc) · 3.62 KB
/
SyntaxHighlighter.cpp
File metadata and controls
113 lines (91 loc) · 3.62 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
#include "SyntaxHighlighter.h"
#ifdef IAITO_ENABLE_KSYNTAXHIGHLIGHTING
#include "Configuration.h"
#include <KSyntaxHighlighting/Theme>
SyntaxHighlighter::SyntaxHighlighter(QTextDocument *document) : KSyntaxHighlighting::SyntaxHighlighter(document)
{
connect(Config(), &Configuration::kSyntaxHighlightingThemeChanged, this, &SyntaxHighlighter::updateTheme);
updateTheme();
}
void SyntaxHighlighter::updateTheme()
{
setTheme(Config()->getKSyntaxHighlightingTheme());
rehighlight();
}
#endif
FallbackSyntaxHighlighter::FallbackSyntaxHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
, commentStartExpression("/\\*")
, commentEndExpression("\\*/")
{
HighlightingRule rule;
QStringList keywordPatterns;
//C language keywords
keywordPatterns << "\\bauto\\b" << "\\bdouble\\b" << "\\bint\\b"
<< "\\bstruct\\b" << "\\bbreak\\b" << "\\belse\\b"
<< "\\blong\\b" << "\\switch\\b" << "\\bcase\\b"
<< "\\benum\\b" << "\\bregister\\b" << "\\btypedef\\b"
<< "\\bchar\\b" << "\\bextern\\b" << "\\breturn\\b"
<< "\\bunion\\b" << "\\bconst\\b" << "\\bfloat\\b"
<< "\\bshort\\b" << "\\bunsigned\\b" << "\\bcontinue\\b"
<< "\\bfor\\b" << "\\bsigned\\b" << "\\bvoid\\b"
<< "\\bdefault\\b" << "\\bgoto\\b" << "\\bsizeof\\b"
<< "\\bvolatile\\b" << "\\bdo\\b" << "\\bif\\b"
<< "\\static\\b" << "\\while\\b";
QTextCharFormat keywordFormat;
keywordFormat.setForeground(QColor(80, 200, 215));
for ( const auto &pattern : keywordPatterns ) {
rule.pattern.setPattern(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
//Functions
rule.pattern.setPattern("\\b[A-Za-z0-9_]+(?=\\()");
rule.format.clearBackground();
rule.format.clearForeground();
rule.format.setFontItalic(true);
rule.format.setForeground(Qt::darkCyan);
highlightingRules.append(rule);
//single-line comment
rule.pattern.setPattern("//[^\n]*");
rule.format.clearBackground();
rule.format.clearForeground();
rule.format.setForeground(Qt::gray);
highlightingRules.append(rule);
//quotation
rule.pattern.setPattern("\".*\"");
rule.format.clearBackground();
rule.format.clearForeground();
rule.format.setForeground(Qt::darkGreen);
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::gray);
}
void FallbackSyntaxHighlighter::highlightBlock(const QString &text)
{
for ( const auto &it : highlightingRules ) {
auto matchIterator = it.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
const auto match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), it.format);
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1) {
startIndex = text.indexOf(commentStartExpression);
}
while (startIndex >= 0) {
const auto 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);
}
}