Skip to content

Commit 28d42fa

Browse files
committed
Create snapshot on execution
1 parent 1defdfc commit 28d42fa

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

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

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

506-
acceptLast(): void {
506+
createSnapshot(): void {
507507
if (this._activeSession?.lastExchange?.response instanceof EditResponse && this._strategy) {
508-
this._activeSession.lastExchange.accepted = true;
508+
this._activeSession.createSnapshot();
509509
}
510510
}
511511

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

Lines changed: 10 additions & 15 deletions
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,20 +88,24 @@ 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}|`;
98107
}
99108

100-
get exchanges(): SessionExchange[] {
101-
return this._exchange;
102-
}
103-
104109
get lastExchange(): SessionExchange | undefined {
105110
return this._exchange[this._exchange.length - 1];
106111
}
@@ -134,16 +139,6 @@ export class Session {
134139

135140

136141
export class SessionExchange {
137-
private _accepted = false;
138-
139-
get accepted() {
140-
return this._accepted;
141-
}
142-
143-
set accepted(value: boolean) {
144-
this._accepted = value;
145-
}
146-
147142
constructor(
148143
readonly prompt: string,
149144
readonly response: MarkdownResponse | EditResponse | EmptyResponse | ErrorResponse

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import { Selection } from 'vs/editor/common/core/selection';
1515
import { LineRangeMapping } from 'vs/editor/common/diff/linesDiffComputer';
1616
import { IEditorDecorationsCollection } from 'vs/editor/common/editorCommon';
1717
import { ICursorStateComputer, IModelDecorationOptions, IModelDeltaDecoration, IValidEditOperation } from 'vs/editor/common/model';
18+
import { createTextBufferFactoryFromSnapshot } from 'vs/editor/common/model/textModel';
1819
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorker';
20+
import { IModelService } from 'vs/editor/common/services/model';
1921
import { localize } from 'vs/nls';
2022
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
2123
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
@@ -206,7 +208,8 @@ export class LiveStrategy extends EditModeStrategy {
206208
@IContextKeyService contextKeyService: IContextKeyService,
207209
@IStorageService protected _storageService: IStorageService,
208210
@IBulkEditService protected readonly _bulkEditService: IBulkEditService,
209-
@IEditorWorkerService protected readonly _editorWorkerService: IEditorWorkerService
211+
@IEditorWorkerService protected readonly _editorWorkerService: IEditorWorkerService,
212+
@IModelService private readonly _modelService: IModelService
210213
) {
211214
super();
212215
this._inlineDiffDecorations = new InlineDiffDecorations(this._editor, this._inlineDiffEnabled);
@@ -257,11 +260,13 @@ export class LiveStrategy extends EditModeStrategy {
257260
}
258261

259262
async cancel() {
260-
const { textModelN: modelN, textModel0: model0 } = this._session;
261-
if (modelN.isDisposed() || model0.isDisposed()) {
263+
const { textModelN: modelN, textModel0: model0, lastSnapshot } = this._session;
264+
if (modelN.isDisposed() || (model0.isDisposed() && !lastSnapshot)) {
262265
return;
263266
}
264-
const edits = await this._editorWorkerService.computeMoreMinimalEdits(modelN.uri, [{ range: modelN.getFullModelRange(), text: model0.getValue() }]);
267+
268+
const initialTextModel = lastSnapshot ? this._modelService.createModel(createTextBufferFactoryFromSnapshot(lastSnapshot), null) : model0;
269+
const edits = await this._editorWorkerService.computeMoreMinimalEdits(modelN.uri, [{ range: modelN.getFullModelRange(), text: initialTextModel.getValue() }]);
265270
if (edits) {
266271
const operations = edits.map(e => EditOperation.replace(Range.lift(e.range), e.text));
267272
modelN.pushEditOperations(null, operations, () => null);
@@ -326,8 +331,9 @@ export class LivePreviewStrategy extends LiveStrategy {
326331
@IBulkEditService bulkEditService: IBulkEditService,
327332
@IEditorWorkerService editorWorkerService: IEditorWorkerService,
328333
@IInstantiationService instaService: IInstantiationService,
334+
@IModelService modelService: IModelService
329335
) {
330-
super(session, editor, widget, contextKeyService, storageService, bulkEditService, editorWorkerService);
336+
super(session, editor, widget, contextKeyService, storageService, bulkEditService, editorWorkerService, modelService);
331337

332338
this._diffZone = instaService.createInstance(InteractiveEditorLivePreviewWidget, editor, session.textModel0);
333339
this._previewZone = instaService.createInstance(InteractiveEditorFileCreatePreviewWidget, editor);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ registerAction2(class ExecuteCell extends NotebookMultiCellAction {
230230
return;
231231
}
232232

233-
controller.acceptLast();
233+
controller.createSnapshot();
234234
}
235235
});
236236

0 commit comments

Comments
 (0)