提交 94efa787 编写于 作者: B Benjamin Pasero

fix #40114

上级 50bc1270
......@@ -23,7 +23,7 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { Scope } from 'vs/workbench/common/memento';
import { getCodeEditor } from 'vs/editor/browser/services/codeEditorService';
import { getCodeEditor, getCodeOrDiffEditor } from 'vs/editor/browser/services/codeEditorService';
import { ITextFileService, SaveReason, AutoSaveMode } from 'vs/workbench/services/textfile/common/textfiles';
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
......@@ -230,48 +230,66 @@ export abstract class BaseTextEditor extends BaseEditor {
}
/**
* Saves the text editor view state under the given key.
* Saves the text editor view state for the given resource.
*/
protected saveTextEditorViewState(key: string): void {
protected saveTextEditorViewState(resource: URI): void {
const editor = getCodeOrDiffEditor(this).codeEditor;
if (!editor) {
return; // not supported for diff editors
}
const model = editor.getModel();
if (!model) {
return; // view state always needs a model
}
const modelUri = model.uri;
if (!modelUri) {
return; // model URI is needed to make sure we save the view state correctly
}
if (modelUri.toString() !== resource.toString()) {
return; // prevent saving view state for a model that is not the expected one
}
const memento = this.getMemento(this.storageService, Scope.WORKSPACE);
let textEditorViewStateMemento: { [key: string]: { [position: number]: IEditorViewState } } = memento[TEXT_EDITOR_VIEW_STATE_PREFERENCE_KEY];
if (!textEditorViewStateMemento) {
textEditorViewStateMemento = Object.create(null);
memento[TEXT_EDITOR_VIEW_STATE_PREFERENCE_KEY] = textEditorViewStateMemento;
}
const editorViewState = this.getControl().saveViewState();
let lastKnownViewState = textEditorViewStateMemento[key];
let lastKnownViewState = textEditorViewStateMemento[resource.toString()];
if (!lastKnownViewState) {
lastKnownViewState = Object.create(null);
textEditorViewStateMemento[key] = lastKnownViewState;
textEditorViewStateMemento[resource.toString()] = lastKnownViewState;
}
if (typeof this.position === 'number') {
lastKnownViewState[this.position] = editorViewState;
lastKnownViewState[this.position] = editor.saveViewState();
}
}
/**
* Clears the text editor view state under the given key.
* Clears the text editor view state for the given resources.
*/
protected clearTextEditorViewState(keys: string[]): void {
protected clearTextEditorViewState(resources: URI[]): void {
const memento = this.getMemento(this.storageService, Scope.WORKSPACE);
const textEditorViewStateMemento: { [key: string]: { [position: number]: IEditorViewState } } = memento[TEXT_EDITOR_VIEW_STATE_PREFERENCE_KEY];
if (textEditorViewStateMemento) {
keys.forEach(key => delete textEditorViewStateMemento[key]);
resources.forEach(resource => delete textEditorViewStateMemento[resource.toString()]);
}
}
/**
* Loads the text editor view state for the given key and returns it.
* Loads the text editor view state for the given resource and returns it.
*/
protected loadTextEditorViewState(key: string): IEditorViewState {
protected loadTextEditorViewState(resource: URI): IEditorViewState {
const memento = this.getMemento(this.storageService, Scope.WORKSPACE);
const textEditorViewStateMemento: { [key: string]: { [position: number]: IEditorViewState } } = memento[TEXT_EDITOR_VIEW_STATE_PREFERENCE_KEY];
if (textEditorViewStateMemento) {
const viewState = textEditorViewStateMemento[key];
const viewState = textEditorViewStateMemento[resource.toString()];
if (viewState) {
return viewState[this.position];
}
......
......@@ -107,7 +107,7 @@ export class AbstractTextResourceEditor extends BaseTextEditor {
protected restoreViewState(input: EditorInput) {
if (input instanceof UntitledEditorInput || input instanceof ResourceEditorInput) {
const viewState = this.loadTextEditorViewState(input.getResource().toString());
const viewState = this.loadTextEditorViewState(input.getResource());
if (viewState) {
this.getControl().restoreViewState(viewState);
}
......@@ -177,20 +177,20 @@ export class AbstractTextResourceEditor extends BaseTextEditor {
return; // only enabled for untitled and resource inputs
}
const key = input.getResource().toString();
const resource = input.getResource();
// Clear view state if input is disposed
if (input.isDisposed()) {
super.clearTextEditorViewState([key]);
super.clearTextEditorViewState([resource]);
}
// Otherwise save it
else {
super.saveTextEditorViewState(key);
super.saveTextEditorViewState(resource);
// Make sure to clean up when the input gets disposed
once(input.onDispose)(() => {
super.clearTextEditorViewState([key]);
super.clearTextEditorViewState([resource]);
});
}
}
......
......@@ -62,7 +62,7 @@ export class TextFileEditor extends BaseTextEditor {
private onFilesChanged(e: FileChangesEvent): void {
const deleted = e.getDeleted();
if (deleted && deleted.length) {
this.clearTextEditorViewState(deleted.map(d => d.resource.toString()));
this.clearTextEditorViewState(deleted.map(d => d.resource));
}
}
......@@ -127,7 +127,7 @@ export class TextFileEditor extends BaseTextEditor {
textEditor.setModel(textFileModel.textEditorModel);
// Always restore View State if any associated
const editorViewState = this.loadTextEditorViewState(this.input.getResource().toString());
const editorViewState = this.loadTextEditorViewState(this.input.getResource());
if (editorViewState) {
textEditor.restoreViewState(editorViewState);
}
......@@ -235,7 +235,7 @@ export class TextFileEditor extends BaseTextEditor {
private doSaveTextEditorViewState(input: FileEditorInput): void {
if (input && !input.isDisposed()) {
this.saveTextEditorViewState(input.getResource().toString());
this.saveTextEditorViewState(input.getResource());
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册