Skip to content

Commit 23a98d3

Browse files
authored
Merge pull request microsoft#181973 from microsoft/rebornix/preliminary-toucan
Experiment: accepting suggestion without dismissing widget.
2 parents d1c1c8c + cb6609c commit 23a98d3

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

src/vs/workbench/contrib/interactiveEditor/browser/interactiveEditorController.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,12 @@ export class InteractiveEditorController implements IEditorContribution {
503503
}
504504
}
505505

506+
createSnapshot(): void {
507+
if (this._activeSession && !this._activeSession.textModel0.equalsTextBuffer(this._activeSession.textModelN.getTextBuffer())) {
508+
this._activeSession.createSnapshot();
509+
}
510+
}
511+
506512
async applyChanges(): Promise<EditResponse | void> {
507513
if (this._activeSession?.lastExchange?.response instanceof EditResponse && this._strategy) {
508514
const strategy = this._strategy;

src/vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { URI } from 'vs/base/common/uri';
88
import { Event } from 'vs/base/common/event';
99
import { ResourceEdit, ResourceFileEdit, ResourceTextEdit } from 'vs/editor/browser/services/bulkEditService';
1010
import { TextEdit } from 'vs/editor/common/languages';
11-
import { ITextModel } from 'vs/editor/common/model';
11+
import { ITextModel, ITextSnapshot } from 'vs/editor/common/model';
1212
import { EditMode, IInteractiveEditorSessionProvider, IInteractiveEditorSession, IInteractiveEditorBulkEditResponse, IInteractiveEditorEditResponse, IInteractiveEditorMessageResponse, IInteractiveEditorResponse, IInteractiveEditorService } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
1313
import { IRange, Range } from 'vs/editor/common/core/range';
1414
import { IActiveCodeEditor, ICodeEditor } from 'vs/editor/browser/editorBrowser';
@@ -56,6 +56,7 @@ type TelemetryDataClassification = {
5656
export class Session {
5757

5858
private _lastInput: string | undefined;
59+
private _lastSnapshot: ITextSnapshot | undefined;
5960
private readonly _exchange: SessionExchange[] = [];
6061
private readonly _startTime = new Date();
6162
private readonly _teldata: Partial<TelemetryData>;
@@ -87,11 +88,19 @@ export class Session {
8788
return this._lastInput;
8889
}
8990

91+
get lastSnapshot(): ITextSnapshot | undefined {
92+
return this._lastSnapshot;
93+
}
94+
9095
get wholeRange(): Range {
9196
return this.textModelN.getDecorationRange(this._wholeRangeMarkerId)!;
9297
// return new Range(1, 1, 1, 1);
9398
}
9499

100+
createSnapshot(): void {
101+
this._lastSnapshot = this.textModelN.createSnapshot();
102+
}
103+
95104
addExchange(exchange: SessionExchange): void {
96105
const newLen = this._exchange.push(exchange);
97106
this._teldata.rounds += `${newLen}|`;

src/vs/workbench/contrib/interactiveEditor/browser/interactiveEditorStrategies.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storag
2323
import { InteractiveEditorFileCreatePreviewWidget, InteractiveEditorLivePreviewWidget } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorLivePreviewWidget';
2424
import { EditResponse, Session } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession';
2525
import { InteractiveEditorWidget } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorWidget';
26+
import { getValueFromSnapshot } from 'vs/workbench/contrib/interactiveEditor/browser/utils';
2627
import { CTX_INTERACTIVE_EDITOR_INLNE_DIFF, CTX_INTERACTIVE_EDITOR_DOCUMENT_CHANGED } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
2728

2829
export abstract class EditModeStrategy {
@@ -206,7 +207,7 @@ export class LiveStrategy extends EditModeStrategy {
206207
@IContextKeyService contextKeyService: IContextKeyService,
207208
@IStorageService protected _storageService: IStorageService,
208209
@IBulkEditService protected readonly _bulkEditService: IBulkEditService,
209-
@IEditorWorkerService protected readonly _editorWorkerService: IEditorWorkerService
210+
@IEditorWorkerService protected readonly _editorWorkerService: IEditorWorkerService,
210211
) {
211212
super();
212213
this._inlineDiffDecorations = new InlineDiffDecorations(this._editor, this._inlineDiffEnabled);
@@ -257,11 +258,16 @@ export class LiveStrategy extends EditModeStrategy {
257258
}
258259

259260
async cancel() {
260-
const { textModelN: modelN, textModel0: model0 } = this._session;
261-
if (modelN.isDisposed() || model0.isDisposed()) {
261+
const { textModelN: modelN, textModel0: model0, lastSnapshot } = this._session;
262+
if (modelN.isDisposed() || (model0.isDisposed() && !lastSnapshot)) {
262263
return;
263264
}
264-
const edits = await this._editorWorkerService.computeMoreMinimalEdits(modelN.uri, [{ range: modelN.getFullModelRange(), text: model0.getValue() }]);
265+
266+
const newText = lastSnapshot
267+
? getValueFromSnapshot(lastSnapshot)
268+
: model0.getValue();
269+
270+
const edits = await this._editorWorkerService.computeMoreMinimalEdits(modelN.uri, [{ range: modelN.getFullModelRange(), text: newText }]);
265271
if (edits) {
266272
const operations = edits.map(e => EditOperation.replace(Range.lift(e.range), e.text));
267273
modelN.pushEditOperations(null, operations, () => null);

src/vs/workbench/contrib/interactiveEditor/browser/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { LineRange } from 'vs/editor/common/core/lineRange';
77
import { Range } from 'vs/editor/common/core/range';
8-
import { ITextModel } from 'vs/editor/common/model';
8+
import { ITextModel, ITextSnapshot } from 'vs/editor/common/model';
99

1010
export function invertLineRange(range: LineRange, model: ITextModel): LineRange[] {
1111
if (range.isEmpty) {
@@ -20,3 +20,15 @@ export function invertLineRange(range: LineRange, model: ITextModel): LineRange[
2020
export function lineRangeAsRange(r: LineRange): Range {
2121
return new Range(r.startLineNumber, 1, r.endLineNumberExclusive - 1, 1);
2222
}
23+
24+
export function getValueFromSnapshot(snapshot: ITextSnapshot): string {
25+
let result = '';
26+
while (true) {
27+
const chunk = snapshot.read();
28+
if (chunk === null) {
29+
break;
30+
}
31+
result += chunk;
32+
}
33+
return result;
34+
}

src/vs/workbench/contrib/notebook/browser/controller/executeActions.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Iterable } from 'vs/base/common/iterator';
77
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
88
import { URI, UriComponents } from 'vs/base/common/uri';
99
import { CancellationTokenSource } from 'vs/base/common/cancellation';
10+
import { isEqual } from 'vs/base/common/resources';
1011
import { ITextModel } from 'vs/editor/common/model';
1112
import { Range } from 'vs/editor/common/core/range';
1213
import { ISelection } from 'vs/editor/common/core/selection';
@@ -31,6 +32,8 @@ import { Schemas } from 'vs/base/common/network';
3132
import { IDebugService } from 'vs/workbench/contrib/debug/common/debug';
3233
import { CTX_INTERACTIVE_EDITOR_FOCUSED, IInteractiveEditorRequest, IInteractiveEditorService } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
3334
import { ICellRange } from 'vs/workbench/contrib/notebook/common/notebookRange';
35+
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
36+
import { InteractiveEditorController } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorController';
3437
import { EditOperation } from 'vs/editor/common/core/editOperation';
3538

3639
const EXECUTE_NOTEBOOK_COMMAND_ID = 'notebook.execute';
@@ -208,7 +211,26 @@ registerAction2(class ExecuteCell extends NotebookMultiCellAction {
208211
await context.notebookEditor.focusNotebookCell(context.cell, 'container', { skipReveal: true });
209212
}
210213

211-
return runCell(editorGroupsService, context);
214+
let foundEditor: ICodeEditor | undefined = undefined;
215+
for (const [, codeEditor] of context.notebookEditor.codeEditors) {
216+
if (isEqual(codeEditor.getModel()?.uri, (context.cell ?? context.selectedCells?.[0])?.uri)) {
217+
foundEditor = codeEditor;
218+
break;
219+
}
220+
}
221+
222+
await runCell(editorGroupsService, context);
223+
224+
if (!foundEditor) {
225+
return;
226+
}
227+
228+
const controller = InteractiveEditorController.get(foundEditor);
229+
if (!controller) {
230+
return;
231+
}
232+
233+
controller.createSnapshot();
212234
}
213235
});
214236

0 commit comments

Comments
 (0)