Skip to content

Commit 2b0800a

Browse files
committed
Simplify Command registration
1 parent 403a586 commit 2b0800a

5 files changed

Lines changed: 40 additions & 26 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,7 @@ function findFocusedEditor(accessor: ServicesAccessor): ICodeEditor {
16201620
}
16211621

16221622
function registerCommand(command: Command) {
1623-
KeybindingsRegistry.registerCommandAndKeybindingRule(command.toCommandAndKeybindingRule(CORE_WEIGHT));
1623+
command.register(CORE_WEIGHT);
16241624
}
16251625

16261626
/**

src/vs/editor/browser/editorExtensions.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ export interface ICommandKeybindingsOptions extends IKeybindings {
3131
kbExpr?: ContextKeyExpr;
3232
weight?: number;
3333
}
34+
// export interface ICommandMenubarOptions {
35+
// group?: string;
36+
// order?: number;
37+
// when?: ContextKeyExpr;
38+
// title?: string;
39+
// }
3440
export interface ICommandOptions {
3541
id: string;
3642
precondition: ContextKeyExpr;
3743
kbOpts?: ICommandKeybindingsOptions;
3844
description?: ICommandHandlerDescription;
45+
// menubarOpts?: ICommandMenubarOptions;
3946
}
4047
export abstract class Command {
4148
public readonly id: string;
@@ -50,7 +57,7 @@ export abstract class Command {
5057
this._description = opts.description;
5158
}
5259

53-
public toCommandAndKeybindingRule(defaultWeight: number): ICommandAndKeybindingRule {
60+
private _toCommandAndKeybindingRule(defaultWeight: number): ICommandAndKeybindingRule {
5461
const kbOpts = this._kbOpts || { primary: 0 };
5562

5663
let kbWhen = kbOpts.kbExpr;
@@ -78,6 +85,10 @@ export abstract class Command {
7885
};
7986
}
8087

88+
public register(defaultWeight: number): void {
89+
KeybindingsRegistry.registerCommandAndKeybindingRule(this._toCommandAndKeybindingRule(defaultWeight));
90+
}
91+
8192
public abstract runCommand(accessor: ServicesAccessor, args: any): void | TPromise<void>;
8293
}
8394

@@ -166,7 +177,7 @@ export abstract class EditorAction extends EditorCommand {
166177
this.menuOpts = opts.menuOpts;
167178
}
168179

169-
public toMenuItem(): IMenuItem {
180+
private _toMenuItem(): IMenuItem {
170181
if (!this.menuOpts) {
171182
return null;
172183
}
@@ -182,6 +193,16 @@ export abstract class EditorAction extends EditorCommand {
182193
};
183194
}
184195

196+
public register(defaultWeight: number): void {
197+
198+
let menuItem = this._toMenuItem();
199+
if (menuItem) {
200+
MenuRegistry.appendMenuItem(MenuId.EditorContext, menuItem);
201+
}
202+
203+
super.register(defaultWeight);
204+
}
205+
185206
public runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | TPromise<void> {
186207
this.reportTelemetry(accessor, editor);
187208
return this.run(accessor, editor, args || {});
@@ -295,14 +316,7 @@ class EditorContributionRegistry {
295316
}
296317

297318
public registerEditorAction(action: EditorAction) {
298-
299-
let menuItem = action.toMenuItem();
300-
if (menuItem) {
301-
MenuRegistry.appendMenuItem(MenuId.EditorContext, menuItem);
302-
}
303-
304-
KeybindingsRegistry.registerCommandAndKeybindingRule(action.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
305-
319+
action.register(KeybindingsRegistry.WEIGHT.editorContrib());
306320
this.editorActions.push(action);
307321
}
308322

@@ -315,7 +329,7 @@ class EditorContributionRegistry {
315329
}
316330

317331
public registerEditorCommand(editorCommand: EditorCommand) {
318-
KeybindingsRegistry.registerCommandAndKeybindingRule(editorCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
332+
editorCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());
319333
this.editorCommands[editorCommand.id] = editorCommand;
320334
}
321335

src/vs/workbench/parts/extensions/electron-browser/extensionEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,4 +1124,4 @@ const showCommand = new ShowExtensionEditorFindCommand({
11241124
primary: KeyMod.CtrlCmd | KeyCode.KEY_F
11251125
}
11261126
});
1127-
KeybindingsRegistry.registerCommandAndKeybindingRule(showCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
1127+
showCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());

src/vs/workbench/parts/preferences/electron-browser/preferences.contribution.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ const startSearchCommand = new StartSearchDefaultSettingsCommand({
353353
precondition: ContextKeyExpr.and(CONTEXT_SETTINGS_EDITOR),
354354
kbOpts: { primary: KeyMod.CtrlCmd | KeyCode.KEY_F }
355355
});
356-
KeybindingsRegistry.registerCommandAndKeybindingRule(startSearchCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
356+
startSearchCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());
357357

358358
class FocusSearchFromSettingsCommand extends SettingsCommand {
359359

@@ -369,7 +369,7 @@ const focusSearchFromSettingsCommand = new FocusSearchFromSettingsCommand({
369369
precondition: ContextKeyExpr.and(CONTEXT_SETTINGS_EDITOR, CONTEXT_SETTINGS_FIRST_ROW_FOCUS),
370370
kbOpts: { primary: KeyCode.UpArrow }
371371
});
372-
KeybindingsRegistry.registerCommandAndKeybindingRule(focusSearchFromSettingsCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.workbenchContrib()));
372+
focusSearchFromSettingsCommand.register(KeybindingsRegistry.WEIGHT.workbenchContrib());
373373

374374

375375
class ClearSearchResultsCommand extends SettingsCommand {
@@ -386,7 +386,7 @@ const clearSearchResultsCommand = new ClearSearchResultsCommand({
386386
precondition: CONTEXT_SETTINGS_SEARCH_FOCUS,
387387
kbOpts: { primary: KeyCode.Escape }
388388
});
389-
KeybindingsRegistry.registerCommandAndKeybindingRule(clearSearchResultsCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
389+
clearSearchResultsCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());
390390

391391
class FocusSettingsFileEditorCommand extends SettingsCommand {
392392

@@ -404,14 +404,14 @@ const focusSettingsFileEditorCommand = new FocusSettingsFileEditorCommand({
404404
precondition: CONTEXT_SETTINGS_SEARCH_FOCUS,
405405
kbOpts: { primary: KeyCode.DownArrow }
406406
});
407-
KeybindingsRegistry.registerCommandAndKeybindingRule(focusSettingsFileEditorCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
407+
focusSettingsFileEditorCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());
408408

409409
const focusSettingsFromSearchCommand = new FocusSettingsFileEditorCommand({
410410
id: SETTINGS_EDITOR_COMMAND_FOCUS_SETTINGS_FROM_SEARCH,
411411
precondition: CONTEXT_SETTINGS_SEARCH_FOCUS,
412412
kbOpts: { primary: KeyCode.DownArrow }
413413
});
414-
KeybindingsRegistry.registerCommandAndKeybindingRule(focusSettingsFromSearchCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.workbenchContrib()));
414+
focusSettingsFromSearchCommand.register(KeybindingsRegistry.WEIGHT.workbenchContrib());
415415

416416
class FocusNextSearchResultCommand extends SettingsCommand {
417417

@@ -427,7 +427,7 @@ const focusNextSearchResultCommand = new FocusNextSearchResultCommand({
427427
precondition: CONTEXT_SETTINGS_SEARCH_FOCUS,
428428
kbOpts: { primary: KeyCode.Enter }
429429
});
430-
KeybindingsRegistry.registerCommandAndKeybindingRule(focusNextSearchResultCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
430+
focusNextSearchResultCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());
431431

432432
class FocusPreviousSearchResultCommand extends SettingsCommand {
433433

@@ -443,7 +443,7 @@ const focusPreviousSearchResultCommand = new FocusPreviousSearchResultCommand({
443443
precondition: CONTEXT_SETTINGS_SEARCH_FOCUS,
444444
kbOpts: { primary: KeyMod.Shift | KeyCode.Enter }
445445
});
446-
KeybindingsRegistry.registerCommandAndKeybindingRule(focusPreviousSearchResultCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
446+
focusPreviousSearchResultCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());
447447

448448
class EditFocusedSettingCommand extends SettingsCommand {
449449

@@ -459,7 +459,7 @@ const editFocusedSettingCommand = new EditFocusedSettingCommand({
459459
precondition: CONTEXT_SETTINGS_SEARCH_FOCUS,
460460
kbOpts: { primary: KeyMod.CtrlCmd | KeyCode.US_DOT }
461461
});
462-
KeybindingsRegistry.registerCommandAndKeybindingRule(editFocusedSettingCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
462+
editFocusedSettingCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());
463463

464464
class EditFocusedSettingCommand2 extends SettingsCommand {
465465

@@ -476,7 +476,7 @@ const editFocusedSettingCommand2 = new EditFocusedSettingCommand2({
476476
precondition: ContextKeyExpr.and(CONTEXT_SETTINGS_EDITOR, CONTEXT_SETTINGS_ROW_FOCUS),
477477
kbOpts: { primary: KeyCode.Enter }
478478
});
479-
KeybindingsRegistry.registerCommandAndKeybindingRule(editFocusedSettingCommand2.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.workbenchContrib()));
479+
editFocusedSettingCommand2.register(KeybindingsRegistry.WEIGHT.workbenchContrib());
480480

481481
class FocusSettingsListCommand extends SettingsCommand {
482482

@@ -493,7 +493,7 @@ const focusSettingsListCommand = new FocusSettingsListCommand({
493493
precondition: ContextKeyExpr.and(CONTEXT_SETTINGS_EDITOR, CONTEXT_TOC_ROW_FOCUS),
494494
kbOpts: { primary: KeyCode.Enter }
495495
});
496-
KeybindingsRegistry.registerCommandAndKeybindingRule(focusSettingsListCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.workbenchContrib()));
496+
focusSettingsListCommand.register(KeybindingsRegistry.WEIGHT.workbenchContrib());
497497

498498
// Preferences menu
499499

src/vs/workbench/parts/webview/electron-browser/webview.contribution.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const showNextFindWdigetCommand = new ShowWebViewEditorFindWidgetCommand({
4545
primary: KeyMod.CtrlCmd | KeyCode.KEY_F
4646
}
4747
});
48-
KeybindingsRegistry.registerCommandAndKeybindingRule(showNextFindWdigetCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
48+
showNextFindWdigetCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());
4949

5050
const hideCommand = new HideWebViewEditorFindCommand({
5151
id: HideWebViewEditorFindCommand.ID,
@@ -56,7 +56,7 @@ const hideCommand = new HideWebViewEditorFindCommand({
5656
primary: KeyCode.Escape
5757
}
5858
});
59-
KeybindingsRegistry.registerCommandAndKeybindingRule(hideCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
59+
hideCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());
6060

6161
const selectAllCommand = new SelectAllWebviewEditorCommand({
6262
id: SelectAllWebviewEditorCommand.ID,
@@ -65,7 +65,7 @@ const selectAllCommand = new SelectAllWebviewEditorCommand({
6565
primary: KeyMod.CtrlCmd | KeyCode.KEY_A
6666
}
6767
});
68-
KeybindingsRegistry.registerCommandAndKeybindingRule(selectAllCommand.toCommandAndKeybindingRule(KeybindingsRegistry.WEIGHT.editorContrib()));
68+
selectAllCommand.register(KeybindingsRegistry.WEIGHT.editorContrib());
6969

7070
actionRegistry.registerWorkbenchAction(
7171
new SyncActionDescriptor(OpenWebviewDeveloperToolsAction, OpenWebviewDeveloperToolsAction.ID, OpenWebviewDeveloperToolsAction.LABEL),

0 commit comments

Comments
 (0)