Can I test my LSP server without writing an extension?

3k views Asked by At

I'm writing a Language Server Protocol (LSP) server for use with neovim, and I'd like to test it against VSCode to make sure I've got all the details right and that I'm not accidentally encoding any neovim-isms into my implementation.

The current docs suggest I should write a VSCode extension to act as the client to my server, but I'm not really interested in putting that much effort into a platform I won't use.

In neovim I can just define a CLI command and the filetype it corresponds to and hit go:

let g:LanguageClient_serverCommands = {
    \ 'rust': ['rustup', 'run', 'nightly', 'rls'],
    \ 'javascript': ['/opt/javascript-typescript-langserver/lib/language-server-stdio.js'],
    \ }

does something similar exist in Visual Studio code?

1

There are 1 answers

1
Youssef SABIH On

It takes little effort to write a language client in VSCode, here is how I did it:

export function activate(context: vscode.ExtensionContext) {

    // This line of code will only be executed once when your extension is activated

    // TODO: Start server exe and communicate with it
    let serverExe = <Path_to_server>;

    let ServerOptions: ServerOptions = {
        run: {command: serverExe, args:['-lsp']},
        debug: {command: serverExe, args:['-lsp']}
    }

    let clientOptions: LanguageClientOptions = {
        // Register the server for plain text documents
        documentSelector: [
            {
                pattern: '**/*.txt',
            }
        ],

    }

    let lspClient = new LanguageClient("Hello LSP", ServerOptions, clientOptions);

    // For debugging only
    //lspClient.trace = Trace.Verbose;

    //add all disposables here
    context.subscriptions.push(lspClient.start());
}

Once the client starts, it starts the server and starts the initialization conversation, the client watches all the important events on VSCode (document opened/closed/modified, Ctrl+space,...) and sends the right requests/notifications to the server