22
33[ ![ npm version] ( https://badge.fury.io/js/codemirror-languageserver.svg )] ( https://www.npmjs.com/package/codemirror-languageserver )
44
5- This plugin enables code completion, hover tooltips, and linter functionality by connecting a CodeMirror 6 editor with a language server over WebSocket.
5+ This plugin connects a [ CodeMirror 6] ( https://codemirror.net/ ) editor with a [ Language Server ] ( https://microsoft.github.io/language- server-protocol/ ) over WebSocket, providing IDE-like features in the browser .
66
77[ How It Works] ( https://hjr265.me/blog/codemirror-lsp/ )
88
@@ -18,83 +18,140 @@ https://user-images.githubusercontent.com/348107/120141150-c6bb9180-c1fd-11eb-8a
1818- 🎨 Document Formatting and Range Formatting
1919- ✏️ Rename Symbol
2020
21- ## Usage
21+ ## Installation
2222
2323```
2424npm i codemirror-languageserver
2525```
2626
27+ ** Peer dependencies:** This package requires ` @codemirror/autocomplete ` , ` @codemirror/lint ` , ` @codemirror/state ` , and ` @codemirror/view ` as peer dependencies. If you're using CodeMirror 6, you likely already have these installed.
28+
29+ ## Quick Start
30+
2731``` js
32+ import { EditorState } from ' @codemirror/state' ;
33+ import { EditorView } from ' @codemirror/view' ;
2834import { languageServer } from ' codemirror-languageserver' ;
2935
30- var ls = languageServer ({
31- // WebSocket server uri and other client options.
32- serverUri,
36+ const ls = languageServer ({
37+ serverUri: ' ws://localhost:8080' ,
3338 rootUri: ' file:///' ,
34-
35- // Alternatively, to share the same client across multiple instances of this plugin.
36- client: new LanguageServerClient ({
37- transport: new WebSocketTransport (serverUri),
38- rootUri: ' file:///' ,
39- }),
40-
4139 documentUri: ` file:///${ filename} ` ,
4240 languageId: ' cpp' , // As defined at https://microsoft.github.io/language-server-protocol/specification#textDocumentItem.
4341});
4442
45- var view = new EditorView ({
43+ const view = new EditorView ({
4644 state: EditorState .create ({
4745 extensions: [
48- // ...
46+ // ... other extensions
4947 ls,
50- // ...
5148 ],
5249 }),
5350});
5451```
5552
56- ### Custom Transport
53+ This sets up all built-in features: completion, hover, diagnostics, go-to-definition, document highlight, and rename support.
5754
58- Use ` languageServerWithTransport ` to provide a custom transport (e.g. for non-WebSocket connections):
55+ ## Options
56+
57+ | Option | Type | Description |
58+ | ---| ---| ---|
59+ | ` serverUri ` | ` ws://... ` or ` wss://... ` | WebSocket server URI |
60+ | ` rootUri ` | ` string ` | Root URI of the workspace |
61+ | ` workspaceFolders ` | ` WorkspaceFolder[] ` | LSP workspace folders |
62+ | ` documentUri ` | ` string ` | URI of the document being edited |
63+ | ` languageId ` | ` string ` | Language identifier ([ spec] ( https://microsoft.github.io/language-server-protocol/specification#textDocumentItem ) ) |
64+ | ` initializationOptions ` | ` object ` | Server-specific initialization options |
65+ | ` locale ` | ` string ` | Locale for the LSP session (e.g. ` 'en' ` ) |
66+ | ` allowHTMLContent ` | ` boolean ` | Trust raw HTML in hover/completion content (default: ` false ` ) |
67+ | ` synchronizationMethod ` | ` SynchronizationMethod ` | ` 'full' ` or ` 'incremental' ` document sync (default: ` 'full' ` ) |
68+ | ` client ` | ` LanguageServerClient ` | Share a client across multiple editor instances |
69+ | ` onCapabilities ` | ` (capabilities) => void ` | Called when server capabilities are available |
70+
71+ ## Shared Client
72+
73+ To share the same language server connection across multiple editor instances:
74+
75+ ``` js
76+ import { LanguageServerClient , languageServerWithTransport , WebSocketTransport } from ' codemirror-languageserver' ;
77+
78+ const client = new LanguageServerClient ({
79+ transport: new WebSocketTransport (' ws://localhost:8080' ),
80+ rootUri: ' file:///' ,
81+ });
82+
83+ // Use the same client for multiple editors
84+ const ls1 = languageServerWithTransport ({
85+ client,
86+ documentUri: ' file:///main.cpp' ,
87+ languageId: ' cpp' ,
88+ });
89+
90+ const ls2 = languageServerWithTransport ({
91+ client,
92+ documentUri: ' file:///utils.cpp' ,
93+ languageId: ' cpp' ,
94+ });
95+ ```
96+
97+ ## Custom Transport
98+
99+ Use ` languageServerWithTransport ` for non-WebSocket connections:
59100
60101``` js
61102import { languageServerWithTransport } from ' codemirror-languageserver' ;
62103
63- var ls = languageServerWithTransport ({
64- transport: myCustomTransport, // Must implement the Transport interface
104+ const ls = languageServerWithTransport ({
105+ transport: myCustomTransport,
65106 rootUri: ' file:///' ,
66107 documentUri: ` file:///${ filename} ` ,
67108 languageId: ' cpp' ,
68109});
69110```
70111
71- The ` Transport ` interface requires ` send ` , ` onMessage ` , ` onClose ` , ` onError ` , and ` close ` methods. You can import ` Transport ` and ` WebSocketTransport ` from the package.
112+ The ` Transport ` interface:
113+
114+ ``` ts
115+ interface Transport {
116+ send(message : string ): void ;
117+ onMessage(callback : (message : string ) => void ): void ;
118+ onClose(callback : () => void ): void ;
119+ onError(callback : (error : Error ) => void ): void ;
120+ close(): void ;
121+ }
122+ ```
123+
124+ You can import ` Transport ` and ` WebSocketTransport ` from the package.
72125
73- ### Document Highlight
126+ ## Document Highlight
74127
75- Document highlights are enabled by default. When the cursor is on a symbol, all occurrences are highlighted. Style them with CSS:
128+ Enabled by default. When the cursor is on a symbol, all occurrences in the document are highlighted. Style with CSS:
76129
77130``` css
78131.cm-lsp-highlight-text ,
79132.cm-lsp-highlight-read { background-color : rgba (255 , 255 , 0 , 0.2 ); }
80133.cm-lsp-highlight-write { background-color : rgba (255 , 165 , 0 , 0.3 ); }
81134```
82135
83- ### Formatting
136+ The three classes correspond to [ DocumentHighlightKind ] ( https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#documentHighlightKind ) : general text occurrences, read accesses, and write accesses.
84137
85- ` formatDocument ` and ` formatSelection ` are CodeMirror commands that you can bind to keys:
138+ ## Formatting
139+
140+ ` formatDocument ` and ` formatSelection ` are CodeMirror [ commands] ( https://codemirror.net/docs/ref/#view.Command ) — bind them to keys:
86141
87142``` js
88143import { keymap } from ' @codemirror/view' ;
89144import { formatDocument , formatSelection } from ' codemirror-languageserver' ;
90145
91- keymap .of ([
146+ const formattingKeymap = keymap .of ([
92147 { key: ' Shift-Alt-f' , run: formatDocument },
93148 { key: ' Ctrl-k Ctrl-f' , run: formatSelection },
94- ])
149+ ]);
95150```
96151
97- To configure formatting options (tab size, spaces vs tabs, etc.), use the ` formattingOptions ` facet:
152+ ### Formatting Options
153+
154+ Configure formatting with the ` formattingOptions ` facet. By default, ` tabSize ` is read from the editor state.
98155
99156``` js
100157import { formattingOptions } from ' codemirror-languageserver' ;
@@ -105,61 +162,89 @@ formattingOptions.of({
105162 insertSpaces: true ,
106163 trimTrailingWhitespace: true ,
107164 insertFinalNewline: true ,
165+ trimFinalNewlines: true ,
108166})
109167```
110168
111- By default, ` tabSize ` is read from the editor state.
112-
113- ### Rename Symbol
169+ ## Rename Symbol
114170
115- ` renameSymbol ` is a CodeMirror command that opens a rename prompt at the top of the editor:
171+ ` renameSymbol ` is a CodeMirror command that opens a rename prompt at the top of the editor. If the server supports ` prepareRename ` , the current symbol name is used as the placeholder.
116172
117173``` js
118174import { keymap } from ' @codemirror/view' ;
119175import { renameSymbol } from ' codemirror-languageserver' ;
120176
121- keymap .of ([
177+ const renameKeymap = keymap .of ([
122178 { key: ' F2' , run: renameSymbol },
123- ])
179+ ]);
124180```
125181
126- Style the rename panel with CSS using the ` cm-lsp-rename-panel ` and ` cm-lsp-rename-input ` classes.
182+ Style the panel with CSS:
127183
128- ### Server Capabilities
184+ ``` css
185+ .cm-lsp-rename-panel {
186+ padding : 4px 8px ;
187+ border-bottom : 1px solid #ddd ;
188+ }
189+ .cm-lsp-rename-input {
190+ font-family : inherit ;
191+ font-size : inherit ;
192+ }
193+ ```
129194
130- Use the ` onCapabilities ` callback to be notified when the server's capabilities are available (e.g. to update a toolbar):
195+ ## Server Capabilities
196+
197+ Use the ` onCapabilities ` callback to react when the server finishes initializing — e.g. to show or hide toolbar buttons:
131198
132199``` js
133200languageServer ({
134- serverUri,
201+ serverUri: ' ws://localhost:8080 ' ,
135202 rootUri: ' file:///' ,
136203 documentUri: ` file:///${ filename} ` ,
137204 languageId: ' cpp' ,
138205 onCapabilities (capabilities ) {
139- // capabilities.documentFormattingProvider, capabilities.renameProvider, etc.
206+ // capabilities.documentFormattingProvider
207+ // capabilities.renameProvider
208+ // capabilities.completionProvider
209+ // etc.
140210 toolbar .update ({ capabilities });
141211 },
142212})
143213```
144214
145- ### Using with Initialization Options
215+ ## Initialization Options
216+
217+ The package includes TypeScript definitions for popular language servers:
146218
147- The plugin includes built-in TypeScript definitions for popular language servers:
219+ - ` PyrightInitializationOptions ` — Python (Pyright)
220+ - ` RustAnalyzerInitializationOptions ` — Rust (rust-analyzer)
221+ - ` TypeScriptInitializationOptions ` — TypeScript/JavaScript
222+ - ` ESLintInitializationOptions ` — ESLint
223+ - ` ClangdInitializationOptions ` — C/C++ (Clangd)
224+ - ` GoplsInitializationOptions ` — Go (Gopls)
148225
149- - ` PyrightInitializationOptions ` - Python (Pyright)
150- - ` RustAnalyzerInitializationOptions ` - Rust (rust-analyzer)
151- - ` TypeScriptInitializationOptions ` - TypeScript/JavaScript
152- - ` ESLintInitializationOptions ` - ESLint
153- - ` ClangdInitializationOptions ` - C/C++ (Clangd)
154- - ` GoplsInitializationOptions ` - Go (Gopls)
226+ ``` ts
227+ import { languageServer } from ' codemirror-languageserver' ;
228+ import type { ClangdInitializationOptions } from ' codemirror-languageserver' ;
229+
230+ const ls = languageServer <ClangdInitializationOptions >({
231+ serverUri: ' ws://localhost:8080' ,
232+ rootUri: ' file:///' ,
233+ documentUri: ' file:///main.cpp' ,
234+ languageId: ' cpp' ,
235+ initializationOptions: {
236+ // Type-checked options specific to Clangd
237+ },
238+ });
239+ ```
155240
156241## Contributing
157242
158243Contributions are welcome.
159244
160245## Real World Uses
161246
162- - [ Toph] ( https://toph.co ) : Competitive programming platform. Toph uses Language Server Plugin for CodeMirror 6 with its integrated code editor.
247+ - [ Toph] ( https://toph.co ) : Competitive programming platform. Toph uses Language Server Plugin for CodeMirror 6 with its integrated code editor.
163248
164249## License
165250
0 commit comments