-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
interactiveEditorWidget.ts
1187 lines (976 loc) · 42.8 KB
/
interactiveEditorWidget.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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'vs/css!./interactiveEditor';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
import { IActiveCodeEditor, ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { Range } from 'vs/editor/common/core/range';
import { IEditorContribution, IEditorDecorationsCollection, ScrollType } from 'vs/editor/common/editorCommon';
import { localize } from 'vs/nls';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { ZoneWidget } from 'vs/editor/contrib/zoneWidget/browser/zoneWidget';
import { assertType } from 'vs/base/common/types';
import { IInteractiveEditorResponse, IInteractiveEditorService, CTX_INTERACTIVE_EDITOR_FOCUSED, CTX_INTERACTIVE_EDITOR_HAS_ACTIVE_REQUEST, CTX_INTERACTIVE_EDITOR_INNER_CURSOR_FIRST, CTX_INTERACTIVE_EDITOR_INNER_CURSOR_LAST, CTX_INTERACTIVE_EDITOR_EMPTY, CTX_INTERACTIVE_EDITOR_OUTER_CURSOR_POSITION, CTX_INTERACTIVE_EDITOR_VISIBLE, MENU_INTERACTIVE_EDITOR_WIDGET, IInteractiveEditorRequest, IInteractiveEditorSession, IInteractiveEditorSlashCommand, IInteractiveEditorSessionProvider, InteractiveEditorResponseFeedbackKind, IInteractiveEditorEditResponse, CTX_INTERACTIVE_EDITOR_LAST_EDIT_TYPE as CTX_INTERACTIVE_EDITOR_LAST_EDIT_KIND, MENU_INTERACTIVE_EDITOR_WIDGET_STATUS, CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK as CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK_KIND, CTX_INTERACTIVE_EDITOR_INLNE_DIFF, CTX_INTERACTIVE_EDITOR_HAS_RESPONSE } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { Iterable } from 'vs/base/common/iterator';
import { ICursorStateComputer, IModelDecorationOptions, IModelDeltaDecoration, ITextModel, IValidEditOperation } from 'vs/editor/common/model';
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
import { Dimension, addDisposableListener, getTotalHeight, getTotalWidth, h, reset } from 'vs/base/browser/dom';
import { Emitter, Event } from 'vs/base/common/event';
import { IEditorConstructionOptions } from 'vs/editor/browser/config/editorConfiguration';
import { CodeEditorWidget, ICodeEditorWidgetOptions } from 'vs/editor/browser/widget/codeEditorWidget';
import { EditorExtensionsRegistry } from 'vs/editor/browser/editorExtensions';
import { SnippetController2 } from 'vs/editor/contrib/snippet/browser/snippetController2';
import { IModelService } from 'vs/editor/common/services/model';
import { URI } from 'vs/base/common/uri';
import { EmbeddedCodeEditorWidget } from 'vs/editor/browser/widget/embeddedCodeEditorWidget';
import { GhostTextController } from 'vs/editor/contrib/inlineCompletions/browser/ghostTextController';
import { HiddenItemStrategy, MenuWorkbenchToolBar } from 'vs/platform/actions/browser/toolbar';
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
import { SuggestController } from 'vs/editor/contrib/suggest/browser/suggestController';
import { IPosition, Position } from 'vs/editor/common/core/position';
import { Selection } from 'vs/editor/common/core/selection';
import { raceCancellationError } from 'vs/base/common/async';
import { isCancellationError } from 'vs/base/common/errors';
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorker';
import { ILogService } from 'vs/platform/log/common/log';
import { StopWatch } from 'vs/base/common/stopwatch';
import { LRUCache } from 'vs/base/common/map';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
import { toErrorMessage } from 'vs/base/common/errorMessage';
import { IInteractiveSessionWidgetService } from 'vs/workbench/contrib/interactiveSession/browser/interactiveSessionWidget';
import { IViewsService } from 'vs/workbench/common/views';
import { IInteractiveSessionContributionService } from 'vs/workbench/contrib/interactiveSession/common/interactiveSessionContributionService';
import { InteractiveSessionViewPane } from 'vs/workbench/contrib/interactiveSession/browser/interactiveSessionSidebar';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
import { CompletionContext, CompletionItem, CompletionItemInsertTextRule, CompletionItemKind, CompletionItemProvider, CompletionList, ProviderResult } from 'vs/editor/common/languages';
import { LanguageSelector } from 'vs/editor/common/languageSelector';
import { DEFAULT_FONT_FAMILY } from 'vs/workbench/browser/style';
import { IInteractiveSessionService } from 'vs/workbench/contrib/interactiveSession/common/interactiveSessionService';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { createActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { splitLines } from 'vs/base/common/strings';
class InteractiveEditorWidget {
private static _modelPool: number = 1;
private static _noop = () => { };
private readonly _elements = h(
'div.interactive-editor@root',
[
h('div.body', [
h('div.content@content', [
h('div.input@input', [
h('div.editor-placeholder@placeholder'),
h('div.editor-container@editor'),
]),
h('div.toolbar@editorToolbar'),
]),
]),
h('div.progress@progress'),
h('div.status@status', [
h('div.actions.hidden@statusToolbar'),
h('div.label@statusLabel'),
]),
]
);
private readonly _store = new DisposableStore();
private readonly _historyStore = new DisposableStore();
readonly inputEditor: ICodeEditor;
private readonly _inputModel: ITextModel;
private readonly _ctxInputEmpty: IContextKey<boolean>;
private readonly _progressBar: ProgressBar;
private readonly _onDidChangeHeight = new Emitter<void>();
readonly onDidChangeHeight: Event<void> = Event.filter(this._onDidChangeHeight.event, _ => !this._isLayouting);
private _editorDim: Dimension | undefined;
private _isLayouting: boolean = false;
public acceptInput: () => void = InteractiveEditorWidget._noop;
private _cancelInput: () => void = InteractiveEditorWidget._noop;
constructor(
parentEditor: ICodeEditor | undefined,
@IModelService private readonly _modelService: IModelService,
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
) {
// editor logic
const editorOptions: IEditorConstructionOptions = {
ariaLabel: localize('aria-label', "Interactive Editor Input"),
fontFamily: DEFAULT_FONT_FAMILY,
fontSize: 13,
lineHeight: 20,
padding: { top: 3, bottom: 2 },
wordWrap: 'on',
overviewRulerLanes: 0,
glyphMargin: false,
lineNumbers: 'off',
folding: false,
selectOnLineNumbers: false,
hideCursorInOverviewRuler: true,
selectionHighlight: false,
scrollbar: {
useShadows: false,
vertical: 'hidden',
horizontal: 'auto',
alwaysConsumeMouseWheel: false
},
lineDecorationsWidth: 0,
overviewRulerBorder: false,
scrollBeyondLastLine: false,
renderLineHighlight: 'none',
fixedOverflowWidgets: true,
dragAndDrop: false,
revealHorizontalRightPadding: 5,
minimap: { enabled: false },
guides: { indentation: false },
rulers: [],
cursorWidth: 1,
wrappingStrategy: 'advanced',
wrappingIndent: 'none',
renderWhitespace: 'none',
dropIntoEditor: { enabled: true },
quickSuggestions: false,
suggest: {
showIcons: false,
showSnippets: false,
showStatusBar: false,
}
};
const codeEditorWidgetOptions: ICodeEditorWidgetOptions = {
isSimpleWidget: true,
contributions: EditorExtensionsRegistry.getSomeEditorContributions([
SnippetController2.ID,
GhostTextController.ID,
SuggestController.ID
])
};
this.inputEditor = parentEditor
? this._instantiationService.createInstance(EmbeddedCodeEditorWidget, this._elements.editor, editorOptions, codeEditorWidgetOptions, parentEditor)
: this._instantiationService.createInstance(CodeEditorWidget, this._elements.editor, editorOptions, codeEditorWidgetOptions);
this._store.add(this.inputEditor);
const uri = URI.from({ scheme: 'vscode', authority: 'interactive-editor', path: `/interactive-editor/model${InteractiveEditorWidget._modelPool++}.txt` });
this._inputModel = this._modelService.getModel(uri) ?? this._modelService.createModel('', null, uri);
this.inputEditor.setModel(this._inputModel);
// show/hide placeholder depending on text model being empty
// content height
const currentContentHeight = 0;
this._ctxInputEmpty = CTX_INTERACTIVE_EDITOR_EMPTY.bindTo(this._contextKeyService);
const togglePlaceholder = () => {
const hasText = this._inputModel.getValueLength() > 0;
this._elements.placeholder.classList.toggle('hidden', hasText);
this._ctxInputEmpty.set(!hasText);
const contentHeight = this.inputEditor.getContentHeight();
if (contentHeight !== currentContentHeight && this._editorDim) {
this._editorDim = this._editorDim.with(undefined, contentHeight);
this.inputEditor.layout(this._editorDim);
this._onDidChangeHeight.fire();
}
};
this._store.add(this._inputModel.onDidChangeContent(togglePlaceholder));
togglePlaceholder();
this._store.add(addDisposableListener(this._elements.placeholder, 'click', () => this.inputEditor.focus()));
const toolbar = this._instantiationService.createInstance(MenuWorkbenchToolBar, this._elements.editorToolbar, MENU_INTERACTIVE_EDITOR_WIDGET, {
telemetrySource: 'interactiveEditorWidget-toolbar',
toolbarOptions: { primaryGroup: 'main' }
});
this._store.add(toolbar);
this._progressBar = new ProgressBar(this._elements.progress);
this._store.add(this._progressBar);
const statusToolbar = this._instantiationService.createInstance(MenuWorkbenchToolBar, this._elements.statusToolbar, MENU_INTERACTIVE_EDITOR_WIDGET_STATUS, {
hiddenItemStrategy: HiddenItemStrategy.NoHide,
toolbarOptions: {
primaryGroup: () => true,
useSeparatorsInPrimaryActions: true
},
actionViewItemProvider: (action, options) => createActionViewItem(this._instantiationService, action, options)
});
this._historyStore.add(statusToolbar);
}
dispose(): void {
this._store.dispose();
this._historyStore.dispose();
this._ctxInputEmpty.reset();
}
get domNode(): HTMLElement {
return this._elements.root;
}
layout(dim: Dimension) {
this._isLayouting = true;
try {
const innerEditorWidth = dim.width - (getTotalWidth(this._elements.editorToolbar) + 8 /* L/R-padding */);
const newDim = new Dimension(innerEditorWidth, this.inputEditor.getContentHeight());
if (!this._editorDim || !Dimension.equals(this._editorDim, newDim)) {
this._editorDim = newDim;
this.inputEditor.layout(this._editorDim);
this._elements.placeholder.style.width = `${innerEditorWidth /* input-padding*/}px`;
}
} finally {
this._isLayouting = false;
}
}
getHeight(): number {
const base = getTotalHeight(this._elements.progress) + getTotalHeight(this._elements.status);
const editorHeight = this.inputEditor.getContentHeight() + 12 /* padding and border */;
return base + editorHeight + 12 /* padding */ + 8 /*shadow*/;
}
updateProgress(show: boolean) {
if (show) {
this._progressBar.infinite();
} else {
this._progressBar.stop();
}
}
getInput(placeholder: string, value: string, token: CancellationToken): Promise<string | undefined> {
this._elements.placeholder.innerText = placeholder;
this._elements.placeholder.style.fontSize = `${this.inputEditor.getOption(EditorOption.fontSize)}px`;
this._elements.placeholder.style.lineHeight = `${this.inputEditor.getOption(EditorOption.lineHeight)}px`;
this._inputModel.setValue(value);
this.inputEditor.setSelection(this._inputModel.getFullModelRange());
this.inputEditor.updateOptions({ ariaLabel: localize('aria-label.N', "Interactive Editor Input: {0}", placeholder) });
const disposeOnDone = new DisposableStore();
disposeOnDone.add(this.inputEditor.onDidLayoutChange(() => this._onDidChangeHeight.fire()));
disposeOnDone.add(this.inputEditor.onDidContentSizeChange(() => this._onDidChangeHeight.fire()));
const ctxInnerCursorFirst = CTX_INTERACTIVE_EDITOR_INNER_CURSOR_FIRST.bindTo(this._contextKeyService);
const ctxInnerCursorLast = CTX_INTERACTIVE_EDITOR_INNER_CURSOR_LAST.bindTo(this._contextKeyService);
const ctxInputEditorFocused = CTX_INTERACTIVE_EDITOR_FOCUSED.bindTo(this._contextKeyService);
return new Promise<string | undefined>(resolve => {
this._cancelInput = () => {
this.acceptInput = InteractiveEditorWidget._noop;
this._cancelInput = InteractiveEditorWidget._noop;
resolve(undefined);
return true;
};
this.acceptInput = () => {
const newValue = this.inputEditor.getModel()!.getValue();
if (newValue.trim().length === 0) {
// empty or whitespace only
this._cancelInput();
return;
}
this.acceptInput = InteractiveEditorWidget._noop;
this._cancelInput = InteractiveEditorWidget._noop;
resolve(newValue);
};
disposeOnDone.add(token.onCancellationRequested(() => this._cancelInput()));
// CONTEXT KEYS
// (1) inner cursor position (last/first line selected)
const updateInnerCursorFirstLast = () => {
if (!this.inputEditor.hasModel()) {
return;
}
const { lineNumber } = this.inputEditor.getPosition();
ctxInnerCursorFirst.set(lineNumber === 1);
ctxInnerCursorLast.set(lineNumber === this.inputEditor.getModel().getLineCount());
};
disposeOnDone.add(this.inputEditor.onDidChangeCursorPosition(updateInnerCursorFirstLast));
updateInnerCursorFirstLast();
// (2) input editor focused or not
const updateFocused = () => {
const hasFocus = this.inputEditor.hasWidgetFocus();
ctxInputEditorFocused.set(hasFocus);
this._elements.content.classList.toggle('synthetic-focus', hasFocus);
};
disposeOnDone.add(this.inputEditor.onDidFocusEditorWidget(updateFocused));
disposeOnDone.add(this.inputEditor.onDidBlurEditorWidget(updateFocused));
updateFocused();
this.focus();
}).finally(() => {
disposeOnDone.dispose();
ctxInnerCursorFirst.reset();
ctxInnerCursorLast.reset();
ctxInputEditorFocused.reset();
});
}
populateInputField(value: string) {
this._inputModel.setValue(value.trim());
this.inputEditor.setSelection(this._inputModel.getFullModelRange());
}
updateToolbar(show: boolean) {
this._elements.statusToolbar.classList.toggle('hidden', !show);
this._onDidChangeHeight.fire();
}
updateMessage(message: string, classes?: string[], resetAfter?: number) {
const isTempMessage = typeof resetAfter === 'number';
if (isTempMessage && !this._elements.statusLabel.dataset['state']) {
const messageNow = this._elements.statusLabel.innerText;
const classes = Array.from(this._elements.statusLabel.classList.values());
setTimeout(() => {
if (messageNow) {
this.updateMessage(messageNow, classes);
} else {
reset(this._elements.statusLabel);
}
}, resetAfter);
}
this._elements.status.classList.toggle('hidden', false);
reset(this._elements.statusLabel, message);
this._elements.statusLabel.className = `label ${(classes ?? []).join(' ')}`;
if (isTempMessage) {
this._elements.statusLabel.dataset['state'] = 'temp';
} else {
delete this._elements.statusLabel.dataset['state'];
}
this._onDidChangeHeight.fire();
}
reset() {
this._ctxInputEmpty.reset();
reset(this._elements.statusLabel);
this._elements.statusToolbar.classList.add('hidden');
}
focus() {
this.inputEditor.focus();
}
}
export class InteractiveEditorZoneWidget extends ZoneWidget {
readonly widget: InteractiveEditorWidget;
private readonly _ctxVisible: IContextKey<boolean>;
private readonly _ctxCursorPosition: IContextKey<'above' | 'below' | ''>;
private _dimension?: Dimension;
constructor(
editor: ICodeEditor,
@IInstantiationService private readonly _instaService: IInstantiationService,
@IContextKeyService contextKeyService: IContextKeyService,
) {
super(editor, { showFrame: false, showArrow: false, isAccessible: true, className: 'interactive-editor-widget', keepEditorSelection: true });
this._ctxVisible = CTX_INTERACTIVE_EDITOR_VISIBLE.bindTo(contextKeyService);
this._ctxCursorPosition = CTX_INTERACTIVE_EDITOR_OUTER_CURSOR_POSITION.bindTo(contextKeyService);
this._disposables.add(toDisposable(() => {
this._ctxVisible.reset();
this._ctxCursorPosition.reset();
}));
this.widget = this._instaService.createInstance(InteractiveEditorWidget, this.editor);
this._disposables.add(this.widget.onDidChangeHeight(() => this._relayout()));
this._disposables.add(this.widget);
this.create();
// todo@jrieken listen ONLY when showing
const updateCursorIsAboveContextKey = () => {
if (!this.position || !this.editor.hasModel()) {
this._ctxCursorPosition.reset();
} else if (this.position.lineNumber === this.editor.getPosition().lineNumber) {
this._ctxCursorPosition.set('above');
} else if (this.position.lineNumber + 1 === this.editor.getPosition().lineNumber) {
this._ctxCursorPosition.set('below');
} else {
this._ctxCursorPosition.reset();
}
};
this._disposables.add(this.editor.onDidChangeCursorPosition(e => updateCursorIsAboveContextKey()));
this._disposables.add(this.editor.onDidFocusEditorText(e => updateCursorIsAboveContextKey()));
updateCursorIsAboveContextKey();
}
protected override _fillContainer(container: HTMLElement): void {
container.appendChild(this.widget.domNode);
}
protected override _onWidth(widthInPixel: number): void {
if (this._dimension) {
this._doLayout(this._dimension.height, widthInPixel);
}
}
protected override _doLayout(heightInPixel: number, widthInPixel: number): void {
const info = this.editor.getLayoutInfo();
const spaceLeft = info.lineNumbersWidth + info.glyphMarginWidth + info.decorationsWidth;
const spaceRight = info.minimap.minimapWidth + info.verticalScrollbarWidth;
const width = Math.min(640, info.contentWidth - (info.glyphMarginWidth + info.decorationsWidth));
this._dimension = new Dimension(width, heightInPixel);
this.widget.domNode.style.marginLeft = `${spaceLeft}px`;
this.widget.domNode.style.marginRight = `${spaceRight}px`;
this.widget.domNode.style.width = `${width}px`;
this.widget.layout(this._dimension);
}
private _computeHeightInLines(): number {
const lineHeight = this.editor.getOption(EditorOption.lineHeight);
return this.widget.getHeight() / lineHeight;
}
protected override _relayout() {
super._relayout(this._computeHeightInLines());
}
async getInput(where: IPosition, placeholder: string, value: string, token: CancellationToken): Promise<string | undefined> {
assertType(this.editor.hasModel());
super.show(where, this._computeHeightInLines());
this._ctxVisible.set(true);
const task = this.widget.getInput(placeholder, value, token);
const result = await task;
return result;
}
updatePosition(where: IPosition) {
// todo@jrieken
// UGYLY: we need to restore focus because showing the zone removes and adds it and that
// means we loose focus for a bit
const hasFocusNow = this.widget.inputEditor.hasWidgetFocus();
super.show(where, this._computeHeightInLines());
if (hasFocusNow) {
this.widget.inputEditor.focus();
}
}
protected override revealRange(_range: Range, _isLastLine: boolean) {
// disabled
}
override hide(): void {
this._ctxVisible.reset();
this._ctxCursorPosition.reset();
this.widget.reset();
super.hide();
}
}
type Exchange = { req: IInteractiveEditorRequest; res: IInteractiveEditorResponse };
export type Recording = { when: Date; session: IInteractiveEditorSession; value: string; exchanges: Exchange[] };
class SessionRecorder {
private readonly _data = new LRUCache<IInteractiveEditorSession, Recording>(3);
add(session: IInteractiveEditorSession, model: ITextModel) {
this._data.set(session, { when: new Date(), session, value: model.getValue(), exchanges: [] });
}
addExchange(session: IInteractiveEditorSession, req: IInteractiveEditorRequest, res: IInteractiveEditorResponse) {
this._data.get(session)?.exchanges.push({ req, res });
}
getAll(): Recording[] {
return [...this._data.values()];
}
}
type TelemetryData = {
extension: string;
rounds: string;
undos: string;
edits: boolean;
terminalEdits: boolean;
startTime: string;
endTime: string;
};
type TelemetryDataClassification = {
owner: 'jrieken';
comment: 'Data about an interaction editor session';
extension: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'The extension providing the data' };
rounds: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'Number of request that were made' };
undos: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; isMeasurement: true; comment: 'Requests that have been undone' };
edits: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; isMeasurement: true; comment: 'Did edits happen while the session was active' };
terminalEdits: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'Did edits terminal the session' };
startTime: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'When the session started' };
endTime: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'When the session ended' };
};
class InlineDiffDecorations {
private readonly _collection: IEditorDecorationsCollection;
private _data: { tracking: IModelDeltaDecoration; decorating: IModelDecorationOptions }[] = [];
private _visible: boolean = false;
constructor(editor: ICodeEditor, visible: boolean = false) {
this._collection = editor.createDecorationsCollection();
this._visible = visible;
}
get visible() {
return this._visible;
}
set visible(value: boolean) {
this._visible = value;
this.update();
}
clear() {
this._collection.clear();
this._data.length = 0;
}
collectEditOperation(op: IValidEditOperation) {
this._data.push(InlineDiffDecorations._asDecorationData(op));
}
update() {
this._collection.set(this._data.map(d => {
const res = { ...d.tracking };
if (this._visible) {
res.options = { ...res.options, ...d.decorating };
}
return res;
}));
}
private static _asDecorationData(edit: IValidEditOperation): { tracking: IModelDeltaDecoration; decorating: IModelDecorationOptions } {
let content = edit.text;
if (content.length > 12) {
content = content.substring(0, 12) + '…';
}
const tracking: IModelDeltaDecoration = {
range: edit.range,
options: {
description: 'interactive-editor-inline-diff',
}
};
const decorating: IModelDecorationOptions = {
description: 'interactive-editor-inline-diff',
className: !edit.range.isEmpty() ? 'interactive-editor-lines-inserted-range' : undefined,
showIfCollapsed: true,
before: {
content,
inlineClassName: 'interactive-editor-lines-deleted-range-inline',
attachedData: edit,
}
};
return { tracking, decorating };
}
}
class LastEditorState {
constructor(
readonly model: ITextModel,
readonly modelVersionId: number,
readonly provider: IInteractiveEditorSessionProvider,
readonly session: IInteractiveEditorSession,
readonly response: IInteractiveEditorEditResponse,
) { }
}
export class InteractiveEditorController implements IEditorContribution {
static ID = 'interactiveEditor';
static get(editor: ICodeEditor) {
return editor.getContribution<InteractiveEditorController>(InteractiveEditorController.ID);
}
private static _inlineDiffStorageKey: string = 'interactiveEditor.storage.inlineDiff';
private static _decoBlock = ModelDecorationOptions.register({
description: 'interactive-editor',
showIfCollapsed: false,
isWholeLine: true,
className: 'interactive-editor-block-selection',
});
private static _decoWholeRange = ModelDecorationOptions.register({
description: 'interactive-editor-marker'
});
private static _promptHistory: string[] = [];
private _historyOffset: number = -1;
private readonly _store = new DisposableStore();
private readonly _recorder = new SessionRecorder();
private readonly _zone: InteractiveEditorZoneWidget;
private readonly _ctxHasActiveRequest: IContextKey<boolean>;
private readonly _ctxHasResponse: IContextKey<boolean>;
private readonly _ctxInlineDiff: IContextKey<boolean>;
private readonly _ctxLastEditKind: IContextKey<'' | 'simple'>;
private readonly _ctxLastFeedbackKind: IContextKey<'helpful' | 'unhelpful' | ''>;
private _lastEditState?: LastEditorState;
private _lastInlineDecorations?: InlineDiffDecorations;
private _inlineDiffEnabled: boolean = false;
private _ctsSession: CancellationTokenSource = new CancellationTokenSource();
private _ctsRequest?: CancellationTokenSource;
constructor(
private readonly _editor: ICodeEditor,
@IInstantiationService private readonly _instaService: IInstantiationService,
@IInteractiveEditorService private readonly _interactiveEditorService: IInteractiveEditorService,
@IBulkEditService private readonly _bulkEditService: IBulkEditService,
@IEditorWorkerService private readonly _editorWorkerService: IEditorWorkerService,
@ILogService private readonly _logService: ILogService,
@ITelemetryService private readonly _telemetryService: ITelemetryService,
@IStorageService private readonly _storageService: IStorageService,
@IContextKeyService contextKeyService: IContextKeyService,
) {
this._zone = this._store.add(_instaService.createInstance(InteractiveEditorZoneWidget, this._editor));
this._ctxHasActiveRequest = CTX_INTERACTIVE_EDITOR_HAS_ACTIVE_REQUEST.bindTo(contextKeyService);
this._ctxHasResponse = CTX_INTERACTIVE_EDITOR_HAS_RESPONSE.bindTo(contextKeyService);
this._ctxInlineDiff = CTX_INTERACTIVE_EDITOR_INLNE_DIFF.bindTo(contextKeyService);
this._ctxLastEditKind = CTX_INTERACTIVE_EDITOR_LAST_EDIT_KIND.bindTo(contextKeyService);
this._ctxLastFeedbackKind = CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK_KIND.bindTo(contextKeyService);
}
dispose(): void {
this._store.dispose();
this._ctsSession.dispose(true);
this._ctsSession.dispose();
}
getId(): string {
return InteractiveEditorController.ID;
}
async run(initialRange?: Range): Promise<void> {
this._ctsSession.dispose(true);
if (!this._editor.hasModel()) {
return;
}
const provider = Iterable.first(this._interactiveEditorService.getAllProvider());
if (!provider) {
this._logService.trace('[IE] NO provider found');
return;
}
const thisSession = this._ctsSession = new CancellationTokenSource();
const textModel = this._editor.getModel();
const selection = this._editor.getSelection();
const session = await provider.prepareInteractiveEditorSession(textModel, selection, this._ctsSession.token);
if (!session) {
this._logService.trace('[IE] NO session', provider.debugName);
return;
}
this._recorder.add(session, textModel);
this._logService.trace('[IE] NEW session', provider.debugName);
const data: TelemetryData = {
extension: provider.debugName,
startTime: new Date().toISOString(),
endTime: new Date().toISOString(),
edits: false,
terminalEdits: false,
rounds: '',
undos: ''
};
this._inlineDiffEnabled = this._storageService.getBoolean(InteractiveEditorController._inlineDiffStorageKey, StorageScope.PROFILE, false);
const inlineDiffDecorations = new InlineDiffDecorations(this._editor, this._inlineDiffEnabled);
this._lastInlineDecorations = inlineDiffDecorations;
this._ctxInlineDiff.set(this._inlineDiffEnabled);
const blockDecoration = this._editor.createDecorationsCollection();
const wholeRangeDecoration = this._editor.createDecorationsCollection();
if (!initialRange) {
initialRange = session.wholeRange ? Range.lift(session.wholeRange) : selection;
}
if (initialRange.isEmpty()) {
initialRange = new Range(
initialRange.startLineNumber, 1,
initialRange.startLineNumber, textModel.getLineMaxColumn(initialRange.startLineNumber)
);
}
wholeRangeDecoration.set([{
range: initialRange,
options: InteractiveEditorController._decoWholeRange
}]);
let placeholder = session.placeholder ?? '';
let value = '';
const store = new DisposableStore();
if (session.slashCommands) {
store.add(this._instaService.invokeFunction(installSlashCommandSupport, this._zone.widget.inputEditor as IActiveCodeEditor, session.slashCommands));
}
this._zone.widget.updateMessage(session.message ?? localize('welcome.1', "AI-generated code may be incorrect."));
// CANCEL when input changes
this._editor.onDidChangeModel(this._ctsSession.cancel, this._ctsSession, store);
// REposition the zone widget whenever the block decoration changes
let lastPost: Position | undefined;
wholeRangeDecoration.onDidChange(e => {
const range = wholeRangeDecoration.getRange(0);
if (range && (!lastPost || !lastPost.equals(range.getEndPosition()))) {
lastPost = range.getEndPosition();
this._zone.updatePosition(lastPost);
}
}, undefined, store);
let ignoreModelChanges = false;
this._editor.onDidChangeModelContent(e => {
if (!ignoreModelChanges) {
// remove inline diff when the model changes
inlineDiffDecorations.clear();
// note when "other" edits happen
data.edits = true;
// CANCEL if the document has changed outside the current range
const wholeRange = wholeRangeDecoration.getRange(0);
if (!wholeRange) {
this._ctsSession.cancel();
this._logService.trace('[IE] ABORT wholeRange seems gone/collapsed');
return;
}
for (const change of e.changes) {
if (!Range.areIntersectingOrTouching(wholeRange, change.range)) {
this._ctsSession.cancel();
this._logService.trace('[IE] CANCEL because of model change OUTSIDE range');
data.terminalEdits = true;
break;
}
}
}
}, undefined, store);
let round = 0;
const roundStore = new DisposableStore();
store.add(roundStore);
do {
round += 1;
const wholeRange = wholeRangeDecoration.getRange(0);
if (!wholeRange) {
// nuked whole file contents?
this._logService.trace('[IE] ABORT wholeRange seems gone/collapsed');
break;
}
// visuals: add block decoration
blockDecoration.set([{
range: wholeRange,
options: InteractiveEditorController._decoBlock
}]);
this._ctsRequest?.dispose(true);
this._ctsRequest = new CancellationTokenSource(this._ctsSession.token);
this._historyOffset = -1;
const inputPromise = this._zone.getInput(wholeRange.getEndPosition(), placeholder, value, this._ctsRequest.token);
this._ctxLastFeedbackKind.reset();
// reveal the line after the whole range to ensure that the input box is visible
this._editor.revealPosition({ lineNumber: wholeRange.endLineNumber + 1, column: 1 }, ScrollType.Smooth);
const input = await inputPromise;
roundStore.clear();
if (!input) {
continue;
}
if (!InteractiveEditorController._promptHistory.includes(input)) {
InteractiveEditorController._promptHistory.unshift(input);
}
const refer = session.slashCommands?.some(value => value.refer && input.startsWith(`/${value.command}`));
if (refer) {
this._logService.info('[IE] seeing refer command, continuing outside editor', provider.debugName);
this._editor.setSelection(wholeRange);
this._instaService.invokeFunction(sendRequest, input);
continue;
}
const sw = StopWatch.create();
const request: IInteractiveEditorRequest = {
prompt: input,
selection: this._editor.getSelection(),
wholeRange
};
const task = provider.provideResponse(session, request, this._ctsRequest.token);
this._logService.trace('[IE] request started', provider.debugName, session, request);
let reply: IInteractiveEditorResponse | null | undefined;
try {
this._zone.widget.updateProgress(true);
this._ctxHasActiveRequest.set(true);
reply = await raceCancellationError(Promise.resolve(task), this._ctsRequest.token);
} catch (e) {
if (!isCancellationError(e)) {
this._logService.error('[IE] ERROR during request', provider.debugName);
this._logService.error(e);
this._zone.widget.updateMessage(toErrorMessage(e), ['error']);
// statusWidget
continue;
}
} finally {
this._ctxHasActiveRequest.set(false);
this._ctxHasResponse.set(!!reply);
this._zone.widget.updateProgress(false);
this._logService.trace('[IE] request took', sw.elapsed(), provider.debugName);
}
if (this._ctsRequest.token.isCancellationRequested) {
this._logService.trace('[IE] request CANCELED', provider.debugName);
value = input;
continue;
}
if (!reply) {
this._logService.trace('[IE] NO reply or edits', provider.debugName);
value = input;
this._zone.widget.updateMessage(localize('empty', "No results, please refine your input and try again."), ['warn']);
continue;
}
if (reply.type === 'bulkEdit') {
this._logService.info('[IE] performaing a BULK EDIT, exiting interactive editor', provider.debugName);
this._bulkEditService.apply(reply.edits, { editor: this._editor, label: localize('ie', "{0}", input), showPreview: true });
// todo@jrieken preview bulk edit?
// todo@jrieken keep interactive editor?
break;
}
if (reply.type === 'message') {
this._logService.info('[IE] received a MESSAGE, continuing outside editor', provider.debugName);
this._instaService.invokeFunction(showMessageResponse, request.prompt, reply.message.value);
continue;
}
// make edits more minimal
this._ctxLastEditKind.set(reply.edits.length === 1 ? 'simple' : '');
const moreMinimalEdits = (await this._editorWorkerService.computeHumanReadableDiff(textModel.uri, reply.edits));
this._logService.trace('[IE] edits from PROVIDER and after making them MORE MINIMAL', provider.debugName, reply.edits, moreMinimalEdits);
this._recorder.addExchange(session, request, reply);
// inline diff
inlineDiffDecorations.clear();
// use whole range from reply
if (reply.wholeRange) {
wholeRangeDecoration.set([{
range: reply.wholeRange,
options: InteractiveEditorController._decoWholeRange
}]);
}
this._lastEditState = new LastEditorState(textModel, textModel.getAlternativeVersionId(), provider, session, reply);
try {
ignoreModelChanges = true;
const cursorStateComputerAndInlineDiffCollection: ICursorStateComputer = (undoEdits) => {
let last: Position | null = null;
for (const edit of undoEdits) {
last = !last || last.isBefore(edit.range.getEndPosition()) ? edit.range.getEndPosition() : last;
inlineDiffDecorations.collectEditOperation(edit);
}
return last && [Selection.fromPositions(last)];
};
this._editor.pushUndoStop();
this._editor.executeEdits(
'interactive-editor',
(moreMinimalEdits ?? reply.edits).map(edit => EditOperation.replace(Range.lift(edit.range), edit.text)),
cursorStateComputerAndInlineDiffCollection
);
this._editor.pushUndoStop();
} finally {
ignoreModelChanges = false;
}
inlineDiffDecorations.update();
// line count
const lineSet = new Set<number>();
let addRemoveCount = 0;
for (const edit of moreMinimalEdits ?? reply.edits) {
const len2 = splitLines(edit.text).length - 1;
if (Range.isEmpty(edit.range) && len2 > 0) {
// insert lines
addRemoveCount += len2;
} else if (Range.isEmpty(edit.range) && edit.text.length === 0) {
// delete
addRemoveCount += edit.range.endLineNumber - edit.range.startLineNumber + 1;
} else {
// edit
for (let line = edit.range.startLineNumber; line <= edit.range.endLineNumber; line++) {
lineSet.add(line);
}
}
}
const linesChanged = addRemoveCount + lineSet.size;
this._zone.widget.updateToolbar(true);
this._zone.widget.updateMessage(linesChanged === 1
? localize('lines.1', "Generated reply and changed 1 line.")
: localize('lines.N', "Generated reply and changed {0} lines.", linesChanged)
);
placeholder = reply.placeholder ?? session.placeholder ?? '';
value = '';
data.rounds += round + '|';
} while (!thisSession.token.isCancellationRequested);
this._inlineDiffEnabled = inlineDiffDecorations.visible;
this._storageService.store(InteractiveEditorController._inlineDiffStorageKey, this._inlineDiffEnabled, StorageScope.PROFILE, StorageTarget.USER);
// done, cleanup
wholeRangeDecoration.clear();
blockDecoration.clear();
inlineDiffDecorations.clear();
store.dispose();
session.dispose?.();
this._ctxLastEditKind.reset();
this._ctxHasResponse.reset();