From 27cb8862c95fa81688b65ab51b19998adbdca08c Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 21 Sep 2018 17:14:48 +0200 Subject: [PATCH] fix #24394 --- .../browser/parts/editor/baseEditor.ts | 16 +++++++++--- .../browser/parts/editor/textEditor.ts | 6 ++--- src/vs/workbench/common/editor.ts | 5 ++-- .../electron-browser/main.contribution.ts | 5 ++++ .../files/browser/editors/textFileEditor.ts | 26 ++++++++++++++++--- .../browser/parts/editor/baseEditor.test.ts | 4 +-- 6 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/vs/workbench/browser/parts/editor/baseEditor.ts b/src/vs/workbench/browser/parts/editor/baseEditor.ts index 2d62841051a..1db485bf0d7 100644 --- a/src/vs/workbench/browser/parts/editor/baseEditor.ts +++ b/src/vs/workbench/browser/parts/editor/baseEditor.ts @@ -240,13 +240,21 @@ export class EditorMemento implements IEditorMemento { return void 0; } - clearState(resource: URI): void; - clearState(editor: EditorInput): void; - clearState(resourceOrEditor: URI | EditorInput): void { + clearState(resource: URI, group?: IEditorGroup): void; + clearState(editor: EditorInput, group?: IEditorGroup): void; + clearState(resourceOrEditor: URI | EditorInput, group?: IEditorGroup): void { const resource = this.doGetResource(resourceOrEditor); if (resource) { const cache = this.doLoad(); - cache.delete(resource.toString()); + + if (group) { + const resourceViewState = cache.get(resource.toString()); + if (resourceViewState) { + delete resourceViewState[group.id]; + } + } else { + cache.delete(resource.toString()); + } } } diff --git a/src/vs/workbench/browser/parts/editor/textEditor.ts b/src/vs/workbench/browser/parts/editor/textEditor.ts index e47bdd9ac16..e98d85cafdd 100644 --- a/src/vs/workbench/browser/parts/editor/textEditor.ts +++ b/src/vs/workbench/browser/parts/editor/textEditor.ts @@ -76,7 +76,7 @@ export abstract class BaseTextEditor extends BaseEditor implements ITextEditor { return this._textFileService; } - private handleConfigurationChangeEvent(configuration?: IEditorConfiguration): void { + protected handleConfigurationChangeEvent(configuration?: IEditorConfiguration): void { if (this.isVisible()) { this.updateEditorConfiguration(configuration); } else { @@ -260,9 +260,9 @@ export abstract class BaseTextEditor extends BaseEditor implements ITextEditor { /** * Clears the text editor view state for the given resources. */ - protected clearTextEditorViewState(resources: URI[]): void { + protected clearTextEditorViewState(resources: URI[], group?: IEditorGroup): void { resources.forEach(resource => { - this.editorMemento.clearState(resource); + this.editorMemento.clearState(resource, group); }); } diff --git a/src/vs/workbench/common/editor.ts b/src/vs/workbench/common/editor.ts index 0ec4e750428..e1cf5599a5e 100644 --- a/src/vs/workbench/common/editor.ts +++ b/src/vs/workbench/common/editor.ts @@ -950,6 +950,7 @@ export interface IWorkbenchEditorPartConfiguration { revealIfOpen?: boolean; swipeToNavigate?: boolean; labelFormat?: 'default' | 'short' | 'medium' | 'long'; + restoreViewState?: boolean; } export interface IResourceOptions { @@ -1010,8 +1011,8 @@ export interface IEditorMemento { loadState(group: IEditorGroup, resource: URI): T; loadState(group: IEditorGroup, editor: EditorInput): T; - clearState(resource: URI): void; - clearState(editor: EditorInput): void; + clearState(resource: URI, group?: IEditorGroup): void; + clearState(editor: EditorInput, group?: IEditorGroup): void; } class EditorInputFactoryRegistry implements IEditorInputFactoryRegistry { diff --git a/src/vs/workbench/electron-browser/main.contribution.ts b/src/vs/workbench/electron-browser/main.contribution.ts index b4a27fe1336..cb727fbfb17 100644 --- a/src/vs/workbench/electron-browser/main.contribution.ts +++ b/src/vs/workbench/electron-browser/main.contribution.ts @@ -563,6 +563,11 @@ configurationRegistry.registerConfiguration({ 'default': false, 'included': isMacintosh }, + 'workbench.editor.restoreViewState': { + 'type': 'boolean', + 'description': nls.localize('restoreViewState', "Restores the last view state (e.g. scroll position) when re-opening files after they have been closed."), + 'default': true, + }, 'workbench.commandPalette.history': { 'type': 'number', 'description': nls.localize('commandHistory', "Controls the number of recently used commands to keep in history for the command palette. Set to 0 to disable command history."), diff --git a/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts b/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts index 3b0816c0ad4..62fc8af6334 100644 --- a/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts +++ b/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts @@ -13,7 +13,7 @@ import * as paths from 'vs/base/common/paths'; import { Action } from 'vs/base/common/actions'; import { VIEWLET_ID, IExplorerViewlet, TEXT_FILE_EDITOR_ID } from 'vs/workbench/parts/files/common/files'; import { ITextFileEditorModel, ITextFileService } from 'vs/workbench/services/textfile/common/textfiles'; -import { BaseTextEditor } from 'vs/workbench/browser/parts/editor/textEditor'; +import { BaseTextEditor, IEditorConfiguration } from 'vs/workbench/browser/parts/editor/textEditor'; import { EditorOptions, TextEditorOptions } from 'vs/workbench/common/editor'; import { BinaryEditorModel } from 'vs/workbench/common/editor/binaryEditorModel'; import { FileEditorInput } from 'vs/workbench/parts/files/common/editors/fileEditorInput'; @@ -40,6 +40,8 @@ export class TextFileEditor extends BaseTextEditor { static readonly ID = TEXT_FILE_EDITOR_ID; + private restoreViewState: boolean; + constructor( @ITelemetryService telemetryService: ITelemetryService, @IFileService private fileService: IFileService, @@ -58,6 +60,8 @@ export class TextFileEditor extends BaseTextEditor { ) { super(TextFileEditor.ID, telemetryService, instantiationService, storageService, configurationService, themeService, textFileService, editorService, editorGroupService, windowService); + this.updateRestoreViewStateConfiguration(); + // Clear view state for deleted files this._register(this.fileService.onFileChanges(e => this.onFilesChanged(e))); } @@ -69,6 +73,16 @@ export class TextFileEditor extends BaseTextEditor { } } + protected handleConfigurationChangeEvent(configuration?: IEditorConfiguration): void { + super.handleConfigurationChangeEvent(configuration); + + this.updateRestoreViewStateConfiguration(); + } + + private updateRestoreViewStateConfiguration(): void { + this.restoreViewState = this.configurationService.getValue(null, 'workbench.editor.restoreViewState'); + } + getTitle(): string { return this.input ? this.input.getName() : nls.localize('textFileEditor', "Text File Editor"); } @@ -80,12 +94,16 @@ export class TextFileEditor extends BaseTextEditor { setEditorVisible(visible: boolean, group: IEditorGroup): void { super.setEditorVisible(visible, group); - // React to editors closing to preserve view state. This needs to happen + // React to editors closing to preserve or clear view state. This needs to happen // in the onWillCloseEditor because at that time the editor has not yet - // been disposed and we can safely persist the view state still. + // been disposed and we can safely persist the view state still as needed. this._register((group as IEditorGroupView).onWillCloseEditor(e => { if (e.editor === this.input) { - this.doSaveTextEditorViewState(this.input); + if (this.restoreViewState) { + this.doSaveTextEditorViewState(this.input); + } else { + this.clearTextEditorViewState([this.input.getResource()], this.group); + } } })); } diff --git a/src/vs/workbench/test/browser/parts/editor/baseEditor.test.ts b/src/vs/workbench/test/browser/parts/editor/baseEditor.test.ts index b6d674c9226..61bf315c2b5 100644 --- a/src/vs/workbench/test/browser/parts/editor/baseEditor.test.ts +++ b/src/vs/workbench/test/browser/parts/editor/baseEditor.test.ts @@ -250,10 +250,10 @@ suite('Workbench base editor', () => { assert.ok(!memento.loadState(testGroup4, URI.file('/E'))); assert.ok(!memento.loadState(testGroup4, URI.file('/C'))); - memento.clearState(URI.file('/C')); + memento.clearState(URI.file('/C'), testGroup4); memento.clearState(URI.file('/E')); - assert.ok(!memento.loadState(testGroup0, URI.file('/C'))); + assert.ok(!memento.loadState(testGroup4, URI.file('/C'))); assert.ok(memento.loadState(testGroup0, URI.file('/D'))); assert.ok(!memento.loadState(testGroup0, URI.file('/E'))); }); -- GitLab