@codemirror/autocomplete 0.20.1 → 6.0.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,23 @@
1
+ ## 6.0.0 (2022-06-08)
2
+
3
+ ### Bug fixes
4
+
5
+ Scroll the cursor into view when inserting a snippet.
6
+
7
+ ## 0.20.3 (2022-05-30)
8
+
9
+ ### Bug fixes
10
+
11
+ Add an aria-label to the completion listbox.
12
+
13
+ Fix a regression that caused transactions generated for completion to not have a `userEvent` annotation.
14
+
15
+ ## 0.20.2 (2022-05-24)
16
+
17
+ ### New features
18
+
19
+ The package now exports an `insertCompletionText` helper that implements the default behavior for applying a completion.
20
+
1
21
  ## 0.20.1 (2022-05-16)
2
22
 
3
23
  ### 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 Object.assign(Object.assign({}, 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
+ })), { userEvent: "input.complete" });
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) {
@@ -529,6 +532,7 @@ class CompletionTooltip {
529
532
  ul.id = id;
530
533
  ul.setAttribute("role", "listbox");
531
534
  ul.setAttribute("aria-expanded", "true");
535
+ ul.setAttribute("aria-label", this.view.state.phrase("Completions"));
532
536
  for (let i = range.from; i < range.to; i++) {
533
537
  let { completion, match } = options[i];
534
538
  const li = ul.appendChild(document.createElement("li"));
@@ -1265,7 +1269,10 @@ function snippet(template) {
1265
1269
  let snippet = Snippet.parse(template);
1266
1270
  return (editor, _completion, from, to) => {
1267
1271
  let { text, ranges } = snippet.instantiate(editor.state, from);
1268
- let spec = { changes: { from, to, insert: state.Text.of(text) } };
1272
+ let spec = {
1273
+ changes: { from, to, insert: state.Text.of(text) },
1274
+ scrollIntoView: true
1275
+ };
1269
1276
  if (ranges.length)
1270
1277
  spec.selection = fieldSelection(ranges, 0);
1271
1278
  if (ranges.length > 1) {
@@ -1753,6 +1760,7 @@ exports.deleteBracketPair = deleteBracketPair;
1753
1760
  exports.ifIn = ifIn;
1754
1761
  exports.ifNotIn = ifNotIn;
1755
1762
  exports.insertBracket = insertBracket;
1763
+ exports.insertCompletionText = insertCompletionText;
1756
1764
  exports.moveCompletionSelection = moveCompletionSelection;
1757
1765
  exports.nextSnippetField = nextSnippetField;
1758
1766
  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 Object.assign(Object.assign({}, 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
+ })), { userEvent: "input.complete" });
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) {
@@ -525,6 +528,7 @@ class CompletionTooltip {
525
528
  ul.id = id;
526
529
  ul.setAttribute("role", "listbox");
527
530
  ul.setAttribute("aria-expanded", "true");
531
+ ul.setAttribute("aria-label", this.view.state.phrase("Completions"));
528
532
  for (let i = range.from; i < range.to; i++) {
529
533
  let { completion, match } = options[i];
530
534
  const li = ul.appendChild(document.createElement("li"));
@@ -1261,7 +1265,10 @@ function snippet(template) {
1261
1265
  let snippet = Snippet.parse(template);
1262
1266
  return (editor, _completion, from, to) => {
1263
1267
  let { text, ranges } = snippet.instantiate(editor.state, from);
1264
- let spec = { changes: { from, to, insert: Text.of(text) } };
1268
+ let spec = {
1269
+ changes: { from, to, insert: Text.of(text) },
1270
+ scrollIntoView: true
1271
+ };
1265
1272
  if (ranges.length)
1266
1273
  spec.selection = fieldSelection(ranges, 0);
1267
1274
  if (ranges.length > 1) {
@@ -1733,4 +1740,4 @@ function setSelectedCompletion(index) {
1733
1740
  return setSelectedEffect.of(index);
1734
1741
  }
1735
1742
 
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 };
1743
+ 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": "6.0.0",
4
4
  "description": "Autocompletion for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",
@@ -26,10 +26,10 @@
26
26
  "sideEffects": false,
27
27
  "license": "MIT",
28
28
  "dependencies": {
29
- "@codemirror/language": "^0.20.0",
30
- "@codemirror/state": "^0.20.0",
31
- "@codemirror/view": "^0.20.0",
32
- "@lezer/common": "^0.16.0"
29
+ "@codemirror/language": "^6.0.0",
30
+ "@codemirror/state": "^6.0.0",
31
+ "@codemirror/view": "^6.0.0",
32
+ "@lezer/common": "^1.0.0"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@codemirror/buildhelper": "^0.1.5"