/
semanticTokens.ts
200 lines (177 loc) · 6.82 KB
/
semanticTokens.ts
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
import * as vscode from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as shared from '@volar/shared';
export function register(
host: ts.LanguageServiceHost,
languageService: ts.LanguageService,
getTextDocument: (uri: string) => TextDocument | undefined,
ts: typeof import('typescript/lib/tsserverlibrary'),
) {
return (uri: string, range: vscode.Range, legend: vscode.SemanticTokensLegend) => {
const document = getTextDocument(uri);
if (!document) return;
const file = shared.getPathOfUri(uri);
const start = range ? document.offsetAt(range.start) : 0;
const length = range ? (document.offsetAt(range.end) - start) : document.getText().length;
if (host.getCancellationToken?.().isCancellationRequested()) return;
let response2: ReturnType<typeof languageService.getEncodedSyntacticClassifications> | undefined;
try { response2 = languageService.getEncodedSyntacticClassifications(file, { start, length }); } catch { }
if (!response2) return;
if (host.getCancellationToken?.().isCancellationRequested()) return;
let response1: ReturnType<typeof languageService.getEncodedSemanticClassifications> | undefined;
try { response1 = languageService.getEncodedSemanticClassifications(file, { start, length }, ts.SemanticClassificationFormat.TwentyTwenty); } catch { }
if (!response1) return;
const tokenSpan = [...response1.spans, ...response2.spans];
const tokens: [number, number, number, number, number][] = [];
let i = 0;
while (i < tokenSpan.length) {
const offset = tokenSpan[i++];
const length = tokenSpan[i++];
const tsClassification = tokenSpan[i++];
let tokenModifiers = 0;
let tokenType = getTokenTypeFromClassification(tsClassification);
if (tokenType !== undefined) {
// it's a classification as returned by the typescript-vscode-sh-plugin
tokenModifiers = getTokenModifierFromClassification(tsClassification);
} else {
// typescript-vscode-sh-plugin is not present
tokenType = tokenTypeMap[tsClassification];
if (tokenType === undefined) {
continue;
}
}
const serverToken = tsTokenTypeToServerTokenType(tokenType);
if (serverToken === undefined) {
continue;
}
const serverTokenModifiers = tsTokenModifierToServerTokenModifier(tokenModifiers);
// we can use the document's range conversion methods because the result is at the same version as the document
const startPos = document.positionAt(offset);
const endPos = document.positionAt(offset + length);
for (let line = startPos.line; line <= endPos.line; line++) {
const startCharacter = (line === startPos.line ? startPos.character : 0);
const endCharacter = (line === endPos.line ? endPos.character : docLineLength(document, line));
tokens.push([line, startCharacter, endCharacter - startCharacter, serverToken, serverTokenModifiers]);
}
}
return tokens;
function tsTokenTypeToServerTokenType(tokenType: number) {
return legend.tokenTypes.indexOf(tokenTypes[tokenType]);
}
function tsTokenModifierToServerTokenModifier(input: number) {
let m = 0;
for (let i = 0; i < tokenModifiers.length; i++) {
if (input & (1 << i)) {
const match = legend.tokenModifiers.indexOf(tokenModifiers[i]);
if (match >= 0) {
m |= 1 << match;
}
}
}
return m;
}
};
}
function docLineLength(document: TextDocument, line: number) {
const currentLineOffset = document.offsetAt({ line, character: 0 });
const nextLineOffset = document.offsetAt({ line: line + 1, character: 0 });
return nextLineOffset - currentLineOffset;
}
// typescript encodes type and modifiers in the classification:
// TSClassification = (TokenType + 1) << 8 + TokenModifier
declare const enum TokenType {
class = 0,
enum = 1,
interface = 2,
namespace = 3,
typeParameter = 4,
type = 5,
parameter = 6,
variable = 7,
enumMember = 8,
property = 9,
function = 10,
method = 11,
_ = 12
}
declare const enum TokenModifier {
declaration = 0,
static = 1,
async = 2,
readonly = 3,
defaultLibrary = 4,
local = 5,
_ = 6
}
declare const enum TokenEncodingConsts {
typeOffset = 8,
modifierMask = 255
}
function getTokenTypeFromClassification(tsClassification: number): number | undefined {
if (tsClassification > TokenEncodingConsts.modifierMask) {
return (tsClassification >> TokenEncodingConsts.typeOffset) - 1;
}
return undefined;
}
function getTokenModifierFromClassification(tsClassification: number) {
return tsClassification & TokenEncodingConsts.modifierMask;
}
const tokenTypes: string[] = [];
tokenTypes[TokenType.class] = 'class';
tokenTypes[TokenType.enum] = 'enum';
tokenTypes[TokenType.interface] = 'interface';
tokenTypes[TokenType.namespace] = 'namespace';
tokenTypes[TokenType.typeParameter] = 'typeParameter';
tokenTypes[TokenType.type] = 'type';
tokenTypes[TokenType.parameter] = 'parameter';
tokenTypes[TokenType.variable] = 'variable';
tokenTypes[TokenType.enumMember] = 'enumMember';
tokenTypes[TokenType.property] = 'property';
tokenTypes[TokenType.function] = 'function';
tokenTypes[TokenType.method] = 'method';
const tokenModifiers: string[] = [];
tokenModifiers[TokenModifier.async] = 'async';
tokenModifiers[TokenModifier.declaration] = 'declaration';
tokenModifiers[TokenModifier.readonly] = 'readonly';
tokenModifiers[TokenModifier.static] = 'static';
tokenModifiers[TokenModifier.local] = 'local'; // missing in server tokenModifiers
tokenModifiers[TokenModifier.defaultLibrary] = 'defaultLibrary';
// mapping for the original ExperimentalProtocol.ClassificationType from TypeScript (only used when plugin is not available)
const tokenTypeMap: number[] = [];
tokenTypeMap[ExperimentalProtocol.ClassificationType.className] = TokenType.class;
tokenTypeMap[ExperimentalProtocol.ClassificationType.enumName] = TokenType.enum;
tokenTypeMap[ExperimentalProtocol.ClassificationType.interfaceName] = TokenType.interface;
tokenTypeMap[ExperimentalProtocol.ClassificationType.moduleName] = TokenType.namespace;
tokenTypeMap[ExperimentalProtocol.ClassificationType.typeParameterName] = TokenType.typeParameter;
tokenTypeMap[ExperimentalProtocol.ClassificationType.typeAliasName] = TokenType.type;
tokenTypeMap[ExperimentalProtocol.ClassificationType.parameterName] = TokenType.parameter;
namespace ExperimentalProtocol {
export const enum ClassificationType {
comment = 1,
identifier = 2,
keyword = 3,
numericLiteral = 4,
operator = 5,
stringLiteral = 6,
regularExpressionLiteral = 7,
whiteSpace = 8,
text = 9,
punctuation = 10,
className = 11,
enumName = 12,
interfaceName = 13,
moduleName = 14,
typeParameterName = 15,
typeAliasName = 16,
parameterName = 17,
docCommentTagName = 18,
jsxOpenTagName = 19,
jsxCloseTagName = 20,
jsxSelfClosingTagName = 21,
jsxAttribute = 22,
jsxText = 23,
jsxAttributeStringLiteralValue = 24,
bigintLiteral = 25,
}
}