@adecore/editor-react 0.62.1 → 0.63.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.
Files changed (264) hide show
  1. package/README.md +2 -2
  2. package/dist/AnchoredPopup.d.ts +2 -1
  3. package/dist/CompletionPopup.js +0 -2
  4. package/dist/DiffView.d.ts +1 -28
  5. package/dist/EditorView.js +0 -1
  6. package/dist/HoverSections.d.ts +1 -8
  7. package/dist/HoverSections.js +2 -2
  8. package/dist/LanguagePopups.js +30 -14
  9. package/dist/PanelResizeHandle.js +0 -1
  10. package/dist/PeekPanel.js +0 -1
  11. package/dist/PickPopup.js +0 -1
  12. package/dist/RenderedDocs.js +0 -2
  13. package/dist/SplitDiffView.d.ts +1 -1
  14. package/dist/SymbolPicker.js +0 -1
  15. package/dist/call-hierarchy-model.js +0 -4
  16. package/dist/call-hierarchy.d.ts +9 -2
  17. package/dist/call-hierarchy.js +51 -67
  18. package/dist/clipboard-history.d.ts +3 -0
  19. package/dist/clipboard-history.js +0 -1
  20. package/dist/code-actions-model.js +1 -1
  21. package/dist/code-actions.d.ts +14 -2
  22. package/dist/code-actions.js +14 -9
  23. package/dist/code-authors.js +4 -12
  24. package/dist/code-vision-model.d.ts +0 -1
  25. package/dist/code-vision-model.js +1 -2
  26. package/dist/code-vision.d.ts +11 -3
  27. package/dist/code-vision.js +13 -18
  28. package/dist/completion-call.d.ts +1 -1
  29. package/dist/completion-call.js +2 -1
  30. package/dist/completion-model.d.ts +5 -4
  31. package/dist/completion-model.js +4 -4
  32. package/dist/completion.d.ts +13 -4
  33. package/dist/completion.js +19 -18
  34. package/dist/context-menu-rows.d.ts +2 -1
  35. package/dist/context-menu.d.ts +9 -3
  36. package/dist/context-menu.js +17 -19
  37. package/dist/definition-link.d.ts +8 -2
  38. package/dist/definition-link.js +4 -2
  39. package/dist/diagnostics-model.d.ts +6 -8
  40. package/dist/diagnostics-model.js +1 -54
  41. package/dist/diagnostics.d.ts +9 -7
  42. package/dist/diagnostics.js +6 -4
  43. package/dist/diff-view-props.d.ts +28 -0
  44. package/dist/diff-view-props.js +1 -0
  45. package/dist/doc-comment-model.js +0 -2
  46. package/dist/document-colors.d.ts +9 -4
  47. package/dist/document-colors.js +7 -4
  48. package/dist/document-links.d.ts +2 -3
  49. package/dist/document-links.js +2 -3
  50. package/dist/document-query.d.ts +2 -2
  51. package/dist/edit-locations.js +4 -22
  52. package/dist/editor-language.d.ts +12 -7
  53. package/dist/editor-language.js +54 -45
  54. package/dist/emmet.d.ts +3 -2
  55. package/dist/folding.d.ts +2 -4
  56. package/dist/folding.js +1 -2
  57. package/dist/format-on-type.d.ts +8 -2
  58. package/dist/format-on-type.js +8 -6
  59. package/dist/highlights.d.ts +2 -6
  60. package/dist/highlights.js +3 -3
  61. package/dist/history.d.ts +8 -2
  62. package/dist/history.js +4 -2
  63. package/dist/host-types.d.ts +39 -22
  64. package/dist/hover-content.js +0 -1
  65. package/dist/hover.d.ts +13 -2
  66. package/dist/hover.js +25 -28
  67. package/dist/index.d.ts +19 -9
  68. package/dist/index.js +5 -2
  69. package/dist/inlay-hints.d.ts +12 -21
  70. package/dist/inlay-hints.js +16 -15
  71. package/dist/inline-completion.d.ts +30 -10
  72. package/dist/inline-completion.js +346 -102
  73. package/dist/key-priority.d.ts +1 -1
  74. package/dist/key-priority.js +2 -2
  75. package/dist/language-context.d.ts +30 -0
  76. package/dist/language-context.js +1 -0
  77. package/dist/language-ids.d.ts +0 -1
  78. package/dist/language-ids.js +0 -32
  79. package/dist/line-text.js +0 -1
  80. package/dist/linked-editing.d.ts +2 -4
  81. package/dist/linked-editing.js +1 -3
  82. package/dist/live-templates.d.ts +14 -2
  83. package/dist/live-templates.js +15 -10
  84. package/dist/locales/en.d.ts +357 -0
  85. package/dist/locales/en.js +356 -0
  86. package/dist/locales/nl.d.ts +357 -0
  87. package/dist/locales/nl.js +356 -0
  88. package/dist/markup-context.d.ts +7 -6
  89. package/dist/markup-context.js +0 -1
  90. package/dist/markup.d.ts +8 -2
  91. package/dist/markup.js +19 -20
  92. package/dist/models.d.ts +2 -1
  93. package/dist/models.js +2 -1
  94. package/dist/navigation-history.d.ts +7 -0
  95. package/dist/navigation.d.ts +11 -8
  96. package/dist/navigation.js +9 -20
  97. package/dist/panel-size.d.ts +4 -0
  98. package/dist/paste-history.d.ts +8 -2
  99. package/dist/paste-history.js +4 -2
  100. package/dist/patch-layout.d.ts +0 -4
  101. package/dist/patch-layout.js +1 -1
  102. package/dist/peek-model.js +0 -1
  103. package/dist/peek.d.ts +8 -2
  104. package/dist/peek.js +31 -42
  105. package/dist/pick.d.ts +4 -2
  106. package/dist/pick.js +38 -15
  107. package/dist/popups.d.ts +25 -20
  108. package/dist/popups.js +31 -15
  109. package/dist/positions.d.ts +7 -0
  110. package/dist/positions.js +54 -0
  111. package/dist/postfix.d.ts +11 -2
  112. package/dist/postfix.js +10 -6
  113. package/dist/project-language.d.ts +11 -3
  114. package/dist/project-language.js +40 -9
  115. package/dist/project-problems.d.ts +7 -1
  116. package/dist/project-problems.js +2 -1
  117. package/dist/rename-model.js +1 -1
  118. package/dist/rename.d.ts +2 -2
  119. package/dist/rename.js +17 -22
  120. package/dist/rendered-docs.d.ts +8 -2
  121. package/dist/rendered-docs.js +5 -4
  122. package/dist/selection-ranges.d.ts +2 -2
  123. package/dist/semantic-tokens.d.ts +2 -4
  124. package/dist/semantic-tokens.js +2 -2
  125. package/dist/shortcut-keys.d.ts +2 -1
  126. package/dist/signature.d.ts +2 -2
  127. package/dist/signature.js +9 -15
  128. package/dist/snippet-field-transforms.js +1 -1
  129. package/dist/snippet-insert.d.ts +4 -2
  130. package/dist/snippet-insert.js +9 -15
  131. package/dist/snippet-session.d.ts +9 -4
  132. package/dist/snippet-session.js +14 -14
  133. package/dist/snippet-transform-format.d.ts +2 -1
  134. package/dist/snippet-transform-queue.d.ts +2 -4
  135. package/dist/snippet-transform-queue.js +5 -37
  136. package/dist/snippet.d.ts +2 -1
  137. package/dist/surround-constructs.d.ts +1 -1
  138. package/dist/surround.d.ts +13 -2
  139. package/dist/surround.js +15 -8
  140. package/dist/symbol-links.d.ts +0 -1
  141. package/dist/symbol-links.js +1 -1
  142. package/dist/symbol-picker-model.d.ts +0 -1
  143. package/dist/symbol-picker-model.js +1 -2
  144. package/dist/symbol-picker.d.ts +2 -2
  145. package/dist/symbol-picker.js +5 -10
  146. package/dist/symbols.d.ts +4 -3
  147. package/dist/template-builtins.d.ts +1 -1
  148. package/dist/template-builtins.js +0 -1
  149. package/dist/template-context.d.ts +3 -3
  150. package/dist/template-context.js +2 -11
  151. package/dist/template-types.d.ts +20 -0
  152. package/dist/template-types.js +1 -0
  153. package/dist/templates.d.ts +5 -20
  154. package/dist/templates.js +0 -1
  155. package/dist/testing.d.ts +6 -0
  156. package/dist/testing.js +42 -0
  157. package/dist/use-problem-counts.d.ts +1 -1
  158. package/dist/word-diff.d.ts +1 -1
  159. package/dist/word-diff.js +16 -25
  160. package/dist/workspace-edit.d.ts +1 -24
  161. package/dist/workspace-edit.js +0 -1
  162. package/package.json +10 -9
  163. package/src/AnchoredPopup.tsx +1 -1
  164. package/src/CompletionPopup.tsx +0 -2
  165. package/src/DiffView.tsx +6 -38
  166. package/src/EditorView.tsx +0 -1
  167. package/src/HoverSections.tsx +2 -2
  168. package/src/LanguagePopups.tsx +20 -14
  169. package/src/PanelResizeHandle.tsx +0 -1
  170. package/src/PeekPanel.tsx +0 -1
  171. package/src/PickPopup.tsx +0 -1
  172. package/src/RenderedDocs.tsx +0 -2
  173. package/src/SplitDiffView.tsx +1 -1
  174. package/src/SymbolPicker.tsx +0 -1
  175. package/src/call-hierarchy-model.ts +0 -7
  176. package/src/call-hierarchy.ts +32 -31
  177. package/src/clipboard-history.ts +5 -2
  178. package/src/code-actions-model.ts +1 -4
  179. package/src/code-actions.ts +26 -10
  180. package/src/code-authors.ts +4 -15
  181. package/src/code-vision-model.ts +1 -2
  182. package/src/code-vision.ts +23 -19
  183. package/src/completion-call.ts +2 -2
  184. package/src/completion-model.ts +12 -7
  185. package/src/completion.ts +31 -16
  186. package/src/context-menu-rows.ts +1 -1
  187. package/src/context-menu.ts +19 -10
  188. package/src/definition-link.ts +12 -4
  189. package/src/diagnostics-model.ts +8 -55
  190. package/src/diagnostics.ts +11 -20
  191. package/src/diff-view-props.ts +38 -0
  192. package/src/doc-comment-model.ts +0 -2
  193. package/src/document-colors.ts +14 -6
  194. package/src/document-links.ts +5 -6
  195. package/src/document-query.ts +3 -3
  196. package/src/edit-locations.ts +4 -19
  197. package/src/editor-language.ts +74 -39
  198. package/src/emmet.ts +2 -3
  199. package/src/folding.ts +4 -5
  200. package/src/format-on-type.ts +14 -7
  201. package/src/highlights.ts +6 -6
  202. package/src/history.ts +12 -4
  203. package/src/host-types.ts +52 -33
  204. package/src/hover-content.ts +0 -1
  205. package/src/hover.ts +29 -22
  206. package/src/index.ts +25 -14
  207. package/src/inlay-hints.ts +23 -17
  208. package/src/inline-completion.ts +340 -90
  209. package/src/key-priority.ts +2 -2
  210. package/src/language-context.ts +60 -0
  211. package/src/language-ids.ts +0 -34
  212. package/src/line-text.ts +0 -1
  213. package/src/linked-editing.ts +4 -6
  214. package/src/live-templates.ts +27 -13
  215. package/src/locales/en.ts +356 -0
  216. package/src/locales/nl.ts +356 -0
  217. package/src/markup-context.ts +6 -9
  218. package/src/markup.ts +19 -10
  219. package/src/models.ts +2 -1
  220. package/src/navigation-history.ts +11 -0
  221. package/src/navigation.ts +18 -20
  222. package/src/panel-size.ts +8 -0
  223. package/src/paste-history.ts +12 -4
  224. package/src/patch-layout.ts +1 -1
  225. package/src/peek-model.ts +0 -1
  226. package/src/peek.ts +22 -18
  227. package/src/pick.ts +30 -16
  228. package/src/popups.ts +61 -40
  229. package/src/positions.ts +54 -0
  230. package/src/postfix.ts +21 -8
  231. package/src/project-language.ts +63 -14
  232. package/src/project-problems.ts +10 -2
  233. package/src/proposal-model.ts +0 -1
  234. package/src/rename-model.ts +1 -1
  235. package/src/rename.ts +21 -24
  236. package/src/rendered-docs.ts +13 -6
  237. package/src/selection-ranges.ts +2 -2
  238. package/src/semantic-tokens.ts +5 -5
  239. package/src/shortcut-keys.ts +1 -1
  240. package/src/signature.ts +8 -8
  241. package/src/snippet-field-transforms.ts +1 -1
  242. package/src/snippet-insert.ts +21 -14
  243. package/src/snippet-session.ts +25 -24
  244. package/src/snippet-transform-format.ts +1 -1
  245. package/src/snippet-transform-queue.ts +5 -51
  246. package/src/snippet.ts +1 -1
  247. package/src/surround-constructs.ts +1 -1
  248. package/src/surround.ts +24 -12
  249. package/src/symbol-links.ts +1 -1
  250. package/src/symbol-picker-model.ts +1 -4
  251. package/src/symbol-picker.ts +6 -7
  252. package/src/symbols.ts +3 -4
  253. package/src/template-builtins.ts +1 -2
  254. package/src/template-context.ts +5 -14
  255. package/src/template-types.ts +43 -0
  256. package/src/templates.ts +9 -45
  257. package/src/testing.ts +52 -0
  258. package/src/use-problem-counts.ts +1 -1
  259. package/src/word-diff.ts +17 -28
  260. package/src/workspace-edit.ts +1 -34
  261. package/dist/locales/en.json +0 -356
  262. package/dist/locales/nl.json +0 -356
  263. package/src/locales/en.json +0 -356
  264. package/src/locales/nl.json +0 -356
