提交 27cb8862 编写于 作者: B Benjamin Pasero

fix #24394

上级 1ed36784
......@@ -240,13 +240,21 @@ export class EditorMemento<T> implements IEditorMemento<T> {
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());
}
}
}
......
......@@ -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);
});
}
......
......@@ -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<T> {
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 {
......
......@@ -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."),
......
......@@ -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);
}
}
}));
}
......
......@@ -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')));
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册