@codemirror/autocomplete 6.12.0 → 6.13.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.13.0 (2024-02-29)
2
+
3
+ ### New features
4
+
5
+ Completions may now provide 'commit characters' that, when typed, commit the completion before inserting the character.
6
+
1
7
  ## 6.12.0 (2024-01-12)
2
8
 
3
9
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -1179,6 +1179,21 @@ const completionPlugin = view.ViewPlugin.fromClass(class {
1179
1179
  }
1180
1180
  }
1181
1181
  });
1182
+ const windows = typeof navigator == "object" && /Win/.test(navigator.platform);
1183
+ const commitCharacters = state.Prec.highest(view.EditorView.domEventHandlers({
1184
+ keydown(event, view) {
1185
+ let field = view.state.field(completionState, false);
1186
+ if (!field || !field.open || field.open.disabled || field.open.selected < 0 ||
1187
+ event.key.length > 1 || event.ctrlKey && !(windows && event.altKey) || event.metaKey)
1188
+ return false;
1189
+ let option = field.open.options[field.open.selected];
1190
+ let result = field.active.find(a => a.source == option.source);
1191
+ let commitChars = option.completion.commitCharacters || result.result.commitCharacters;
1192
+ if (commitChars && commitChars.indexOf(event.key) > -1)
1193
+ applyCompletion(view, option);
1194
+ return false;
1195
+ }
1196
+ }));
1182
1197
 
1183
1198
  const baseTheme = view.EditorView.baseTheme({
1184
1199
  ".cm-tooltip.cm-tooltip-autocomplete": {
@@ -1900,6 +1915,7 @@ Returns an extension that enables autocompletion.
1900
1915
  */
1901
1916
  function autocompletion(config = {}) {
1902
1917
  return [
1918
+ commitCharacters,
1903
1919
  completionState,
1904
1920
  completionConfig.of(config),
1905
1921
  completionPlugin,
package/dist/index.d.cts CHANGED
@@ -54,6 +54,11 @@ interface Completion {
54
54
  */
55
55
  type?: string;
56
56
  /**
57
+ When this option is selected, and one of these characters is
58
+ typed, insert the completion before typing the character.
59
+ */
60
+ commitCharacters?: readonly string[];
61
+ /**
57
62
  When given, should be a number from -99 to 99 that adjusts how
58
63
  this completion is ranked compared to other completions that
59
64
  match the input as well as this one. A negative number moves it
@@ -255,6 +260,12 @@ interface CompletionResult {
255
260
  completion still applies in the new state.
256
261
  */
257
262
  update?: (current: CompletionResult, from: number, to: number, context: CompletionContext) => CompletionResult | null;
263
+ /**
264
+ Set a default set of [commit
265
+ characters](https://codemirror.net/6/docs/ref/#autocomplete.Completion.commitCharacters) for all
266
+ options in this result.
267
+ */
268
+ commitCharacters?: readonly string[];
258
269
  }
259
270
  /**
260
271
  This annotation is added to transactions that are produced by
package/dist/index.d.ts CHANGED
@@ -54,6 +54,11 @@ interface Completion {
54
54
  */
55
55
  type?: string;
56
56
  /**
57
+ When this option is selected, and one of these characters is
58
+ typed, insert the completion before typing the character.
59
+ */
60
+ commitCharacters?: readonly string[];
61
+ /**
57
62
  When given, should be a number from -99 to 99 that adjusts how
58
63
  this completion is ranked compared to other completions that
59
64
  match the input as well as this one. A negative number moves it
@@ -255,6 +260,12 @@ interface CompletionResult {
255
260
  completion still applies in the new state.
256
261
  */
257
262
  update?: (current: CompletionResult, from: number, to: number, context: CompletionContext) => CompletionResult | null;
263
+ /**
264
+ Set a default set of [commit
265
+ characters](https://codemirror.net/6/docs/ref/#autocomplete.Completion.commitCharacters) for all
266
+ options in this result.
267
+ */
268
+ commitCharacters?: readonly string[];
258
269
  }
259
270
  /**
260
271
  This annotation is added to transactions that are produced by
package/dist/index.js CHANGED
@@ -1177,6 +1177,21 @@ const completionPlugin = /*@__PURE__*/ViewPlugin.fromClass(class {
1177
1177
  }
1178
1178
  }
1179
1179
  });
1180
+ const windows = typeof navigator == "object" && /*@__PURE__*//Win/.test(navigator.platform);
1181
+ const commitCharacters = /*@__PURE__*/Prec.highest(/*@__PURE__*/EditorView.domEventHandlers({
1182
+ keydown(event, view) {
1183
+ let field = view.state.field(completionState, false);
1184
+ if (!field || !field.open || field.open.disabled || field.open.selected < 0 ||
1185
+ event.key.length > 1 || event.ctrlKey && !(windows && event.altKey) || event.metaKey)
1186
+ return false;
1187
+ let option = field.open.options[field.open.selected];
1188
+ let result = field.active.find(a => a.source == option.source);
1189
+ let commitChars = option.completion.commitCharacters || result.result.commitCharacters;
1190
+ if (commitChars && commitChars.indexOf(event.key) > -1)
1191
+ applyCompletion(view, option);
1192
+ return false;
1193
+ }
1194
+ }));
1180
1195
 
1181
1196
  const baseTheme = /*@__PURE__*/EditorView.baseTheme({
1182
1197
  ".cm-tooltip.cm-tooltip-autocomplete": {
@@ -1898,6 +1913,7 @@ Returns an extension that enables autocompletion.
1898
1913
  */
1899
1914
  function autocompletion(config = {}) {
1900
1915
  return [
1916
+ commitCharacters,
1901
1917
  completionState,
1902
1918
  completionConfig.of(config),
1903
1919
  completionPlugin,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/autocomplete",
3
- "version": "6.12.0",
3
+ "version": "6.13.0",
4
4
  "description": "Autocompletion for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",