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