From cb4ef36e6d489f74a1346fa89cdabe727bcd6027 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Fri, 10 Jan 2020 16:24:55 -0800 Subject: [PATCH] Make sure we restore the previous editor when switching back to it --- src/vs/workbench/api/common/apiCommands.ts | 10 +++-- .../contrib/customEditor/browser/commands.ts | 7 ++-- .../customEditor/browser/customEditors.ts | 39 ++++++++++++------- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/vs/workbench/api/common/apiCommands.ts b/src/vs/workbench/api/common/apiCommands.ts index d2c28563d48..69fc23a9af1 100644 --- a/src/vs/workbench/api/common/apiCommands.ts +++ b/src/vs/workbench/api/common/apiCommands.ts @@ -134,16 +134,20 @@ CommandsRegistry.registerCommand(OpenAPICommand.ID, adjustHandler(OpenAPICommand export class OpenWithAPICommand { public static readonly ID = 'vscode.openWith'; - public static execute(executor: ICommandsExecutor, resource: URI, viewType: string, column?: vscode.ViewColumn): Promise { + public static execute(executor: ICommandsExecutor, resource: URI, viewType: string, columnOrOptions?: vscode.ViewColumn | vscode.TextDocumentShowOptions): Promise { + let options: ITextEditorOptions | undefined; let position: EditorViewColumn | undefined; - if (typeof column === 'number') { - position = typeConverters.ViewColumn.from(column); + if (typeof columnOrOptions === 'number') { + position = typeConverters.ViewColumn.from(columnOrOptions); + } else if (typeof columnOrOptions !== 'undefined') { + options = typeConverters.TextEditorOptions.from(columnOrOptions); } return executor.executeCommand('_workbench.openWith', [ resource, viewType, + options, position ]); } diff --git a/src/vs/workbench/contrib/customEditor/browser/commands.ts b/src/vs/workbench/contrib/customEditor/browser/commands.ts index 5db57f251a4..6814383e70b 100644 --- a/src/vs/workbench/contrib/customEditor/browser/commands.ts +++ b/src/vs/workbench/contrib/customEditor/browser/commands.ts @@ -20,18 +20,19 @@ import { defaultEditorId } from 'vs/workbench/contrib/customEditor/browser/custo import { CONTEXT_FOCUSED_CUSTOM_EDITOR_IS_EDITABLE, CONTEXT_HAS_CUSTOM_EDITORS, ICustomEditorService } from 'vs/workbench/contrib/customEditor/common/customEditor'; import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; +import type { ITextEditorOptions } from 'vs/platform/editor/common/editor'; const viewCategory = nls.localize('viewCategory', "View"); // #region Open With -CommandsRegistry.registerCommand('_workbench.openWith', (accessor: ServicesAccessor, args: [URI, string, EditorViewColumn]) => { +CommandsRegistry.registerCommand('_workbench.openWith', (accessor: ServicesAccessor, args: [URI, string, ITextEditorOptions | undefined, EditorViewColumn | undefined]) => { const customEditorService = accessor.get(ICustomEditorService); const editorGroupService = accessor.get(IEditorGroupsService); - const [resource, viewType, position] = args; + const [resource, viewType, options, position] = args; const group = viewColumnToEditorGroup(editorGroupService, position); - customEditorService.openWith(resource, viewType, undefined, editorGroupService.getGroup(group)); + customEditorService.openWith(resource, viewType, options, editorGroupService.getGroup(group)); }); // #endregion diff --git a/src/vs/workbench/contrib/customEditor/browser/customEditors.ts b/src/vs/workbench/contrib/customEditor/browser/customEditors.ts index 0336f929f0c..25ce3d2fc38 100644 --- a/src/vs/workbench/contrib/customEditor/browser/customEditors.ts +++ b/src/vs/workbench/contrib/customEditor/browser/customEditors.ts @@ -240,23 +240,25 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ options?: IEditorOptions, group?: IEditorGroup ): Promise { - if (group) { - const existingEditors = group.editors.filter(editor => editor.getResource() && isEqual(editor.getResource(), resource)); - if (existingEditors.length) { - const existing = existingEditors[0]; - if (!input.matches(existing)) { - await this.editorService.replaceEditors([{ - editor: existing, - replacement: input, - options: options ? EditorOptions.create(options) : undefined, - }], group); + const targetGroup = group || this.editorGroupService.activeGroup; - if (existing instanceof CustomFileEditorInput) { - existing.dispose(); - } + // Try to replace existing editors for resource + const existingEditors = targetGroup.editors.filter(editor => editor.getResource() && isEqual(editor.getResource(), resource)); + if (existingEditors.length) { + const existing = existingEditors[0]; + if (!input.matches(existing)) { + await this.editorService.replaceEditors([{ + editor: existing, + replacement: input, + options: options ? EditorOptions.create(options) : undefined, + }], targetGroup); + + if (existing instanceof CustomFileEditorInput) { + existing.dispose(); } } } + return this.editorService.openEditor(input, options, group); } @@ -345,6 +347,17 @@ export class CustomEditorContribution implements IWorkbenchContribution { options: ITextEditorOptions | undefined, group: IEditorGroup ): IOpenEditorOverride | undefined { + // Check to see if there already an editor for the resource in the group. + // If there is, we want to open that instead of creating a new editor. + // This ensures that we preserve whatever state the editor was previously in + // when the user switches back to it. + const existingEditorForResource = group.editors.find(editor => isEqual(resource, editor.getResource())); + if (existingEditorForResource) { + return { + override: this.editorService.openEditor(existingEditorForResource, { ...options, ignoreOverrides: true }, group) + }; + } + const userConfiguredEditors = this.customEditorService.getUserConfiguredCustomEditors(resource); if (userConfiguredEditors.length) { return {