This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvcreatorhighlighter.cpp
215 lines (176 loc) · 7.96 KB
/
vcreatorhighlighter.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "vcreatorhighlighter.h"
#include "vcreatorlexer.h"
#include <texteditor/textdocument.h>
#include <texteditor/texteditorconstants.h>
#include <utils/porting.h>
#include <QRegularExpression>
namespace VCreator {
namespace Internal {
VlangHighlighter::VlangHighlighter()
:
m_braceDepth(0),
m_foldingIndent(0),
m_inMultilineComment(false)
{
m_currentBlockParentheses.reserve(20);
setDefaultTextFormatCategories();
}
void VlangHighlighter::highlightBlock(const QString &text)
{
const QList<Token> tokens = m_scanner(text, onBlockStart());
int index = 0;
while (index < tokens.size()) {
const Token &token = tokens.at(index);
switch (token.kind) {
case Token::Keyword:
setFormat(token.offset, token.length, formatForCategory(TextEditor::C_KEYWORD));
break;
case Token::String: {
QRegularExpression re(R"del((\$([\w.]+|\{.*?\})))del");
QString t = text.mid(token.offset, token.length);
auto it = re.globalMatch(t);
setFormat(token.offset, token.length, formatForCategory(TextEditor::C_STRING));
while(it.hasNext()) {
auto match = it.next();
setFormat(token.offset + match.capturedStart(1), match.capturedEnd(1) - match.capturedStart(1), formatForCategory(TextEditor::C_TYPE));
}
}
break;
case Token::Number:
setFormat(token.offset, token.length, formatForCategory(TextEditor::C_NUMBER));
break;
case Token::Hash: {
static QSet<QString> hashDecls = {"#include", "#flag", "#pkgconfig", "#define"};
if (hashDecls.contains(text.mid(token.offset, token.length)))
setFormat(token.offset, token.length, formatForCategory(TextEditor::C_PREPROCESSOR));
else
setFormat(token.offset, token.length, formatForCategory(TextEditor::C_TEXT));
} break;
case Token::Comment:
if (m_inMultilineComment
&& Utils::midView(text, token.end() - 2, 2) == QLatin1String("*/")) {
onClosingParenthesis(QLatin1Char('-'), token.end() - 1, index == tokens.size()-1);
m_inMultilineComment = false;
} else if (!m_inMultilineComment
&& (m_scanner.state() & Scanner::MultiLineMask) == Scanner::MultiLineComment
&& index == tokens.size() - 1) {
onOpeningParenthesis(QLatin1Char('+'), token.offset, index == 0);
m_inMultilineComment = true;
}
setFormat(token.offset, token.length, formatForCategory(TextEditor::C_COMMENT));
break;
case Token::BuiltinType:
setFormat(token.offset, token.length, formatForCategory(TextEditor::C_PRIMITIVE_TYPE));
break;
case Token::BuiltinFn:
case Token::Function:
setFormat(token.offset, token.length, formatForCategory(TextEditor::C_FUNCTION));
break;
case Token::LeftParenthesis:
onOpeningParenthesis(QLatin1Char('('), token.offset, index == 0);
break;
case Token::RightParenthesis:
onClosingParenthesis(QLatin1Char(')'), token.offset, index == tokens.size()-1);
break;
case Token::LeftBrace:
onOpeningParenthesis(QLatin1Char('{'), token.offset, index == 0);
break;
case Token::RightBrace:
onClosingParenthesis(QLatin1Char('}'), token.offset, index == tokens.size()-1);
break;
case Token::LeftBracket:
onOpeningParenthesis(QLatin1Char('['), token.offset, index == 0);
break;
case Token::RightBracket:
onClosingParenthesis(QLatin1Char(']'), token.offset, index == tokens.size()-1);
break;
case Token::Identifier: {
setFormat(token.offset, token.length, formatForCategory(TextEditor::C_TEXT));
} break;
case Token::Delimiter:
break;
default:
break;
} // end swtich
++index;
}
int previousVlangTokenEnd = 0;
for (const auto &VlangToken : tokens) {
setFormat(previousVlangTokenEnd, VlangToken.begin() - previousVlangTokenEnd, formatForCategory(TextEditor::C_VISUAL_WHITESPACE));
switch (VlangToken.kind) {
case Token::Comment:
case Token::String: {
int i = VlangToken.begin(), e = VlangToken.end();
while (i < e) {
const QChar ch = text.at(i);
if (ch.isSpace()) {
const int start = i;
do {
++i;
} while (i < e && text.at(i).isSpace());
setFormat(start, i - start, formatForCategory(TextEditor::C_VISUAL_WHITESPACE));
} else {
++i;
}
}
} break;
default:
break;
} // end of switch
previousVlangTokenEnd = VlangToken.end();
}
setFormat(previousVlangTokenEnd, text.length() - previousVlangTokenEnd, formatForCategory(TextEditor::C_VISUAL_WHITESPACE));
setCurrentBlockState(m_scanner.state());
onBlockEnd(m_scanner.state());
}
int VlangHighlighter::onBlockStart()
{
m_currentBlockParentheses.clear();
m_braceDepth = 0;
m_foldingIndent = 0;
m_inMultilineComment = false;
if (TextEditor::TextBlockUserData *userData = TextEditor::TextDocumentLayout::textUserData(currentBlock())) {
userData->setFoldingIndent(0);
userData->setFoldingStartIncluded(false);
userData->setFoldingEndIncluded(false);
}
int state = 0;
int previousState = previousBlockState();
if (previousState != -1) {
state = previousState & 0xff;
m_braceDepth = (previousState >> 8);
m_inMultilineComment = ((state & Scanner::MultiLineMask) == Scanner::MultiLineComment);
}
m_foldingIndent = m_braceDepth;
return state;
}
void VlangHighlighter::onBlockEnd(int state)
{
setCurrentBlockState((m_braceDepth << 8) | state);
TextEditor::TextDocumentLayout::setParentheses(currentBlock(), m_currentBlockParentheses);
TextEditor::TextDocumentLayout::setFoldingIndent(currentBlock(), m_foldingIndent);
}
void VlangHighlighter::onOpeningParenthesis(QChar parenthesis, int pos, bool atStart)
{
if (parenthesis == QLatin1Char('{') || parenthesis == QLatin1Char('[') || parenthesis == QLatin1Char('(')) {
++m_braceDepth;
// if a folding block opens at the beginning of a line, treat the entire line
// as if it were inside the folding block
if (atStart)
TextEditor::TextDocumentLayout::userData(currentBlock())->setFoldingStartIncluded(true);
}
m_currentBlockParentheses.push_back(TextEditor::Parenthesis(TextEditor::Parenthesis::Opened, parenthesis, pos));
}
void VlangHighlighter::onClosingParenthesis(QChar parenthesis, int pos, bool atEnd)
{
if (parenthesis == QLatin1Char('}') || parenthesis == QLatin1Char(']') || parenthesis == QLatin1Char(')')) {
--m_braceDepth;
if (atEnd)
TextEditor::TextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true);
else
m_foldingIndent = qMin(m_braceDepth, m_foldingIndent); // folding indent is the minimum brace depth of a block
}
m_currentBlockParentheses.push_back(TextEditor::Parenthesis(TextEditor::Parenthesis::Closed, parenthesis, pos));
}
} // namespace Internal
} // namespace Vcreator