Skip to content

Commit 724b433

Browse files
committed
simplify how editor commands/actions register menu entries
1 parent 14928af commit 724b433

20 files changed

Lines changed: 116 additions & 83 deletions

File tree

src/vs/editor/browser/controller/coreCommands.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,7 @@ registerCommand(new EditorOrNativeTextInputCommand({
17531753
kbExpr: null,
17541754
primary: KeyMod.CtrlCmd | KeyCode.KEY_A
17551755
},
1756-
menubarOpts: {
1756+
menuOpts: {
17571757
menuId: MenuId.MenubarSelectionMenu,
17581758
group: '1_basic',
17591759
title: nls.localize({ key: 'miSelectAll', comment: ['&& denotes a mnemonic'] }, "&&Select All"),
@@ -1771,7 +1771,7 @@ registerCommand(new EditorOrNativeTextInputCommand({
17711771
kbExpr: EditorContextKeys.textInputFocus,
17721772
primary: KeyMod.CtrlCmd | KeyCode.KEY_Z
17731773
},
1774-
menubarOpts: {
1774+
menuOpts: {
17751775
menuId: MenuId.MenubarEditMenu,
17761776
group: '1_do',
17771777
title: nls.localize({ key: 'miUndo', comment: ['&& denotes a mnemonic'] }, "&&Undo"),
@@ -1792,7 +1792,7 @@ registerCommand(new EditorOrNativeTextInputCommand({
17921792
secondary: [KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_Z],
17931793
mac: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_Z }
17941794
},
1795-
menubarOpts: {
1795+
menuOpts: {
17961796
menuId: MenuId.MenubarEditMenu,
17971797
group: '1_do',
17981798
title: nls.localize({ key: 'miRedo', comment: ['&& denotes a mnemonic'] }, "&&Redo"),

src/vs/editor/browser/editorExtensions.ts

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ export interface ICommandKeybindingsOptions extends IKeybindings {
4242
kbExpr?: ContextKeyExpr | null;
4343
weight: number;
4444
}
45-
export interface ICommandMenubarOptions {
45+
export interface ICommandMenuOptions {
4646
menuId: MenuId;
47-
group: string;
48-
order: number;
47+
group?: string;
48+
order?: number;
4949
when?: ContextKeyExpr;
5050
title: string;
5151
}
@@ -54,36 +54,29 @@ export interface ICommandOptions {
5454
precondition: ContextKeyExpr | undefined;
5555
kbOpts?: ICommandKeybindingsOptions;
5656
description?: ICommandHandlerDescription;
57-
menubarOpts?: ICommandMenubarOptions;
57+
menuOpts?: ICommandMenuOptions | ICommandMenuOptions[];
5858
}
5959
export abstract class Command {
6060
public readonly id: string;
6161
public readonly precondition: ContextKeyExpr | undefined;
6262
private readonly _kbOpts: ICommandKeybindingsOptions | undefined;
63-
private readonly _menubarOpts: ICommandMenubarOptions | undefined;
63+
private readonly _menuOpts: ICommandMenuOptions | ICommandMenuOptions[] | undefined;
6464
private readonly _description: ICommandHandlerDescription | undefined;
6565

6666
constructor(opts: ICommandOptions) {
6767
this.id = opts.id;
6868
this.precondition = opts.precondition;
6969
this._kbOpts = opts.kbOpts;
70-
this._menubarOpts = opts.menubarOpts;
70+
this._menuOpts = opts.menuOpts;
7171
this._description = opts.description;
7272
}
7373

7474
public register(): void {
7575

76-
if (this._menubarOpts) {
77-
MenuRegistry.appendMenuItem(this._menubarOpts.menuId, {
78-
group: this._menubarOpts.group,
79-
command: {
80-
id: this.id,
81-
title: this._menubarOpts.title,
82-
// precondition: this.precondition
83-
},
84-
when: this._menubarOpts.when,
85-
order: this._menubarOpts.order
86-
});
76+
if (Array.isArray(this._menuOpts)) {
77+
this._menuOpts.forEach(this._registerMenuItem, this);
78+
} else if (this._menuOpts) {
79+
this._registerMenuItem(this._menuOpts);
8780
}
8881

8982
if (this._kbOpts) {
@@ -119,6 +112,19 @@ export abstract class Command {
119112
}
120113
}
121114

115+
private _registerMenuItem(item: ICommandMenuOptions): void {
116+
MenuRegistry.appendMenuItem(item.menuId, {
117+
group: item.group,
118+
command: {
119+
id: this.id,
120+
title: item.title,
121+
// precondition: this.precondition
122+
},
123+
when: item.when,
124+
order: item.order
125+
});
126+
}
127+
122128
public abstract runCommand(accessor: ServicesAccessor, args: any): void | Promise<void>;
123129
}
124130

@@ -184,44 +190,70 @@ export abstract class EditorCommand extends Command {
184190

185191
//#region EditorAction
186192

187-
export interface IEditorCommandMenuOptions {
193+
export interface IEditorActionContextMenuOptions {
188194
group: string;
189195
order: number;
190196
when?: ContextKeyExpr;
191197
}
192-
export interface IActionOptions extends ICommandOptions {
198+
export interface IActionOptions {
199+
id: string;
193200
label: string;
194201
alias: string;
195-
menuOpts?: IEditorCommandMenuOptions;
202+
description?: ICommandHandlerDescription;
203+
precondition: ContextKeyExpr | undefined;
204+
kbOpts?: ICommandKeybindingsOptions;
205+
contextMenuOpts?: IEditorActionContextMenuOptions;
206+
menuOpts?: Partial<ICommandMenuOptions> | Partial<ICommandMenuOptions[]>;
196207
}
208+
197209
export abstract class EditorAction extends EditorCommand {
198210

211+
private static convertOptions(opts: IActionOptions): ICommandOptions {
212+
213+
function patch(menu: Partial<ICommandMenuOptions>): ICommandMenuOptions {
214+
if (!menu.title) {
215+
menu.title = opts.label;
216+
}
217+
if (!menu.menuId) {
218+
menu.menuId = MenuId.EditorContext;
219+
}
220+
if (!menu.when) {
221+
menu.when = opts.precondition;
222+
}
223+
return <ICommandMenuOptions>menu;
224+
}
225+
226+
let menuOpts: ICommandMenuOptions[];
227+
if (Array.isArray(opts.menuOpts)) {
228+
menuOpts = opts.menuOpts.map(m => patch(m!));
229+
} else if (opts.menuOpts) {
230+
menuOpts = [patch(opts.menuOpts)];
231+
} else {
232+
menuOpts = [];
233+
}
234+
235+
if (opts.contextMenuOpts) {
236+
const contextMenuItem = {
237+
title: opts.label,
238+
when: ContextKeyExpr.and(opts.precondition, opts.contextMenuOpts.when),
239+
menuId: MenuId.EditorContext,
240+
group: opts.contextMenuOpts.group,
241+
order: opts.contextMenuOpts.order
242+
};
243+
menuOpts.push(contextMenuItem);
244+
}
245+
246+
opts.menuOpts = menuOpts;
247+
return <ICommandOptions>opts;
248+
}
249+
199250
public readonly label: string;
200251
public readonly alias: string;
201-
private readonly menuOpts: IEditorCommandMenuOptions | undefined;
202252

203253
constructor(opts: IActionOptions) {
204-
super(opts);
254+
super(EditorAction.convertOptions(opts));
205255
this.label = opts.label;
206256
this.alias = opts.alias;
207-
this.menuOpts = opts.menuOpts;
208-
}
209-
210-
public register(): void {
211-
212-
if (this.menuOpts) {
213-
MenuRegistry.appendMenuItem(MenuId.EditorContext, {
214-
command: {
215-
id: this.id,
216-
title: this.label
217-
},
218-
when: ContextKeyExpr.and(this.precondition, this.menuOpts.when),
219-
group: this.menuOpts.group,
220-
order: this.menuOpts.order
221-
});
222-
}
223-
224-
super.register();
225257
}
226258

227259
public runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | Promise<void> {

src/vs/editor/contrib/clipboard/clipboard.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ class ExecCommandCutAction extends ExecCommandAction {
7777
alias: 'Cut',
7878
precondition: EditorContextKeys.writable,
7979
kbOpts: kbOpts,
80-
menuOpts: {
80+
contextMenuOpts: {
8181
group: CLIPBOARD_CONTEXT_MENU_GROUP,
8282
order: 1
8383
},
84-
menubarOpts: {
84+
menuOpts: {
8585
menuId: MenuId.MenubarEditMenu,
8686
group: '2_ccp',
8787
title: nls.localize({ key: 'miCut', comment: ['&& denotes a mnemonic'] }, "Cu&&t"),
@@ -126,11 +126,11 @@ class ExecCommandCopyAction extends ExecCommandAction {
126126
alias: 'Copy',
127127
precondition: undefined,
128128
kbOpts: kbOpts,
129-
menuOpts: {
129+
contextMenuOpts: {
130130
group: CLIPBOARD_CONTEXT_MENU_GROUP,
131131
order: 2
132132
},
133-
menubarOpts: {
133+
menuOpts: {
134134
menuId: MenuId.MenubarEditMenu,
135135
group: '2_ccp',
136136
title: nls.localize({ key: 'miCopy', comment: ['&& denotes a mnemonic'] }, "&&Copy"),
@@ -175,11 +175,11 @@ class ExecCommandPasteAction extends ExecCommandAction {
175175
alias: 'Paste',
176176
precondition: EditorContextKeys.writable,
177177
kbOpts: kbOpts,
178-
menuOpts: {
178+
contextMenuOpts: {
179179
group: CLIPBOARD_CONTEXT_MENU_GROUP,
180180
order: 3
181181
},
182-
menubarOpts: {
182+
menuOpts: {
183183
menuId: MenuId.MenubarEditMenu,
184184
group: '2_ccp',
185185
title: nls.localize({ key: 'miPaste', comment: ['&& denotes a mnemonic'] }, "&&Paste"),

src/vs/editor/contrib/codeAction/codeActionCommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export class RefactorAction extends EditorAction {
265265
},
266266
weight: KeybindingWeight.EditorContrib
267267
},
268-
menuOpts: {
268+
contextMenuOpts: {
269269
group: '1_modification',
270270
order: 2,
271271
when: ContextKeyExpr.and(
@@ -308,7 +308,7 @@ export class SourceAction extends EditorAction {
308308
label: nls.localize('source.label', "Source Action..."),
309309
alias: 'Source Action...',
310310
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasCodeActionsProvider),
311-
menuOpts: {
311+
contextMenuOpts: {
312312
group: '1_modification',
313313
order: 2.1,
314314
when: ContextKeyExpr.and(

src/vs/editor/contrib/comment/comment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ToggleCommentLineAction extends CommentLineAction {
5656
primary: KeyMod.CtrlCmd | KeyCode.US_SLASH,
5757
weight: KeybindingWeight.EditorContrib
5858
},
59-
menubarOpts: {
59+
menuOpts: {
6060
menuId: MenuId.MenubarEditMenu,
6161
group: '5_insert',
6262
title: nls.localize({ key: 'miToggleLineComment', comment: ['&& denotes a mnemonic'] }, "&&Toggle Line Comment"),
@@ -112,7 +112,7 @@ class BlockCommentAction extends EditorAction {
112112
linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_A },
113113
weight: KeybindingWeight.EditorContrib
114114
},
115-
menubarOpts: {
115+
menuOpts: {
116116
menuId: MenuId.MenubarEditMenu,
117117
group: '5_insert',
118118
title: nls.localize({ key: 'miToggleBlockComment', comment: ['&& denotes a mnemonic'] }, "Toggle &&Block Comment"),

src/vs/editor/contrib/find/findController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ export class StartFindAction extends EditorAction {
455455
primary: KeyMod.CtrlCmd | KeyCode.KEY_F,
456456
weight: KeybindingWeight.EditorContrib
457457
},
458-
menubarOpts: {
458+
menuOpts: {
459459
menuId: MenuId.MenubarEditMenu,
460460
group: '3_find',
461461
title: nls.localize({ key: 'miFind', comment: ['&& denotes a mnemonic'] }, "&&Find"),
@@ -701,7 +701,7 @@ export class StartFindReplaceAction extends EditorAction {
701701
mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_F },
702702
weight: KeybindingWeight.EditorContrib
703703
},
704-
menubarOpts: {
704+
menuOpts: {
705705
menuId: MenuId.MenubarEditMenu,
706706
group: '3_find',
707707
title: nls.localize({ key: 'miReplace', comment: ['&& denotes a mnemonic'] }, "&&Replace"),

src/vs/editor/contrib/format/formatActions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class FormatDocumentAction extends EditorAction {
219219
linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_I },
220220
weight: KeybindingWeight.EditorContrib
221221
},
222-
menuOpts: {
222+
contextMenuOpts: {
223223
when: EditorContextKeys.hasDocumentFormattingProvider,
224224
group: '1_modification',
225225
order: 1.3
@@ -248,7 +248,7 @@ class FormatSelectionAction extends EditorAction {
248248
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_F),
249249
weight: KeybindingWeight.EditorContrib
250250
},
251-
menuOpts: {
251+
contextMenuOpts: {
252252
when: ContextKeyExpr.and(EditorContextKeys.hasDocumentSelectionFormattingProvider, EditorContextKeys.hasNonEmptySelection),
253253
group: '1_modification',
254254
order: 1.31

src/vs/editor/contrib/linesOperations/linesOperations.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CopyLinesUpAction extends AbstractCopyLinesAction {
6464
linux: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyMod.Shift | KeyCode.UpArrow },
6565
weight: KeybindingWeight.EditorContrib
6666
},
67-
menubarOpts: {
67+
menuOpts: {
6868
menuId: MenuId.MenubarSelectionMenu,
6969
group: '2_line',
7070
title: nls.localize({ key: 'miCopyLinesUp', comment: ['&& denotes a mnemonic'] }, "&&Copy Line Up"),
@@ -87,7 +87,7 @@ class CopyLinesDownAction extends AbstractCopyLinesAction {
8787
linux: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyMod.Shift | KeyCode.DownArrow },
8888
weight: KeybindingWeight.EditorContrib
8989
},
90-
menubarOpts: {
90+
menuOpts: {
9191
menuId: MenuId.MenubarSelectionMenu,
9292
group: '2_line',
9393
title: nls.localize({ key: 'miCopyLinesDown', comment: ['&& denotes a mnemonic'] }, "Co&&py Line Down"),
@@ -105,7 +105,7 @@ export class DuplicateSelectionAction extends EditorAction {
105105
label: nls.localize('duplicateSelection', "Duplicate Selection"),
106106
alias: 'Duplicate Selection',
107107
precondition: EditorContextKeys.writable,
108-
menubarOpts: {
108+
menuOpts: {
109109
menuId: MenuId.MenubarSelectionMenu,
110110
group: '2_line',
111111
title: nls.localize({ key: 'miDuplicateSelection', comment: ['&& denotes a mnemonic'] }, "&&Duplicate Selection"),
@@ -178,7 +178,7 @@ class MoveLinesUpAction extends AbstractMoveLinesAction {
178178
linux: { primary: KeyMod.Alt | KeyCode.UpArrow },
179179
weight: KeybindingWeight.EditorContrib
180180
},
181-
menubarOpts: {
181+
menuOpts: {
182182
menuId: MenuId.MenubarSelectionMenu,
183183
group: '2_line',
184184
title: nls.localize({ key: 'miMoveLinesUp', comment: ['&& denotes a mnemonic'] }, "Mo&&ve Line Up"),
@@ -201,7 +201,7 @@ class MoveLinesDownAction extends AbstractMoveLinesAction {
201201
linux: { primary: KeyMod.Alt | KeyCode.DownArrow },
202202
weight: KeybindingWeight.EditorContrib
203203
},
204-
menubarOpts: {
204+
menuOpts: {
205205
menuId: MenuId.MenubarSelectionMenu,
206206
group: '2_line',
207207
title: nls.localize({ key: 'miMoveLinesDown', comment: ['&& denotes a mnemonic'] }, "Move &&Line Down"),

0 commit comments

Comments
 (0)