提交 724b433e 编写于 作者: J Johannes Rieken

simplify how editor commands/actions register menu entries

上级 14928afa
......@@ -1753,7 +1753,7 @@ registerCommand(new EditorOrNativeTextInputCommand({
kbExpr: null,
primary: KeyMod.CtrlCmd | KeyCode.KEY_A
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '1_basic',
title: nls.localize({ key: 'miSelectAll', comment: ['&& denotes a mnemonic'] }, "&&Select All"),
......@@ -1771,7 +1771,7 @@ registerCommand(new EditorOrNativeTextInputCommand({
kbExpr: EditorContextKeys.textInputFocus,
primary: KeyMod.CtrlCmd | KeyCode.KEY_Z
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '1_do',
title: nls.localize({ key: 'miUndo', comment: ['&& denotes a mnemonic'] }, "&&Undo"),
......@@ -1792,7 +1792,7 @@ registerCommand(new EditorOrNativeTextInputCommand({
secondary: [KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_Z],
mac: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_Z }
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '1_do',
title: nls.localize({ key: 'miRedo', comment: ['&& denotes a mnemonic'] }, "&&Redo"),
......
......@@ -42,10 +42,10 @@ export interface ICommandKeybindingsOptions extends IKeybindings {
kbExpr?: ContextKeyExpr | null;
weight: number;
}
export interface ICommandMenubarOptions {
export interface ICommandMenuOptions {
menuId: MenuId;
group: string;
order: number;
group?: string;
order?: number;
when?: ContextKeyExpr;
title: string;
}
......@@ -54,36 +54,29 @@ export interface ICommandOptions {
precondition: ContextKeyExpr | undefined;
kbOpts?: ICommandKeybindingsOptions;
description?: ICommandHandlerDescription;
menubarOpts?: ICommandMenubarOptions;
menuOpts?: ICommandMenuOptions | ICommandMenuOptions[];
}
export abstract class Command {
public readonly id: string;
public readonly precondition: ContextKeyExpr | undefined;
private readonly _kbOpts: ICommandKeybindingsOptions | undefined;
private readonly _menubarOpts: ICommandMenubarOptions | undefined;
private readonly _menuOpts: ICommandMenuOptions | ICommandMenuOptions[] | undefined;
private readonly _description: ICommandHandlerDescription | undefined;
constructor(opts: ICommandOptions) {
this.id = opts.id;
this.precondition = opts.precondition;
this._kbOpts = opts.kbOpts;
this._menubarOpts = opts.menubarOpts;
this._menuOpts = opts.menuOpts;
this._description = opts.description;
}
public register(): void {
if (this._menubarOpts) {
MenuRegistry.appendMenuItem(this._menubarOpts.menuId, {
group: this._menubarOpts.group,
command: {
id: this.id,
title: this._menubarOpts.title,
// precondition: this.precondition
},
when: this._menubarOpts.when,
order: this._menubarOpts.order
});
if (Array.isArray(this._menuOpts)) {
this._menuOpts.forEach(this._registerMenuItem, this);
} else if (this._menuOpts) {
this._registerMenuItem(this._menuOpts);
}
if (this._kbOpts) {
......@@ -119,6 +112,19 @@ export abstract class Command {
}
}
private _registerMenuItem(item: ICommandMenuOptions): void {
MenuRegistry.appendMenuItem(item.menuId, {
group: item.group,
command: {
id: this.id,
title: item.title,
// precondition: this.precondition
},
when: item.when,
order: item.order
});
}
public abstract runCommand(accessor: ServicesAccessor, args: any): void | Promise<void>;
}
......@@ -184,44 +190,70 @@ export abstract class EditorCommand extends Command {
//#region EditorAction
export interface IEditorCommandMenuOptions {
export interface IEditorActionContextMenuOptions {
group: string;
order: number;
when?: ContextKeyExpr;
}
export interface IActionOptions extends ICommandOptions {
export interface IActionOptions {
id: string;
label: string;
alias: string;
menuOpts?: IEditorCommandMenuOptions;
description?: ICommandHandlerDescription;
precondition: ContextKeyExpr | undefined;
kbOpts?: ICommandKeybindingsOptions;
contextMenuOpts?: IEditorActionContextMenuOptions;
menuOpts?: Partial<ICommandMenuOptions> | Partial<ICommandMenuOptions[]>;
}
export abstract class EditorAction extends EditorCommand {
private static convertOptions(opts: IActionOptions): ICommandOptions {
function patch(menu: Partial<ICommandMenuOptions>): ICommandMenuOptions {
if (!menu.title) {
menu.title = opts.label;
}
if (!menu.menuId) {
menu.menuId = MenuId.EditorContext;
}
if (!menu.when) {
menu.when = opts.precondition;
}
return <ICommandMenuOptions>menu;
}
let menuOpts: ICommandMenuOptions[];
if (Array.isArray(opts.menuOpts)) {
menuOpts = opts.menuOpts.map(m => patch(m!));
} else if (opts.menuOpts) {
menuOpts = [patch(opts.menuOpts)];
} else {
menuOpts = [];
}
if (opts.contextMenuOpts) {
const contextMenuItem = {
title: opts.label,
when: ContextKeyExpr.and(opts.precondition, opts.contextMenuOpts.when),
menuId: MenuId.EditorContext,
group: opts.contextMenuOpts.group,
order: opts.contextMenuOpts.order
};
menuOpts.push(contextMenuItem);
}
opts.menuOpts = menuOpts;
return <ICommandOptions>opts;
}
public readonly label: string;
public readonly alias: string;
private readonly menuOpts: IEditorCommandMenuOptions | undefined;
constructor(opts: IActionOptions) {
super(opts);
super(EditorAction.convertOptions(opts));
this.label = opts.label;
this.alias = opts.alias;
this.menuOpts = opts.menuOpts;
}
public register(): void {
if (this.menuOpts) {
MenuRegistry.appendMenuItem(MenuId.EditorContext, {
command: {
id: this.id,
title: this.label
},
when: ContextKeyExpr.and(this.precondition, this.menuOpts.when),
group: this.menuOpts.group,
order: this.menuOpts.order
});
}
super.register();
}
public runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | Promise<void> {
......
......@@ -77,11 +77,11 @@ class ExecCommandCutAction extends ExecCommandAction {
alias: 'Cut',
precondition: EditorContextKeys.writable,
kbOpts: kbOpts,
menuOpts: {
contextMenuOpts: {
group: CLIPBOARD_CONTEXT_MENU_GROUP,
order: 1
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '2_ccp',
title: nls.localize({ key: 'miCut', comment: ['&& denotes a mnemonic'] }, "Cu&&t"),
......@@ -126,11 +126,11 @@ class ExecCommandCopyAction extends ExecCommandAction {
alias: 'Copy',
precondition: undefined,
kbOpts: kbOpts,
menuOpts: {
contextMenuOpts: {
group: CLIPBOARD_CONTEXT_MENU_GROUP,
order: 2
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '2_ccp',
title: nls.localize({ key: 'miCopy', comment: ['&& denotes a mnemonic'] }, "&&Copy"),
......@@ -175,11 +175,11 @@ class ExecCommandPasteAction extends ExecCommandAction {
alias: 'Paste',
precondition: EditorContextKeys.writable,
kbOpts: kbOpts,
menuOpts: {
contextMenuOpts: {
group: CLIPBOARD_CONTEXT_MENU_GROUP,
order: 3
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '2_ccp',
title: nls.localize({ key: 'miPaste', comment: ['&& denotes a mnemonic'] }, "&&Paste"),
......
......@@ -265,7 +265,7 @@ export class RefactorAction extends EditorAction {
},
weight: KeybindingWeight.EditorContrib
},
menuOpts: {
contextMenuOpts: {
group: '1_modification',
order: 2,
when: ContextKeyExpr.and(
......@@ -308,7 +308,7 @@ export class SourceAction extends EditorAction {
label: nls.localize('source.label', "Source Action..."),
alias: 'Source Action...',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasCodeActionsProvider),
menuOpts: {
contextMenuOpts: {
group: '1_modification',
order: 2.1,
when: ContextKeyExpr.and(
......
......@@ -56,7 +56,7 @@ class ToggleCommentLineAction extends CommentLineAction {
primary: KeyMod.CtrlCmd | KeyCode.US_SLASH,
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '5_insert',
title: nls.localize({ key: 'miToggleLineComment', comment: ['&& denotes a mnemonic'] }, "&&Toggle Line Comment"),
......@@ -112,7 +112,7 @@ class BlockCommentAction extends EditorAction {
linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_A },
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '5_insert',
title: nls.localize({ key: 'miToggleBlockComment', comment: ['&& denotes a mnemonic'] }, "Toggle &&Block Comment"),
......
......@@ -455,7 +455,7 @@ export class StartFindAction extends EditorAction {
primary: KeyMod.CtrlCmd | KeyCode.KEY_F,
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '3_find',
title: nls.localize({ key: 'miFind', comment: ['&& denotes a mnemonic'] }, "&&Find"),
......@@ -701,7 +701,7 @@ export class StartFindReplaceAction extends EditorAction {
mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_F },
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '3_find',
title: nls.localize({ key: 'miReplace', comment: ['&& denotes a mnemonic'] }, "&&Replace"),
......
......@@ -219,7 +219,7 @@ class FormatDocumentAction extends EditorAction {
linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_I },
weight: KeybindingWeight.EditorContrib
},
menuOpts: {
contextMenuOpts: {
when: EditorContextKeys.hasDocumentFormattingProvider,
group: '1_modification',
order: 1.3
......@@ -248,7 +248,7 @@ class FormatSelectionAction extends EditorAction {
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_F),
weight: KeybindingWeight.EditorContrib
},
menuOpts: {
contextMenuOpts: {
when: ContextKeyExpr.and(EditorContextKeys.hasDocumentSelectionFormattingProvider, EditorContextKeys.hasNonEmptySelection),
group: '1_modification',
order: 1.31
......
......@@ -64,7 +64,7 @@ class CopyLinesUpAction extends AbstractCopyLinesAction {
linux: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyMod.Shift | KeyCode.UpArrow },
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '2_line',
title: nls.localize({ key: 'miCopyLinesUp', comment: ['&& denotes a mnemonic'] }, "&&Copy Line Up"),
......@@ -87,7 +87,7 @@ class CopyLinesDownAction extends AbstractCopyLinesAction {
linux: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyMod.Shift | KeyCode.DownArrow },
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '2_line',
title: nls.localize({ key: 'miCopyLinesDown', comment: ['&& denotes a mnemonic'] }, "Co&&py Line Down"),
......@@ -105,7 +105,7 @@ export class DuplicateSelectionAction extends EditorAction {
label: nls.localize('duplicateSelection', "Duplicate Selection"),
alias: 'Duplicate Selection',
precondition: EditorContextKeys.writable,
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '2_line',
title: nls.localize({ key: 'miDuplicateSelection', comment: ['&& denotes a mnemonic'] }, "&&Duplicate Selection"),
......@@ -178,7 +178,7 @@ class MoveLinesUpAction extends AbstractMoveLinesAction {
linux: { primary: KeyMod.Alt | KeyCode.UpArrow },
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '2_line',
title: nls.localize({ key: 'miMoveLinesUp', comment: ['&& denotes a mnemonic'] }, "Mo&&ve Line Up"),
......@@ -201,7 +201,7 @@ class MoveLinesDownAction extends AbstractMoveLinesAction {
linux: { primary: KeyMod.Alt | KeyCode.DownArrow },
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '2_line',
title: nls.localize({ key: 'miMoveLinesDown', comment: ['&& denotes a mnemonic'] }, "Move &&Line Down"),
......
......@@ -46,7 +46,7 @@ export class InsertCursorAbove extends EditorAction {
},
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '3_multi',
title: nls.localize({ key: 'miInsertCursorAbove', comment: ['&& denotes a mnemonic'] }, "&&Add Cursor Above"),
......@@ -95,7 +95,7 @@ export class InsertCursorBelow extends EditorAction {
},
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '3_multi',
title: nls.localize({ key: 'miInsertCursorBelow', comment: ['&& denotes a mnemonic'] }, "A&&dd Cursor Below"),
......@@ -140,7 +140,7 @@ class InsertCursorAtEndOfEachLineSelected extends EditorAction {
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_I,
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '3_multi',
title: nls.localize({ key: 'miInsertCursorAtEndOfEachLineSelected', comment: ['&& denotes a mnemonic'] }, "Add C&&ursors to Line Ends"),
......@@ -662,7 +662,7 @@ export class AddSelectionToNextFindMatchAction extends MultiCursorSelectionContr
primary: KeyMod.CtrlCmd | KeyCode.KEY_D,
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '3_multi',
title: nls.localize({ key: 'miAddSelectionToNextFindMatch', comment: ['&& denotes a mnemonic'] }, "Add &&Next Occurrence"),
......@@ -682,7 +682,7 @@ export class AddSelectionToPreviousFindMatchAction extends MultiCursorSelectionC
label: nls.localize('addSelectionToPreviousFindMatch', "Add Selection To Previous Find Match"),
alias: 'Add Selection To Previous Find Match',
precondition: undefined,
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '3_multi',
title: nls.localize({ key: 'miAddSelectionToPreviousFindMatch', comment: ['&& denotes a mnemonic'] }, "Add P&&revious Occurrence"),
......@@ -740,7 +740,7 @@ export class SelectHighlightsAction extends MultiCursorSelectionControllerAction
primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_L,
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '3_multi',
title: nls.localize({ key: 'miSelectHighlights', comment: ['&& denotes a mnemonic'] }, "Select All &&Occurrences"),
......@@ -765,7 +765,7 @@ export class CompatChangeAll extends MultiCursorSelectionControllerAction {
primary: KeyMod.CtrlCmd | KeyCode.F2,
weight: KeybindingWeight.EditorContrib
},
menuOpts: {
contextMenuOpts: {
group: '1_modification',
order: 1.2
}
......
......@@ -238,7 +238,7 @@ export class RenameAction extends EditorAction {
primary: KeyCode.F2,
weight: KeybindingWeight.EditorContrib
},
menuOpts: {
contextMenuOpts: {
group: '1_modification',
order: 1.1
}
......
......@@ -167,7 +167,7 @@ class GrowSelectionAction extends AbstractSmartSelect {
},
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '1_basic',
title: nls.localize({ key: 'miSmartSelectGrow', comment: ['&& denotes a mnemonic'] }, "&&Expand Selection"),
......@@ -196,7 +196,7 @@ class ShrinkSelectionAction extends AbstractSmartSelect {
},
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarSelectionMenu,
group: '1_basic',
title: nls.localize({ key: 'miSmartSelectShrink', comment: ['&& denotes a mnemonic'] }, "&&Shrink Selection"),
......
......@@ -88,7 +88,7 @@ export class QuickCommandAction extends BaseEditorQuickOpenAction {
primary: (browser.isIE ? KeyMod.Alt | KeyCode.F1 : KeyCode.F1),
weight: KeybindingWeight.EditorContrib
},
menuOpts: {
contextMenuOpts: {
group: 'z_commands',
order: 1
}
......
......@@ -121,7 +121,7 @@ export class QuickOutlineAction extends BaseEditorQuickOpenAction {
primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_O,
weight: KeybindingWeight.EditorContrib
},
menuOpts: {
contextMenuOpts: {
group: 'navigation',
order: 3
}
......
......@@ -65,6 +65,7 @@ export const enum MenuId {
DebugWatchContext,
DebugToolBar,
EditorContext,
EditorContextPeek,
EditorTitle,
EditorTitleContext,
EmptyEditorGroupContext,
......
......@@ -160,7 +160,7 @@ registerEditorAction(class extends EditorAction {
id: 'editor.showCallHierarchy',
label: localize('title', "Peek Call Hierarchy"),
alias: 'Peek Call Hierarchy',
menuOpts: {
contextMenuOpts: {
group: 'navigation',
order: 1.48
},
......
......@@ -111,7 +111,7 @@ export class RunToCursorAction extends EditorAction {
label: RunToCursorAction.LABEL,
alias: 'Debug: Run to Cursor',
precondition: ContextKeyExpr.and(CONTEXT_IN_DEBUG_MODE, PanelFocusContext.toNegated(), CONTEXT_DEBUG_STATE.isEqualTo('stopped'), EditorContextKeys.editorTextFocus),
menuOpts: {
contextMenuOpts: {
group: 'debug',
order: 2
}
......@@ -160,7 +160,7 @@ class SelectionToReplAction extends EditorAction {
label: nls.localize('debugEvaluate', "Debug: Evaluate"),
alias: 'Debug: Evaluate',
precondition: ContextKeyExpr.and(EditorContextKeys.hasNonEmptySelection, CONTEXT_IN_DEBUG_MODE, EditorContextKeys.editorTextFocus),
menuOpts: {
contextMenuOpts: {
group: 'debug',
order: 0
}
......@@ -190,7 +190,7 @@ class SelectionToWatchExpressionsAction extends EditorAction {
label: nls.localize('debugAddToWatch', "Debug: Add to Watch"),
alias: 'Debug: Add to Watch',
precondition: ContextKeyExpr.and(EditorContextKeys.hasNonEmptySelection, CONTEXT_IN_DEBUG_MODE, EditorContextKeys.editorTextFocus),
menuOpts: {
contextMenuOpts: {
group: 'debug',
order: 1
}
......
......@@ -29,7 +29,7 @@ class ExpandAbbreviationAction extends EmmetEditorAction {
),
weight: KeybindingWeight.EditorContrib
},
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '5_insert',
title: nls.localize({ key: 'miEmmetExpandAbbreviation', comment: ['&& denotes a mnemonic'] }, "Emmet: E&&xpand Abbreviation"),
......
......@@ -21,7 +21,7 @@ class ShowEmmetCommandsAction extends EditorAction {
label: nls.localize('showEmmetCommands', "Show Emmet Commands"),
alias: 'Show Emmet Commands',
precondition: EditorContextKeys.writable,
menubarOpts: {
menuOpts: {
menuId: MenuId.MenubarEditMenu,
group: '5_insert',
title: nls.localize({ key: 'miShowEmmetCommands', comment: ['&& denotes a mnemonic'] }, "E&&mmet..."),
......
......@@ -253,7 +253,7 @@ registerEditorAction(class FormatDocumentMultipleAction extends EditorAction {
label: nls.localize('formatDocument.label.multiple', "Format Document With..."),
alias: 'Format Document...',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasMultipleDocumentFormattingProvider),
menuOpts: {
contextMenuOpts: {
group: '1_modification',
order: 1.3
}
......@@ -284,7 +284,7 @@ registerEditorAction(class FormatSelectionMultipleAction extends EditorAction {
label: nls.localize('formatSelection.label.multiple', "Format Selection With..."),
alias: 'Format Code...',
precondition: ContextKeyExpr.and(ContextKeyExpr.and(EditorContextKeys.writable), EditorContextKeys.hasMultipleDocumentSelectionFormattingProvider),
menuOpts: {
contextMenuOpts: {
when: ContextKeyExpr.and(EditorContextKeys.hasNonEmptySelection),
group: '1_modification',
order: 1.31
......
......@@ -214,7 +214,7 @@ class CommandPaletteEditorAction extends EditorAction {
label: localize('showCommands.label', "Command Palette..."),
alias: 'Command Palette',
precondition: undefined,
menuOpts: {
contextMenuOpts: {
group: 'z_commands',
order: 1
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册