@codemirror/autocomplete 6.13.0 → 6.14.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/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 6.14.0 (2024-03-10)
2
+
3
+ ### New features
4
+
5
+ Completion results can now define a `map` method that can be used to adjust position-dependent information for document changes.
6
+
1
7
  ## 6.13.0 (2024-02-29)
2
8
 
3
9
  ### New features
package/dist/index.cjs CHANGED
@@ -909,26 +909,33 @@ class ActiveResult extends ActiveSource {
909
909
  hasResult() { return true; }
910
910
  handleUserEvent(tr, type, conf) {
911
911
  var _a;
912
+ let result = this.result;
913
+ if (result.map && !tr.changes.empty)
914
+ result = result.map(result, tr.changes);
912
915
  let from = tr.changes.mapPos(this.from), to = tr.changes.mapPos(this.to, 1);
913
916
  let pos = cur(tr.state);
914
917
  if ((this.explicitPos < 0 ? pos <= from : pos < this.from) ||
915
- pos > to ||
918
+ pos > to || !result ||
916
919
  type == "delete" && cur(tr.startState) == this.from)
917
920
  return new ActiveSource(this.source, type == "input" && conf.activateOnTyping ? 1 /* State.Pending */ : 0 /* State.Inactive */);
918
- let explicitPos = this.explicitPos < 0 ? -1 : tr.changes.mapPos(this.explicitPos), updated;
919
- if (checkValid(this.result.validFor, tr.state, from, to))
920
- return new ActiveResult(this.source, explicitPos, this.result, from, to);
921
- if (this.result.update &&
922
- (updated = this.result.update(this.result, from, to, new CompletionContext(tr.state, pos, explicitPos >= 0))))
923
- return new ActiveResult(this.source, explicitPos, updated, updated.from, (_a = updated.to) !== null && _a !== void 0 ? _a : cur(tr.state));
921
+ let explicitPos = this.explicitPos < 0 ? -1 : tr.changes.mapPos(this.explicitPos);
922
+ if (checkValid(result.validFor, tr.state, from, to))
923
+ return new ActiveResult(this.source, explicitPos, result, from, to);
924
+ if (result.update &&
925
+ (result = result.update(result, from, to, new CompletionContext(tr.state, pos, explicitPos >= 0))))
926
+ return new ActiveResult(this.source, explicitPos, result, result.from, (_a = result.to) !== null && _a !== void 0 ? _a : cur(tr.state));
924
927
  return new ActiveSource(this.source, 1 /* State.Pending */, explicitPos);
925
928
  }
926
929
  handleChange(tr) {
927
930
  return tr.changes.touchesRange(this.from, this.to) ? new ActiveSource(this.source, 0 /* State.Inactive */) : this.map(tr.changes);
928
931
  }
929
932
  map(mapping) {
930
- return mapping.empty ? this :
931
- new ActiveResult(this.source, this.explicitPos < 0 ? -1 : mapping.mapPos(this.explicitPos), this.result, mapping.mapPos(this.from), mapping.mapPos(this.to, 1));
933
+ if (mapping.empty)
934
+ return this;
935
+ let result = this.result.map ? this.result.map(this.result, mapping) : this.result;
936
+ if (!result)
937
+ return new ActiveSource(this.source, 0 /* State.Inactive */);
938
+ return new ActiveResult(this.source, this.explicitPos < 0 ? -1 : mapping.mapPos(this.explicitPos), this.result, mapping.mapPos(this.from), mapping.mapPos(this.to, 1));
932
939
  }
933
940
  }
934
941
  function checkValid(validFor, state, from, to) {
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _codemirror_state from '@codemirror/state';
2
- import { EditorState, TransactionSpec, Transaction, StateCommand, Facet, Extension, StateEffect } from '@codemirror/state';
2
+ import { EditorState, ChangeDesc, TransactionSpec, Transaction, StateCommand, Facet, Extension, StateEffect } from '@codemirror/state';
3
3
  import { EditorView, Rect, KeyBinding, Command } from '@codemirror/view';
4
4
  import * as _lezer_common from '@lezer/common';
5
5
 
@@ -261,6 +261,14 @@ interface CompletionResult {
261
261
  */
262
262
  update?: (current: CompletionResult, from: number, to: number, context: CompletionContext) => CompletionResult | null;
263
263
  /**
264
+ When results contain position-dependent information in, for
265
+ example, `apply` methods, you can provide this method to update
266
+ the result for transactions that happen after the query. It is
267
+ not necessary to update `from` and `to`—those are tracked
268
+ automatically.
269
+ */
270
+ map?: (current: CompletionResult, changes: ChangeDesc) => CompletionResult | null;
271
+ /**
264
272
  Set a default set of [commit
265
273
  characters](https://codemirror.net/6/docs/ref/#autocomplete.Completion.commitCharacters) for all
266
274
  options in this result.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _codemirror_state from '@codemirror/state';
2
- import { EditorState, TransactionSpec, Transaction, StateCommand, Facet, Extension, StateEffect } from '@codemirror/state';
2
+ import { EditorState, ChangeDesc, TransactionSpec, Transaction, StateCommand, Facet, Extension, StateEffect } from '@codemirror/state';
3
3
  import { EditorView, Rect, KeyBinding, Command } from '@codemirror/view';
4
4
  import * as _lezer_common from '@lezer/common';
5
5
 
@@ -261,6 +261,14 @@ interface CompletionResult {
261
261
  */
262
262
  update?: (current: CompletionResult, from: number, to: number, context: CompletionContext) => CompletionResult | null;
263
263
  /**
264
+ When results contain position-dependent information in, for
265
+ example, `apply` methods, you can provide this method to update
266
+ the result for transactions that happen after the query. It is
267
+ not necessary to update `from` and `to`—those are tracked
268
+ automatically.
269
+ */
270
+ map?: (current: CompletionResult, changes: ChangeDesc) => CompletionResult | null;
271
+ /**
264
272
  Set a default set of [commit
265
273
  characters](https://codemirror.net/6/docs/ref/#autocomplete.Completion.commitCharacters) for all
266
274
  options in this result.
package/dist/index.js CHANGED
@@ -907,26 +907,33 @@ class ActiveResult extends ActiveSource {
907
907
  hasResult() { return true; }
908
908
  handleUserEvent(tr, type, conf) {
909
909
  var _a;
910
+ let result = this.result;
911
+ if (result.map && !tr.changes.empty)
912
+ result = result.map(result, tr.changes);
910
913
  let from = tr.changes.mapPos(this.from), to = tr.changes.mapPos(this.to, 1);
911
914
  let pos = cur(tr.state);
912
915
  if ((this.explicitPos < 0 ? pos <= from : pos < this.from) ||
913
- pos > to ||
916
+ pos > to || !result ||
914
917
  type == "delete" && cur(tr.startState) == this.from)
915
918
  return new ActiveSource(this.source, type == "input" && conf.activateOnTyping ? 1 /* State.Pending */ : 0 /* State.Inactive */);
916
- let explicitPos = this.explicitPos < 0 ? -1 : tr.changes.mapPos(this.explicitPos), updated;
917
- if (checkValid(this.result.validFor, tr.state, from, to))
918
- return new ActiveResult(this.source, explicitPos, this.result, from, to);
919
- if (this.result.update &&
920
- (updated = this.result.update(this.result, from, to, new CompletionContext(tr.state, pos, explicitPos >= 0))))
921
- return new ActiveResult(this.source, explicitPos, updated, updated.from, (_a = updated.to) !== null && _a !== void 0 ? _a : cur(tr.state));
919
+ let explicitPos = this.explicitPos < 0 ? -1 : tr.changes.mapPos(this.explicitPos);
920
+ if (checkValid(result.validFor, tr.state, from, to))
921
+ return new ActiveResult(this.source, explicitPos, result, from, to);
922
+ if (result.update &&
923
+ (result = result.update(result, from, to, new CompletionContext(tr.state, pos, explicitPos >= 0))))
924
+ return new ActiveResult(this.source, explicitPos, result, result.from, (_a = result.to) !== null && _a !== void 0 ? _a : cur(tr.state));
922
925
  return new ActiveSource(this.source, 1 /* State.Pending */, explicitPos);
923
926
  }
924
927
  handleChange(tr) {
925
928
  return tr.changes.touchesRange(this.from, this.to) ? new ActiveSource(this.source, 0 /* State.Inactive */) : this.map(tr.changes);
926
929
  }
927
930
  map(mapping) {
928
- return mapping.empty ? this :
929
- new ActiveResult(this.source, this.explicitPos < 0 ? -1 : mapping.mapPos(this.explicitPos), this.result, mapping.mapPos(this.from), mapping.mapPos(this.to, 1));
931
+ if (mapping.empty)
932
+ return this;
933
+ let result = this.result.map ? this.result.map(this.result, mapping) : this.result;
934
+ if (!result)
935
+ return new ActiveSource(this.source, 0 /* State.Inactive */);
936
+ return new ActiveResult(this.source, this.explicitPos < 0 ? -1 : mapping.mapPos(this.explicitPos), this.result, mapping.mapPos(this.from), mapping.mapPos(this.to, 1));
930
937
  }
931
938
  }
932
939
  function checkValid(validFor, state, from, to) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/autocomplete",
3
- "version": "6.13.0",
3
+ "version": "6.14.0",
4
4
  "description": "Autocompletion for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",