@codemirror/autocomplete 0.20.1 → 0.20.2

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
+ ## 0.20.2 (2022-05-24)
2
+
3
+ ### New features
4
+
5
+ The package now exports an `insertCompletionText` helper that implements the default behavior for applying a completion.
6
+
1
7
  ## 0.20.1 (2022-05-16)
2
8
 
3
9
  ### New features
package/dist/index.cjs CHANGED
@@ -153,32 +153,35 @@ This annotation is added to transactions that are produced by
153
153
  picking a completion.
154
154
  */
155
155
  const pickedCompletion = state.Annotation.define();
156
+ /**
157
+ Helper function that returns a transaction spec which inserts a
158
+ completion's text in the main selection range, and any other
159
+ selection range that has the same text in front of it.
160
+ */
161
+ function insertCompletionText(state$1, text, from, to) {
162
+ return state$1.changeByRange(range => {
163
+ if (range == state$1.selection.main)
164
+ return {
165
+ changes: { from: from, to: to, insert: text },
166
+ range: state.EditorSelection.cursor(from + text.length)
167
+ };
168
+ let len = to - from;
169
+ if (!range.empty ||
170
+ len && state$1.sliceDoc(range.from - len, range.from) != state$1.sliceDoc(from, to))
171
+ return { range };
172
+ return {
173
+ changes: { from: range.from - len, to: range.from, insert: text },
174
+ range: state.EditorSelection.cursor(range.from - len + text.length)
175
+ };
176
+ });
177
+ }
156
178
  function applyCompletion(view, option) {
157
179
  const apply = option.completion.apply || option.completion.label;
158
180
  let result = option.source;
159
- if (typeof apply == "string") {
160
- view.dispatch(view.state.changeByRange(range => {
161
- if (range == view.state.selection.main)
162
- return {
163
- changes: { from: result.from, to: result.to, insert: apply },
164
- range: state.EditorSelection.cursor(result.from + apply.length)
165
- };
166
- let len = result.to - result.from;
167
- if (!range.empty ||
168
- len && view.state.sliceDoc(range.from - len, range.from) != view.state.sliceDoc(result.from, result.to))
169
- return { range };
170
- return {
171
- changes: { from: range.from - len, to: range.from, insert: apply },
172
- range: state.EditorSelection.cursor(range.from - len + apply.length)
173
- };
174
- }), {
175
- userEvent: "input.complete",
176
- annotations: pickedCompletion.of(option.completion)
177
- });
178
- }
179
- else {
181
+ if (typeof apply == "string")
182
+ view.dispatch(insertCompletionText(view.state, apply, result.from, result.to));
183
+ else
180
184
  apply(view, option.completion, result.from, result.to);
181
- }
182
185
  }
183
186
  const SourceCache = new WeakMap();
