簡體   English   中英

Vscode語言客戶端擴展 - 如何從服務器向客戶端發送消息?

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

我一直在使用語言服務器協議開發一個使用客戶端和服務器的vscode擴展。

目前,我正在嘗試執行以下操作:當服務器檢測到某種情況時,他請求客戶端將一定數量的文件加載到工作區中。

我這樣做有嚴重的問題。 由於語言服務器協議沒有執行此操作的特定請求,因此我考慮從服務器向客戶端發送消息,並且一旦客戶端檢測到此消息,他將繼續執行此命令。

問題是,我也不知道該怎么做。 誰能幫幫我嗎?

只要您確定該名稱不會與現有的LSP方法發生沖突,您就可以定義自己的任意方法。 例如,在官方的lsp-sample中 ,你可以這樣做:

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

(在server/src/server.tsdocuments.onDidChangeContent偵聽器中)

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

每當您更改.txt文件的內容時,都會將以下內容輸出到開發控制台(因為該示例使用plaintext作為其文檔選擇器):

加載文件path / to / file / a.txt,path / to / file / b.txt

在自定義方法的名稱,參數或調用它們時,您在這里幾乎具有完全的靈活性。 語言服務器使用這樣的自定義方法是很常見的,這些方法不是用於各種目的的協議的一部分(高級功能,內部調試/開發功能等......)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM