Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vscode Language Client extension - how to send a message from the server to the client?

I have been developing a vscode extension that consits of client and server using the language server protocol.

At the moment, I am trying to do the following thing: when the server detects a certain condition, he requests the client to load a certain number of files into the workspace.

I am having serious problems doing this. Since the language server protocol does not have a specific request to do this I thought about sending a message from the server to the client and once the client detects this message he would proceed to execute this command.

The problem is, I also do not know how to do this. Can anyone please help me?

like image 593
Ricardo Ferreira da Silva Avatar asked Jun 26 '18 11:06

Ricardo Ferreira da Silva


People also ask

How does VS Code run client and server?

Press Ctrl+Shift+B to start the build task. The task compiles both the client and the server. Open the Run view, select the Launch Client launch configuration, and press the Start Debugging button to launch an additional Extension Development Host instance of VS Code that executes the extension code.

How do you send a VS Code?

If you select Start Collaboration session from the Session Details menu, an invitation link to your session will automatically be copied to your clipboard. You can share this link with anyone you'd like to collaborate with, as long as they also have VS Code and the Live Share Extension Pack downloaded.

What is CodeLens in Visual Studio Code?

CodeLens lets you stay focused on your work while you find out what happened to your code–without leaving the editor. You can find references to a piece of code, changes to your code, linked bugs, work items, code reviews, and unit tests.


1 Answers

As long as you're sure the name doesn't collide with existing LSP methods, you can define arbitrary methods of your own. For instance, in the official lsp-sample, you could do this:

(at the end of client/src/extension.ts)

let client = new LanguageClient('lspSample', 'Language Server Example', serverOptions, clientOptions);
client.onReady().then(() => {
    client.onNotification("custom/loadFiles", (files: Array<String>) => {
        console.log("loading files " + files);
    });
});
context.subscriptions.push(client.start());

(in the documents.onDidChangeContent listener of server/src/server.ts)

var files = ["path/to/file/a.txt", "path/to/file/b.txt"];
connection.sendNotification("custom/loadFiles", [files]);

This will output the following to the dev console whenever you change the contents of a .txt file (since the sample uses plaintext as its document selector):

loading files path/to/file/a.txt,path/to/file/b.txt

You pretty much have complete flexibility here when it comes to the names of custom methods, their parameters or when you invoke them. It's quite common for language servers to use custom methods like this that are not part of the protocol for various purposes (advanced functionality, internal debugging/development features, etc...).

like image 127
Gama11 Avatar answered Oct 04 '22 16:10

Gama11