184
187
  function asSource(source) {
@@ -1753,6 +1756,7 @@ exports.deleteBracketPair = deleteBracketPair;
1753
1756
  exports.ifIn = ifIn;
1754
1757
  exports.ifNotIn = ifNotIn;
1755
1758
  exports.insertBracket = insertBracket;
1759
+ exports.insertCompletionText = insertCompletionText;
1756
1760
  exports.moveCompletionSelection = moveCompletionSelection;
1757
1761
  exports.nextSnippetField = nextSnippetField;
1758
1762
  exports.pickedCompletion = pickedCompletion;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _codemirror_state from '@codemirror/state';
2
- import { EditorState, Transaction, StateCommand, Facet, Extension, StateEffect } from '@codemirror/state';
2
+ import { EditorState, TransactionSpec, Transaction, StateCommand, Facet, Extension, StateEffect } from '@codemirror/state';
3
3
  import { EditorView, KeyBinding, Command } from '@codemirror/view';
4
4
  import * as _lezer_common from '@lezer/common';
5
5
 
@@ -270,6 +270,12 @@ This annotation is added to transactions that are produced by
270
270
  picking a completion.
271
271
  */
272
272
  declare const pickedCompletion: _codemirror_state.AnnotationType<Completion>;
273
+ /**
274
+ Helper function that returns a transaction spec which inserts a
275
+ completion's text in the main selection range, and any other
276
+ selection range that has the same text in front of it.
277
+ */
278
+ declare function insertCompletionText(state: EditorState, text: string, from: number, to: number): TransactionSpec;
273
279
 
274
280
  /**
275
281
  Convert a snippet template to a function that can
@@ -445,4 +451,4 @@ the currently selected completion.
445
451
  */
446
452
  declare function setSelectedCompletion(index: number): StateEffect<unknown>;
447
453
 
448
- export { CloseBracketConfig, Completion, CompletionContext, CompletionResult, CompletionSource, acceptCompletion, autocompletion, clearSnippet, closeBrackets, closeBracketsKeymap, closeCompletion, completeAnyWord, completeFromList, completionKeymap, completionStatus, currentCompletions, deleteBracketPair, ifIn, ifNotIn, insertBracket, moveCompletionSelection, nextSnippetField, pickedCompletion, prevSnippetField, selectedCompletion, selectedCompletionIndex, setSelectedCompletion, snippet, snippetCompletion, snippetKeymap, startCompletion };
454
+ export { CloseBracketConfig, Completion, CompletionContext, CompletionResult, CompletionSource, acceptCompletion, autocompletion, clearSnippet, closeBrackets, closeBracketsKeymap, closeCompletion, completeAnyWord, completeFromList, completionKeymap, completionStatus, currentCompletions, deleteBracketPair, ifIn, ifNotIn, insertBracket, insertCompletionText, moveCompletionSelection, nextSnippetField, pickedCompletion, prevSnippetField, selectedCompletion, selectedCompletionIndex, setSelectedCompletion, snippet, snippetCompletion, snippetKeymap, startCompletion };
package/dist/index.js CHANGED
@@ -149,32 +149,35 @@ This annotation is added to transactions that are produced by
149
149
  picking a completion.
150
150
  */
151
151
  const pickedCompletion = /*@__PURE__*/Annotation.define();
152
+ /**
153
+ Helper function that returns a transaction spec which inserts a
154
+ completion's text in the main selection range, and any other
155
+ selection range that has the same text in front of it.
156
+ */
157
+ function insertCompletionText(state, text, from, to) {
158
+ return state.changeByRange(range => {
159
+ if (range == state.selection.main)
160
+ return {
161
+ changes: { from: from, to: to, insert: text },
162
+ range: EditorSelection.cursor(from + text.length)
163
+ };
164
+ let len = to - from;
165
+ if (!range.empty ||
166
+ len && state.sliceDoc(range.from - len, range.from) != state.sliceDoc(from, to))
167
+ return { range };
168
+ return {
169
+ changes: { from: range.from - len, to: range.from, insert: text },
170
+ range: EditorSelection.cursor(range.from - len + text.length)
171
+ };
172
+ });
173
+ }
152
174
  function applyCompletion(view, option) {
153
175
  const apply = option.completion.apply || option.completion.label;
154
176
  let result = option.source;
155
- if (typeof apply == "string") {
156
- view.dispatch(view.state.changeByRange(range => {
157
- if (range == view.state.selection.main)
158
- return {
159
- changes: { from: result.from, to: result.to, insert: apply },
160
- range: EditorSelection.cursor(result.from + apply.length)
161
- };
162
- let len = result.to - result.from;
163
- if (!range.empty ||
164
- len && view.state.sliceDoc(range.from - len, range.from) != view.state.sliceDoc(result.from, result.to))
165
- return { range };
166
- return {
167
- changes: { from: range.from - len, to: range.from, insert: apply },
168
- range: EditorSelection.cursor(range.from - len + apply.length)
169
- };
170
- }), {
171
- userEvent: "input.complete",
172
- annotations: pickedCompletion.of(option.completion)
173
- });
174
- }
175
- else {
177
+ if (typeof apply == "string")
178
+ view.dispatch(insertCompletionText(view.state, apply, result.from, result.to));
179
+ else
176
180
  apply(view, option.completion, result.from, result.to);
177
- }
178
181
  }
179
182
  const SourceCache = /*@__PURE__*/new WeakMap();
180
183
  function asSource(source) {
@@ -1733,4 +1736,4 @@ function setSelectedCompletion(index) {
1733
1736
  return setSelectedEffect.of(index);
1734
1737
  }
1735
1738
 
1736
- export { CompletionContext, acceptCompletion, autocompletion, clearSnippet, closeBrackets, closeBracketsKeymap, closeCompletion, completeAnyWord, completeFromList, completionKeymap, completionStatus, currentCompletions, deleteBracketPair, ifIn, ifNotIn, insertBracket, moveCompletionSelection, nextSnippetField, pickedCompletion, prevSnippetField, selectedCompletion, selectedCompletionIndex, setSelectedCompletion, snippet, snippetCompletion, snippetKeymap, startCompletion };
1739
+ export { CompletionContext, acceptCompletion, autocompletion, clearSnippet, closeBrackets, closeBracketsKeymap, closeCompletion, completeAnyWord, completeFromList, completionKeymap, completionStatus, currentCompletions, deleteBracketPair, ifIn, ifNotIn, insertBracket, insertCompletionText, moveCompletionSelection, nextSnippetField, pickedCompletion, prevSnippetField, selectedCompletion, selectedCompletionIndex, setSelectedCompletion, snippet, snippetCompletion, snippetKeymap, startCompletion };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/autocomplete",
3
- "version": "0.20.1",
3
+ "version": "0.20.2",
4
4
  "description": "Autocompletion for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",