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

眉间皱痕 提交于 2019-12-01 06:14:11

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...).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!