Skip to content

Commit cb6609c

Browse files
committed
add getValueFromSnapshot util
1 parent 28d42fa commit cb6609c

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ export class InteractiveEditorController implements IEditorContribution {
504504
}
505505

506506
createSnapshot(): void {
507-
if (this._activeSession?.lastExchange?.response instanceof EditResponse && this._strategy) {
507+
if (this._activeSession && !this._activeSession.textModel0.equalsTextBuffer(this._activeSession.textModelN.getTextBuffer())) {
508508
this._activeSession.createSnapshot();
509509
}
510510
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ 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';
1918
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorker';
20-
import { IModelService } from 'vs/editor/common/services/model';
2119
import { localize } from 'vs/nls';
2220
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
2321
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
2422
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
2523
import { InteractiveEditorFileCreatePreviewWidget, InteractiveEditorLivePreviewWidget } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorLivePreviewWidget';
2624
import { EditResponse, Session } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession';
2725
import { InteractiveEditorWidget } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorWidget';
26+
import { getValueFromSnapshot } from 'vs/workbench/contrib/interactiveEditor/browser/utils';
2827
import { CTX_INTERACTIVE_EDITOR_INLNE_DIFF, CTX_INTERACTIVE_EDITOR_DOCUMENT_CHANGED } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
2928

3029
export abstract class EditModeStrategy {
@@ -209,7 +208,6 @@ export class LiveStrategy extends EditModeStrategy {
209208
@IStorageService protected _storageService: IStorageService,
210209
@IBulkEditService protected readonly _bulkEditService: IBulkEditService,
211210
@IEditorWorkerService protected readonly _editorWorkerService: IEditorWorkerService,
212-
@IModelService private readonly _modelService: IModelService
213211
) {
214212
super();
215213
this._inlineDiffDecorations = new InlineDiffDecorations(this._editor, this._inlineDiffEnabled);
@@ -265,8 +263,11 @@ export class LiveStrategy extends EditModeStrategy {
265263
return;
266264
}
267265

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() }]);
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 }]);
270271
if (edits) {
271272
const operations = edits.map(e => EditOperation.replace(Range.lift(e.range), e.text));
272273
modelN.pushEditOperations(null, operations, () => null);
@@ -331,9 +332,8 @@ export class LivePreviewStrategy extends LiveStrategy {
331332
@IBulkEditService bulkEditService: IBulkEditService,
332333
@IEditorWorkerService editorWorkerService: IEditorWorkerService,
333334
@IInstantiationService instaService: IInstantiationService,
334-
@IModelService modelService: IModelService
335335
) {
336-
super(session, editor, widget, contextKeyService, storageService, bulkEditService, editorWorkerService, modelService);
336+
super(session, editor, widget, contextKeyService, storageService, bulkEditService, editorWorkerService);
337337

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

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+
}

0 commit comments

Comments
 (0)