简体   繁体   English

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

[英]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. 我一直在使用语言服务器协议开发一个使用客户端和服务器的vscode扩展。

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? 谁能帮帮我吗?

As long as you're sure the name doesn't collide with existing LSP methods, you can define arbitrary methods of your own. 只要您确定该名称不会与现有的LSP方法发生冲突,您就可以定义自己的任意方法。 For instance, in the official lsp-sample , you could do this: 例如,在官方的lsp-sample中 ,你可以这样做:

(at the end of client/src/extension.ts ) (在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 ) (在server/src/server.tsdocuments.onDidChangeContent侦听器中)

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): 每当您更改.txt文件的内容时,都会将以下内容输出到开发控制台(因为该示例使用plaintext作为其文档选择器):

loading files path/to/file/a.txt,path/to/file/b.txt 加载文件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...). 语言服务器使用这样的自定义方法是很常见的,这些方法不是用于各种目的的协议的一部分(高级功能,内部调试/开发功能等......)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM