Language Server Protocol implementation for Node.js providing comprehensive LSP server capabilities including text synchronization, diagnostics, code completion, and workspace symbol searching
npx @tessl/cli install tessl/npm-vscode-languageserver@9.0.0The VSCode Language Server is a comprehensive implementation of the Language Server Protocol (LSP) for Node.js and browser environments. It provides server-side infrastructure for building language servers that power intelligent code features in Visual Studio Code and other editors supporting LSP.
npm install vscode-languageserverimport { createConnection, TextDocuments, ProposedFeatures } from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";For CommonJS:
const { createConnection, TextDocuments, ProposedFeatures } = require("vscode-languageserver");Node.js specific imports:
import { createConnection } from "vscode-languageserver/node";Browser specific imports:
import { createConnection } from "vscode-languageserver/browser";import { createConnection, TextDocuments, ProposedFeatures } from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
// Create a connection for the server
const connection = createConnection(ProposedFeatures.all);
// Create a simple text document manager
const documents = new TextDocuments(TextDocument);
// Listen for document changes
documents.onDidChangeContent(change => {
// Validate the document when its content has changed
validateTextDocument(change.document);
});
// Listen for when a text document is opened
documents.onDidOpen(event => {
validateTextDocument(event.document);
});
// Provide hover information
connection.onHover(params => {
return {
contents: "Hover information here"
};
});
// Start listening for messages
documents.listen(connection);
connection.listen();The VSCode Language Server is built around several key components:
Core connection establishment and communication infrastructure for Language Server Protocol implementations. Essential for all language servers.
function createConnection(): Connection;
function createConnection(options?: ConnectionStrategy | ConnectionOptions): Connection;
function createConnection(
inputStream: NodeJS.ReadableStream,
outputStream: NodeJS.WritableStream,
options?: ConnectionStrategy | ConnectionOptions
): Connection;
function createConnection(
reader: MessageReader,
writer: MessageWriter,
options?: ConnectionStrategy | ConnectionOptions
): Connection;Text document lifecycle management and synchronization between client and server. Handles document open, change, save, and close events.
class TextDocuments<T extends { uri: DocumentUri }> {
constructor(configuration: TextDocumentsConfiguration<T>);
onDidOpen: Event<TextDocumentChangeEvent<T>>;
onDidClose: Event<TextDocumentChangeEvent<T>>;
onDidChangeContent: Event<TextDocumentChangeEvent<T>>;
onDidSave: Event<TextDocumentChangeEvent<T>>;
onWillSave: Event<TextDocumentWillSaveEvent<T>>;
get(uri: DocumentUri): T | undefined;
all(): T[];
keys(): DocumentUri[];
listen(connection: TextDocumentConnection): void;
}
interface TextDocumentsConfiguration<T extends { uri: DocumentUri }> {
create(uri: DocumentUri, languageId: string, version: number, content: string): T;
update(document: T, changes: TextDocumentContentChangeEvent[], version: number): T;
}Complete Language Server Protocol feature implementations including completion, hover, diagnostics, and all standard LSP capabilities.
interface Connection {
onCompletion(handler: CompletionHandler): Disposable;
onHover(handler: HoverHandler): Disposable;
onDefinition(handler: DefinitionHandler): Disposable;
onReferences(handler: ReferencesHandler): Disposable;
onDocumentSymbol(handler: DocumentSymbolHandler): Disposable;
// ... all other LSP handlers
}
type CompletionHandler = RequestHandler<CompletionParams, CompletionItem[] | CompletionList | null, CompletionItem>;
type HoverHandler = RequestHandler<HoverParams, Hover | null, void>;
type DefinitionHandler = RequestHandler<DefinitionParams, Definition | DefinitionLink[] | null, void>;Advanced Language Server Protocol features including semantic tokens, call hierarchy, type hierarchy, and diagnostics.
interface Languages {
semanticTokens: {
on(handler: SemanticTokensHandler): Disposable;
onDelta(handler: SemanticTokensDeltaHandler): Disposable;
onRange(handler: SemanticTokensRangeHandler): Disposable;
refresh(): void;
};
callHierarchy: {
onPrepare(handler: CallHierarchyPrepareHandler): Disposable;
onIncomingCalls(handler: CallHierarchyIncomingCallsHandler): Disposable;
onOutgoingCalls(handler: CallHierarchyOutgoingCallsHandler): Disposable;
};
typeHierarchy: {
onPrepare(handler: TypeHierarchyPrepareHandler): Disposable;
onSupertypes(handler: TypeHierarchySupertypesHandler): Disposable;
onSubtypes(handler: TypeHierarchySubtypesHandler): Disposable;
};
}Notebook document synchronization and management for Jupyter notebooks and other notebook formats. Supports both notebook-level and cell-level text document management.
class NotebookDocuments<T extends { uri: DocumentUri }> {
constructor(configurationOrTextDocuments: TextDocumentsConfiguration<T> | TextDocuments<T>);
onDidOpen: Event<NotebookDocument>;
onDidChange: Event<NotebookDocumentChangeEvent>;
onDidSave: Event<NotebookDocument>;
onDidClose: Event<NotebookDocument>;
getCellTextDocument(cellUri: DocumentUri): T | undefined;
getNotebookDocument(notebookUri: DocumentUri): NotebookDocument | undefined;
getNotebookCell(cellUri: DocumentUri): NotebookCell | undefined;
}Helper classes for common language server tasks including semantic token building, error tracking, and progress reporting.
class SemanticTokensBuilder {
constructor();
push(line: number, char: number, length: number, tokenType: number, tokenModifiers?: number): void;
build(): SemanticTokens;
buildEdits(): SemanticTokensEdits;
}
class ErrorMessageTracker {
constructor();
add(message: string): void;
sendErrors(connection: { window: RemoteWindow }): void;
}interface Connection extends _Connection {
console: RemoteConsole;
tracer: RemoteTracer;
telemetry: Telemetry;
client: RemoteClient;
window: RemoteWindow;
workspace: RemoteWorkspace;
languages: Languages;
notebooks: Notebooks;
onInitialize(handler: RequestHandler<InitializeParams, InitializeResult, InitializeError>): Disposable;
onInitialized(handler: NotificationHandler<InitializedParams>): Disposable;
onShutdown(handler: RequestHandler0<void, void>): Disposable;
onExit(handler: NotificationHandler0): Disposable;
sendDiagnostics(params: PublishDiagnosticsParams): void;
sendProgress<T>(type: ProgressType<T>, token: string | number, value: T): void;
listen(): void;
dispose(): void;
}
interface TextDocumentChangeEvent<T> {
document: T;
}
interface TextDocumentWillSaveEvent<T> {
document: T;
reason: TextDocumentSaveReason;
}
type DocumentUri = string;
interface Disposable {
dispose(): void;
}
type Event<T> = (listener: (e: T) => any, thisArg?: any) => Disposable;