Skip to content

Commit 1e52b05

Browse files
committed
editor open in group context
1 parent 5f39cbb commit 1e52b05

11 files changed

Lines changed: 33 additions & 37 deletions

File tree

src/vs/platform/editor/common/editor.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,6 @@ export interface IEditorOptions {
201201
*/
202202
readonly ignoreOverrides?: boolean;
203203

204-
/**
205-
* When editor overrides are used while opening the editor, the original editor input
206-
* will be closed after the new editor is opened, if `shouldCreateNewWhenOverride` is true.
207-
*/
208-
readonly shouldCreateNewWhenOverride?: boolean;
209-
210204
/**
211205
* A optional hint to signal in which context the editor opens.
212206
*

src/vs/workbench/browser/parts/editor/editor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { GroupIdentifier, IWorkbenchEditorConfiguration, EditorOptions, TextEditorOptions, IEditorInput, IEditorIdentifier, IEditorCloseEvent, IEditorPane, IEditorPartOptions, IEditorPartOptionsChangeEvent, EditorInput } from 'vs/workbench/common/editor';
77
import { EditorGroup } from 'vs/workbench/common/editor/editorGroup';
8-
import { IEditorGroup, GroupDirection, IAddGroupOptions, IMergeGroupOptions, GroupsOrder, GroupsArrangement } from 'vs/workbench/services/editor/common/editorGroupsService';
8+
import { IEditorGroup, GroupDirection, IAddGroupOptions, IMergeGroupOptions, GroupsOrder, GroupsArrangement, OpenEditorInGroupContext } from 'vs/workbench/services/editor/common/editorGroupsService';
99
import { IDisposable } from 'vs/base/common/lifecycle';
1010
import { Dimension } from 'vs/base/browser/dom';
1111
import { Event } from 'vs/base/common/event';
@@ -89,6 +89,8 @@ export interface IEditorOpeningEvent extends IEditorIdentifier {
8989
*/
9090
options?: IEditorOptions;
9191

92+
context?: OpenEditorInGroupContext;
93+
9294
/**
9395
* Allows to prevent the opening of an editor by providing a callback
9496
* that will be executed instead. By returning another editor promise

src/vs/workbench/browser/parts/editor/editorGroupView.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { attachProgressBarStyler } from 'vs/platform/theme/common/styler';
1616
import { IThemeService, registerThemingParticipant, Themable } from 'vs/platform/theme/common/themeService';
1717
import { editorBackground, contrastBorder } from 'vs/platform/theme/common/colorRegistry';
1818
import { EDITOR_GROUP_HEADER_TABS_BACKGROUND, EDITOR_GROUP_HEADER_NO_TABS_BACKGROUND, EDITOR_GROUP_EMPTY_BACKGROUND, EDITOR_GROUP_FOCUSED_EMPTY_BORDER, EDITOR_GROUP_HEADER_BORDER } from 'vs/workbench/common/theme';
19-
import { IMoveEditorOptions, ICopyEditorOptions, ICloseEditorsFilter, IGroupChangeEvent, GroupChangeKind, GroupsOrder, ICloseEditorOptions, ICloseAllEditorsOptions } from 'vs/workbench/services/editor/common/editorGroupsService';
19+
import { IMoveEditorOptions, ICopyEditorOptions, ICloseEditorsFilter, IGroupChangeEvent, GroupChangeKind, GroupsOrder, ICloseEditorOptions, ICloseAllEditorsOptions, OpenEditorInGroupContext } from 'vs/workbench/services/editor/common/editorGroupsService';
2020
import { TabsTitleControl } from 'vs/workbench/browser/parts/editor/tabsTitleControl';
2121
import { EditorControl } from 'vs/workbench/browser/parts/editor/editorControl';
2222
import { IEditorProgressService } from 'vs/platform/progress/common/progress';
@@ -863,15 +863,15 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
863863

864864
//#region openEditor()
865865

866-
async openEditor(editor: EditorInput, options?: EditorOptions): Promise<IEditorPane | null> {
866+
async openEditor(editor: EditorInput, options?: EditorOptions, context?: OpenEditorInGroupContext): Promise<IEditorPane | null> {
867867

868868
// Guard against invalid inputs
869869
if (!editor) {
870870
return null;
871871
}
872872

873873
// Editor opening event allows for prevention
874-
const event = new EditorOpeningEvent(this._group.id, editor, options);
874+
const event = new EditorOpeningEvent(this._group.id, editor, options, context);
875875
this._onWillOpenEditor.fire(event);
876876
const prevented = event.isPrevented();
877877
if (prevented) {
@@ -1168,7 +1168,6 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
11681168
...moveOptions,
11691169
pinned: true, // always pin moved editor
11701170
sticky: this._group.isSticky(editor), // preserve sticky state,
1171-
shouldCreateNewWhenOverride: keepCopy
11721171
}));
11731172

11741173
// A move to another group is an open first...
@@ -1718,7 +1717,8 @@ class EditorOpeningEvent implements IEditorOpeningEvent {
17181717
constructor(
17191718
private _group: GroupIdentifier,
17201719
private _editor: EditorInput,
1721-
private _options: EditorOptions | undefined
1720+
private _options: EditorOptions | undefined,
1721+
private _context: OpenEditorInGroupContext | undefined
17221722
) {
17231723
}
17241724

@@ -1734,6 +1734,10 @@ class EditorOpeningEvent implements IEditorOpeningEvent {
17341734
return this._options;
17351735
}
17361736

1737+
get context(): OpenEditorInGroupContext | undefined {
1738+
return this._context;
1739+
}
1740+
17371741
prevent(callback: () => Promise<IEditorPane | undefined>): void {
17381742
this.override = callback;
17391743
}

src/vs/workbench/common/editor.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,12 +1087,6 @@ export class EditorOptions implements IEditorOptions {
10871087
*/
10881088
ignoreOverrides: boolean | undefined;
10891089

1090-
/**
1091-
* When editor overrides are used while opening the editor, the original editor input
1092-
* will be closed after the new editor is opened, if `shouldCreateNewWhenOverride` is true.
1093-
*/
1094-
shouldCreateNewWhenOverride: boolean | undefined;
1095-
10961090
/**
10971091
* A optional hint to signal in which context the editor opens.
10981092
*
@@ -1145,10 +1139,6 @@ export class EditorOptions implements IEditorOptions {
11451139
this.ignoreError = options.ignoreError;
11461140
}
11471141

1148-
if (typeof options.shouldCreateNewWhenOverride === 'boolean') {
1149-
this.shouldCreateNewWhenOverride = options.shouldCreateNewWhenOverride;
1150-
}
1151-
11521142
if (typeof options.index === 'number') {
11531143
this.index = options.index;
11541144
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ export class CustomEditorContribution extends Disposable implements IWorkbenchCo
428428
super();
429429

430430
this._register(this.editorService.overrideOpenEditor({
431-
open: (editor, options, group, id) => {
431+
open: (editor, options, group, context, id) => {
432432
return this.onEditorOpening(editor, options, group, id);
433433
},
434434
getEditorOverrides: (resource: URI, _options: IEditorOptions | undefined, group: IEditorGroup | undefined): IOpenEditorOverrideEntry[] => {

src/vs/workbench/contrib/files/browser/fileActions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { sequence, timeout } from 'vs/base/common/async';
4848
import { IWorkingCopyFileService } from 'vs/workbench/services/workingCopy/common/workingCopyFileService';
4949
import { once } from 'vs/base/common/functional';
5050
import { IEditorOptions } from 'vs/platform/editor/common/editor';
51-
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
51+
import { IEditorGroup, OpenEditorInGroupContext } from 'vs/workbench/services/editor/common/editorGroupsService';
5252
import { Codicon } from 'vs/base/common/codicons';
5353
import { IViewsService } from 'vs/workbench/common/views';
5454
import { openEditorWith, getAllAvailableEditors } from 'vs/workbench/contrib/files/common/openWith';
@@ -576,7 +576,7 @@ export class ToggleEditorTypeCommand extends Action {
576576
return;
577577
}
578578

579-
await firstNonActiveOverride[0].open(input, options, group, firstNonActiveOverride[1].id)?.override;
579+
await firstNonActiveOverride[0].open(input, options, group, OpenEditorInGroupContext.NEW_EDITOR, firstNonActiveOverride[1].id)?.override;
580580
}
581581
}
582582

src/vs/workbench/contrib/files/common/openWith.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { IEditorInput, IEditorPane } from 'vs/workbench/common/editor';
1313
import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput';
1414
import { DEFAULT_EDITOR_ID } from 'vs/workbench/contrib/files/common/files';
1515
import { CustomEditorAssociation, CustomEditorsAssociations, customEditorsAssociationsSettingId } from 'vs/workbench/services/editor/common/editorAssociationsSetting';
16-
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
16+
import { IEditorGroup, OpenEditorInGroupContext } from 'vs/workbench/services/editor/common/editorGroupsService';
1717
import { IEditorService, IOpenEditorOverrideEntry, IOpenEditorOverrideHandler } from 'vs/workbench/services/editor/common/editorService';
1818

1919
const builtinProviderDisplayName = nls.localize('builtinProviderDisplayName', "Built-in");
@@ -45,7 +45,7 @@ export async function openEditorWith(
4545

4646
const overrideToUse = typeof id === 'string' && allEditorOverrides.find(([_, entry]) => entry.id === id);
4747
if (overrideToUse) {
48-
return overrideToUse[0].open(input, options, group, id)?.override;
48+
return overrideToUse[0].open(input, options, group, OpenEditorInGroupContext.NEW_EDITOR, id)?.override;
4949
}
5050

5151
// Prompt
@@ -108,7 +108,7 @@ export async function openEditorWith(
108108
picker.show();
109109
});
110110

111-
return pickedItem?.handler.open(input!, options, group, pickedItem.id)?.override;
111+
return pickedItem?.handler.open(input!, options, group, OpenEditorInGroupContext.NEW_EDITOR, pickedItem.id)?.override;
112112
}
113113

114114
export const defaultEditorOverrideEntry = Object.freeze({

src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookS
3030
import { NotebookService } from 'vs/workbench/contrib/notebook/browser/notebookServiceImpl';
3131
import { CellKind, CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon';
3232
import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookProvider';
33-
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
33+
import { IEditorGroup, OpenEditorInGroupContext } from 'vs/workbench/services/editor/common/editorGroupsService';
3434
import { IEditorService, IOpenEditorOverride } from 'vs/workbench/services/editor/common/editorService';
3535
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
3636
import { CustomEditorsAssociations, customEditorsAssociationsSettingId } from 'vs/workbench/services/editor/common/editorAssociationsSetting';
@@ -142,7 +142,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri
142142
};
143143
});
144144
},
145-
open: (editor, options, group, id) => this.onEditorOpening(editor, options, group, id)
145+
open: (editor, options, group, context, id) => this.onEditorOpening(editor, options, group, context, id)
146146
}));
147147

148148
this._register(this.editorService.onDidVisibleEditorsChange(() => {
@@ -200,7 +200,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri
200200
return this.notebookService.getContributedNotebookProviders(resource);
201201
}
202202

203-
private onEditorOpening(originalInput: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup, id: string | undefined): IOpenEditorOverride | undefined {
203+
private onEditorOpening(originalInput: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup, context: OpenEditorInGroupContext, id: string | undefined): IOpenEditorOverride | undefined {
204204
if (originalInput instanceof NotebookEditorInput) {
205205
if ((originalInput.group === group.id || originalInput.group === undefined) && (originalInput.viewType === id || typeof id !== 'string')) {
206206
// No need to do anything
@@ -215,7 +215,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri
215215
const copiedInput = this.instantiationService.createInstance(NotebookEditorInput, originalInput.resource, originalInput.name, originalInput.viewType);
216216
copiedInput.updateGroup(group.id);
217217

218-
if (!options?.shouldCreateNewWhenOverride) {
218+
if (context === OpenEditorInGroupContext.MOVE_EDITOR) {
219219
// transfer ownership of editor widget
220220
const widgetRef = NotebookRegistry.getNotebookEditorWidget(originalInput);
221221
if (widgetRef) {

src/vs/workbench/services/editor/browser/editorService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Event, Emitter } from 'vs/base/common/event';
1717
import { URI } from 'vs/base/common/uri';
1818
import { basename, isEqualOrParent, joinPath } from 'vs/base/common/resources';
1919
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
20-
import { IEditorGroupsService, IEditorGroup, GroupsOrder, IEditorReplacement, GroupChangeKind, preferredSideBySideGroupDirection } from 'vs/workbench/services/editor/common/editorGroupsService';
20+
import { IEditorGroupsService, IEditorGroup, GroupsOrder, IEditorReplacement, GroupChangeKind, preferredSideBySideGroupDirection, OpenEditorInGroupContext } from 'vs/workbench/services/editor/common/editorGroupsService';
2121
import { IResourceEditorInputType, SIDE_GROUP, IResourceEditorReplacement, IOpenEditorOverrideHandler, IEditorService, SIDE_GROUP_TYPE, ACTIVE_GROUP_TYPE, ISaveEditorsOptions, ISaveAllEditorsOptions, IRevertAllEditorsOptions, IBaseSaveRevertAllEditorOptions, IOpenEditorOverrideEntry, ICustomEditorViewTypesHandler, ICustomEditorInfo } from 'vs/workbench/services/editor/common/editorService';
2222
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
2323
import { Disposable, IDisposable, dispose, toDisposable, DisposableStore } from 'vs/base/common/lifecycle';
@@ -501,7 +501,7 @@ export class EditorService extends Disposable implements EditorServiceImpl {
501501
}
502502

503503
for (const handler of this.openEditorHandlers) {
504-
const result = handler.open(event.editor, event.options, group);
504+
const result = handler.open(event.editor, event.options, group, event.context || OpenEditorInGroupContext.NEW_EDITOR);
505505
const override = result?.override;
506506
if (override) {
507507
event.prevent((() => override.then(editor => withNullAsUndefined(editor))));

src/vs/workbench/services/editor/common/editorGroupsService.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ export interface IGroupChangeEvent {
369369
editorIndex?: number;
370370
}
371371

372+
export const enum OpenEditorInGroupContext {
373+
NEW_EDITOR,
374+
MOVE_EDITOR,
375+
COPY_EDITOR
376+
}
377+
372378
export interface IEditorGroup {
373379

374380
/**
@@ -463,7 +469,7 @@ export interface IEditorGroup {
463469
* @returns a promise that resolves around an IEditor instance unless
464470
* the call failed, or the editor was not opened as active editor.
465471
*/
466-
openEditor(editor: IEditorInput, options?: IEditorOptions | ITextEditorOptions): Promise<IEditorPane | null>;
472+
openEditor(editor: IEditorInput, options?: IEditorOptions | ITextEditorOptions, context?: OpenEditorInGroupContext): Promise<IEditorPane | null>;
467473

468474
/**
469475
* Opens editors in this group.

0 commit comments

Comments
 (0)