-
-
Notifications
You must be signed in to change notification settings - Fork 973
/
Copy pathgolanghighlighter.cpp
239 lines (212 loc) · 7.63 KB
/
golanghighlighter.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**************************************************************************
** This file is part of LiteIDE
**
** Copyright (c) 2011-2013 LiteIDE. All rights reserved.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** In addition, as a special exception, that plugins developed for LiteIDE,
** are allowed to remain closed sourced and can be distributed under any license .
** These rights are included in the file LGPL_EXCEPTION.txt in this package.
**
**************************************************************************/
// Module: golanghighlighter.cpp
// Creator: visualfc <[email protected]>
#include "golanghighlighter.h"
//lite_memory_check_begin
#if defined(WIN32) && defined(_MSC_VER) && defined(_DEBUG)
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
#define new DEBUG_NEW
#endif
//lite_memory_check_end
GolangHighlighter::GolangHighlighter(QTextDocument* document):
QSyntaxHighlighter(document), allWords(new QSet<QString>)
{
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFontWeight(QFont::Bold);
identFormat.setForeground(Qt::darkBlue);
functionFormat.setForeground(Qt::blue);
numberFormat.setForeground(Qt::darkMagenta);
quotesFormat.setForeground(Qt::darkGreen);
singleLineCommentFormat.setForeground(Qt::darkCyan);
multiLineCommentFormat.setForeground(Qt::darkCyan);
QString word;
HighlightingRule rule;
//number
rule.pattern = QRegExp("(\\b|\\.)([0-9]+|0[xX][0-9a-fA-F]+|0[0-7]+)(\\.[0-9]+)?([eE][+-]?[0-9]+i?)?\\b");
rule.format = numberFormat;
highlightingRules.push_back(rule);
//function
rule.pattern = QRegExp("\\b[a-zA-Z_][a-zA-Z0-9_]+\\s*(?=\\()");
rule.format = functionFormat;
highlightingRules.push_back(rule);
//indent
rule.pattern = QRegExp("\\b"
"(bool|byte|complex64|complex128|float32|float64|"
"int8|int16|int32|int64|string|uint8|uint16|uint32|uint64|"
"int|uint|uintptr|"
"true|false|iota|"
"nil|"
"append|cap|close|closed|complex|copy|imag|len|"
"make|new|panic|print|println|real|recover)"
"\\b");
rule.format = identFormat;
highlightingRules.append(rule);
//
word = rule.pattern.pattern();
word.remove("\\b");
word.remove("(");
word.remove(")");
foreach(QString v, word.split("|")) {
allWords->insert(v);
}
//keyword
rule.pattern = QRegExp("\\b"
"(break|default|func|interface|select|"
"case|defer|go|map|struct|"
"chan|else|goto|package|switch|"
"const|fallthrough|if|range|type|"
"continue|for|import|return|var)"
"\\b");
rule.format = keywordFormat;
highlightingRules.append(rule);
word = rule.pattern.pattern();
word.remove("\\b");
word.remove("(");
word.remove(")");
foreach(QString v, word.split("|")) {
allWords->insert(v);
}
//quotes and comment
regexpQuotesAndComment = QRegExp("//|\\\"|'|`|/\\*");
}
bool GolangHighlighter::highlightPreBlock(QString const& text, int& startPos, int& endPos)
{
int state = previousBlockState();
if (state == -1)
state = 0;
if (state & STATE_BACKQUOTES) {
endPos = findQuotesEndPos(text, startPos, '`');
if (endPos == -1) {
setFormat(0, text.length(), quotesFormat);
setCurrentBlockState(STATE_BACKQUOTES);
return true;
} else {
endPos += 1;
setFormat(0, endPos - startPos, quotesFormat);
startPos = endPos;
}
} else if (state & STATE_MULTILINE_COMMENT) {
endPos = text.indexOf("*/", startPos);
if (endPos == -1) {
setFormat(0, text.length(), multiLineCommentFormat);
setCurrentBlockState(previousBlockState());
return true;
} else {
endPos += 2;
setFormat(0, endPos - startPos, multiLineCommentFormat);
startPos = endPos;
}
} else if (state & STATE_SINGLELINE_COMMENT) {
setFormat(0, text.length(), singleLineCommentFormat);
if (text.endsWith("\\")) {
setCurrentBlockState(STATE_SINGLELINE_COMMENT);
}
return true;
}
return false;
}
void GolangHighlighter::highlightBlock(const QString &text)
{
int startPos = 0;
int endPos = text.length();
setCurrentBlockState(0);
if (highlightPreBlock(text, startPos, endPos)) {
return;
}
//keyword and func
foreach (const HighlightingRule &rule, highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text,startPos);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
allWords->insert(text.mid(index,length));
index = expression.indexIn(text, startPos+index + length);
}
}
//quote and comment
while (true)
{
startPos = text.indexOf(regexpQuotesAndComment, startPos);
if (startPos == -1)
break;
QString cap = regexpQuotesAndComment.cap();
if ((cap == "\"") || (cap == "'") || (cap == "`"))
{
endPos = findQuotesEndPos(text, startPos + 1, cap.at(0));
if (endPos == -1)
{
//multiline
setFormat(startPos, text.length() - startPos, quotesFormat);
if (cap == "`") {
setCurrentBlockState(STATE_BACKQUOTES);
}
return;
}
else
{
endPos += 1;
setFormat(startPos, endPos - startPos, quotesFormat);
startPos = endPos;
}
}
else if (cap == "//")
{
setFormat(startPos, text.length() - startPos, singleLineCommentFormat);
if (text.endsWith("\\"))
setCurrentBlockState(STATE_SINGLELINE_COMMENT);
return;
}
else if (cap == "/*")
{
endPos = text.indexOf("*/", startPos+2);
if (endPos == -1)
{
//miltiline
setFormat(startPos, text.length() - startPos, multiLineCommentFormat);
setCurrentBlockState(STATE_MULTILINE_COMMENT);
return;
}
else
{
endPos += 2;
setFormat(startPos, endPos - startPos, multiLineCommentFormat);
startPos = endPos;
}
}
}
}
int GolangHighlighter::findQuotesEndPos(const QString &text, int startPos, const QChar &endChar)
{
for (int pos = startPos; pos < text.length(); pos++)
{
if (text.at(pos) == endChar) {
return pos;
} else if (text.at(pos) == QChar('\\') && endChar != QChar('`')) {
++pos;
}
}
return -1;
}