codemirror-languageserver 1.11.0 → 1.12.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/dist/index.d.ts CHANGED
@@ -87,6 +87,10 @@ interface LanguageServerOptions extends LanguageServerClientOptions {
87
87
  interface LanguageServerWebsocketOptions extends LanguageServerBaseOptions {
88
88
  serverUri: `ws://${string}` | `wss://${string}`;
89
89
  }
90
- declare function languageServer(options: LanguageServerWebsocketOptions): import("@codemirror/state").Extension[];
91
- declare function languageServerWithTransport(options: LanguageServerOptions): import("@codemirror/state").Extension[];
90
+ declare function languageServer(options: LanguageServerWebsocketOptions): ({
91
+ extension: import("@codemirror/state").Extension;
92
+ } | readonly import("@codemirror/state").Extension[])[];
93
+ declare function languageServerWithTransport(options: LanguageServerOptions): ({
94
+ extension: import("@codemirror/state").Extension;
95
+ } | readonly import("@codemirror/state").Extension[])[];
92
96
  export { LanguageServerClient, languageServer, languageServerWithTransport };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { autocompletion } from '@codemirror/autocomplete';
1
+ import { autocompletion, insertCompletionText } from '@codemirror/autocomplete';
2
2
  import { setDiagnostics } from '@codemirror/lint';
3
3
  import { Facet } from '@codemirror/state';
4
4
  import { ViewPlugin, hoverTooltip } from '@codemirror/view';
@@ -245,47 +245,77 @@ class LanguageServerPlugin {
245
245
  });
246
246
  if (!result)
247
247
  return null;
248
- const items = 'items' in result ? result.items : result;
249
- let options = items.map(({ detail, label, kind, textEdit, documentation, sortText, filterText, }) => {
250
- var _a;
251
- const completion = {
252
- label,
253
- detail,
254
- apply: (_a = textEdit === null || textEdit === void 0 ? void 0 : textEdit.newText) !== null && _a !== void 0 ? _a : label,
255
- type: kind && CompletionItemKindMap[kind].toLowerCase(),
256
- sortText: sortText !== null && sortText !== void 0 ? sortText : label,
257
- filterText: filterText !== null && filterText !== void 0 ? filterText : label,
258
- };
259
- if (documentation) {
260
- completion.info = formatContents(documentation);
261
- }
262
- return completion;
263
- });
264
- const [span, match] = prefixMatch(options);
248
+ let items = 'items' in result ? result.items : result;
249
+ const [span, match] = prefixMatch(items);
265
250
  const token = context.matchBefore(match);
266
251
  let { pos } = context;
267
252
  if (token) {
268
253
  pos = token.from;
269
254
  const word = token.text.toLowerCase();
270
255
  if (/^\w+$/.test(word)) {
271
- options = options
272
- .filter(({ filterText }) => filterText.toLowerCase().startsWith(word))
273
- .sort(({ apply: a }, { apply: b }) => {
256
+ items = items
257
+ .filter(({ label, filterText }) => {
258
+ const text = filterText !== null && filterText !== void 0 ? filterText : label;
259
+ return text.toLowerCase().startsWith(word);
260
+ })
261
+ .sort((a, b) => {
262
+ var _a, _b;
263
+ const aText = (_a = a.sortText) !== null && _a !== void 0 ? _a : a.label;
264
+ const bText = (_b = b.sortText) !== null && _b !== void 0 ? _b : b.label;
274
265
  switch (true) {
275
- case a.startsWith(token.text) &&
276
- !b.startsWith(token.text):
266
+ case aText.startsWith(token.text) &&
267
+ !bText.startsWith(token.text):
277
268
  return -1;
278
- case !a.startsWith(token.text) &&
279
- b.startsWith(token.text):
269
+ case !aText.startsWith(token.text) &&
270
+ bText.startsWith(token.text):
280
271
  return 1;
281
272
  }
282
273
  return 0;
283
274
  });
284
275
  }
285
276
  }
277
+ const options = items.map(({ detail, label, kind, textEdit, documentation, additionalTextEdits, }) => {
278
+ const completion = {
279
+ label,
280
+ detail,
281
+ apply: function (view, completion, from, to) {
282
+ var _a;
283
+ const text = (_a = textEdit === null || textEdit === void 0 ? void 0 : textEdit.newText) !== null && _a !== void 0 ? _a : label;
284
+ view.dispatch(insertCompletionText(view.state, text, from, to));
285
+ if (!additionalTextEdits) {
286
+ return;
287
+ }
288
+ additionalTextEdits
289
+ .sort(({ range: { end: a } }, { range: { end: b } }) => {
290
+ if (posToOffset(view.state.doc, a) < posToOffset(view.state.doc, b)) {
291
+ return 1;
292
+ }
293
+ else if (posToOffset(view.state.doc, a) > posToOffset(view.state.doc, b)) {
294
+ return -1;
295
+ }
296
+ return 0;
297
+ })
298
+ .forEach((textEdit) => {
299
+ view.dispatch(view.state.update({
300
+ changes: {
301
+ from: posToOffset(view.state.doc, textEdit.range.start),
302
+ to: posToOffset(view.state.doc, textEdit.range.end),
303
+ insert: textEdit.newText
304
+ }
305
+ }));
306
+ });
307
+ },
308
+ type: kind && CompletionItemKindMap[kind].toLowerCase()
309
+ };
310
+ if (documentation) {
311
+ completion.info = formatContents(documentation);
312
+ }
313
+ return completion;
314
+ });
286
315
  return {
287
316
  from: pos,
288
317
  options,
318
+ filter: false
289
319
  };
290
320
  }
291
321
  processNotification(notification) {
@@ -410,11 +440,12 @@ function toSet(chars) {
410
440
  }
411
441
  return `[${preamble}${flat.replace(/[^\w\s]/g, '\\$&')}]`;
412
442
  }
413
- function prefixMatch(options) {
443
+ function prefixMatch(items) {
444
+ var _a;
414
445
  const first = new Set();
415
446
  const rest = new Set();
416
- for (const { apply } of options) {
417
- const [initial, ...restStr] = apply;
447
+ for (const item of items) {
448
+ const [initial, ...restStr] = ((_a = item.textEdit) === null || _a === void 0 ? void 0 : _a.newText) || item.label;
418
449
  first.add(initial);
419
450
  for (const char of restStr) {
420
451
  rest.add(char);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codemirror-languageserver",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "description": "Language Server Plugin for CodeMirror 6",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -30,12 +30,12 @@
30
30
  "typescript": "^4.7.4"
31
31
  },
32
32
  "dependencies": {
33
- "@codemirror/autocomplete": "^6.0.2",
34
- "@codemirror/lint": "^6.0.0",
35
- "@codemirror/state": "^6.1.0",
36
- "@codemirror/view": "^6.0.2",
37
- "@open-rpc/client-js": "^1.7.1",
38
- "vscode-languageserver-protocol": "^3.16.0"
33
+ "@codemirror/autocomplete": "^6.18.4",
34
+ "@codemirror/lint": "^6.8.4",
35
+ "@codemirror/state": "^6.5.1",
36
+ "@codemirror/view": "^6.36.2",
37
+ "@open-rpc/client-js": "^1.8.1",
38
+ "vscode-languageserver-protocol": "^3.17.5"
39
39
  },
40
40
  "prettier": {
41
41
  "tabWidth": 4,