Skip to content

Commit 3e824cf

Browse files
authored
make sure EditModeStrategy#apply/cancel isn't called twice, keep reference onto editor model to prevent dispose before reset (microsoft#180952)
fixes microsoft/vscode-internalbacklog#3963
1 parent 9d2090b commit 3e824cf

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { ModelDecorationOptions, createTextBufferFactoryFromSnapshot } from 'vs/
3131
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorker';
3232
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
3333
import { IModelService } from 'vs/editor/common/services/model';
34+
import { ITextModelService } from 'vs/editor/common/services/resolverService';
3435
import { InlineCompletionsController } from 'vs/editor/contrib/inlineCompletions/browser/inlineCompletionsController';
3536
import { localize } from 'vs/nls';
3637
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
@@ -310,6 +311,7 @@ export class InteractiveEditorController implements IEditorContribution {
310311
@IStorageService private readonly _storageService: IStorageService,
311312
@IConfigurationService private readonly _configurationService: IConfigurationService,
312313
@IModelService private readonly _modelService: IModelService,
314+
@ITextModelService private readonly _textModelService: ITextModelService,
313315
@INotebookEditorService private readonly _notebookEditorService: INotebookEditorService,
314316
@IContextKeyService contextKeyService: IContextKeyService,
315317

@@ -375,15 +377,16 @@ export class InteractiveEditorController implements IEditorContribution {
375377

376378
const store = new DisposableStore();
377379

380+
// keep a snapshot of the "actual" model
381+
const textModel0 = this._modelService.createModel(createTextBufferFactoryFromSnapshot(textModel.createSnapshot()), { languageId: textModel.getLanguageId(), onDidChange: Event.None }, undefined, true);
382+
store.add(textModel0);
383+
384+
// keep a reference to prevent disposal of the "actual" model
385+
const refTextModelN = await this._textModelService.createModelReference(textModel.uri);
386+
store.add(refTextModelN);
378387

379388
let textModel0Changes: LineRangeMapping[] | undefined;
380-
const textModel0 = this._modelService.createModel(
381-
createTextBufferFactoryFromSnapshot(textModel.createSnapshot()),
382-
{ languageId: textModel.getLanguageId(), onDidChange: Event.None },
383-
undefined, true
384-
);
385389

386-
store.add(textModel0);
387390
this._currentSession = new Session(editMode, textModel0, textModel, provider, session);
388391
this._strategy = this._instaService.createInstance(EditModeStrategy.ctor(editMode), this._currentSession);
389392

@@ -459,9 +462,6 @@ export class InteractiveEditorController implements IEditorContribution {
459462

460463
}, undefined, store);
461464

462-
const roundStore = new DisposableStore();
463-
store.add(roundStore);
464-
465465
const diffZone = this._instaService.createInstance(InteractiveEditorDiffWidget, this._editor, textModel0);
466466

467467
do {
@@ -507,7 +507,6 @@ export class InteractiveEditorController implements IEditorContribution {
507507
this.accept();
508508
}
509509
const input = await inputPromise;
510-
roundStore.clear();
511510

512511
if (!input) {
513512
continue;
@@ -805,16 +804,20 @@ export class InteractiveEditorController implements IEditorContribution {
805804
}
806805

807806
async applyChanges(): Promise<EditResponse | void> {
808-
if (this._currentSession?.lastResponse instanceof EditResponse) {
807+
if (this._currentSession?.lastResponse instanceof EditResponse && this._strategy) {
809808
const { lastResponse } = this._currentSession;
810-
await this._strategy?.apply();
809+
const strategy = this._strategy;
810+
this._strategy = undefined;
811+
await strategy?.apply();
811812
this._ctsSession.cancel();
812813
return lastResponse;
813814
}
814815
}
815816

816817
async cancelSession() {
817-
await this._strategy?.cancel();
818+
const strategy = this._strategy;
819+
this._strategy = undefined;
820+
await strategy?.cancel();
818821
this._ctsSession.cancel();
819822
}
820823
}

0 commit comments

Comments
 (0)