VSCode Language Client

9.0.1 · active · verified Tue Apr 21

The `vscode-languageclient` package provides the foundational client-side implementation for integrating Language Servers into VS Code extensions. It fully adheres to the Language Server Protocol (LSP), abstracting away the complexities of inter-process communication, managing the language server's lifecycle (startup, shutdown, restart), and seamlessly mapping LSP messages to the corresponding VS Code API calls. This library is the official, actively maintained client by Microsoft, ensuring deep integration and compatibility with the editor's evolving features and API, making it the de-facto standard for building robust language extensions. The current stable version is 9.0.1, with active development evident through frequent `10.0.0-next.x` pre-releases, often aligning with major VS Code updates. Its release cadence is primarily driven by the needs of VS Code's extension ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and activate a `LanguageClient` in a VS Code extension, configuring it to connect to a Node.js-based language server via standard I/O (stdio) and handle its lifecycle.

import * as path from 'path';
import { ExtensionContext, workspace } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node';

let client: LanguageClient;

export function activate(context: ExtensionContext) {
  // The server is implemented in Node.js
  // Path to the server module (your actual language server's main file)
  const serverModule = context.asAbsolutePath(path.join('server', 'out', 'server.js'));

  // Debug options for the server
  // --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to it
  const debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] };

  // Server options: either run or debug mode
  const serverOptions: ServerOptions = {
    run: { module: serverModule, transport: TransportKind.stdio },
    debug: { module: serverModule, transport: TransportKind.stdio, options: debugOptions }
  };

  // Options to control the language client
  const clientOptions: LanguageClientOptions = {
    // Register the server for documents matching a specific language ID or scheme
    documentSelector: [{ scheme: 'file', language: 'plaintext' }],
    synchronize: {
      // Notify the server about file changes to '.clientrc' files in the workspace
      fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
    },
    outputChannelName: 'My Language Client',
    traceOutputChannel: workspace.window.createOutputChannel('My Language Client Trace')
  };

  // Create the language client and start it. This will also launch the server.
  client = new LanguageClient(
    'myLanguageServer',
    'My Language Server',
    serverOptions,
    clientOptions
  );

  // Start the client. This returns a disposable which will stop the client
  // when the extension is deactivated.
  client.start();

  // Add the client to the context's subscriptions so it is stopped on deactivate
  context.subscriptions.push(client);
}

export function deactivate(): Thenable<void> | undefined {
  if (!client) {
    return undefined;
  }
  return client.stop();
}

view raw JSON →