10000 feat(language-server): notification-based tsserver request forwarding · vuejs/language-tools@9e31394 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e31394

Browse files
committed
feat(language-server): notification-based tsserver request forwarding
1 parent 5fd492c commit 9e31394

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

extensions/vscode/src/languageClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ async function getInitializationOptions(context: vscode.ExtensionContext): Promi
106106
return {
107107
typescript: {
108108
tsdk: (await lsp.getTsdk(context))!.tsdk,
109-
tsserverRequestCommand: 'tsserverRequest',
109+
tsserverNotificationCommands: {
110+
request: 'typescript.tsserverRequest',
111+
response: 'typescript.tsserverResponse',
112+
},
110113
},
111114
};
112115
}

extensions/vscode/src/nodeClientMain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const { activate, deactivate } = defineExtension(async () => {
8585

8686
updateProviders(client);
8787

88-
client.onRequest('tsserverRequest', async ([command, args]) => {
88+
client.onNotification('typescript.tsserverRequest', async ([id, command, args]) => {
8989
const tsserver = (globalThis as any).__TSSERVER__?.semantic;
9090
if (!tsserver) {
9191
return;
@@ -97,7 +97,7 @@ export const { activate, deactivate } = defineExtension(async () => {
9797
lowPriority: true,
9898
requireSemantic: true,
9999
})[0];
100-
return res.body;
100+
client.sendNotification('typescript.tsserverResponse', [id, res.body]);
101101
} catch {
102102
// noop
103103
}

packages/language-server/lib/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ export type VueInitializationOptions = {
22
typescript: {
33
tsdk: string;
44
tsserverRequestCommand?: string;
5+
tsserverNotificationCommands?: {
6+
request: string;
7+
response: string;
8+
};
59
};
610
};
711

packages/language-server/node.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ connection.onInitialize(params => {
1818
if (!options.typescript?.tsdk) {
1919
throw new Error('typescript.tsdk is required');
2020
}
21-
if (!options.typescript?.tsserverRequestCommand) {
22-
connection.console.warn('typescript.tsserverRequestCommand is required since >= 3.0 for complete TS features');
21+
if (!options.typescript?.tsserverRequestCommand && !options.typescript?.tsserverNotificationCommands) {
22+
connection.console.warn('typescript.tsserverRequestCommand/typescript.tsserverNotificationCommands is required since >= 3.0 for complete TS features');
2323
}
2424

2525
const { typescript: ts } = loadTsdkByPath(options.typescript.tsdk, params.locale);
@@ -38,13 +38,23 @@ connection.onInitialize(params => {
3838
});
3939

4040
let simpleLs: LanguageService | undefined;
41+
let tsserverRequestId = 0;
42+
43+
const tsserverRequestHandlers = new Map<number, (res: any) => void>();
44+
45+
if (options.typescript.tsserverNotificationCommands) {
46+
connection.onNotification(options.typescript.tsserverNotificationCommands.response, ([id, res]) => {
47+
tsserverRequestHandlers.get(id)?.(res);
48+
tsserverRequestHandlers.delete(id);
49+
});
50+
}
4151

4252
return server.initialize(
4353
params,
4454
{
4555
setup() { },
4656
async getLanguageService(uri) {
47-
if (uri.scheme === 'file' && options.typescript.tsserverRequestCommand) {
57+
if (uri.scheme === 'file' && (options.typescript.tsserverRequestCommand || options.typescript.tsserverNotificationCommands)) {
4858
const fileName = uri.fsPath.replace(/\\/g, '/');
4959
let projectInfoPromise = file2ProjectInfo.get(fileName);
5060
if (!projectInfoPromise) {
@@ -87,7 +97,7 @@ connection.onInitialize(params => {
8797
simpleLs = undefined;
8898
},
8999
},
90-
getHybridModeLanguageServicePlugins(ts, options.typescript.tsserverRequestCommand ? {
100+
getHybridModeLanguageServicePlugins(ts, (options.typescript.tsserverRequestCommand || options.typescript.tsserverNotificationCommands) ? {
91101
collectExtractProps(...args) {
92102
return sendTsRequest('vue:collectExtractProps', args);
93103
},
@@ -139,8 +149,16 @@ connection.onInitialize(params => {
139149
} : undefined)
140150
);
141151

142-
function sendTsRequest<T>(command: string, args: any): Promise<T | null> {
143-
return connection.sendRequest<T>(options.typescript.tsserverRequestCommand!, [command, args]);
152+
async function sendTsRequest<T>(command: string, args: any): Promise<T | null> {
153+
if (options.typescript.tsserverNotificationCommands) {
154+
let resolve: (res: T | null) => void;
155+
const id = ++tsserverRequestId;
156+
tsserverRequestHandlers.set(id, res => resolve(res));
157+
connection.sendNotification(options.typescript.tsserverNotificationCommands.request, [id, command, args]);
158+
return new Promise<T | null>(_resolve => resolve = _resolve);
159+
} else {
160+
return await connection.sendRequest<T>(options.typescript.tsserverRequestCommand!, [command, args]);
161+
}
144162
}
145163

146164
function createLs(server: LanguageServer, tsconfig: string | undefined) {

0 commit comments

Comments
 (0)
0