Skip to content

Commit

Permalink
make sure EditModeStrategy#apply/cancel isn't called twice, keep refe…
Browse files Browse the repository at this point in the history
…rence onto editor model to prevent dispose before reset (#180952)

fixes microsoft/vscode-internalbacklog#3963
  • Loading branch information
jrieken authored Apr 26, 2023
1 parent 9d2090b commit 3e824cf
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ModelDecorationOptions, createTextBufferFactoryFromSnapshot } from 'vs/
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorker';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
import { IModelService } from 'vs/editor/common/services/model';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { InlineCompletionsController } from 'vs/editor/contrib/inlineCompletions/browser/inlineCompletionsController';
import { localize } from 'vs/nls';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
Expand Down Expand Up @@ -310,6 +311,7 @@ export class InteractiveEditorController implements IEditorContribution {
@IStorageService private readonly _storageService: IStorageService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IModelService private readonly _modelService: IModelService,
@ITextModelService private readonly _textModelService: ITextModelService,
@INotebookEditorService private readonly _notebookEditorService: INotebookEditorService,
@IContextKeyService contextKeyService: IContextKeyService,

Expand Down Expand Up @@ -375,15 +377,16 @@ export class InteractiveEditorController implements IEditorContribution {

const store = new DisposableStore();

// keep a snapshot of the "actual" model
const textModel0 = this._modelService.createModel(createTextBufferFactoryFromSnapshot(textModel.createSnapshot()), { languageId: textModel.getLanguageId(), onDidChange: Event.None }, undefined, true);
store.add(textModel0);

// keep a reference to prevent disposal of the "actual" model
const refTextModelN = await this._textModelService.createModelReference(textModel.uri);
store.add(refTextModelN);

let textModel0Changes: LineRangeMapping[] | undefined;
const textModel0 = this._modelService.createModel(
createTextBufferFactoryFromSnapshot(textModel.createSnapshot()),
{ languageId: textModel.getLanguageId(), onDidChange: Event.None },
undefined, true
);

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

Expand Down Expand Up @@ -459,9 +462,6 @@ export class InteractiveEditorController implements IEditorContribution {

}, undefined, store);

const roundStore = new DisposableStore();
store.add(roundStore);

const diffZone = this._instaService.createInstance(InteractiveEditorDiffWidget, this._editor, textModel0);

do {
Expand Down Expand Up @@ -507,7 +507,6 @@ export class InteractiveEditorController implements IEditorContribution {
this.accept();
}
const input = await inputPromise;
roundStore.clear();

if (!input) {
continue;
Expand Down Expand Up @@ -805,16 +804,20 @@ export class InteractiveEditorController implements IEditorContribution {
}

async applyChanges(): Promise<EditResponse | void> {
if (this._currentSession?.lastResponse instanceof EditResponse) {
if (this._currentSession?.lastResponse instanceof EditResponse && this._strategy) {
const { lastResponse } = this._currentSession;
await this._strategy?.apply();
const strategy = this._strategy;
this._strategy = undefined;
await strategy?.apply();
this._ctsSession.cancel();
return lastResponse;
}
}

async cancelSession() {
await this._strategy?.cancel();
const strategy = this._strategy;
this._strategy = undefined;
await strategy?.cancel();
this._ctsSession.cancel();
}
}
Expand Down

0 comments on commit 3e824cf

Please sign in to comment.