Skip to content

Commit cb4ef36

Browse files
committed
Make sure we restore the previous editor when switching back to it
1 parent 4fb0341 commit cb4ef36

3 files changed

Lines changed: 38 additions & 20 deletions

File tree

src/vs/workbench/api/common/apiCommands.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,20 @@ CommandsRegistry.registerCommand(OpenAPICommand.ID, adjustHandler(OpenAPICommand
134134

135135
export class OpenWithAPICommand {
136136
public static readonly ID = 'vscode.openWith';
137-
public static execute(executor: ICommandsExecutor, resource: URI, viewType: string, column?: vscode.ViewColumn): Promise<any> {
137+
public static execute(executor: ICommandsExecutor, resource: URI, viewType: string, columnOrOptions?: vscode.ViewColumn | vscode.TextDocumentShowOptions): Promise<any> {
138+
let options: ITextEditorOptions | undefined;
138139
let position: EditorViewColumn | undefined;
139140

140-
if (typeof column === 'number') {
141-
position = typeConverters.ViewColumn.from(column);
141+
if (typeof columnOrOptions === 'number') {
142+
position = typeConverters.ViewColumn.from(columnOrOptions);
143+
} else if (typeof columnOrOptions !== 'undefined') {
144+
options = typeConverters.TextEditorOptions.from(columnOrOptions);
142145
}
143146

144147
return executor.executeCommand('_workbench.openWith', [
145148
resource,
146149
viewType,
150+
options,
147151
position
148152
]);
149153
}

src/vs/workbench/contrib/customEditor/browser/commands.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ import { defaultEditorId } from 'vs/workbench/contrib/customEditor/browser/custo
2020
import { CONTEXT_FOCUSED_CUSTOM_EDITOR_IS_EDITABLE, CONTEXT_HAS_CUSTOM_EDITORS, ICustomEditorService } from 'vs/workbench/contrib/customEditor/common/customEditor';
2121
import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
2222
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
23+
import type { ITextEditorOptions } from 'vs/platform/editor/common/editor';
2324

2425
const viewCategory = nls.localize('viewCategory', "View");
2526

2627
// #region Open With
2728

28-
CommandsRegistry.registerCommand('_workbench.openWith', (accessor: ServicesAccessor, args: [URI, string, EditorViewColumn]) => {
29+
CommandsRegistry.registerCommand('_workbench.openWith', (accessor: ServicesAccessor, args: [URI, string, ITextEditorOptions | undefined, EditorViewColumn | undefined]) => {
2930
const customEditorService = accessor.get(ICustomEditorService);
3031
const editorGroupService = accessor.get(IEditorGroupsService);
3132

32-
const [resource, viewType, position] = args;
33+
const [resource, viewType, options, position] = args;
3334
const group = viewColumnToEditorGroup(editorGroupService, position);
34-
customEditorService.openWith(resource, viewType, undefined, editorGroupService.getGroup(group));
35+
customEditorService.openWith(resource, viewType, options, editorGroupService.getGroup(group));
3536
});
3637

3738
// #endregion

src/vs/workbench/contrib/customEditor/browser/customEditors.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,23 +240,25 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ
240240
options?: IEditorOptions,
241241
group?: IEditorGroup
242242
): Promise<IEditor | undefined> {
243-
if (group) {
244-
const existingEditors = group.editors.filter(editor => editor.getResource() && isEqual(editor.getResource(), resource));
245-
if (existingEditors.length) {
246-
const existing = existingEditors[0];
247-
if (!input.matches(existing)) {
248-
await this.editorService.replaceEditors([{
249-
editor: existing,
250-
replacement: input,
251-
options: options ? EditorOptions.create(options) : undefined,
252-
}], group);
253-
254-
if (existing instanceof CustomFileEditorInput) {
255-
existing.dispose();
256-
}
243+
const targetGroup = group || this.editorGroupService.activeGroup;
244+
245+
// Try to replace existing editors for resource
246+
const existingEditors = targetGroup.editors.filter(editor => editor.getResource() && isEqual(editor.getResource(), resource));
247+
if (existingEditors.length) {
248+
const existing = existingEditors[0];
249+
if (!input.matches(existing)) {
250+
await this.editorService.replaceEditors([{
251+
editor: existing,
252+
replacement: input,
253+
options: options ? EditorOptions.create(options) : undefined,
254+
}], targetGroup);
255+
256+
if (existing instanceof CustomFileEditorInput) {
257+
existing.dispose();
257258
}
258259
}
259260
}
261+
260262
return this.editorService.openEditor(input, options, group);
261263
}
262264

@@ -345,6 +347,17 @@ export class CustomEditorContribution implements IWorkbenchContribution {
345347
options: ITextEditorOptions | undefined,
346348
group: IEditorGroup
347349
): IOpenEditorOverride | undefined {
350+
// Check to see if there already an editor for the resource in the group.
351+
// If there is, we want to open that instead of creating a new editor.
352+
// This ensures that we preserve whatever state the editor was previously in
353+
// when the user switches back to it.
354+
const existingEditorForResource = group.editors.find(editor => isEqual(resource, editor.getResource()));
355+
if (existingEditorForResource) {
356+
return {
357+
override: this.editorService.openEditor(existingEditorForResource, { ...options, ignoreOverrides: true }, group)
358+
};
359+
}
360+
348361
const userConfiguredEditors = this.customEditorService.getUserConfiguredCustomEditors(resource);
349362
if (userConfiguredEditors.length) {
350363
return {

0 commit comments

Comments
 (0)