VS Code Language Server

9.0.1 · active · verified Sun Apr 19

The `vscode-languageserver` package provides a Node.js-based implementation of the Language Server Protocol (LSP), allowing developers to build language servers that offer rich language-specific features (e.g., autocomplete, diagnostics, go-to-definition, refactoring) to various text editors and IDEs, primarily VS Code. It abstracts the complexities of the LSP specification and JSON-RPC communication, enabling a focus on core language logic. The current stable version is `9.0.1`, with `10.0.0` actively in `next` development, signaling upcoming significant updates and LSP specification alignments. This project is a core component of the official VS Code ecosystem, characterized by its robust, well-maintained, and protocol-compliant tooling, making it a de facto standard for Node.js-based LSP servers. The release cadence includes frequent `next` versions for client, server, protocol, and jsonrpc packages, reflecting continuous integration and adaptation to evolving LSP standards.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic Language Server that listens for 'initialize' and 'didChangeContent' events, and provides simple completion items. It demonstrates connection creation, document management, and basic server capabilities declaration.

import {
  createConnection,
  TextDocuments,
  ProposedFeatures,
  InitializeParams,
  InitializeResult,
  TextDocumentSyncKind,
} from 'vscode-languageserver/node';
import { TextDocument } from 'vscode-languageserver-textdocument';

// Create a connection for the server. The connection uses Node's IPC as a transport.
const connection = createConnection(ProposedFeatures.all);

// Create a simple text document manager. The text document manager
// supports full document sync only and tracks open, change, and close events.
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);

connection.onInitialize((params: InitializeParams) => {
  const capabilities = params.capabilities;

  const result: InitializeResult = {
    capabilities: {
      textDocumentSync: TextDocumentSyncKind.Full,
      // Tell the client that the server supports code completion
      completionProvider: {
        resolveProvider: true, // We need to resolve additional information for a completion item
        triggerCharacters: ['.']
      },
      hoverProvider: true,
    },
  };
  return result;
});

connection.onInitialized(() => {
  connection.console.log('Language server initialized!');
});

// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent(change => {
  connection.console.log(`Document changed: ${change.document.uri}`);
  // In a real server, you would perform diagnostics here
  // connection.sendDiagnostics({ uri: change.document.uri, diagnostics: [] });
});

connection.onCompletion(
  (_textDocumentPosition, _token) => {
    // This is a very basic completion provider. In a real server,
    // you would analyze the document and provide context-aware suggestions.
    return [
      { label: 'console', kind: 18 }, // Method
      { label: 'log', kind: 6 }, // Function
      { label: 'warn', kind: 6 },
      { label: 'error', kind: 6 }
    ];
  }
);

// This handler resolves additional information for the selected completion item.
connection.onCompletionResolve((item) => {
  if (item.label === 'log') {
    item.detail = 'Logs a message to the console.';
    item.documentation = 'The `console.log()` method outputs a message to the web console.';
  }
  return item;
});

// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection);

// Listen on the connection
connection.listen();

view raw JSON →