Communicate language server with client in VSCode extension
vscode extension - published at 14/08/2023
In vscode extension, language client can send messeages with client.sendRequest
and language server can receive them with connection.onRequest
.
// shared/requests.ts
import { RequestType } from 'vscode-jsonrpc/node';
export type MyPackageFooRequestParams = {
someArg: string;
};
export const MyPackageFooRequestType = new RequestType<
// payload
MyPackageFooRequestParams,
// return type
string,
void
>('my-package/foo');
// client
const params: MyPackageFooRequestParams = {
someArg: 'anything',
};
const res = await client.sendRequest(MyPackageFooRequestType, params);
// server
connection.onRequest(MyPackageFooRequestType, async (params) => {
const { someArg } = params;
// do something...
// send back to client
return 'anything';
});
RequestType
can be imported from vscode-languageclient/node
and vscode-languageserver/node
, but if you want to share the code, I recommend to import from vscode-jsonrpc
, which is internally used in both vscode-languageclient/node
and vscode-languageserver/node
.
References: