From 57903bcf3f11fcf2d4f58eb90a069042459de348 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Wed, 29 Sep 2021 10:52:12 +0200 Subject: [PATCH] Editor view state is too sticky for diff editors (fix #134098) --- .../parts/editor/editorWithViewState.ts | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/vs/workbench/browser/parts/editor/editorWithViewState.ts b/src/vs/workbench/browser/parts/editor/editorWithViewState.ts index 20dbb031ce9..2f1abb31153 100644 --- a/src/vs/workbench/browser/parts/editor/editorWithViewState.ts +++ b/src/vs/workbench/browser/parts/editor/editorWithViewState.ts @@ -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'; @@ -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'; @@ -28,6 +29,8 @@ export abstract class AbstractEditorWithViewState extends Edit private readonly groupListener = this._register(new MutableDisposable()); + private editorViewStateDisposables: Map | undefined; + constructor( id: string, viewStateStorageKey: string, @@ -96,6 +99,22 @@ export abstract class AbstractEditorWithViewState 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(); + } + + 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 @@ -179,6 +198,18 @@ export abstract class AbstractEditorWithViewState 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 /**