-
Notifications
You must be signed in to change notification settings - Fork 29.8k
/
inlineCompletionsModel.ts
901 lines (773 loc) · 28.2 KB
/
inlineCompletionsModel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { assertNever } from 'vs/base/common/assert';
import { CancelablePromise, createCancelablePromise, RunOnceScheduler } from 'vs/base/common/async';
import { CancellationToken } from 'vs/base/common/cancellation';
import { onUnexpectedError, onUnexpectedExternalError } from 'vs/base/common/errors';
import { Emitter } from 'vs/base/common/event';
import { matchesSubString } from 'vs/base/common/filters';
import { Disposable, IDisposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { CoreEditingCommands } from 'vs/editor/browser/coreCommands';
import { IActiveCodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { CursorChangeReason } from 'vs/editor/common/cursorEvents';
import { LanguageFeatureRegistry } from 'vs/editor/common/languageFeatureRegistry';
import { Command, InlineCompletion, InlineCompletionContext, InlineCompletions, InlineCompletionsProvider, InlineCompletionTriggerKind } from 'vs/editor/common/languages';
import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry';
import { ITextModel } from 'vs/editor/common/model';
import { fixBracketsInLine } from 'vs/editor/common/model/bracketPairsTextModelPart/fixBrackets';
import { IFeatureDebounceInformation, ILanguageFeatureDebounceService } from 'vs/editor/common/services/languageFeatureDebounce';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
import { inlineSuggestCommitId } from 'vs/editor/contrib/inlineCompletions/browser/consts';
import { BaseGhostTextWidgetModel, GhostText, GhostTextReplacement, GhostTextWidgetModel } from 'vs/editor/contrib/inlineCompletions/browser/ghostText';
import { SharedInlineCompletionCache } from 'vs/editor/contrib/inlineCompletions/browser/ghostTextModel';
import { inlineCompletionToGhostText, NormalizedInlineCompletion } from 'vs/editor/contrib/inlineCompletions/browser/inlineCompletionToGhostText';
import { getReadonlyEmptyArray } from 'vs/editor/contrib/inlineCompletions/browser/utils';
import { SnippetController2 } from 'vs/editor/contrib/snippet/browser/snippetController2';
import { SnippetParser, Text } from 'vs/editor/contrib/snippet/browser/snippetParser';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
export class InlineCompletionsModel extends Disposable implements GhostTextWidgetModel {
protected readonly onDidChangeEmitter = new Emitter<void>();
public readonly onDidChange = this.onDidChangeEmitter.event;
public readonly completionSession = this._register(
new MutableDisposable<InlineCompletionsSession>()
);
private active: boolean = false;
private disposed = false;
private readonly debounceValue = this.debounceService.for(
this.languageFeaturesService.inlineCompletionsProvider,
'InlineCompletionsDebounce',
{ min: 50, max: 50 }
);
constructor(
private readonly editor: IActiveCodeEditor,
private readonly cache: SharedInlineCompletionCache,
@ICommandService private readonly commandService: ICommandService,
@ILanguageConfigurationService private readonly languageConfigurationService: ILanguageConfigurationService,
@ILanguageFeaturesService private readonly languageFeaturesService: ILanguageFeaturesService,
@ILanguageFeatureDebounceService private readonly debounceService: ILanguageFeatureDebounceService,
@IConfigurationService configurationService: IConfigurationService,
) {
super();
this._register(
commandService.onDidExecuteCommand((e) => {
// These commands don't trigger onDidType.
const commands = new Set([
CoreEditingCommands.Tab.id,
CoreEditingCommands.DeleteLeft.id,
CoreEditingCommands.DeleteRight.id,
inlineSuggestCommitId,
'acceptSelectedSuggestion',
]);
if (commands.has(e.commandId) && editor.hasTextFocus()) {
this.handleUserInput();
}
})
);
this._register(
this.editor.onDidType((e) => {
this.handleUserInput();
})
);
this._register(
this.editor.onDidChangeCursorPosition((e) => {
if (e.reason === CursorChangeReason.Explicit ||
this.session && !this.session.isValid) {
this.hide();
}
})
);
this._register(
toDisposable(() => {
this.disposed = true;
})
);
this._register(
this.editor.onDidBlurEditorWidget(() => {
// This is a hidden setting very useful for debugging
if (configurationService.getValue('editor.inlineSuggest.hideOnBlur')) {
return;
}
this.hide();
})
);
}
private handleUserInput() {
if (this.session && !this.session.isValid) {
this.hide();
}
setTimeout(() => {
if (this.disposed) {
return;
}
// Wait for the cursor update that happens in the same iteration loop iteration
this.startSessionIfTriggered();
}, 0);
}
private get session(): InlineCompletionsSession | undefined {
return this.completionSession.value;
}
public get ghostText(): GhostText | GhostTextReplacement | undefined {
return this.session?.ghostText;
}
public get minReservedLineCount(): number {
return this.session ? this.session.minReservedLineCount : 0;
}
public get expanded(): boolean {
return this.session ? this.session.expanded : false;
}
public setExpanded(expanded: boolean): void {
this.session?.setExpanded(expanded);
}
public setActive(active: boolean) {
this.active = active;
if (active) {
this.session?.scheduleAutomaticUpdate();
}
}
private startSessionIfTriggered(): void {
const suggestOptions = this.editor.getOption(EditorOption.inlineSuggest);
if (!suggestOptions.enabled) {
return;
}
if (this.session && this.session.isValid) {
return;
}
this.trigger(InlineCompletionTriggerKind.Automatic);
}
public trigger(triggerKind: InlineCompletionTriggerKind): void {
if (this.completionSession.value) {
if (triggerKind === InlineCompletionTriggerKind.Explicit) {
void this.completionSession.value.ensureUpdateWithExplicitContext();
}
return;
}
this.completionSession.value = new InlineCompletionsSession(
this.editor,
this.editor.getPosition(),
() => this.active,
this.commandService,
this.cache,
triggerKind,
this.languageConfigurationService,
this.languageFeaturesService.inlineCompletionsProvider,
this.debounceValue
);
this.completionSession.value.takeOwnership(
this.completionSession.value.onDidChange(() => {
this.onDidChangeEmitter.fire();
})
);
}
public hide(): void {
this.completionSession.clear();
this.onDidChangeEmitter.fire();
}
public commitCurrentSuggestion(): void {
// Don't dispose the session, so that after committing, more suggestions are shown.
this.session?.commitCurrentCompletion();
}
public commitCurrentSuggestionPartially(): void {
this.session?.commitCurrentCompletionNextWord();
}
public showNext(): void {
this.session?.showNextInlineCompletion();
}
public showPrevious(): void {
this.session?.showPreviousInlineCompletion();
}
public async hasMultipleInlineCompletions(): Promise<boolean> {
const result = await this.session?.hasMultipleInlineCompletions();
return result !== undefined ? result : false;
}
}
export class InlineCompletionsSession extends BaseGhostTextWidgetModel {
public readonly minReservedLineCount = 0;
private readonly updateOperation = this._register(new MutableDisposable<UpdateOperation>());
private readonly updateSoon = this._register(new RunOnceScheduler(() => {
const triggerKind = this.initialTriggerKind;
// All subsequent triggers are automatic.
this.initialTriggerKind = InlineCompletionTriggerKind.Automatic;
return this.update(triggerKind);
}, 50));
constructor(
editor: IActiveCodeEditor,
private readonly triggerPosition: Position,
private readonly shouldUpdate: () => boolean,
private readonly commandService: ICommandService,
private readonly cache: SharedInlineCompletionCache,
private initialTriggerKind: InlineCompletionTriggerKind,
private readonly languageConfigurationService: ILanguageConfigurationService,
private readonly registry: LanguageFeatureRegistry<InlineCompletionsProvider>,
private readonly debounce: IFeatureDebounceInformation,
) {
super(editor);
let lastCompletionItem: InlineCompletion | undefined = undefined;
this._register(this.onDidChange(() => {
const currentCompletion = this.currentCompletion;
if (currentCompletion && currentCompletion.sourceInlineCompletion !== lastCompletionItem) {
lastCompletionItem = currentCompletion.sourceInlineCompletion;
const provider = currentCompletion.sourceProvider;
provider.handleItemDidShow?.(currentCompletion.sourceInlineCompletions, lastCompletionItem);
}
}));
this._register(toDisposable(() => {
this.cache.clear();
}));
this._register(this.editor.onDidChangeCursorPosition((e) => {
if (e.reason === CursorChangeReason.Explicit) {
return;
}
// Ghost text depends on the cursor position
this.cache.value?.updateRanges();
if (this.cache.value) {
this.updateFilteredInlineCompletions();
this.onDidChangeEmitter.fire();
}
}));
this._register(this.editor.onDidChangeModelContent((e) => {
// Call this in case `onDidChangeModelContent` calls us first.
this.cache.value?.updateRanges();
this.updateFilteredInlineCompletions();
this.scheduleAutomaticUpdate();
}));
this._register(this.registry.onDidChange(() => {
this.updateSoon.schedule(this.debounce.get(this.editor.getModel()));
}));
this.scheduleAutomaticUpdate();
}
private filteredCompletions: readonly CachedInlineCompletion[] = [];
private updateFilteredInlineCompletions() {
if (!this.cache.value) {
this.filteredCompletions = [];
return;
}
const model = this.editor.getModel();
const cursorPosition = model.validatePosition(this.editor.getPosition());
this.filteredCompletions = this.cache.value.completions.filter(c => {
const originalValue = model.getValueInRange(c.synchronizedRange).toLowerCase();
const filterText = c.inlineCompletion.filterText.toLowerCase();
const indent = model.getLineIndentColumn(c.synchronizedRange.startLineNumber);
const cursorPosIndex = Math.max(0, cursorPosition.column - c.synchronizedRange.startColumn);
let filterTextBefore = filterText.substring(0, cursorPosIndex);
let filterTextAfter = filterText.substring(cursorPosIndex);
let originalValueBefore = originalValue.substring(0, cursorPosIndex);
let originalValueAfter = originalValue.substring(cursorPosIndex);
if (c.synchronizedRange.startColumn <= indent) {
// Remove indentation
originalValueBefore = originalValueBefore.trimStart();
if (originalValueBefore.length === 0) {
originalValueAfter = originalValueAfter.trimStart();
}
filterTextBefore = filterTextBefore.trimStart();
if (filterTextBefore.length === 0) {
filterTextAfter = filterTextAfter.trimStart();
}
}
return filterTextBefore.startsWith(originalValueBefore)
&& matchesSubString(originalValueAfter, filterTextAfter);
});
}
//#region Selection
// We use a semantic id to track the selection even if the cache changes.
private currentlySelectedCompletionId: string | undefined = undefined;
private fixAndGetIndexOfCurrentSelection(): number {
if (!this.currentlySelectedCompletionId || !this.cache.value) {
return 0;
}
if (this.cache.value.completions.length === 0) {
// don't reset the selection in this case
return 0;
}
const idx = this.filteredCompletions.findIndex(v => v.semanticId === this.currentlySelectedCompletionId);
if (idx === -1) {
// Reset the selection so that the selection does not jump back when it appears again
this.currentlySelectedCompletionId = undefined;
return 0;
}
return idx;
}
private get currentCachedCompletion(): CachedInlineCompletion | undefined {
if (!this.cache.value) {
return undefined;
}
return this.filteredCompletions[this.fixAndGetIndexOfCurrentSelection()];
}
public async showNextInlineCompletion(): Promise<void> {
await this.ensureUpdateWithExplicitContext();
const completions = this.filteredCompletions || [];
if (completions.length > 0) {
const newIdx = (this.fixAndGetIndexOfCurrentSelection() + 1) % completions.length;
this.currentlySelectedCompletionId = completions[newIdx].semanticId;
} else {
this.currentlySelectedCompletionId = undefined;
}
this.onDidChangeEmitter.fire();
}
public async showPreviousInlineCompletion(): Promise<void> {
await this.ensureUpdateWithExplicitContext();
const completions = this.filteredCompletions || [];
if (completions.length > 0) {
const newIdx = (this.fixAndGetIndexOfCurrentSelection() + completions.length - 1) % completions.length;
this.currentlySelectedCompletionId = completions[newIdx].semanticId;
} else {
this.currentlySelectedCompletionId = undefined;
}
this.onDidChangeEmitter.fire();
}
public async ensureUpdateWithExplicitContext(): Promise<void> {
if (this.updateOperation.value) {
// Restart or wait for current update operation
if (this.updateOperation.value.triggerKind === InlineCompletionTriggerKind.Explicit) {
await this.updateOperation.value.promise;
} else {
await this.update(InlineCompletionTriggerKind.Explicit);
}
} else if (this.cache.value?.triggerKind !== InlineCompletionTriggerKind.Explicit) {
// Refresh cache
await this.update(InlineCompletionTriggerKind.Explicit);
}
}
public async hasMultipleInlineCompletions(): Promise<boolean> {
await this.ensureUpdateWithExplicitContext();
return (this.cache.value?.completions.length || 0) > 1;
}
//#endregion
public get ghostText(): GhostText | GhostTextReplacement | undefined {
const currentCompletion = this.currentCompletion;
if (!currentCompletion) {
return undefined;
}
const cursorPosition = this.editor.getPosition();
if (currentCompletion.range.getEndPosition().isBefore(cursorPosition)) {
return undefined;
}
const mode = this.editor.getOptions().get(EditorOption.inlineSuggest).mode;
const ghostText = inlineCompletionToGhostText(currentCompletion, this.editor.getModel(), mode, cursorPosition);
if (ghostText) {
if (ghostText.isEmpty()) {
return undefined;
}
return ghostText;
}
return new GhostTextReplacement(
currentCompletion.range.startLineNumber,
currentCompletion.range.startColumn,
currentCompletion.range.endColumn - currentCompletion.range.startColumn,
currentCompletion.insertText.split('\n'),
0
);
}
get currentCompletion(): TrackedInlineCompletion | undefined {
const completion = this.currentCachedCompletion;
if (!completion) {
return undefined;
}
return completion.toLiveInlineCompletion();
}
get isValid(): boolean {
return this.editor.getPosition().lineNumber === this.triggerPosition.lineNumber;
}
public scheduleAutomaticUpdate(): void {
// Since updateSoon debounces, starvation can happen.
// To prevent stale cache, we clear the current update operation.
this.updateOperation.clear();
this.updateSoon.schedule(this.debounce.get(this.editor.getModel()));
}
private async update(triggerKind: InlineCompletionTriggerKind): Promise<void> {
if (!this.shouldUpdate()) {
return;
}
const position = this.editor.getPosition();
const startTime = new Date();
const promise = createCancelablePromise(async token => {
let result;
try {
result = await provideInlineCompletions(this.registry, position,
this.editor.getModel(),
{ triggerKind, selectedSuggestionInfo: undefined },
token,
this.languageConfigurationService
);
const endTime = new Date();
this.debounce.update(this.editor.getModel(), endTime.getTime() - startTime.getTime());
} catch (e) {
onUnexpectedError(e);
return;
}
if (token.isCancellationRequested) {
return;
}
this.cache.setValue(
this.editor,
result,
triggerKind
);
this.updateFilteredInlineCompletions();
this.onDidChangeEmitter.fire();
});
const operation = new UpdateOperation(promise, triggerKind);
this.updateOperation.value = operation;
await promise;
if (this.updateOperation.value === operation) {
this.updateOperation.clear();
}
}
public takeOwnership(disposable: IDisposable): void {
this._register(disposable);
}
public commitCurrentCompletionNextWord(): void {
const ghostText = this.ghostText;
if (!ghostText) {
return;
}
const completion = this.currentCompletion;
if (!completion) {
return;
}
if (completion.snippetInfo || completion.filterText !== completion.insertText) {
// not in WYSIWYG mode, partial commit might change completion, thus it is not supported
this.commit(completion);
return;
}
if (ghostText.parts.length === 0) {
return;
}
const firstPart = ghostText.parts[0];
const position = new Position(ghostText.lineNumber, firstPart.column);
const line = firstPart.lines[0];
const langId = this.editor.getModel()!.getLanguageIdAtPosition(ghostText.lineNumber, 1);
const config = this.languageConfigurationService.getLanguageConfiguration(langId);
const r = new RegExp(config.wordDefinition, config.wordDefinition.flags.replace('g', ''));
const m = line.match(r);
let acceptUntilIndexExclusive = 0;
if (m && m.index !== undefined) {
if (m.index === 0) {
acceptUntilIndexExclusive = m[0].length;
} else {
acceptUntilIndexExclusive = m.index;
}
} else {
acceptUntilIndexExclusive = line.length;
}
const partialText = line.substring(0, acceptUntilIndexExclusive);
this.editor.pushUndoStop();
this.editor.executeEdits(
'inlineSuggestion.accept',
[
EditOperation.replace(Range.fromPositions(position), partialText),
]
);
this.editor.setPosition(position.delta(0, partialText.length));
}
public commitCurrentCompletion(): void {
const ghostText = this.ghostText;
if (!ghostText) {
// No ghost text was shown for this completion.
// Thus, we don't want to commit anything.
return;
}
const completion = this.currentCompletion;
if (completion) {
this.commit(completion);
}
}
public commit(completion: TrackedInlineCompletion): void {
// Mark the cache as stale, but don't dispose it yet,
// otherwise command args might get disposed.
const cache = this.cache.clearAndLeak();
this.editor.pushUndoStop();
if (completion.snippetInfo) {
this.editor.executeEdits(
'inlineSuggestion.accept',
[
EditOperation.replaceMove(completion.range, ''),
...completion.additionalTextEdits
]
);
this.editor.setPosition(completion.snippetInfo.range.getStartPosition());
SnippetController2.get(this.editor)?.insert(completion.snippetInfo.snippet, { undoStopBefore: false });
} else {
this.editor.executeEdits(
'inlineSuggestion.accept',
[
EditOperation.replaceMove(completion.range, completion.insertText),
...completion.additionalTextEdits
]
);
}
if (completion.command) {
this.commandService
.executeCommand(completion.command.id, ...(completion.command.arguments || []))
.finally(() => {
cache?.dispose();
})
.then(undefined, onUnexpectedExternalError);
} else {
cache?.dispose();
}
this.onDidChangeEmitter.fire();
}
public get commands(): Command[] {
const lists = new Set(this.cache.value?.completions.map(c => c.inlineCompletion.sourceInlineCompletions) || []);
return [...lists].flatMap(l => l.commands || []);
}
}
export class UpdateOperation implements IDisposable {
constructor(public readonly promise: CancelablePromise<void>, public readonly triggerKind: InlineCompletionTriggerKind) {
}
dispose() {
this.promise.cancel();
}
}
/**
* The cache keeps itself in sync with the editor.
* It also owns the completions result and disposes it when the cache is diposed.
*/
export class SynchronizedInlineCompletionsCache extends Disposable {
public readonly completions: readonly CachedInlineCompletion[];
private isDisposing = false;
constructor(
completionsSource: TrackedInlineCompletions,
private readonly editor: IActiveCodeEditor,
private readonly onChange: () => void,
public readonly triggerKind: InlineCompletionTriggerKind,
) {
super();
const decorationIds = editor.changeDecorations((changeAccessor) => {
return changeAccessor.deltaDecorations(
[],
completionsSource.items.map(i => ({
range: i.range,
options: {
description: 'inline-completion-tracking-range'
},
}))
);
});
this._register(toDisposable(() => {
this.isDisposing = true;
editor.removeDecorations(decorationIds);
}));
this.completions = completionsSource.items.map((c, idx) => new CachedInlineCompletion(c, decorationIds[idx]));
this._register(editor.onDidChangeModelContent(() => {
this.updateRanges();
}));
this._register(completionsSource);
}
public updateRanges(): void {
if (this.isDisposing) {
return;
}
let hasChanged = false;
const model = this.editor.getModel();
for (const c of this.completions) {
const newRange = model.getDecorationRange(c.decorationId);
if (!newRange) {
onUnexpectedError(new Error('Decoration has no range'));
continue;
}
if (!c.synchronizedRange.equalsRange(newRange)) {
hasChanged = true;
c.synchronizedRange = newRange;
}
}
if (hasChanged) {
this.onChange();
}
}
}
class CachedInlineCompletion {
public readonly semanticId: string = JSON.stringify({
text: this.inlineCompletion.insertText,
abbreviation: this.inlineCompletion.filterText,
startLine: this.inlineCompletion.range.startLineNumber,
startColumn: this.inlineCompletion.range.startColumn,
command: this.inlineCompletion.command
});
/**
* The range, synchronized with text model changes.
*/
public synchronizedRange: Range;
constructor(
public readonly inlineCompletion: TrackedInlineCompletion,
public readonly decorationId: string,
) {
this.synchronizedRange = inlineCompletion.range;
}
public toLiveInlineCompletion(): TrackedInlineCompletion | undefined {
return {
insertText: this.inlineCompletion.insertText,
range: this.synchronizedRange,
command: this.inlineCompletion.command,
sourceProvider: this.inlineCompletion.sourceProvider,
sourceInlineCompletions: this.inlineCompletion.sourceInlineCompletions,
sourceInlineCompletion: this.inlineCompletion.sourceInlineCompletion,
snippetInfo: this.inlineCompletion.snippetInfo,
filterText: this.inlineCompletion.filterText,
additionalTextEdits: this.inlineCompletion.additionalTextEdits,
};
}
}
export async function provideInlineCompletions(
registry: LanguageFeatureRegistry<InlineCompletionsProvider>,
position: Position,
model: ITextModel,
context: InlineCompletionContext,
token: CancellationToken = CancellationToken.None,
languageConfigurationService?: ILanguageConfigurationService
): Promise<TrackedInlineCompletions> {
const defaultReplaceRange = getDefaultRange(position, model);
const providers = registry.all(model);
const results = await Promise.all(
providers.map(
async provider => {
const completions = await Promise.resolve(provider.provideInlineCompletions(model, position, context, token)).catch(onUnexpectedExternalError);
return ({
completions,
provider,
dispose: () => {
if (completions) {
provider.freeInlineCompletions(completions);
}
}
});
}
)
);
const itemsByHash = new Map<string, TrackedInlineCompletion>();
for (const result of results) {
const completions = result.completions;
if (!completions) {
continue;
}
for (const item of completions.items) {
let range = item.range ? Range.lift(item.range) : defaultReplaceRange;
if (range.startLineNumber !== range.endLineNumber) {
// Ignore invalid ranges.
continue;
}
let insertText: string;
let snippetInfo: {
snippet: string;
/* Could be different than the main range */
range: Range;
}
| undefined;
if (typeof item.insertText === 'string') {
insertText = item.insertText;
if (languageConfigurationService && item.completeBracketPairs) {
insertText = closeBrackets(
insertText,
range.getStartPosition(),
model,
languageConfigurationService
);
// Modify range depending on if brackets are added or removed
const diff = insertText.length - item.insertText.length;
if (diff !== 0) {
range = new Range(range.startLineNumber, range.startColumn, range.endLineNumber, range.endColumn + diff);
}
}
snippetInfo = undefined;
} else if ('snippet' in item.insertText) {
const preBracketCompletionLength = item.insertText.snippet.length;
if (languageConfigurationService && item.completeBracketPairs) {
item.insertText.snippet = closeBrackets(
item.insertText.snippet,
range.getStartPosition(),
model,
languageConfigurationService
);
// Modify range depending on if brackets are added or removed
const diff = item.insertText.snippet.length - preBracketCompletionLength;
if (diff !== 0) {
range = new Range(range.startLineNumber, range.startColumn, range.endLineNumber, range.endColumn + diff);
}
}
const snippet = new SnippetParser().parse(item.insertText.snippet);
if (snippet.children.length === 1 && snippet.children[0] instanceof Text) {
insertText = snippet.children[0].value;
snippetInfo = undefined;
} else {
insertText = snippet.toString();
snippetInfo = {
snippet: item.insertText.snippet,
range: range
};
}
} else {
assertNever(item.insertText);
}
const trackedItem: TrackedInlineCompletion = ({
insertText,
snippetInfo,
range,
command: item.command,
sourceProvider: result.provider,
sourceInlineCompletions: completions,
sourceInlineCompletion: item,
filterText: item.filterText || insertText,
additionalTextEdits: item.additionalTextEdits || getReadonlyEmptyArray()
});
itemsByHash.set(JSON.stringify({ insertText, range: item.range }), trackedItem);
}
}
return {
items: [...itemsByHash.values()],
dispose: () => {
for (const result of results) {
result.dispose();
}
},
};
}
/**
* Contains no duplicated items and can be disposed.
*/
export interface TrackedInlineCompletions {
readonly items: readonly TrackedInlineCompletion[];
dispose(): void;
}
/**
* A normalized inline completion that tracks which inline completion it has been constructed from.
*/
export interface TrackedInlineCompletion extends NormalizedInlineCompletion {
sourceProvider: InlineCompletionsProvider;
/**
* A reference to the original inline completion this inline completion has been constructed from.
* Used for event data to ensure referential equality.
*/
sourceInlineCompletion: InlineCompletion;
/**
* A reference to the original inline completion list this inline completion has been constructed from.
* Used for event data to ensure referential equality.
*/
sourceInlineCompletions: InlineCompletions;
}
function getDefaultRange(position: Position, model: ITextModel): Range {
const word = model.getWordAtPosition(position);
const maxColumn = model.getLineMaxColumn(position.lineNumber);
// By default, always replace up until the end of the current line.
// This default might be subject to change!
return word
? new Range(position.lineNumber, word.startColumn, position.lineNumber, maxColumn)
: Range.fromPositions(position, position.with(undefined, maxColumn));
}
function closeBrackets(text: string, position: Position, model: ITextModel, languageConfigurationService: ILanguageConfigurationService): string {
const lineStart = model.getLineContent(position.lineNumber).substring(0, position.column - 1);
const newLine = lineStart + text;
const newTokens = model.tokenization.tokenizeLineWithEdit(position, newLine.length - (position.column - 1), text);
const slicedTokens = newTokens?.sliceAndInflate(position.column - 1, newLine.length, 0);
if (!slicedTokens) {
return text;
}
const newText = fixBracketsInLine(slicedTokens, languageConfigurationService);
return newText;
}