Skip to content

Commit

Permalink
Editor view state is too sticky for diff editors (fix microsoft#134098)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Sep 29, 2021
1 parent 0e11dc3 commit 57903bc
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/vs/workbench/browser/parts/editor/editorWithViewState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { URI } from 'vs/base/common/uri';
import { Event } from 'vs/base/common/event';
import { IEditorMemento, IEditorCloseEvent, IEditorOpenContext, EditorResourceAccessor, SideBySideEditor } from 'vs/workbench/common/editor';
import { EditorPane } from 'vs/workbench/browser/parts/editor/editorPane';
import { IStorageService } from 'vs/platform/storage/common/storage';
Expand All @@ -14,7 +15,7 @@ import { ITextResourceConfigurationService } from 'vs/editor/common/services/tex
import { IEditorGroupsService, IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IExtUri } from 'vs/base/common/resources';
import { MutableDisposable } from 'vs/base/common/lifecycle';
import { IDisposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { IEditorOptions } from 'vs/platform/editor/common/editor';
import { CancellationToken } from 'vs/base/common/cancellation';
Expand All @@ -28,6 +29,8 @@ export abstract class AbstractEditorWithViewState<T extends object> extends Edit

private readonly groupListener = this._register(new MutableDisposable());

private editorViewStateDisposables: Map<EditorInput, IDisposable> | undefined;

constructor(
id: string,
viewStateStorageKey: string,
Expand Down Expand Up @@ -96,6 +99,22 @@ export abstract class AbstractEditorWithViewState<T extends object> extends Edit
return; // we need a resource
}

// If we are not tracking disposed editor view state
// make sure to clear the view state once the editor
// is disposed.
if (!this.tracksDisposedEditorViewState()) {
if (!this.editorViewStateDisposables) {
this.editorViewStateDisposables = new Map<EditorInput, IDisposable>();
}

if (!this.editorViewStateDisposables.has(input)) {
this.editorViewStateDisposables.set(input, Event.once(input.onWillDispose)(() => {
this.clearEditorViewState(resource, this.group);
this.editorViewStateDisposables?.delete(input);
}));
}
}

// Clear the editor view state if:
// - the editor view state should not be tracked for disposed editors
// - the user configured to not restore view state unless the editor is still opened in the group
Expand Down Expand Up @@ -179,6 +198,18 @@ export abstract class AbstractEditorWithViewState<T extends object> extends Edit
this.viewState.clearEditorState(resource, group);
}

override dispose(): void {
super.dispose();

if (this.editorViewStateDisposables) {
for (const [, disposables] of this.editorViewStateDisposables) {
disposables.dispose();
}

this.editorViewStateDisposables = undefined;
}
}

//#region Subclasses should/could override based on needs

/**
Expand Down

0 comments on commit 57903bc

Please sign in to comment.