codemirror-languageserver 1.17.1 → 1.18.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.
@@ -0,0 +1,3 @@
1
+ import type { Text, ChangeSet } from '@codemirror/state';
2
+ import type * as LSP from 'vscode-languageserver-protocol';
3
+ export declare function changeSetToEvents(doc: Text, changes: ChangeSet): LSP.TextDocumentContentChangeEvent[];
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- export { LanguageServerClient, languageServerPlugin } from './plugin';
1
+ export { LanguageServerClient, languageServerPlugin, SynchronizationMethod, } from './plugin';
2
2
  export { jumpToDefinition, jumpToDefinitionPos, jumpToDefinitionKeymap, } from './definition';
3
3
  export { PyrightInitializationOptions, RustAnalyzerInitializationOptions, TypeScriptInitializationOptions, ESLintInitializationOptions, ClangdInitializationOptions, GoplsInitializationOptions, } from './initialization';
4
- import { LanguageServerClient } from './plugin';
4
+ import { LanguageServerClient, SynchronizationMethod } from './plugin';
5
5
  import type { LanguageServerClientOptions, LanguageServerBaseOptions } from './plugin';
6
6
  interface LanguageServerOptions<InitializationOptions = unknown> extends LanguageServerClientOptions<InitializationOptions> {
7
7
  client?: LanguageServerClient<InitializationOptions>;
8
8
  allowHTMLContent?: boolean;
9
+ synchronizationMethod?: SynchronizationMethod;
9
10
  }
10
11
  interface LanguageServerWebsocketOptions<InitializationOptions = unknown> extends LanguageServerBaseOptions {
11
12
  serverUri: `ws://${string}` | `wss://${string}`;
package/dist/index.js CHANGED
@@ -24,8 +24,28 @@ function offsetToPos(doc, offset) {
24
24
  };
25
25
  }
26
26
 
27
+ function changeSetToEvents(doc, changes) {
28
+ const events = [];
29
+ changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
30
+ events.push({
31
+ range: {
32
+ start: offsetToPos(doc, fromA),
33
+ end: offsetToPos(doc, toA),
34
+ },
35
+ text: inserted.toString(),
36
+ });
37
+ });
38
+ events.sort((a, b) => {
39
+ if (!('range' in a))
40
+ return 1;
41
+ if (!('range' in b))
42
+ return -1;
43
+ return (posToOffset(doc, b.range.start) - posToOffset(doc, a.range.start));
44
+ });
45
+ return events;
46
+ }
47
+
27
48
  const timeout = 10000;
28
- const changesDelay = 500;
29
49
  const CompletionItemKindMap = Object.fromEntries(Object.entries(CompletionItemKind).map(([key, value]) => [value, key]));
30
50
  const useLast = (values) => values.reduce((_, v) => v, '');
31
51
  Facet.define({
@@ -186,32 +206,41 @@ class LanguageServerClient {
186
206
  }
187
207
  }
188
208
  }
