简体   繁体   中英

Connect external language server to VSCode extension

I want to implement a VSCode extension that uses the Language Server Protocol , but I want the server component to be on an actual server (in the cloud), and not a part of the VSCode extension.

Can I set the client extension to connect to a server via websockets or HTTP?

I am not sure if you can control the location of the language server, but there is another option. You do not need to implement the Language Server Protocol to, for example, provide parsing help. In that case you can implement your own convenient parsing service API (tailored to the nature of the language you want to support).

  1. Within your extension you subscribe to workspace edit events using workspace.onDidChangeTextDocument
  2. Re-start a 1sec timeout every time the file on-change event is raised
  3. When the timeout expires without any further file modification, gather all relevant files and send them to your parsing server
  4. In your extension, create a DiagnosticCollection using https://code.visualstudio.com/api/references/vscode-api#languages.createDiagnosticCollection and replace populate it with the warnings/errors/hints resulting from the parsing server in the cloud.
  5. Subscribe to other workspace events, eg workspace.onDidOpenTextDocument or workspace.onDidCloseTextDocument in order to keep the DiagnosticCollection content relevant

Multiple ServerOptions are supported when you initialize a LanguageClient according to the signature of ServerOptions . 在此处输入图片说明

you can use the StreamInfo if you want to use a real remove server as your language server. Here is a sample code to connect to your server via WebSocket and initialize a LanguageClient .

const connection = connectToServer(hostname, path);
const client = new LanguageClient(
    "docfxLanguageServer",
    "Docfx Language Server",
    () => Promise.resolve<StreamInfo>({
        reader: connection,
        writer: connection,
    }),
    {});

private connectToServer(hostname: string, path: string): Duplex {
    const ws = new WebSocket(`ws://${hostname}/${path}`);
    return WebSocket.createWebSocketStream(ws);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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