cdturner
cdturner

Reputation: 432

visual studio language extension, how do I call my own functions?

I've implemented my own language server (based on the lsp-sample in the visual studio sample extensions git tree). It does basic language parsing ..etc.

I would like to provide some simple functions in the client side of my extension, that need access to data that is already parsed in the server portion of my extension.

how do I add my own calls to the client/server so I can ask my server for stuff ?

I'm looking to send commands/calls from the client side, to the server side and get an answer back.

I looked around and there is a LanguageClient.registerFeature() is that part of the answer ?

Any help would be greatly appreciated. Especially with sample code.

cdturner

Upvotes: 3

Views: 1747

Answers (1)

Gama11
Gama11

Reputation: 34138

You can use the generic sendRequest() / onRequest() with custom method names for this. A simple example based on the lsp-sample could look like this:

(in activate() of client/src/extension.ts)

client.onReady().then(() => {
    client.sendRequest("custom/data", "foo").then(data => console.log(data));
});

(in the onInitialized callback of server/src/server.ts)

connection.onRequest("custom/data", param => "received parameter '" + param + "'");

With those changes, the following output can be observed in the dev console:


Communication in the opposite direction looks very similar - see here for an example of sending a custom notification from the server to the client.

Upvotes: 3

Related Questions