codemirror-languageserver 1.20.0 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -67,6 +67,28 @@ This sets up all built-in features: completion, hover, diagnostics, go-to-defini
67
67
  | `synchronizationMethod` | `SynchronizationMethod` | `'full'` or `'incremental'` document sync (default: `'full'`) |
68
68
  | `client` | `LanguageServerClient` | Share a client across multiple editor instances |
69
69
  | `onCapabilities` | `(capabilities) => void` | Called when server capabilities are available |
70
+ | `onError` | `(error: Error) => void` | Called when the connection encounters an error |
71
+ | `onClose` | `() => void` | Called when the connection is closed |
72
+
73
+ ## Error Handling
74
+
75
+ The plugin does not crash on connection failures. Use `onError` and `onClose` to handle disconnections:
76
+
77
+ ```js
78
+ languageServer({
79
+ serverUri: 'ws://localhost:8080',
80
+ rootUri: 'file:///',
81
+ documentUri: `file:///${filename}`,
82
+ languageId: 'cpp',
83
+ onError(error) {
84
+ console.error('LSP error:', error);
85
+ },
86
+ onClose() {
87
+ console.log('LSP connection closed');
88
+ // Recreate the editor extensions to reconnect
89
+ },
90
+ })
91
+ ```
70
92
 
71
93
  ## Shared Client
72
94
 
package/dist/index.js CHANGED
@@ -35,8 +35,10 @@ class JSONRPCClient {
35
35
  this.nextId = 0;
36
36
  this.pending = new Map();
37
37
  this.notificationHandlers = [];
38
+ this.errorHandlers = [];
39
+ this.closeHandlers = [];
38
40
  this.transport = transport;
39
- this.ready = new Promise((resolve, reject) => {
41
+ this.ready = new Promise((resolve) => {
40
42
  if (transport instanceof WebSocketTransport) {
41
43
  const ws = transport.connection;
42
44
  if (ws.readyState === WebSocket.OPEN) {
@@ -44,13 +46,27 @@ class JSONRPCClient {
44
46
  }
45
47
  else {
46
48
  ws.addEventListener('open', () => resolve());
47
- ws.addEventListener('error', () => reject(new Error('WebSocket connection failed')));
48
49
  }
49
50
  }
50
51
  else {
51
52
  resolve();
52
53
  }
53
54
  });
55
+ transport.onError((error) => {
56
+ for (const handler of this.errorHandlers) {
57
+ handler(error);
58
+ }
59
+ });
60
+ transport.onClose(() => {
61
+ for (const [, pending] of this.pending) {
62
+ clearTimeout(pending.timer);
63
+ pending.reject(new Error('Connection closed'));
64
+ }
65
+ this.pending.clear();
66
+ for (const handler of this.closeHandlers) {
67
+ handler();
68
+ }
69
+ });
54
70
  transport.onMessage((raw) => {
55
71
  let msg;
56
72
  try {
@@ -107,6 +123,12 @@ class JSONRPCClient {
107
123
  onNotification(handler) {
108
124
  this.notificationHandlers.push(handler);
109
125
  }
126
+ onError(handler) {
127
+ this.errorHandlers.push(handler);
128
+ }
129
+ onClose(handler) {
130
+ this.closeHandlers.push(handler);
131
+ }
110
132
  close() {
111
133
  for (const [id, pending] of this.pending) {
112
134
  clearTimeout(pending.timer);
@@ -192,7 +214,19 @@ class LanguageServerClient {
192
214
  }
193
215
  this.processNotification(data);
194
216
  });
195
- this.initializePromise = this.initialize();
217
+ this.client.onError((error) => {
218
+ var _a, _b;
219
+ (_b = (_a = this.options).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
220
+ });
221
+ this.client.onClose(() => {
222
+ var _a, _b;
223
+ this.ready = false;
224
+ (_b = (_a = this.options).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
225
+ });
226
+ this.initializePromise = this.initialize().catch((error) => {
227
+ var _a, _b;
228
+ (_b = (_a = this.options).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
229
+ });
196
230
  }
197
231
  getInitializeParams() {
198
232
  var _a;
package/dist/jsonrpc.d.ts CHANGED
@@ -27,6 +27,8 @@ export declare class JSONRPCClient {
27
27
  private nextId;
28
28
  private pending;
29
29
  private notificationHandlers;
30
+ private errorHandlers;
31
+ private closeHandlers;
30
32
  private ready;
31
33
  constructor(transport: Transport);
32
34
  request(req: {
@@ -38,5 +40,7 @@ export declare class JSONRPCClient {
38
40
  params: any;
39
41
  }): Promise<void>;
40
42
  onNotification(handler: (data: any) => void): void;
43
+ onError(handler: (error: Error) => void): void;
44
+ onClose(handler: () => void): void;
41
45
  close(): void;
42
46
  }
package/dist/plugin.d.ts CHANGED
@@ -215,5 +215,9 @@ export interface LanguageServerClientOptions<InitializationOptions = unknown> ex
215
215
  locale?: string;
216
216
  /** Called once after initialization with the server's capabilities. */
217
217
  onCapabilities?: (capabilities: LSP.ServerCapabilities) => void;
218
+ /** Called when the connection encounters an error. */
219
+ onError?: (error: Error) => void;
220
+ /** Called when the connection is closed. */
221
+ onClose?: () => void;
218
222
  }
219
223
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codemirror-languageserver",
3
- "version": "1.20.0",
3
+ "version": "1.21.0",
4
4
  "description": "Language Server Plugin for CodeMirror 6",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",