-
Notifications
You must be signed in to change notification settings - Fork 319
/
semanticTokens.ts
218 lines (203 loc) · 10.2 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as vscode from 'vscode';
import { ClientCapabilities, ServerCapabilities, DocumentSelector, SemanticTokenTypes, SemanticTokenModifiers, SemanticTokens,
TokenFormat, SemanticTokensOptions, SemanticTokensRegistrationOptions, SemanticTokensParams,
SemanticTokensRequest, SemanticTokensDeltaParams, SemanticTokensDeltaRequest, SemanticTokensRangeParams, SemanticTokensRangeRequest, SemanticTokensRefreshRequest,
SemanticTokensRegistrationType
} from 'vscode-languageserver-protocol';
import { FeatureClient, TextDocumentLanguageFeature, ensure } from './features';
import * as Is from './utils/is';
export interface DocumentSemanticsTokensSignature {
(this: void, document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult<vscode.SemanticTokens>;
}
export interface DocumentSemanticsTokensEditsSignature {
(this: void, document: vscode.TextDocument, previousResultId: string, token: vscode.CancellationToken): vscode.ProviderResult<vscode.SemanticTokensEdits | vscode.SemanticTokens>;
}
export interface DocumentRangeSemanticTokensSignature {
(this: void, document: vscode.TextDocument, range: vscode.Range, token: vscode.CancellationToken): vscode.ProviderResult<vscode.SemanticTokens>;
}
/**
* The semantic token middleware
*
* @since 3.16.0
*/
export interface SemanticTokensMiddleware {
provideDocumentSemanticTokens?: (this: void, document: vscode.TextDocument, token: vscode.CancellationToken, next: DocumentSemanticsTokensSignature) => vscode.ProviderResult<vscode.SemanticTokens>;
provideDocumentSemanticTokensEdits?: (this: void, document: vscode.TextDocument, previousResultId: string, token: vscode.CancellationToken, next: DocumentSemanticsTokensEditsSignature) => vscode.ProviderResult<vscode.SemanticTokensEdits | vscode.SemanticTokens>;
provideDocumentRangeSemanticTokens?: (this: void, document: vscode.TextDocument, range: vscode.Range, token: vscode.CancellationToken, next: DocumentRangeSemanticTokensSignature) => vscode.ProviderResult<vscode.SemanticTokens>;
}
export interface SemanticTokensProviderShape {
range?: vscode.DocumentRangeSemanticTokensProvider;
full?: vscode.DocumentSemanticTokensProvider;
onDidChangeSemanticTokensEmitter: vscode.EventEmitter<void>;
}
export class SemanticTokensFeature extends TextDocumentLanguageFeature<boolean | SemanticTokensOptions, SemanticTokensRegistrationOptions, SemanticTokensProviderShape, SemanticTokensMiddleware> {
constructor(client: FeatureClient<SemanticTokensMiddleware>) {
super(client, SemanticTokensRegistrationType.type);
}
public fillClientCapabilities(capabilities: ClientCapabilities): void {
const capability = ensure(ensure(capabilities, 'textDocument')!, 'semanticTokens')!;
capability.dynamicRegistration = true;
capability.tokenTypes = [
SemanticTokenTypes.namespace,
SemanticTokenTypes.type,
SemanticTokenTypes.class,
SemanticTokenTypes.enum,
SemanticTokenTypes.interface,
SemanticTokenTypes.struct,
SemanticTokenTypes.typeParameter,
SemanticTokenTypes.parameter,
SemanticTokenTypes.variable,
SemanticTokenTypes.property,
SemanticTokenTypes.enumMember,
SemanticTokenTypes.event,
SemanticTokenTypes.function,
SemanticTokenTypes.method,
SemanticTokenTypes.macro,
SemanticTokenTypes.keyword,
SemanticTokenTypes.modifier,
SemanticTokenTypes.comment,
SemanticTokenTypes.string,
SemanticTokenTypes.number,
SemanticTokenTypes.regexp,
SemanticTokenTypes.operator,
SemanticTokenTypes.decorator,
SemanticTokenTypes.label
];
capability.tokenModifiers = [
SemanticTokenModifiers.declaration,
SemanticTokenModifiers.definition,
SemanticTokenModifiers.readonly,
SemanticTokenModifiers.static,
SemanticTokenModifiers.deprecated,
SemanticTokenModifiers.abstract,
SemanticTokenModifiers.async,
SemanticTokenModifiers.modification,
SemanticTokenModifiers.documentation,
SemanticTokenModifiers.defaultLibrary
];
capability.formats = [TokenFormat.Relative];
capability.requests = {
range: true,
full: {
delta: true
}
};
capability.multilineTokenSupport = false;
capability.overlappingTokenSupport = false;
capability.serverCancelSupport = true;
capability.augmentsSyntaxTokens = true;
ensure(ensure(capabilities, 'workspace')!, 'semanticTokens')!.refreshSupport = true;
}
public initialize(capabilities: ServerCapabilities, documentSelector: DocumentSelector): void {
const client = this._client;
client.onRequest(SemanticTokensRefreshRequest.type, async () => {
for (const provider of this.getAllProviders()) {
provider.onDidChangeSemanticTokensEmitter.fire();
}
});
const [id, options] = this.getRegistration(documentSelector, capabilities.semanticTokensProvider);
if (!id || !options) {
return;
}
this.register({ id: id, registerOptions: options });
}
protected registerLanguageProvider(options: SemanticTokensRegistrationOptions): [vscode.Disposable, SemanticTokensProviderShape] {
const selector = options.documentSelector!;
const fullProvider = Is.boolean(options.full) ? options.full : options.full !== undefined;
const hasEditProvider = options.full !== undefined && typeof options.full !== 'boolean' && options.full.delta === true;
const eventEmitter: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
const documentProvider: vscode.DocumentSemanticTokensProvider | undefined = fullProvider
? {
onDidChangeSemanticTokens: eventEmitter.event,
provideDocumentSemanticTokens: (document, token) => {
const client = this._client;
const middleware = client.middleware;
const provideDocumentSemanticTokens: DocumentSemanticsTokensSignature = (document, token) => {
const params: SemanticTokensParams = {
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document)
};
return client.sendRequest(SemanticTokensRequest.type, params, token).then((result) => {
if (token.isCancellationRequested) {
return null;
}
return client.protocol2CodeConverter.asSemanticTokens(result, token);
}, (error: any) => {
return client.handleFailedRequest(SemanticTokensRequest.type, token, error, null);
});
};
return middleware.provideDocumentSemanticTokens
? middleware.provideDocumentSemanticTokens(document, token, provideDocumentSemanticTokens)
: provideDocumentSemanticTokens(document, token);
},
provideDocumentSemanticTokensEdits: hasEditProvider
? (document, previousResultId, token) => {
const client = this._client;
const middleware = client.middleware;
const provideDocumentSemanticTokensEdits: DocumentSemanticsTokensEditsSignature = (document, previousResultId, token) => {
const params: SemanticTokensDeltaParams = {
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document),
previousResultId
};
return client.sendRequest(SemanticTokensDeltaRequest.type, params, token).then(async (result) => {
if (token.isCancellationRequested) {
return null;
}
if (SemanticTokens.is(result)) {
return await client.protocol2CodeConverter.asSemanticTokens(result, token);
} else {
return await client.protocol2CodeConverter.asSemanticTokensEdits(result, token);
}
}, (error: any) => {
return client.handleFailedRequest(SemanticTokensDeltaRequest.type, token, error, null);
});
};
return middleware.provideDocumentSemanticTokensEdits
? middleware.provideDocumentSemanticTokensEdits(document, previousResultId, token, provideDocumentSemanticTokensEdits)
: provideDocumentSemanticTokensEdits(document, previousResultId, token);
}
: undefined
}
: undefined;
const hasRangeProvider: boolean = options.range === true;
const rangeProvider: vscode.DocumentRangeSemanticTokensProvider | undefined = hasRangeProvider
? {
provideDocumentRangeSemanticTokens: (document: vscode.TextDocument, range: vscode.Range, token: vscode.CancellationToken) => {
const client = this._client;
const middleware = client.middleware;
const provideDocumentRangeSemanticTokens: DocumentRangeSemanticTokensSignature = (document, range, token) => {
const params: SemanticTokensRangeParams = {
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document),
range: client.code2ProtocolConverter.asRange(range)
};
return client.sendRequest(SemanticTokensRangeRequest.type, params, token).then((result) => {
if (token.isCancellationRequested) {
return null;
}
return client.protocol2CodeConverter.asSemanticTokens(result, token);
}, (error: any) => {
return client.handleFailedRequest(SemanticTokensRangeRequest.type, token, error, null);
});
};
return middleware.provideDocumentRangeSemanticTokens
? middleware.provideDocumentRangeSemanticTokens(document, range, token, provideDocumentRangeSemanticTokens)
: provideDocumentRangeSemanticTokens(document, range, token);
}
}
: undefined;
const disposables: vscode.Disposable[] = [];
const client = this._client;
const legend: vscode.SemanticTokensLegend = client.protocol2CodeConverter.asSemanticTokensLegend(options.legend);
const documentSelector = client.protocol2CodeConverter.asDocumentSelector(selector);
if (documentProvider !== undefined) {
disposables.push(vscode.languages.registerDocumentSemanticTokensProvider(documentSelector, documentProvider, legend));
}
if (rangeProvider !== undefined) {
disposables.push(vscode.languages.registerDocumentRangeSemanticTokensProvider(documentSelector, rangeProvider, legend));
}
return [new vscode.Disposable(() => disposables.forEach(item => item.dispose())), { range: rangeProvider, full: documentProvider, onDidChangeSemanticTokensEmitter: eventEmitter }];
}
}