Language Server Protocol support for CodeMirror 6 — completions, hover, diagnostics, and more.
Autocomplete with resolve support, triggered by typing or trigger characters.
Rich hover tooltips with Markdown rendering for any symbol.
Real-time errors and warnings from the language server, shown inline.
Jump to definition, declaration, or type definition with F12.
All occurrences of a symbol are highlighted when the cursor is on it.
Format the entire document or a selection via the language server.
Rename a symbol across the document with an inline prompt.
WebSocket out of the box, or bring your own transport (Web Workers, MessagePort, etc.).
This editor is connected to a minimal JavaScript LSP server running entirely in a Web Worker — no backend needed.
npm i codemirror-languageserver
import { EditorState } from '@codemirror/state';
import { EditorView } from '@codemirror/view';
import { languageServer } from 'codemirror-languageserver';
const ls = languageServer({
serverUri: 'ws://localhost:8080',
rootUri: 'file:///',
documentUri: 'file:///main.cpp',
languageId: 'cpp',
});
const view = new EditorView({
state: EditorState.create({
extensions: [/* other extensions, */ ls],
}),
});
import { languageServerWithTransport } from 'codemirror-languageserver';
const worker = new Worker('./lsp-worker.js', { type: 'module' });
// WorkerTransport implements the Transport interface:
// send(), onMessage(), onClose(), onError(), close()
const transport = new WorkerTransport(worker);
const ls = languageServerWithTransport({
transport,
rootUri: 'file:///',
documentUri: 'file:///demo.js',
languageId: 'javascript',
});
import { keymap } from '@codemirror/view';
import {
formatDocument,
formatSelection,
renameSymbol,
} from 'codemirror-languageserver';
keymap.of([
{ key: 'Shift-Alt-f', run: formatDocument },
{ key: 'Ctrl-k Ctrl-f', run: formatSelection },
{ key: 'F2', run: renameSymbol },
])
languageServer({
serverUri: 'ws://localhost:8080',
rootUri: 'file:///',
documentUri: 'file:///main.cpp',
languageId: 'cpp',
onCapabilities(capabilities) {
// Update your UI based on what the server supports
// e.g. capabilities.documentFormattingProvider
},
onError(error) {
console.error('LSP error:', error);
},
onClose() {
console.log('LSP connection closed');
},
})