Skip to content

Commit 0bfb6dd

Browse files
committed
Experiment: accepting suggestion without dismissing widget.
1 parent 07b73db commit 0bfb6dd

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { IRange, Range } from 'vs/editor/common/core/range';
2222
import { Selection } from 'vs/editor/common/core/selection';
2323
import { LineRangeMapping } from 'vs/editor/common/diff/linesDiffComputer';
2424
import { IEditorContribution, IEditorDecorationsCollection, ScrollType } from 'vs/editor/common/editorCommon';
25+
import { TextEdit } from 'vs/editor/common/languages';
2526
import { ICursorStateComputer, IModelDecorationOptions, IModelDeltaDecoration, IValidEditOperation } from 'vs/editor/common/model';
2627
import { ModelDecorationOptions, createTextBufferFactoryFromSnapshot } from 'vs/editor/common/model/textModel';
2728
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorker';
@@ -582,6 +583,12 @@ export class InteractiveEditorController implements IEditorContribution {
582583
}
583584
}
584585

586+
acceptLast(): void {
587+
if (this._activeSession?.lastExchange?.response instanceof EditResponse && this._strategy) {
588+
this._activeSession.lastExchange.accepted = true;
589+
}
590+
}
591+
585592
async applyChanges(): Promise<EditResponse | void> {
586593
if (this._activeSession?.lastExchange?.response instanceof EditResponse && this._strategy) {
587594
const strategy = this._strategy;
@@ -757,11 +764,26 @@ class LiveStrategy extends EditModeStrategy {
757764
}
758765

759766
async cancel() {
760-
const { modelN, model0 } = this._session;
767+
const { modelN, model0, exchanges, lastExchange } = this._session;
761768
if (modelN.isDisposed() || model0.isDisposed()) {
762769
return;
763770
}
764-
const edits = await this._editorWorkerService.computeMoreMinimalEdits(modelN.uri, [{ range: modelN.getFullModelRange(), text: model0.getValue() }]);
771+
772+
if (lastExchange?.accepted) {
773+
return;
774+
}
775+
776+
// find the last exchange which is accepted
777+
const lastAcceptedExchange = exchanges.reverse().find(exchange => exchange.accepted);
778+
let edits: TextEdit[] | undefined = undefined;
779+
if (lastAcceptedExchange && lastAcceptedExchange.response instanceof EditResponse) {
780+
// apply change on model0 with the last accepted exchange, and then compute minimal edits to apply on modelN
781+
model0.applyEdits(lastAcceptedExchange.response.localEdits);
782+
edits = await this._editorWorkerService.computeMoreMinimalEdits(modelN.uri, [{ range: modelN.getFullModelRange(), text: model0.getValue() }]);
783+
} else {
784+
edits = await this._editorWorkerService.computeMoreMinimalEdits(modelN.uri, [{ range: modelN.getFullModelRange(), text: model0.getValue() }]);
785+
}
786+
765787
if (edits) {
766788
const operations = edits.map(e => EditOperation.replace(Range.lift(e.range), e.text));
767789
modelN.pushEditOperations(null, operations, () => null);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export class Session {
7171
this._teldata.rounds += `${newLen}|`;
7272
}
7373

74+
get exchanges(): SessionExchange[] {
75+
return this._exchange;
76+
}
77+
7478
get lastExchange(): SessionExchange | undefined {
7579
return this._exchange[this._exchange.length - 1];
7680
}
@@ -97,6 +101,16 @@ export class Session {
97101

98102

99103
export class SessionExchange {
104+
private _accepted = false;
105+
106+
get accepted() {
107+
return this._accepted;
108+
}
109+
110+
set accepted(value: boolean) {
111+
this._accepted = value;
112+
}
113+
100114
constructor(
101115
readonly prompt: string,
102116
readonly response: MarkdownResponse | EditResponse

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 { ISelection } from 'vs/editor/common/core/selection';
1213
import { ILanguageService } from 'vs/editor/common/languages/language';
@@ -30,6 +31,8 @@ import { Schemas } from 'vs/base/common/network';
3031
import { IDebugService } from 'vs/workbench/contrib/debug/common/debug';
3132
import { IInteractiveEditorRequest, IInteractiveEditorService } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
3233
import { ICellRange } from 'vs/workbench/contrib/notebook/common/notebookRange';
34+
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
35+
import { InteractiveEditorController } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorController';
3336

3437
const EXECUTE_NOTEBOOK_COMMAND_ID = 'notebook.execute';
3538
const CANCEL_NOTEBOOK_COMMAND_ID = 'notebook.cancelExecution';
@@ -206,7 +209,26 @@ registerAction2(class ExecuteCell extends NotebookMultiCellAction {
206209
await context.notebookEditor.focusNotebookCell(context.cell, 'container', { skipReveal: true });
207210
}
208211

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

0 commit comments

Comments
 (0)