Skip to content

Commit 757cb98

Browse files
refactor: use typescript.tsserverRequest command instead of hack to expose tsserver process (#5443)
1 parent 073a7cb commit 757cb98

File tree

3 files changed

+38
-47
lines changed

3 files changed

+38
-47
lines changed

extensions/vscode/index.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,14 @@ function launch(context: vscode.ExtensionContext) {
112112
},
113113
);
114114

115-
client.onNotification('tsserver/request', async ([id, command, args]) => {
116-
const tsserver = (globalThis as any).__TSSERVER__?.semantic;
117-
if (!tsserver) {
118-
return;
119-
}
120-
try {
121-
const res = await tsserver.executeImpl(command, args, {
122-
isAsync: true,
123-
expectsResult: true,
124-
lowPriority: true,
125-
requireSemantic: true,
126-
})[0];
127-
client.sendNotification('tsserver/response', [id, res.body]);
128-
} catch {
129-
// noop
130-
}
115+
client.onNotification('tsserver/request', async ([seq, command, args]) => {
116+
const res = await vscode.commands.executeCommand<{ body: { result: unknown; }; } | undefined>(
117+
'typescript.tsserverRequest',
118+
command,
119+
args,
120+
{ lowPriority: true, requireSemantic: true },
121+
);
122+
client.sendNotification('tsserver/response', [seq, res?.body]);
131123
});
132124
client.start();
133125

@@ -159,16 +151,6 @@ try {
159151
].join(''),
160152
);
161153

162-
// Expose tsserver process in SingleTsServer constructor
163-
text = text.replace(
164-
',this._callbacks.destroy("server errored")}))',
165-
s => s + ',globalThis.__TSSERVER__||={},globalThis.__TSSERVER__[arguments[1]]=this',
166-
);
167-
168-
/**
169-
* VSCode >= 1.87.0
170-
*/
171-
172154
// patch jsTsLanguageModes
173155
text = text.replace(
174156
't.jsTsLanguageModes=[t.javascript,t.javascriptreact,t.typescript,t.typescriptreact]',

packages/language-server/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ connection.onInitialize(params => {
4949
let projectInfoPromise = file2ProjectInfo.get(fileName);
5050
if (!projectInfoPromise) {
5151
projectInfoPromise = sendTsRequest<ts.server.protocol.ProjectInfo>(
52-
ts.server.protocol.CommandTypes.ProjectInfo,
52+
'_vue:' + ts.server.protocol.CommandTypes.ProjectInfo,
5353
{
5454
file: fileName,
5555
needFileNameList: false,
@@ -89,35 +89,35 @@ connection.onInitialize(params => {
8989
},
9090
createVueLanguageServicePlugins(ts, {
9191
collectExtractProps(...args) {
92-
return sendTsRequest('vue:collectExtractProps', args);
92+
return sendTsRequest('_vue:collectExtractProps', args);
9393
},
9494
getComponentDirectives(...args) {
95-
return sendTsRequest('vue:getComponentDirectives', args);
95+
return sendTsRequest('_vue:getComponentDirectives', args);
9696
},
9797
getComponentEvents(...args) {
98-
return sendTsRequest('vue:getComponentEvents', args);
98+
return sendTsRequest('_vue:getComponentEvents', args);
9999
},
100100
getComponentNames(...args) {
101-
return sendTsRequest('vue:getComponentNames', args);
101+
return sendTsRequest('_vue:getComponentNames', args);
102102
},
103103
getComponentProps(...args) {
104-
return sendTsRequest('vue:getComponentProps', args);
104+
return sendTsRequest('_vue:getComponentProps', args);
105105
},
106106
getElementAttrs(...args) {
107-
return sendTsRequest('vue:getElementAttrs', args);
107+
return sendTsRequest('_vue:getElementAttrs', args);
108108
},
109109
getElementNames(...args) {
110-
return sendTsRequest('vue:getElementNames', args);
110+
return sendTsRequest('_vue:getElementNames', args);
111111
},
112112
getImportPathForFile(...args) {
113-
return sendTsRequest('vue:getImportPathForFile', args);
113+
return sendTsRequest('_vue:getImportPathForFile', args);
114114
},
115115
getPropertiesAtLocation(...args) {
116-
return sendTsRequest('vue:getPropertiesAtLocation', args);
116+
return sendTsRequest('_vue:getPropertiesAtLocation', args);
117117
},
118118
getDocumentHighlights(fileName, position) {
119119
return sendTsRequest(
120-
'documentHighlights-full', // internal command
120+
'_vue:documentHighlights-full',
121121
{
122122
file: fileName,
123123
...{ position } as unknown as { line: number; offset: number; },
@@ -127,7 +127,7 @@ connection.onInitialize(params => {
127127
},
128128
async getQuickInfoAtPosition(fileName, { line, character }) {
129129
const result = await sendTsRequest<ts.QuickInfo>(
130-
ts.server.protocol.CommandTypes.Quickinfo,
130+
'_vue:' + ts.server.protocol.CommandTypes.Quickinfo,
131131
{
132132
file: fileName,
133133
line: line + 1,

packages/typescript-plugin/index.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,47 +76,56 @@ export = createLanguageServicePlugin(
7676

7777
(session as any).vueCommandsAdded = true;
7878

79-
session.addProtocolHandler('vue:collectExtractProps', ({ arguments: args }) => {
79+
session.addProtocolHandler('_vue:projectInfo', ({ arguments: args }) => {
80+
return (session as any).handlers.get('projectInfo')?.({ arguments: args });
81+
});
82+
session.addProtocolHandler('_vue:documentHighlights-full', ({ arguments: args }) => {
83+
return (session as any).handlers.get('documentHighlights-full')?.({ arguments: args });
84+
});
85+
session.addProtocolHandler('_vue:quickinfo', ({ arguments: args }) => {
86+
return (session as any).handlers.get('quickinfo')?.({ arguments: args });
87+
});
88+
session.addProtocolHandler('_vue:collectExtractProps', ({ arguments: args }) => {
8089
return {
8190
response: collectExtractProps.apply(getRequestContext(args[0]), args),
8291
};
8392
});
84-
session.addProtocolHandler('vue:getImportPathForFile', ({ arguments: args }) => {
93+
session.addProtocolHandler('_vue:getImportPathForFile', ({ arguments: args }) => {
8594
return {
8695
response: getImportPathForFile.apply(getRequestContext(args[0]), args),
8796
};
8897
});
89-
session.addProtocolHandler('vue:getPropertiesAtLocation', ({ arguments: args }) => {
98+
session.addProtocolHandler('_vue:getPropertiesAtLocation', ({ arguments: args }) => {
9099
return {
91100
response: getPropertiesAtLocation.apply(getRequestContext(args[0]), args),
92101
};
93102
});
94-
session.addProtocolHandler('vue:getComponentNames', ({ arguments: args }) => {
103+
session.addProtocolHandler('_vue:getComponentNames', ({ arguments: args }) => {
95104
return {
96105
response: getComponentNames.apply(getRequestContext(args[0]), args) ?? [],
97106
};
98107
});
99-
session.addProtocolHandler('vue:getComponentProps', ({ arguments: args }) => {
108+
session.addProtocolHandler('_vue:getComponentProps', ({ arguments: args }) => {
100109
return {
101110
response: getComponentProps.apply(getRequestContext(args[0]), args),
102111
};
103112
});
104-
session.addProtocolHandler('vue:getComponentEvents', ({ arguments: args }) => {
113+
session.addProtocolHandler('_vue:getComponentEvents', ({ arguments: args }) => {
105114
return {
106115
response: getComponentEvents.apply(getRequestContext(args[0]), args),
107116
};
108117
});
109-
session.addProtocolHandler('vue:getComponentDirectives', ({ arguments: args }) => {
118+
session.addProtocolHandler('_vue:getComponentDirectives', ({ arguments: args }) => {
110119
return {
111120
response: getComponentDirectives.apply(getRequestContext(args[0]), args),
112121
};
113122
});
114-
session.addProtocolHandler('vue:getElementAttrs', ({ arguments: args }) => {
123+
session.addProtocolHandler('_vue:getElementAttrs', ({ arguments: args }) => {
115124
return {
116125
response: getElementAttrs.apply(getRequestContext(args[0]), args),
117126
};
118127
});
119-
session.addProtocolHandler('vue:getElementNames', ({ arguments: args }) => {
128+
session.addProtocolHandler('_vue:getElementNames', ({ arguments: args }) => {
120129
return {
121130
response: getElementNames.apply(getRequestContext(args[0]), args),
122131
};

0 commit comments

Comments
 (0)