209
+ var SynchronizationMethod;
210
+ (function (SynchronizationMethod) {
211
+ SynchronizationMethod["Full"] = "full";
212
+ SynchronizationMethod["Incremental"] = "incremental";
213
+ })(SynchronizationMethod || (SynchronizationMethod = {}));
189
214
  class LanguageServerPlugin {
190
- constructor(view, { client, documentUri, languageId, allowHTMLContent, }) {
215
+ constructor(view, { client, documentUri, languageId, allowHTMLContent, synchronizationMethod, }) {
191
216
  this.view = view;
192
217
  this.client = client;
193
218
  this.documentUri = documentUri;
194
219
  this.languageId = languageId;
195
220
  this.allowHTMLContent = allowHTMLContent;
221
+ this.synchronizationMethod = synchronizationMethod;
196
222
  this.documentVersion = 0;
197
- this.changesTimeout = 0;
198
223
  this.client.attachPlugin(this);
199
224
  this.initialize({
200
225
  documentText: this.view.state.doc.toString(),
201
226
  });
202
227
  }
203
- update({ docChanged }) {
228
+ update({ state, changes, startState, docChanged }) {
204
229
  if (!docChanged) {
205
230
  return;
206
231
  }
207
- if (this.changesTimeout) {
208
- clearTimeout(this.changesTimeout);
232
+ switch (this.synchronizationMethod) {
233
+ case SynchronizationMethod.Full:
234
+ this.sendChanges([
235
+ {
236
+ text: this.view.state.doc.toString(),
237
+ },
238
+ ]);
239
+ break;
240
+ case SynchronizationMethod.Incremental:
241
+ this.sendChanges(changeSetToEvents(startState.doc, changes));
242
+ break;
209
243
  }
210
- this.changesTimeout = self.setTimeout(() => {
211
- this.sendChange({
212
- documentText: this.view.state.doc.toString(),
213
- });
214
- }, changesDelay);
215
244
  }
216
245
  destroy() {
217
246
  this.client.detachPlugin(this);
@@ -229,7 +258,7 @@ class LanguageServerPlugin {
229
258
  },
230
259
  });
231
260
  }
232
- async sendChange({ documentText }) {
261
+ async sendChanges(contentChanges) {
233
262
  if (!this.client.ready) {
234
263
  return;
235
264
  }
@@ -239,7 +268,7 @@ class LanguageServerPlugin {
239
268
  uri: this.documentUri,
240
269
  version: this.documentVersion++,
241
270
  },
242
- contentChanges: [{ text: documentText }],
271
+ contentChanges,
243
272
  });
244
273
  }
245
274
  catch (e) {
@@ -247,7 +276,7 @@ class LanguageServerPlugin {
247
276
  }
248
277
  }
249
278
  requestDiagnostics(view) {
250
- this.sendChange({ documentText: view.state.doc.toString() });
279
+ this.sendChanges([{ text: view.state.doc.toString() }]);
251
280
  }
252
281
  async requestHoverTooltip(view, { line, character }) {
253
282
  if (!this.client.ready || !this.client.capabilities.hoverProvider) {
@@ -290,9 +319,6 @@ class LanguageServerPlugin {
290
319
  !this.client.capabilities.completionProvider) {
291
320
  return null;
292
321
  }
293
- this.sendChange({
294
- documentText: context.state.doc.toString(),
295
- });
296
322
  const result = await this.client.textDocumentCompletion({
297
323
  textDocument: { uri: this.documentUri },
298
324
  position: { line, character },
@@ -621,6 +647,7 @@ function languageServerWithTransport(options) {
621
647
  documentUri: options.documentUri,
622
648
  languageId: options.languageId,
623
649
  allowHTMLContent: options.allowHTMLContent,
650
+ synchronizationMethod: options.synchronizationMethod,
624
651
  }),
625
652
  hoverTooltip(),
626
653
  autocompletion(),
@@ -629,4 +656,4 @@ function languageServerWithTransport(options) {
629
656
  ];
630
657
  }
631
658
 
632
- export { LanguageServerClient, jumpToDefinition, jumpToDefinitionKeymap, jumpToDefinitionPos, languageServer, languageServerPlugin, languageServerWithTransport };
659
+ export { LanguageServerClient, SynchronizationMethod, jumpToDefinition, jumpToDefinitionKeymap, jumpToDefinitionPos, languageServer, languageServerPlugin, languageServerWithTransport };
package/dist/plugin.d.ts CHANGED
@@ -77,6 +77,10 @@ export declare class LanguageServerClient<InitializationOptions = unknown> {
77
77
  protected notify<K extends keyof LSPNotifyMap>(method: K, params: LSPNotifyMap[K]): Promise<LSPNotifyMap[K]>;
78
78
  protected processNotification(notification: Notification): void;
79
79
  }
80
+ export declare enum SynchronizationMethod {
81
+ Full = "full",
82
+ Incremental = "incremental"
83
+ }
80
84
  export declare class LanguageServerPlugin implements PluginValue {
81
85
  private view;
82
86
  client: LanguageServerClient;
@@ -84,21 +88,20 @@ export declare class LanguageServerPlugin implements PluginValue {
84
88
  private languageId;
85
89
  private documentVersion;
86
90
  private allowHTMLContent;
87
- private changesTimeout;
88
- constructor(view: EditorView, { client, documentUri, languageId, allowHTMLContent, }: {
91
+ private synchronizationMethod;
92
+ constructor(view: EditorView, { client, documentUri, languageId, allowHTMLContent, synchronizationMethod, }: {
89
93
  client: LanguageServerClient;
90
94
  documentUri: string;
91
95
  languageId: string;
92
96
  allowHTMLContent: boolean;
97
+ synchronizationMethod: SynchronizationMethod;
93
98
  });
94
- update({ docChanged }: ViewUpdate): void;
99
+ update({ state, changes, startState, docChanged }: ViewUpdate): void;
95
100
  destroy(): void;
96
101
  initialize({ documentText }: {
97
102
  documentText: string;
98
103
  }): Promise<void>;
99
- sendChange({ documentText }: {
100
- documentText: string;
101
- }): Promise<void>;
104
+ sendChanges(contentChanges: LSP.TextDocumentContentChangeEvent[]): Promise<void>;
102
105
  requestDiagnostics(view: EditorView): void;
103
106
  requestHoverTooltip(view: EditorView, { line, character }: {
104
107
  line: number;
@@ -147,6 +150,7 @@ export declare const languageServerPlugin: ViewPlugin<LanguageServerPlugin, {
147
150
  documentUri: string;
148
151
  languageId: string;
149
152
  allowHTMLContent: boolean;
153
+ synchronizationMethod: SynchronizationMethod;
150
154
  }>;
151
155
  export interface LanguageServerBaseOptions {
152
156
  rootUri: string | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codemirror-languageserver",
3
- "version": "1.17.1",
3
+ "version": "1.18.0",
4
4
  "description": "Language Server Plugin for CodeMirror 6",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",