Make sure we restore the previous editor when switching back to it

This commit is contained in:
Matt Bierner
2020-01-10 16:24:55 -08:00
parent 4fb03413db
commit cb4ef36e6d
3 changed files with 37 additions and 19 deletions

View File

@@ -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<any> {
public static execute(executor: ICommandsExecutor, resource: URI, viewType: string, columnOrOptions?: vscode.ViewColumn | vscode.TextDocumentShowOptions): Promise<any> {
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
]);
}

View File

@@ -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

View File

@@ -240,23 +240,25 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ
options?: IEditorOptions,
group?: IEditorGroup
): Promise<IEditor | undefined> {
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 {