From 82d9feb342f995246eea12624f7199587c74270d Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Mon, 31 Oct 2016 15:01:15 +0100 Subject: [PATCH] better fix for #8202 --- .../editor/browser/widget/codeEditorWidget.ts | 26 +++++++-------- src/vs/editor/common/editorCommon.ts | 5 +-- src/vs/monaco.d.ts | 8 ++--- src/vs/workbench/common/editor.ts | 13 +++----- .../files/browser/editors/textFileEditor.ts | 32 +++++++++---------- 5 files changed, 34 insertions(+), 50 deletions(-) diff --git a/src/vs/editor/browser/widget/codeEditorWidget.ts b/src/vs/editor/browser/widget/codeEditorWidget.ts index 02981ec6439..4548e94f9dd 100644 --- a/src/vs/editor/browser/widget/codeEditorWidget.ts +++ b/src/vs/editor/browser/widget/codeEditorWidget.ts @@ -251,23 +251,19 @@ export abstract class CodeEditorWidget extends CommonCodeEditor implements edito if (!this.cursor || !this.hasView) { return; } - if (state) { - var codeEditorState = state; - var cursorState = codeEditorState.cursorState; - if (cursorState) { - if (Array.isArray(cursorState)) { - this.cursor.restoreState(cursorState); - } else { - // Backwards compatibility - this.cursor.restoreState([cursorState]); - } - } - - if (codeEditorState.viewState) { - this._view.restoreState(codeEditorState.viewState); + var s = state; + if (s && s.cursorState && s.viewState) { + var codeEditorState = s; + var cursorState = codeEditorState.cursorState; + if (Array.isArray(cursorState)) { + this.cursor.restoreState(cursorState); + } else { + // Backwards compatibility + this.cursor.restoreState([cursorState]); } + this._view.restoreState(codeEditorState.viewState); - let contributionsState = codeEditorState.contributionsState || {}; + let contributionsState = s.contributionsState || {}; let keys = Object.keys(this._contributions); for (let i = 0, len = keys.length; i < len; i++) { let id = keys[i]; diff --git a/src/vs/editor/common/editorCommon.ts b/src/vs/editor/common/editorCommon.ts index baa968da486..0074cbe6e8d 100644 --- a/src/vs/editor/common/editorCommon.ts +++ b/src/vs/editor/common/editorCommon.ts @@ -2965,10 +2965,7 @@ export interface IViewState { export interface ICodeEditorViewState extends IEditorViewState { cursorState: ICursorState[]; viewState: IViewState; - contributionsState: IContributionsViewState; -} -export interface IContributionsViewState { - [id: string]: any; + contributionsState: { [id: string]: any }; } /** diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 3d44d7ed432..0329b1eb5c0 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -2665,11 +2665,9 @@ declare module monaco.editor { export interface ICodeEditorViewState extends IEditorViewState { cursorState: ICursorState[]; viewState: IViewState; - contributionsState: IContributionsViewState; - } - - export interface IContributionsViewState { - [id: string]: any; + contributionsState: { + [id: string]: any; + }; } /** diff --git a/src/vs/workbench/common/editor.ts b/src/vs/workbench/common/editor.ts index 56e84b9beb3..316e19103d2 100644 --- a/src/vs/workbench/common/editor.ts +++ b/src/vs/workbench/common/editor.ts @@ -8,7 +8,7 @@ import { TPromise } from 'vs/base/common/winjs.base'; import Event, { Emitter } from 'vs/base/common/event'; import types = require('vs/base/common/types'); import URI from 'vs/base/common/uri'; -import { IEditor, ICommonCodeEditor, IEditorViewState, IEditorOptions as ICodeEditorOptions, ICodeEditorViewState, IContributionsViewState, EditorType } from 'vs/editor/common/editorCommon'; +import { IEditor, ICommonCodeEditor, IEditorViewState, IEditorOptions as ICodeEditorOptions } from 'vs/editor/common/editorCommon'; import { IEditorInput, IEditorModel, IEditorOptions, ITextEditorOptions, IResourceInput, Position } from 'vs/platform/editor/common/editor'; import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace'; import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService'; @@ -614,7 +614,7 @@ export class TextEditorOptions extends EditorOptions { * * @return if something was applied */ - public apply(editor: IEditor, extraContributionsViewState?: IContributionsViewState): boolean { + public apply(editor: IEditor): boolean { // Editor options if (this.editorOptions) { @@ -622,10 +622,10 @@ export class TextEditorOptions extends EditorOptions { } // View state - return this.applyViewState(editor, extraContributionsViewState); + return this.applyViewState(editor); } - private applyViewState(editor: IEditor, extraContributionsViewState?: IContributionsViewState): boolean { + private applyViewState(editor: IEditor): boolean { let gotApplied = false; // First try viewstate @@ -637,11 +637,6 @@ export class TextEditorOptions extends EditorOptions { // Otherwise check for selection else if (!types.isUndefinedOrNull(this.startLineNumber) && !types.isUndefinedOrNull(this.startColumn)) { - // Additional Contributions View State only applies if we did not have view state otherwise - if (extraContributionsViewState && editor.getEditorType() === EditorType.ICodeEditor) { - editor.restoreViewState({ contributionsState: extraContributionsViewState } as ICodeEditorViewState); - } - // Select if (!types.isUndefinedOrNull(this.endLineNumber) && !types.isUndefinedOrNull(this.endColumn)) { const range = { diff --git a/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts b/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts index e8d4425a970..b71b135b4af 100644 --- a/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts +++ b/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts @@ -10,7 +10,7 @@ import errors = require('vs/base/common/errors'); import { toErrorMessage } from 'vs/base/common/errorMessage'; import types = require('vs/base/common/types'); import paths = require('vs/base/common/paths'); -import { ICodeEditorViewState } from 'vs/editor/common/editorCommon'; +import { IEditorViewState } from 'vs/editor/common/editorCommon'; import { Action } from 'vs/base/common/actions'; import { Scope } from 'vs/workbench/common/memento'; import { IEditorOptions } from 'vs/editor/common/editorCommon'; @@ -37,9 +37,9 @@ import { IThemeService } from 'vs/workbench/services/themes/common/themeService' const TEXT_EDITOR_VIEW_STATE_PREFERENCE_KEY = 'textEditorViewState'; interface ITextEditorViewState { - 0?: ICodeEditorViewState; - 1?: ICodeEditorViewState; - 2?: ICodeEditorViewState; + 0?: IEditorViewState; + 1?: IEditorViewState; + 2?: IEditorViewState; } /** @@ -84,7 +84,7 @@ export class TextFileEditor extends BaseTextEditor { return super.getInput(); } - public setInput(input: FileEditorInput, options: TextEditorOptions): TPromise { + public setInput(input: FileEditorInput, options: EditorOptions): TPromise { const oldInput = this.getInput(); super.setInput(input, options); @@ -105,8 +105,8 @@ export class TextFileEditor extends BaseTextEditor { if (!forceOpen && input.matches(oldInput)) { // TextOptions (avoiding instanceof here for a reason, do not change!) - if (options && types.isFunction(options.apply)) { - options.apply(this.getControl()); + if (options && types.isFunction((options).apply)) { + (options).apply(this.getControl()); } return TPromise.as(null); @@ -145,17 +145,15 @@ export class TextFileEditor extends BaseTextEditor { const textEditor = this.getControl(); textEditor.setModel(textFileModel.textEditorModel); - const previousEditorViewState = this.loadTextEditorViewState(this.storageService, this.getInput().getResource().toString()); - - // TextOptions (avoiding instanceof here for a reason, do not change!) - let optionsGotApplied = false; - if (options && types.isFunction(options.apply)) { - optionsGotApplied = options.apply(textEditor, previousEditorViewState && previousEditorViewState.contributionsState); + // Always restore View State if any associated + const editorViewState = this.loadTextEditorViewState(this.storageService, this.getInput().getResource().toString()); + if (editorViewState) { + textEditor.restoreViewState(editorViewState); } - // Otherwise restore previous View State if available - if (!optionsGotApplied && previousEditorViewState) { - textEditor.restoreViewState(previousEditorViewState); + // TextOptions (avoiding instanceof here for a reason, do not change!) + if (options && types.isFunction((options).apply)) { + (options).apply(textEditor); } }, (error) => { @@ -266,7 +264,7 @@ export class TextFileEditor extends BaseTextEditor { /** * Loads the text editor view state for the given key and returns it. */ - private loadTextEditorViewState(storageService: IStorageService, key: string): ICodeEditorViewState { + private loadTextEditorViewState(storageService: IStorageService, key: string): IEditorViewState { const memento = this.getMemento(storageService, Scope.WORKSPACE); const textEditorViewStateMemento = memento[TEXT_EDITOR_VIEW_STATE_PREFERENCE_KEY]; if (textEditorViewStateMemento) { -- GitLab