|
| 1 | +import { performance } from "perf_hooks"; |
| 2 | +import { Event, EventEmitter } from "vscode"; |
| 3 | +import { CancellationToken, LanguageClient, LanguageClientOptions, ProtocolRequestType, ProtocolRequestType0, RequestType, RequestType0, ServerOptions } from "vscode-languageclient/node"; |
| 4 | +import { TraceEvent } from "./extension.api"; |
| 5 | + |
| 6 | +const requestEventEmitter = new EventEmitter<TraceEvent>(); |
| 7 | +export const onDidRequestEnd: Event<TraceEvent> = requestEventEmitter.event; |
| 8 | + |
| 9 | +export class TracingLanguageClient extends LanguageClient { |
| 10 | + private isStarted: boolean = false; |
| 11 | + |
| 12 | + constructor(id: string, name: string, serverOptions: ServerOptions, clientOptions: LanguageClientOptions, forceDebug?: boolean) { |
| 13 | + super(id, name, serverOptions, clientOptions, forceDebug); |
| 14 | + } |
| 15 | + |
| 16 | + start(): Promise<void> { |
| 17 | + const isFirstTimeStart: boolean = !this.isStarted; |
| 18 | + this.isStarted = true; |
| 19 | + const startAt: number = performance.now(); |
| 20 | + return super.start().then(value => { |
| 21 | + if (isFirstTimeStart) { |
| 22 | + this.fireTraceEvent("initialize", startAt); |
| 23 | + } |
| 24 | + return value; |
| 25 | + }, reason => { |
| 26 | + if (isFirstTimeStart) { |
| 27 | + this.fireTraceEvent("initialize", startAt, reason); |
| 28 | + } |
| 29 | + throw reason; |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + stop(timeout?: number): Promise<void> { |
| 34 | + this.isStarted = false; |
| 35 | + return super.stop(timeout); |
| 36 | + } |
| 37 | + |
| 38 | + sendRequest<R, PR, E, RO>(type: ProtocolRequestType0<R, PR, E, RO>, token?: CancellationToken): Promise<R>; |
| 39 | + sendRequest<P, R, PR, E, RO>(type: ProtocolRequestType<P, R, PR, E, RO>, params: P, token?: CancellationToken): Promise<R>; |
| 40 | + sendRequest<R, E>(type: RequestType0<R, E>, token?: CancellationToken): Promise<R>; |
| 41 | + sendRequest<P, R, E>(type: RequestType<P, R, E>, params: P, token?: CancellationToken): Promise<R>; |
| 42 | + sendRequest<R>(method: string, token?: CancellationToken): Promise<R>; |
| 43 | + sendRequest<R>(method: string, param: any, token?: CancellationToken): Promise<R>; |
| 44 | + sendRequest(method: any, ...args) { |
| 45 | + const startAt: number = performance.now(); |
| 46 | + const requestType: string = this.getRequestType(method, ...args); |
| 47 | + return this.sendRequest0(method, ...args).then(value => { |
| 48 | + this.fireTraceEvent(requestType, startAt); |
| 49 | + return value; |
| 50 | + }, reason => { |
| 51 | + this.fireTraceEvent(requestType, startAt, reason); |
| 52 | + throw reason; |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + private sendRequest0(method: any, ...args) { |
| 57 | + if (!args || !args.length) { |
| 58 | + return super.sendRequest(method); |
| 59 | + } |
| 60 | + |
| 61 | + const first = args[0]; |
| 62 | + const last = args[args.length - 1]; |
| 63 | + if (CancellationToken.is(last)) { |
| 64 | + if (first === last) { |
| 65 | + return super.sendRequest(method, last); |
| 66 | + } else { |
| 67 | + return super.sendRequest(method, first, last); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return super.sendRequest(method, first); |
| 72 | + } |
| 73 | + |
| 74 | + private getRequestType(method: any, ...args): string { |
| 75 | + let requestType: string; |
| 76 | + if (typeof method === 'string' || method instanceof String) { |
| 77 | + requestType = String(method); |
| 78 | + } else { |
| 79 | + requestType = method?.method; |
| 80 | + } |
| 81 | + |
| 82 | + if (requestType === "workspace/executeCommand") { |
| 83 | + if (args?.[0]?.command) { |
| 84 | + requestType = `workspace/executeCommand/${args[0].command}`; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return requestType; |
| 89 | + } |
| 90 | + |
| 91 | + private fireTraceEvent(type: string, startAt: number, reason?: any): void { |
| 92 | + const duration: number = performance.now() - startAt; |
| 93 | + requestEventEmitter.fire({ |
| 94 | + type, |
| 95 | + duration, |
| 96 | + error: reason, |
| 97 | + }); |
| 98 | + } |
| 99 | +} |
0 commit comments