-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsyntaxhighlighter.cpp
More file actions
380 lines (302 loc) · 11 KB
/
syntaxhighlighter.cpp
File metadata and controls
380 lines (302 loc) · 11 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#ifdef _WIN32
#include "syntaxhighlighter.h"
#else
#include "/home/adamj/QtProjects/CodeWizard/syntaxhighlighter.h"
#endif
#include <QTextBlock>
#include <QTextLayout>
#include <QFont>
#include <qregularexpression.h>
#include <QDialog>
#include <QLabel>
#include <qboxlayout.h>
QList<QTextCharFormat> formats = {QTextCharFormat(), QTextCharFormat(), QTextCharFormat(), QTextCharFormat(), QTextCharFormat(), QTextCharFormat(), QTextCharFormat(), QTextCharFormat(), QTextCharFormat()};
std::unordered_map<QString, int> knownTypes = {{"true",8}, {"false", 8}, {"none", 8}};
QRegularExpression *regex = new QRegularExpression("[a-zA-Z0-9]");
QSet<QString> naughtyTypes = {"escape_sequence"};
QString curLang = "Python";
void SyntaxHighlighter::setFormats(QList<QTextCharFormat> newFormats){
formats = newFormats;
}
void SyntaxHighlighter::setLanguage(QString language){
curLang = language;
}
// Convert tree-sitter tree into list of highlight blocks
QList<HighlightBlock> SyntaxHighlighter::getHighlightBlocks(TSNode root, uint32_t start, uint32_t end) {
QList<HighlightBlock> blocks;
traverseNode(QStringList(), "", root, blocks, start, end, QList<QString>());
return blocks;
}
void SyntaxHighlighter::fullDocRehighlight(QTextDocument* document, TSTree* tree){
const QList<HighlightBlock>& newBlocks = getHighlightBlocks(ts_tree_root_node(tree), 0, document->toPlainText().length()+1);
QTextBlock block = document->begin();
while (true) {
QTextLayout* layout = block.layout();
if (!block.isValid()){
break;
}
QList<QTextLayout::FormatRange> currentFormats = layout->formats();
// Find highlights that intersect with this block
QList<HighlightBlock> relevantBlocks;
int blockStart = block.position();
int blockEnd = blockStart + block.length();
for (const auto& highlight : newBlocks) {
if (highlight.startPosition + highlight.length >= blockStart &&
highlight.startPosition < blockEnd) {
relevantBlocks.append(highlight);
}
}
QList<QTextLayout::FormatRange> newFormats;
for (const auto& highlight : relevantBlocks) {
QTextLayout::FormatRange range;
range.start = highlight.startPosition - blockStart;
range.length = highlight.length;
range.format = getFormatForType(highlight.type);
newFormats.append(range);
}
layout->setFormats(newFormats);
block = block.next();
}
document->markContentsDirty(0, document->characterCount());
}
// Apply highlighting only where needed
void SyntaxHighlighter::updateHighlighting(QTextDocument* document, int cursorPos, int addedLen, TSTree* oldTree, TSTree* newTree, bool forceFull) {
uint32_t rangeCount;
TSRange* ranges = ts_tree_get_changed_ranges(oldTree, newTree, &rangeCount);
// Traverse each changed range
for (uint32_t z = 0; z < rangeCount; ++z) {
QTextBlock B1 = document->findBlock(ranges[z].start_byte);
QTextBlock B2 = document->findBlock(ranges[z].end_byte);
const QList<HighlightBlock>& newBlocks = getHighlightBlocks(ts_tree_root_node(newTree), B1.position(), B2.position()+B2.length()+1);
int blockNumber1 = B1.blockNumber();
int blockNumber2 = B2.blockNumber();
if (forceFull){
B1 = document->begin();
blockNumber1 = 0;
blockNumber2 = document->blockCount();
}
for (QTextBlock block = B1; block.isValid() && block.blockNumber() < blockNumber2+1; block = block.next()) {
QTextLayout* layout = block.layout();
QList<QTextLayout::FormatRange> currentFormats = layout->formats();
// Find highlights that intersect with this block
QList<HighlightBlock> relevantBlocks;
int blockStart = block.position();
int blockEnd = blockStart + block.length();
for (const auto& highlight : newBlocks) {
if (highlight.startPosition + highlight.length >= blockStart &&
highlight.startPosition < blockEnd) {
relevantBlocks.append(highlight);
}
}
// Check if current formatting matches what we want
bool needsUpdate = false;
if (currentFormats.size() != relevantBlocks.size()) {
needsUpdate = true;
} else {
for (int i = 0; i < currentFormats.size(); ++i) {
// Compare format properties with highlight block
if (currentFormats[i].start != relevantBlocks[i].startPosition - blockStart ||
currentFormats[i].length != relevantBlocks[i].length) {
needsUpdate = true;
break;
}
}
}
if (needsUpdate) {
QList<QTextLayout::FormatRange> newFormats;
for (const auto& highlight : relevantBlocks) {
QTextLayout::FormatRange range;
range.start = highlight.startPosition - blockStart;
range.length = highlight.length;
range.format = getFormatForType(highlight.type);
newFormats.append(range);
}
layout->setFormats(newFormats);
document->markContentsDirty(blockStart, block.length());
}
}
}
QTextBlock block = document->findBlock(cursorPos);
if (block.previous().isValid()){
block = block.previous();
}
QTextBlock block2 = document->findBlock(cursorPos+addedLen);
if (block2.next().isValid()){
block2 = block2.next();
}
int s = block.position();
int e = block2.length()+block2.position();
int lessThan = block2.blockNumber()-block.blockNumber();
if (forceFull){
s = 0;
QTextCursor cursor(document);
cursor.movePosition(QTextCursor::End);
QTextBlock block2 = cursor.block();
e = block2.length()+block2.position();
block = document->begin();
lessThan = document->blockCount();
}
const QList<HighlightBlock>& newBlocks = getHighlightBlocks(ts_tree_root_node(newTree), s, e+1);
for (int z = -1; z < lessThan; z ++) {
QTextLayout* layout = block.layout();
if (!block.isValid()){
break;
}
QList<QTextLayout::FormatRange> currentFormats = layout->formats();
// Find highlights that intersect with this block
QList<HighlightBlock> relevantBlocks;
int blockStart = block.position();
int blockEnd = blockStart + block.length();
for (const auto& highlight : newBlocks) {
if (highlight.startPosition + highlight.length >= blockStart &&
highlight.startPosition < blockEnd) {
relevantBlocks.append(highlight);
}
}
// Check if current formatting matches what we want
bool needsUpdate = false;
if (currentFormats.size() != relevantBlocks.size()) {
needsUpdate = true;
} else {
for (int i = 0; i < currentFormats.size(); ++i) {
// Compare format properties with highlight block
if (currentFormats[i].start != relevantBlocks[i].startPosition - blockStart ||
currentFormats[i].length != relevantBlocks[i].length) {
needsUpdate = true;
break;
}
}
}
if (needsUpdate) {
QList<QTextLayout::FormatRange> newFormats;
for (const auto& highlight : relevantBlocks) {
QTextLayout::FormatRange range;
range.start = highlight.startPosition - blockStart;
range.length = highlight.length;
range.format = getFormatForType(highlight.type);
newFormats.append(range);
}
layout->setFormats(newFormats);
document->markContentsDirty(blockStart, block.length());
}
block = block.next();
}
}
void SyntaxHighlighter::traverseNode(QStringList parents, QString p, TSNode node, QList<HighlightBlock>& blocks, uint32_t start, uint32_t end, QList<QString> siblings) {
if (!p.isEmpty()){
parents.append(p);
}
QString nodeType = QString::fromUtf8(ts_node_type(node));
if (shouldHighlight(node)) {
uint32_t startByte = ts_node_start_byte(node);
uint32_t endByte = ts_node_end_byte(node);
QString parentType = "";
for (int i = 0; i < parents.count(); i++){
if (parents[i] != nodeType){
parentType = parents[i];
}
}
blocks.append(HighlightBlock{
static_cast<int>(startByte),
static_cast<int>(endByte - startByte),
parents.join("/.CodeWiz./")+"/.SEPERATOR./"+parentType+"/.CodeWiz./"+nodeType+"/.SEPERATOR./"+siblings.join("/.CodeWiz./")
});
}
// Recurse through children
uint32_t childCount = ts_node_child_count(node);
QList<QString> followingSibs;
for (uint32_t i = 0; i < childCount ; ++i) {
TSNode child = ts_node_child(node, childCount-i-1);
uint32_t startByte = ts_node_start_byte(child);
uint32_t endByte = ts_node_end_byte(child);
if (start < endByte && end > startByte){
traverseNode(parents, nodeType, child, blocks, start, end, followingSibs);
}
followingSibs.push_back(ts_node_type(child));
}
}
bool SyntaxHighlighter::shouldHighlight(TSNode nodeType) {
if (ts_node_child_count(nodeType) == 0){
return true;
}
bool allNaughty = true;
uint32_t childCount = ts_node_child_count(nodeType);
for (uint32_t i = 0; i < childCount; ++i) {
TSNode child = ts_node_child(nodeType, i);
if (!naughtyTypes.contains(ts_node_type(child))){
allNaughty = false;
break;
}
}
if (allNaughty){
return true;
}
for (uint32_t i = 0; i < childCount; ++i) {
TSNode child = ts_node_child(nodeType, i);
if (regex->match(ts_node_type(child)).hasMatch()){
return false;
}
}
return true;
}
int SyntaxHighlighter::specificPythonFix(QStringList parents, QStringList PAT, QString following){
if (parents.length() < 2 || following != ""){
return -1;
}
if (parents[parents.length()-1] != "attribute" || parents[parents.length()-2] != "call"){
return -1;
}
return 5;
}
QTextCharFormat SyntaxHighlighter::getFormatForType(const QString& lotsOinfo) {
QStringList parts = lotsOinfo.split("/.SEPERATOR./");
QString parentAndType = parts[1];
QStringList PAT = parentAndType.split("/.CodeWiz./");
QStringList parents = parts[0].split("/.CodeWiz./");
QString following = parts[2];
if (curLang == "Python"){
int pythonFix = specificPythonFix(parents, PAT, following);
if (pythonFix != -1){
return formats[pythonFix];
}
}
auto it = colormap.find(parentAndType); // Find the iterator for the type
if (it != colormap.end()) {
return formats[it->second]; // Access the value of the iterator (which is the key for formats)
}
QString type = PAT[1];
auto it2 = colormap.find(type); // Find the iterator for the type
if (it2 != colormap.end()) {
return formats[it2->second]; // Access the value of the iterator (which is the key for formats)
}
if (parentAndType.contains("string") || parentAndType.contains("char")) {
return formats[1];
}
if (parentAndType.contains("comment")) {
return formats[2];
}
if (parentAndType.contains("type") || parentAndType.contains("public") || parentAndType.contains("private") || parentAndType.contains("static")) {
return formats[4];
}
if (parentAndType.contains("arguments")) {
return formats[3];
}
if (parentAndType.contains("\"") || parentAndType.contains("\'")) {
return formats[1];
}
if (!regex->match(type).hasMatch()){
return formats[7];
}
if (type.contains("if") || type.contains("else") || type.contains("then") || type.contains("end") || type.contains("return") || type.contains("continue") || type.contains("break") || type.contains("yeild")){
return formats[6];
}
if (type.contains("function")){
return formats[5];
}
auto it3 = knownTypes.find(type); // Find the iterator for the type
if (it3 != knownTypes.end()) {
return formats[it3->second]; // Access the value of the iterator (which is the key for formats)
}
// qDebug() << "Unknown type " << parentAndType;
return formats[8]; // Return a default QTextCharFormat if not found
}