-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathLanguageClient.ts
More file actions
332 lines (304 loc) · 11.2 KB
/
LanguageClient.ts
File metadata and controls
332 lines (304 loc) · 11.2 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
/* eslint-disable lines-between-class-members */
import {
CancellationToken,
CancellationTokenSource,
Disposable,
ExitNotification,
InitializeRequest,
InitializedNotification,
Message,
NotificationHandler,
NotificationMessage,
ProtocolNotificationType,
ProtocolRequestType,
ProtocolRequestType0,
RequestHandler,
RequestMessage,
ResponseError,
ResponseMessage,
ShutdownRequest,
MessageSignature,
ProtocolNotificationType0,
ServerCapabilities,
ClientCapabilities,
} from 'vscode-languageserver-protocol';
export interface PromiseCompletion {
resolve(value: unknown): void;
reject(error: unknown): void;
}
export interface Dependencies {
clientCapabilities: ClientCapabilities;
initializationOptions?: any;
log(...args: any[]): void;
}
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
// Useful for mocking/stubbing the client in our tests. aka our "public" API.
export interface AbstractLanguageClient {
clientCapabilities: ClientCapabilities;
serverCapabilities: ServerCapabilities | null;
serverInfo: any;
onRequest: LanguageClient['onRequest'];
onNotification: LanguageClient['onNotification'];
sendRequest: LanguageClient['sendRequest'];
sendNotification: LanguageClient['sendNotification'];
}
export class LanguageClient extends EventTarget implements AbstractLanguageClient {
public readonly clientCapabilities: ClientCapabilities;
public readonly initializationOptions: any;
public serverCapabilities: ServerCapabilities | null;
public serverInfo: any;
private requestId: number;
private requests: Map<string, PromiseCompletion>;
private dispose: () => void;
private disposables: Disposable[];
private log: Dependencies['log'];
constructor(
public readonly worker: Worker,
dependencies: Dependencies,
) {
super();
this.requests = new Map();
this.requestId = 0;
this.dispose = () => {};
this.disposables = [];
this.clientCapabilities = dependencies.clientCapabilities;
this.initializationOptions = dependencies.initializationOptions;
this.log = dependencies.log;
this.serverCapabilities = null;
this.serverInfo = null;
}
/**
* Lifecycle method to start the server
*/
async start() {
/**
* Here we setup the web worker event handler.
* We route the message sent via the worker's postMessage to our event handler.
*/
const handler = (ev: MessageEvent<Message>) => this.handleMessage(ev.data);
this.worker.addEventListener('message', handler);
this.dispose = () => {
this.worker.removeEventListener('message', handler);
};
/**
* We send the `initialize` request and obtain the capabilities supported by
* the language server.
*
* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize
*/
const response = await this.sendRequest(InitializeRequest.type, {
capabilities: this.clientCapabilities,
initializationOptions: this.initializationOptions,
processId: 0,
rootUri: 'browser:///',
});
/**
* We unpack the response from the server and remember the capabilities.
* Those will be useful.
*/
this.serverCapabilities = response.capabilities;
this.serverInfo = response.serverInfo;
/**
* Once we're ready, we send the `initialized` notification to tell the
* server that we're ready to roll!
*
* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialized
*/
this.sendNotification(InitializedNotification.type, {});
}
/**
* Lifecycle method to stop the server
*/
async stop() {
await Promise.race([this.sendRequest(ShutdownRequest.type), sleep(1000)]);
this.disposables.forEach((disposable) => disposable.dispose());
this.disposables = [];
this.dispose();
this.sendNotification(ExitNotification.type);
}
/**
* For when you want to handle requests sent from the language server.
*
* @param {MessageSignature} type this is an export from vscode-languageserver-protocol
* @param {RequestHandler} handler a type-inferred event handler whose return value matches the MessageSignature
*
* @example
* import {ApplyWorkspaceEditRequest, ApplyWorkspaceEditResponse} from 'vscode-languageserver-protocol';
* client.onRequest(ApplyWorkspaceEditRequest.type, (params, cancellationToken) => {
* // `params` and `cancellationToken` are typed inferred
* // The return value must match the MessageSignature (ApplyWorkspaceEditResponse)
* return result as ApplyWorkspaceEditResponse;
* })
*/
onRequest<P, R, PR, E, RO>(
type: ProtocolRequestType<P, R, PR, E, RO>,
handler: RequestHandler<P, R, E>,
): Disposable;
onRequest<P>(type: string | MessageSignature, handler: RequestHandler<P, any, any>): Disposable {
const method = typeof type === 'string' ? type : type.method;
const callback = async (event: Event) => {
const { params, id } = (event as CustomEvent<RequestMessage>).detail;
const cancellationToken: CancellationToken = new CancellationTokenSource().token;
try {
const response = await handler(params as any as P, cancellationToken);
if (response && typeof response === 'object' && 'code' in response) {
this.sendResponse(id!, undefined, response as any as ResponseError);
} else {
this.sendResponse(id!, response, undefined);
}
} catch (error) {
this.sendResponse(
id!,
undefined,
new ResponseError(1, error instanceof Error ? error.message : (error as string)),
);
}
};
this.addEventListener(method, callback);
return this.disposable(() => this.removeEventListener(method, callback));
}
/**
* For when you want to handle notifications sent from the language server.
*
* @param {MessageSignature} type this is an export from vscode-languageserver-protocol
* @param {NotificationHandler} handler this is a type-inferred event handler
*
* @example
* import {PublishDiagnosticsNotification} from 'vscode-languageserver-protocol';
* client.onNotification(PublishDiagnosticsNotification.type, (params) => {
* // the type of `params` is inferred from the first argument
* });
*/
onNotification<P, RO>(
type: ProtocolNotificationType<P, RO>,
handler: NotificationHandler<P>,
): Disposable;
onNotification<P>(type: string | MessageSignature, handler: NotificationHandler<P>): Disposable {
const method = typeof type === 'string' ? type : type.method;
const callback = (event: Event) => {
const { params } = (event as CustomEvent<NotificationMessage>).detail;
handler(params as any as P);
};
this.addEventListener(method, callback);
return this.disposable(() => this.removeEventListener(method, callback));
}
/**
* Send a request to the language server and await a response.
*
* @param {MessageSignature} type this is an export from vscode-languageserver-protocol
* @param [params] its type is inferred from the first argument
* @returns {any} response, its type is inferred from the first argument
*
* @example
*
* import {CompletionRequest} from 'vscode-languageserver-protocol';
* const completions = await client.sendRequest(
* CompletionRequest.type,
* params
* );
*/
public sendRequest<R, PR, E, RO>(type: ProtocolRequestType0<R, PR, E, RO>): Promise<R>;
public sendRequest<P, R, PR, E, RO>(
type: ProtocolRequestType<P, R, PR, E, RO>,
params?: P,
): Promise<R>;
public async sendRequest<R>(type: string | MessageSignature, params?: any): Promise<R> {
this.requestId += 1;
const requestId = this.requestId;
const method = typeof type === 'string' ? type : type.method;
const request: RequestMessage = {
jsonrpc: '2.0',
id: requestId,
method,
};
if (params) request.params = params;
this.log(`Client->Server ${request.method} request [${request.id}]`, request);
this.sendMessage(request);
return new Promise((resolve, reject) => {
this.requests.set(requestId.toString(), { resolve, reject });
});
}
/**
* Send a notification to the language server. Notifications are fire and forget.
*
* @param {MessageSignature} type this is an export from vscode-languageserver-protocol
* @param [params] its type is inferred from the first argument
*
* @example
*
* import {DidChangeTextDocumentNotification} from 'vscode-languageserver-protocol';
* client.sendNotification(
* DidChangeTextDocumentNotification.type,
* params
* );
*/
public sendNotification<RO>(type: ProtocolNotificationType0<RO>): void;
public sendNotification<P, RO>(type: ProtocolNotificationType<P, RO>, params?: P): void;
public sendNotification(type: string | MessageSignature, params?: any): void {
const method = typeof type === 'string' ? type : type.method;
const notification: NotificationMessage = {
jsonrpc: '2.0',
method,
};
if (params) notification.params = params;
this.log(`Client->Server ${method} notification`, notification);
this.sendMessage(notification);
}
private sendResponse(id: number | string, result: any, error?: ResponseError) {
const response: ResponseMessage = { jsonrpc: '2.0', id };
if (result !== undefined) response.result = result;
if (error !== undefined) response.error = error.toJson();
this.log(`Client->Server response [${id}]`, response);
this.sendMessage(response);
}
private sendMessage(message: Message) {
this.worker.postMessage(message);
}
/**
* Map messages to their correct destition.
* - Responses -> resolve or reject the corresponding Request
* - Notification -> emit(message.method, message)
* - Request -> emit(message.method, message)
*/
private handleMessage(message: any) {
if (isResponse(message)) {
this.log(`Server->Client response [${message.id}]`, message);
const id = message.id!.toString();
if ('result' in message) {
this.requests.get(id)!.resolve(message.result!);
} else {
this.requests.get(id)!.reject(message.error);
}
this.requests.delete(id);
} else if (isRequest(message)) {
this.log(`Server->Client ${message.method} request [${message.id}]`, message);
this.dispatchEvent(new CustomEvent<RequestMessage>(message.method, { detail: message }));
} else if (isNotification(message)) {
this.log(`Server->Client ${message.method} notification`, message);
this.dispatchEvent(
new CustomEvent(message.method, {
detail: message,
}),
);
} else {
this.log('what?', message);
}
}
private disposable(dispose: () => void) {
const disp = disposable(dispose);
this.disposables.push(disp);
return disp;
}
}
export function disposable(dispose: () => void): Disposable {
return { dispose };
}
function isResponse(message: any): message is ResponseMessage {
return 'id' in message && ('error' in message || 'result' in message);
}
function isRequest(message: any): message is RequestMessage {
return 'id' in message && 'method' in message;
}
function isNotification(message: any): message is NotificationMessage {
return !('id' in message) && 'method' in message;
}