@@ -1,10 +1,9 @@
1
- import { comparePositions } from './diagnostics-model.js';
2
1
  import { KEY_PRIORITY } from './key-priority.js';
2
+ import { comparePositions, endOfInsertion } from './positions.js';
3
3
  import { isShortcut } from './shortcut-keys.js';
4
4
  import { parseSnippet } from './snippet.js';
5
5
  import { realTimers } from './timers.js';
6
6
  const METHOD = 'textDocument/inlineCompletion';
7
- /* How long typing rests before a suggestion is asked. */
8
7
  const DELAY_MS = 300;
9
8
  export function itemsOf(result) {
10
9
  if (result === null) {
@@ -12,6 +11,19 @@ export function itemsOf(result) {
12
11
  }
13
12
  return Array.isArray(result) ? result : result.items;
14
13
  }
14
+ /* Whether the suggestion still continues the text at the caret: the caret is in its range and what lies between the range's start and it is how the suggestion begins. */
15
+ function continuesAt(suggestion, caret, textOf) {
16
+ const {
17
+ range
18
+ } = suggestion;
19
+ if (comparePositions(range.start, caret) > 0 || comparePositions(range.end, caret) < 0) {
20
+ return false;
21
+ }
22
+ return suggestion.text.startsWith(textOf({
23
+ start: range.start,
24
+ end: caret
25
+ }));
26
+ }
15
27
  /*
16
28
  * A server's item as a suggestion at the caret, or null when it does not continue what the line holds: the text the
17
29
  * item replaces up to the caret must start its insert text, so the rest can stand after the caret.
@@ -22,17 +34,16 @@ export function suggestionOf(item, caret, textOf) {
22
34
  start: caret,
23
35
  end: caret
24
36
  };
25
- if (comparePositions(range.start, caret) > 0 || comparePositions(range.end, caret) < 0) {
37
+ if (!continuesAt({
38
+ text,
39
+ range
40
+ }, caret, textOf)) {
26
41
  return null;
27
42
  }
28
- const typed = textOf({
43
+ const rest = text.slice(textOf({
29
44
  start: range.start,
30
45
  end: caret
31
- });
32
- if (!text.startsWith(typed)) {
33
- return null;
34
- }
35
- const rest = text.slice(typed.length);
46
+ }).length);
36
47
  return rest === '' ? null : {
37
48
  item,
38
49
  text,
@@ -40,30 +51,95 @@ export function suggestionOf(item, caret, textOf) {
40
51
  rest
41
52
  };
42
53
  }
54
+ /* The first word of a suggestion with the whitespace before it, which is what taking a word takes. */
55
+ export function firstWord(text) {
56
+ const match = /^\s*(?:[\p{L}\p{N}_$]+|[^\p{L}\p{N}_$\s]+)/u.exec(text);
57
+ return match === null ? text : match[0];
58
+ }
59
+ /* Lets go of what a phase holds outside the feature. Aborting runs the source's listeners, which may call back in. */
60
+ function release(phase, timers) {
61
+ if (phase.kind === 'scheduled') {
62
+ timers.clear(phase.timer);
63
+ } else if (phase.kind === 'asking') {
64
+ phase.controller.abort();
65
+ }
66
+ }
67
+ /* The language servers as a source: asked after typing stops, once the host turned it on. */
68
+ class ServerSuggestions {
69
+ automatic = true;
70
+ enabledByHost = false;
71
+ language;
72
+ constructor(language) {
73
+ this.language = language;
74
+ }
75
+ get enabled() {
76
+ return this.enabledByHost && this.supported;
77
+ }
78
+ get supported() {
79
+ const {
80
+ service
81
+ } = this.language;
82
+ return service.inlineCompletions !== undefined && service.supports(METHOD, this.language.uri);
83
+ }
84
+ async suggest(request) {
85
+ const {
86
+ editor,
87
+ service,
88
+ uri
89
+ } = this.language;
90
+ try {
91
+ const result = await service.inlineCompletions(uri, request.caret, {
92
+ triggerKind: request.triggerKind
93
+ }, {
94
+ signal: request.signal
95
+ });
96
+ return itemsOf(result).flatMap(item => suggestionOf(item, request.caret, range => editor.textInRange(range)) ?? []);
97
+ } catch {
98
+ // A refusal or a failing server is no suggestion; the next pause asks again.
99
+ return [];
100
+ }
101
+ }
102
+ accepted(suggestion) {
103
+ const {
104
+ service,
105
+ uri
106
+ } = this.language;
107
+ const command = suggestion.item?.command;
108
+ if (command !== undefined && service.supports('workspace/executeCommand', uri)) {
109
+ void service.executeCommand(uri, command).catch(() => undefined);
110
+ }
111
+ }
112
+ }
43
113
  /*
44
- * Suggestions a server writes ahead of the caret, drawn as ghost text: asked a moment after typing stops, or at
45
- * once with the `suggestInline` key of the host. Tab takes the suggestion, Alt+] and Alt+[ step through the others,
46
- * Escape and any key that moves away put it away. Off unless the host turns it on, since a host may draw
47
- * suggestions of its own in the same place.
114
+ * The one owner of the editor's ghost text. Sources give it suggestions: the language servers' (off unless the host
115
+ * turns it on with `configure`) and any an app adds with `addSource`. It draws one at a time, takes it with Tab,
116
+ * steps through the others with Alt+] and Alt+[ and puts it away with Escape or any edit or move away from it.
117
+ *
118
+ * A key of the host's (`suggestInline`) asks the enabled source of the highest priority at once; typing that stops asks
119
+ * the automatic source of the highest priority. Tab takes the suggestion before a suggestion list when a person asked
120
+ * for it, and after the list when it came by itself.
48
121
  */
49
122
  export class InlineCompletionFeature {
50
123
  language;
51
124
  timers;
52
- enabled = false;
53
- suggestions = [];
54
- index = 0;
55
- timer = null;
56
- controller = null;
57
- revision = 0;
125
+ servers;
126
+ sources = [];
127
+ phase = {
128
+ kind: 'idle'
129
+ };
58
130
  accepting = false;
59
131
  constructor(language, timers = realTimers) {
60
132
  this.language = language;
61
133
  this.timers = timers;
134
+ this.servers = new ServerSuggestions(language);
135
+ this.addSource(this.servers, -1);
62
136
  const {
63
137
  editor
64
138
  } = language;
65
- const offs = [language.onKey(event => this.key(event), KEY_PRIORITY.inlineCompletion), language.onKey(event => this.tab(event), KEY_PRIORITY.inlineAccept), editor.onTextChange(change => this.edited(change)), editor.onCaret(() => queueMicrotask(() => this.moved()))];
139
+ const offs = [language.onKey(event => this.key(event), KEY_PRIORITY.inlineCompletion), language.onKey(event => this.tab(event), KEY_PRIORITY.suggestionAccept), editor.onTextChange(change => this.edited(change)), editor.onCaret(() => this.moved())];
66
140
  language.onDispose(() => {
141
+ // No source is asked again, also not by one that calls back in while it is let go of.
142
+ this.sources.splice(0);
67
143
  this.hide();
68
144
  for (const off of offs) {
69
145
  off();
@@ -71,97 +147,216 @@ export class InlineCompletionFeature {
71
147
  });
72
148
  }
73
149
  get supported() {
74
- const {
75
- service
76
- } = this.language;
77
- return service.inlineCompletions !== undefined && service.supports(METHOD, this.language.uri);
150
+ return this.servers.supported;
78
151
  }
79
152
  get isEnabled() {
80
- return this.enabled;
153
+ return this.servers.enabledByHost;
81
154
  }
82
- /* The suggestion drawn now, or null. */
83
155
  get current() {
84
- return this.suggestions[this.index] ?? null;
156
+ const {
157
+ phase
158
+ } = this;
159
+ return phase.kind === 'showing' ? phase.suggestions[phase.index] ?? null : null;
160
+ }
161
+ holds(source) {
162
+ return this.phase.kind === 'showing' && this.phase.source === source;
163
+ }
164
+ /* Adds a source; of two the one with the higher priority is asked first, and the language servers' has -1. */
165
+ addSource(source, priority = 0) {
166
+ const entry = {
167
+ source,
168
+ priority
169
+ };
170
+ const index = this.sources.findIndex(other => other.priority < priority);
171
+ this.sources.splice(index === -1 ? this.sources.length : index, 0, entry);
172
+ return () => {
173
+ const at = this.sources.indexOf(entry);
174
+ if (at !== -1) {
175
+ this.sources.splice(at, 1);
176
+ }
177
+ if (this.uses(source)) {
178
+ this.hide();
179
+ }
180
+ };
85
181
  }
182
+ /* Turns the language servers' suggestions on or off; they are off until the host asks. */
86
183
  configure(settings) {
87
- this.enabled = settings.enabled === true;
88
- if (!this.enabled) {
184
+ this.servers.enabledByHost = settings.enabled === true;
185
+ if (!this.servers.enabledByHost && this.uses(this.servers)) {
89
186
  this.hide();
90
187
  }
91
188
  }
92
- /* Asks for suggestions at the caret now, as a person asking for one does. */
189
+ /* Whether the source has a suggestion drawn, a request out or an ask queued. */
190
+ uses(source) {
191
+ return this.phase.kind !== 'idle' && this.phase.source === source;
192
+ }
193
+ /* Whether the source is registered and enabled. An app's setting can turn a source off without telling this feature, so it is read at every step. */
194
+ asks(source) {
195
+ return source.enabled && this.sources.some(entry => entry.source === source);
196
+ }
197
+ /* Asks the enabled source of the highest priority for suggestions at the caret now, as a person asking for one does; false when none is enabled. */
93
198
  trigger() {
94
- this.timers.clear(this.timer);
95
- void this.ask(1);
199
+ const source = this.sources.find(entry => entry.source.enabled)?.source;
200
+ if (source === undefined) {
201
+ return false;
202
+ }
203
+ void this.ask(source, 1);
204
+ return true;
205
+ }
206
+ /* Asks one source now, for an app whose own command asks for its suggestion; settles once the answer is drawn or dropped. */
207
+ triggerSource(source) {
208
+ return this.ask(source, 1);
96
209
  }
97
210
  /* Takes the drawn suggestion into the text; false when there is none. */
98
211
  accept() {
212
+ const {
213
+ phase
214
+ } = this;
99
215
  const suggestion = this.current;
100
- if (suggestion === null) {
216
+ if (phase.kind !== 'showing' || suggestion === null) {
101
217
  return false;
102
218
  }
103
- const {
104
- editor,
105
- service,
106
- uri
107
- } = this.language;
108
- this.accepting = true;
109
- try {
110
- editor.applyEdits([{
111
- range: suggestion.range,
112
- text: suggestion.text
113
- }]);
114
- } finally {
115
- this.accepting = false;
219
+ this.insert(suggestion.range, suggestion.text, endOfInsertion(suggestion.range.start, suggestion.text));
220
+ if (this.phase === phase) {
221
+ this.enter({
222
+ kind: 'idle'
223
+ });
116
224
  }
117
- this.hide();
225
+ // Last: the disposer of the accessory ran in between and may have taken the source away.
226
+ if (this.asks(phase.source)) {
227
+ phase.source.accepted?.(suggestion);
228
+ }
229
+ return true;
230
+ }
231
+ /* Takes the first word of a suggestion that allows it; the rest stays drawn after it. */
232
+ acceptWord() {
118
233
  const {
119
- command
120
- } = suggestion.item;
121
- if (command !== undefined && service.supports('workspace/executeCommand', uri)) {
122
- void service.executeCommand(uri, command).catch(() => undefined);
234
+ phase
235
+ } = this;
236
+ const suggestion = this.current;
237
+ if (phase.kind !== 'showing' || suggestion === null || suggestion.words !== true) {
238
+ return false;
239
+ }
240
+ const caret = this.language.editor.getCaret();
241
+ const word = firstWord(suggestion.rest);
242
+ const next = endOfInsertion(caret, word);
243
+ this.insert({
244
+ start: caret,
245
+ end: caret
246
+ }, word, next);
247
+ if (this.phase !== phase) {
248
+ return true;
123
249
  }
250
+ const rest = suggestion.rest.slice(word.length);
251
+ this.enter(rest.trim() === '' ? {
252
+ kind: 'idle'
253
+ } : {
254
+ ...phase,
255
+ suggestions: [{
256
+ ...suggestion,
257
+ range: {
258
+ start: next,
259
+ end: next
260
+ },
261
+ text: rest,
262
+ rest
263
+ }],
264
+ index: 0
265
+ });
124
266
  return true;
125
267
  }
126
- /* Steps to the next or the previous suggestion. */
127
268
  cycle(step) {
128
- if (this.suggestions.length > 1) {
129
- this.index = (this.index + step + this.suggestions.length) % this.suggestions.length;
130
- this.draw();
269
+ const {
270
+ phase
271
+ } = this;
272
+ if (phase.kind !== 'showing' || phase.suggestions.length < 2) {
273
+ return;
131
274
  }
275
+ const count = phase.suggestions.length;
276
+ this.enter({
277
+ ...phase,
278
+ index: (phase.index + step + count) % count
279
+ });
132
280
  }
281
+ /* Puts the suggestions away and ends a request that is still out. */
133
282
  hide() {
134
- this.revision++;
135
- this.timers.clear(this.timer);
136
- this.controller?.abort();
137
- if (this.suggestions.length > 0) {
138
- this.suggestions = [];
139
- this.language.editor.setGhostText(null);
283
+ this.enter({
284
+ kind: 'idle'
285
+ });
286
+ }
287
+ /*
288
+ * The one way the phase changes: state first, then the previous phase let go of, then the ghost drawn from
289
+ * `this.phase` alone. Letting go runs the source's abort listeners, so drawing waits until they are done and
290
+ * is skipped when they entered a phase of their own, which drew itself.
291
+ */
292
+ enter(next) {
293
+ const previous = this.phase;
294
+ this.phase = next;
295
+ release(previous, this.timers);
296
+ if (this.phase !== next) {
297
+ return;
298
+ }
299
+ if (previous.kind === 'showing' || next.kind === 'showing') {
300
+ this.draw();
301
+ }
302
+ }
303
+ /* An edit and caret of the feature's own, which neither puts the suggestion away nor asks again. */
304
+ insert(range, text, caret) {
305
+ const {
306
+ editor
307
+ } = this.language;
308
+ this.accepting = true;
309
+ try {
310
+ editor.applyEdits([{
311
+ range,
312
+ text
313
+ }]);
314
+ editor.setCaret(caret);
315
+ } finally {
316
+ this.accepting = false;
140
317
  }
141
318
  }
142
319
  /* Tab takes the suggestion on screen before any feature that expands on Tab; without one it is left alone. */
143
320
  tab(event) {
144
- if (this.current === null || event.isComposing) {
321
+ const {
322
+ phase
323
+ } = this;
324
+ if (phase.kind !== 'showing' || event.isComposing) {
145
325
  return false;
146
326
  }
147
- if (event.key === 'Tab' && !event.shiftKey && !event.altKey && !event.ctrlKey && !event.metaKey) {
148
- return this.accept();
327
+ if (event.key !== 'Tab' || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
328
+ return false;
149
329
  }
150
- return false;
330
+ const {
331
+ popups
332
+ } = this.language;
333
+ if (!phase.manual && (popups.slot('completion').get() !== null || popups.slot('pick').get() !== null)) {
334
+ return false;
335
+ }
336
+ return this.accept();
151
337
  }
152
338
  key(event) {
153
- if (isShortcut(this.language.shortcuts.suggestInline, event) && this.enabled) {
154
- this.trigger();
155
- return true;
339
+ if (isShortcut(this.language.shortcuts.suggestInline, event)) {
340
+ return this.trigger();
156
341
  }
157
- if (this.current === null || event.isComposing) {
342
+ if (event.isComposing) {
158
343
  return false;
159
344
  }
160
- if (event.key === 'Escape') {
345
+ const {
346
+ phase
347
+ } = this;
348
+ if (event.key === 'Escape' && (phase.kind === 'showing' || phase.kind === 'asking' && phase.manual)) {
161
349
  this.hide();
162
350
  return true;
163
351
  }
164
- if (event.altKey && !event.ctrlKey && !event.metaKey && (event.code === 'BracketRight' || event.code === 'BracketLeft')) {
352
+ if (phase.kind !== 'showing') {
353
+ return false;
354
+ }
355
+ if (isShortcut(this.language.shortcuts.acceptGhostWord, event) && this.acceptWord()) {
356
+ return true;
357
+ }
358
+ // With one suggestion there is nothing to cycle to, so the key goes on to the editor and types its character.
359
+ if (phase.suggestions.length > 1 && event.altKey && !event.ctrlKey && !event.metaKey && (event.code === 'BracketRight' || event.code === 'BracketLeft')) {
165
360
  this.cycle(event.code === 'BracketRight' ? 1 : -1);
166
361
  return true;
167
362
  }
@@ -171,58 +366,107 @@ export class InlineCompletionFeature {
171
366
  if (this.accepting) {
172
367
  return;
173
368
  }
174
- this.hide();
175
- if (this.enabled && change.source === 'input' && this.supported) {
176
- this.timer = this.timers.set(() => void this.ask(2), DELAY_MS);
369
+ const source = this.sources.find(entry => entry.source.automatic && entry.source.enabled)?.source;
370
+ if (source === undefined || change.source !== 'input') {
371
+ this.hide();
372
+ return;
373
+ }
374
+ const timer = this.timers.set(() => this.fire(timer), DELAY_MS);
375
+ this.enter({
376
+ kind: 'scheduled',
377
+ source,
378
+ timer
379
+ });
380
+ }
381
+ fire(timer) {
382
+ const {
383
+ phase
384
+ } = this;
385
+ if (phase.kind === 'scheduled' && phase.timer === timer) {
386
+ void this.ask(phase.source, 2);
177
387
  }
178
388
  }
179
- /* A caret that left the place of the suggestion puts it away. */
389
+ /* A caret that left the place of the suggestion, or of the request still out, puts it away. */
180
390
  moved() {
391
+ if (this.accepting) {
392
+ return;
393
+ }
394
+ const {
395
+ phase
396
+ } = this;
397
+ const {
398
+ editor
399
+ } = this.language;
400
+ const caret = editor.getCaret();
401
+ if (phase.kind === 'asking' && comparePositions(phase.caret, caret) !== 0) {
402
+ this.hide();
403
+ return;
404
+ }
181
405
  const suggestion = this.current;
182
406
  if (suggestion === null) {
183
407
  return;
184
408
  }
185
- const caret = this.language.editor.getCaret();
186
- const selections = this.language.editor.getSelections();
187
- if (selections.length !== 1 || suggestionOf(suggestion.item, caret, range => this.language.editor.textInRange(range)) === null) {
409
+ if (editor.getSelections().length !== 1 || !continuesAt(suggestion, caret, range => editor.textInRange(range))) {
188
410
  this.hide();
189
411
  }
190
412
  }
191
- async ask(triggerKind) {
413
+ async ask(source, triggerKind) {
192
414
  const {
193
- editor,
194
- service,
195
- uri
415
+ editor
196
416
  } = this.language;
197
- if (!this.enabled || !this.supported || editor.getSelections().length !== 1 || editor.isReadOnly?.() === true) {
417
+ if (!this.asks(source) || editor.getSelections().length !== 1 || editor.isReadOnly?.() === true) {
198
418
  return;
199
419
  }
200
- this.controller?.abort();
201
- const controller = new AbortController();
202
- this.controller = controller;
203
- const revision = this.revision;
204
- const caret = editor.getCaret();
420
+ const asking = {
421
+ kind: 'asking',
422
+ source,
423
+ controller: new AbortController(),
424
+ caret: editor.getCaret(),
425
+ manual: triggerKind === 1
426
+ };
427
+ this.enter(asking);
428
+ if (this.phase !== asking) {
429
+ return;
430
+ }
431
+ let found = [];
205
432
  try {
206
- const result = await service.inlineCompletions(uri, caret, {
207
- triggerKind
208
- }, {
209
- signal: controller.signal
433
+ found = await source.suggest({
434
+ caret: asking.caret,
435
+ triggerKind,
436
+ signal: asking.controller.signal
210
437
  });
211
- if (revision !== this.revision || comparePositions(editor.getCaret(), caret) !== 0) {
212
- return;
213
- }
214
- this.suggestions = itemsOf(result).flatMap(item => suggestionOf(item, caret, range => editor.textInRange(range)) ?? []);
215
- this.index = 0;
216
- this.draw();
217
438
  } catch {
218
- // A refusal or a failing server is no suggestion; the next pause asks again.
439
+ // A source that fails gives no suggestion; the next pause asks again.
440
+ }
441
+ if (this.phase !== asking) {
442
+ return;
443
+ }
444
+ // The answer ends the request without aborting it; there is nothing left to let go of.
445
+ this.phase = {
446
+ kind: 'idle'
447
+ };
448
+ if (!this.asks(source) || comparePositions(editor.getCaret(), asking.caret) !== 0 || found.length === 0) {
449
+ return;
219
450
  }
451
+ this.enter({
452
+ kind: 'showing',
453
+ source,
454
+ suggestions: found,
455
+ index: 0,
456
+ manual: asking.manual
457
+ });
220
458
  }
221
459
  draw() {
460
+ const {
461
+ editor
462
+ } = this.language;
222
463
  const suggestion = this.current;
223
- this.language.editor.setGhostText(suggestion === null ? null : {
224
- position: this.language.editor.getCaret(),
225
- text: suggestion.rest
464
+ editor.setGhostText(suggestion === null ? null : {
465
+ position: editor.getCaret(),
466
+ text: suggestion.rest,
467
+ ...(suggestion.accessory === undefined ? {} : {
468
+ accessory: suggestion.accessory
469
+ })
226
470
  });
227
471
  }
228
472
  }
@@ -1,10 +1,10 @@
1
1
  import type { EditorKeyHandler } from '@adecore/editor';
2
2
  export declare const KEY_PRIORITY: {
3
+ readonly suggestionAccept: 1700;
3
4
  readonly pick: 1600;
4
5
  readonly diagnostics: 1500;
5
6
  readonly hover: 1400;
6
7
  readonly completion: 1300;
7
- readonly inlineAccept: 1250;
8
8
  readonly snippets: 1200;
9
9
  readonly templates: 1150;
10
10
  readonly markup: 1100;
@@ -3,12 +3,12 @@
3
3
  * apart, so a handler of the app can stand between two features; 0, the default, comes after all of them.
4
4
  */
5
5
  export const KEY_PRIORITY = {
6
+ // Tab only: the suggestion a person asked for is taken before a list; one that came by itself gives way to the list in the handler.
7
+ suggestionAccept: 1700,
6
8
  pick: 1600,
7
9
  diagnostics: 1500,
8
10
  hover: 1400,
9
11
  completion: 1300,
10
- // Tab only: a suggestion on screen is taken before a template or an abbreviation expands, so the spacing of a hundred bends here too.
11
- inlineAccept: 1250,
12
12
  snippets: 1200,
13
13
  // Between snippets and markup, so the spacing of a hundred bends here: Tab has to reach the templates after a running snippet and before Emmet.
14
14
  templates: 1150,
@@ -0,0 +1,30 @@
1
+ import type { Editor, EditorKeyHandler, EditorPosition } from '@adecore/editor';
2
+ import type { Keymap } from '@adecore/editor/keymap';
3
+ import type { LanguageService, Location, TextDocumentSaveReason } from '@adecore/lsp';
4
+ import type { Place } from './navigation-history.ts';
5
+ import type { Popups } from './popups.ts';
6
+ import type { LanguageDocumentHandle, ProjectLanguage } from './project-language.ts';
7
+ import type { shortcutsOf } from './shortcut-keys.ts';
8
+ export interface LanguageContext {
9
+ readonly editor: Editor;
10
+ readonly apple: boolean;
11
+ readonly keymap: Keymap;
12
+ readonly shortcuts: ReturnType<typeof shortcutsOf>;
13
+ readonly project: ProjectLanguage;
14
+ readonly document: LanguageDocumentHandle;
15
+ readonly languageId: string;
16
+ readonly served: boolean;
17
+ readonly popups: Popups;
18
+ readonly uri: string;
19
+ readonly service: LanguageService;
20
+ readonly isDisposed: boolean;
21
+ readonly place: Place;
22
+ goTo(location: Location): void;
23
+ jump(position: EditorPosition): void;
24
+ visit(place: Place): void;
25
+ onProvidersChanged(listener: () => void): () => void;
26
+ onKey(handler: EditorKeyHandler, priority?: number): () => void;
27
+ onServedKey(handler: EditorKeyHandler, priority?: number): () => void;
28
+ prepareSave(reason?: TextDocumentSaveReason): Promise<void>;
29
+ onDispose(dispose: () => void): void;
30
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,2 @@
1
- export declare function lspLanguageIdOf(language: string | undefined): string | null;
2
1
  export declare function shikiLanguageOf(lspLanguageId: string): string;
3
2
  export declare function shikiLanguageOfPath(path: string): string;
@@ -1,35 +1,3 @@
1
- /* Highlighter ids mapped to the LSP id of the same language. */
2
- const LSP_IDS = {
3
- typescript: 'typescript',
4
- tsx: 'typescriptreact',
5
- javascript: 'javascript',
6
- jsx: 'javascriptreact',
7
- vue: 'vue',
8
- php: 'php',
9
- css: 'css',
10
- scss: 'scss',
11
- less: 'less',
12
- html: 'html',
13
- json: 'json',
14
- jsonc: 'jsonc',
15
- yaml: 'yaml',
16
- python: 'python',
17
- shellscript: 'shellscript',
18
- docker: 'dockerfile',
19
- twig: 'twig',
20
- sql: 'sql',
21
- rust: 'rust',
22
- markdown: 'markdown',
23
- toml: 'toml',
24
- lua: 'lua',
25
- go: 'go',
26
- swift: 'swift',
27
- zig: 'zig'
28
- };
29
- /* The LSP language id of a file, or null when no language server here knows its language. */
30
- export function lspLanguageIdOf(language) {
31
- return language === undefined ? null : LSP_IDS[language] ?? null;
32
- }
33
1
  const SHIKI_IDS = {
34
2
  typescriptreact: 'tsx',
35
3
  javascriptreact: 'jsx'
package/dist/line-text.js CHANGED
@@ -10,7 +10,6 @@ export function lineTextOf(editor, line) {
10
10
  }
11
11
  });
12
12
  }
13
- /* The text of a position's line up to the position. */
14
13
  export function textBeforeOf(editor, position) {
15
14
  return editor.textInRange({
16
15
  start: {