-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticTokenDecoder.cpp
More file actions
72 lines (59 loc) · 2.44 KB
/
Copy pathSemanticTokenDecoder.cpp
File metadata and controls
72 lines (59 loc) · 2.44 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
#include "syntax/SemanticTokenDecoder.h"
#include <QJsonObject>
namespace cold {
SemanticTokensLegend parseInitializeSemanticLegend(const QJsonObject& initializeResponse) {
SemanticTokensLegend legend;
const QJsonObject caps = initializeResponse.value(QStringLiteral("capabilities")).toObject();
const QJsonObject provider = caps.value(QStringLiteral("semanticTokensProvider")).toObject();
const QJsonObject legendObj = provider.value(QStringLiteral("legend")).toObject();
for (const QJsonValueConstRef v : legendObj.value(QStringLiteral("tokenTypes")).toArray()) {
legend.tokenTypes.append(v.toString());
}
for (const QJsonValueConstRef v : legendObj.value(QStringLiteral("tokenModifiers")).toArray()) {
legend.tokenModifiers.append(v.toString());
}
return legend;
}
QVector<HighlightRange> decodeSemanticTokens(const QJsonArray& data,
const SemanticTokensLegend& legend, int lineCount,
const QHash<QString, uint16_t>& typeToScope) {
QVector<HighlightRange> ranges;
if (data.isEmpty() || legend.tokenTypes.isEmpty()) {
return ranges;
}
int line = 0;
int startChar = 0;
ranges.reserve(data.size() / 5);
for (int i = 0; i + 4 < data.size(); i += 5) {
const int deltaLine = data.at(i).toInt(0);
const int deltaStart = data.at(i + 1).toInt(0);
const int length = data.at(i + 2).toInt(0);
const int typeIndex = data.at(i + 3).toInt(0);
Q_UNUSED(data.at(i + 4));
line += deltaLine;
if (deltaLine > 0) {
startChar = deltaStart;
} else {
startChar += deltaStart;
}
if (length <= 0 || line < 0 || (lineCount > 0 && line >= lineCount)) {
continue;
}
HighlightRange range;
range.startLine = line;
range.startColumn = startChar;
range.endLine = line;
range.endColumn = startChar + length;
QString typeName;
if (typeIndex >= 0 && typeIndex < legend.tokenTypes.size()) {
typeName = legend.tokenTypes.at(typeIndex);
}
range.scopeId = typeToScope.value(typeName, 0);
if (range.scopeId == 0 && typeIndex >= 0 && typeIndex < legend.tokenTypes.size()) {
range.scopeId = static_cast<uint16_t>(typeIndex + 1);
}
ranges.push_back(range);
}
return ranges;
}
} // namespace cold