且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Vscode语言客户端扩展 - 如何从服务器向客户端发送消息?

更新时间:2022-06-09 20:05:56

只要您确定该名称不会与现有的LSP方法发生冲突,可以定义自己的任意方法。例如,在官方lsp样本中,你可以这样做:

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:

(在 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());

(在 documents.onDidChangeContent 的监听器中 server / src / server.ts

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

每次更改 .txt 文件(因为该示例使用 plaintext 作为其文档选择器):

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


加载文件路径/到/ file / a.txt,路径/到/ file / b.txt

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