2

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

cdturner
  • 392
  • 3
  • 11
  • Possible duplicate of [Vscode Language Client extension - how to send a message from the server to the client?](https://stackoverflow.com/questions/51041337/vscode-language-client-extension-how-to-send-a-message-from-the-server-to-the) – Gama11 Aug 12 '18 at 09:24
  • thats really handy to know, for other reasons, but is the wrong direction, that sends notifications from server back to client side. I need the client side to ask/send/call to the server side, and get an answer back. – cdturner Aug 13 '18 at 18:07
  • Ah. Well, you can create custom methods like that in both directions. – Gama11 Aug 13 '18 at 21:23
  • do you have any reference to how to create things like that ? I've found no samples. – cdturner Aug 14 '18 at 05:04
  • I added an answer with a simple example. – Gama11 Aug 14 '18 at 08:40

1 Answers1

2

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.

Gama11
  • 31,714
  • 9
  • 78
  • 100