Skip to content

Commit 75a9a1f

Browse files
committed
Towards using a const enum for keybindings weights
1 parent 608c07c commit 75a9a1f

24 files changed

Lines changed: 76 additions & 68 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ registerEditorCommand(new FindCommand({
631631
precondition: CONTEXT_FIND_WIDGET_VISIBLE,
632632
handler: x => x.closeFindWidget(),
633633
kbOpts: {
634-
weight: KeybindingsRegistry.WEIGHT.editorContrib(5),
634+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 5,
635635
kbExpr: EditorContextKeys.focus,
636636
primary: KeyCode.Escape,
637637
secondary: [KeyMod.Shift | KeyCode.Escape]
@@ -643,7 +643,7 @@ registerEditorCommand(new FindCommand({
643643
precondition: null,
644644
handler: x => x.toggleCaseSensitive(),
645645
kbOpts: {
646-
weight: KeybindingsRegistry.WEIGHT.editorContrib(5),
646+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 5,
647647
kbExpr: EditorContextKeys.focus,
648648
primary: ToggleCaseSensitiveKeybinding.primary,
649649
mac: ToggleCaseSensitiveKeybinding.mac,
@@ -657,7 +657,7 @@ registerEditorCommand(new FindCommand({
657657
precondition: null,
658658
handler: x => x.toggleWholeWords(),
659659
kbOpts: {
660-
weight: KeybindingsRegistry.WEIGHT.editorContrib(5),
660+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 5,
661661
kbExpr: EditorContextKeys.focus,
662662
primary: ToggleWholeWordKeybinding.primary,
663663
mac: ToggleWholeWordKeybinding.mac,
@@ -671,7 +671,7 @@ registerEditorCommand(new FindCommand({
671671
precondition: null,
672672
handler: x => x.toggleRegex(),
673673
kbOpts: {
674-
weight: KeybindingsRegistry.WEIGHT.editorContrib(5),
674+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 5,
675675
kbExpr: EditorContextKeys.focus,
676676
primary: ToggleRegexKeybinding.primary,
677677
mac: ToggleRegexKeybinding.mac,
@@ -685,7 +685,7 @@ registerEditorCommand(new FindCommand({
685685
precondition: null,
686686
handler: x => x.toggleSearchScope(),
687687
kbOpts: {
688-
weight: KeybindingsRegistry.WEIGHT.editorContrib(5),
688+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 5,
689689
kbExpr: EditorContextKeys.focus,
690690
primary: ToggleSearchScopeKeybinding.primary,
691691
mac: ToggleSearchScopeKeybinding.mac,
@@ -699,7 +699,7 @@ registerEditorCommand(new FindCommand({
699699
precondition: CONTEXT_FIND_WIDGET_VISIBLE,
700700
handler: x => x.replace(),
701701
kbOpts: {
702-
weight: KeybindingsRegistry.WEIGHT.editorContrib(5),
702+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 5,
703703
kbExpr: EditorContextKeys.focus,
704704
primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_1
705705
}
@@ -710,7 +710,7 @@ registerEditorCommand(new FindCommand({
710710
precondition: CONTEXT_FIND_WIDGET_VISIBLE,
711711
handler: x => x.replaceAll(),
712712
kbOpts: {
713-
weight: KeybindingsRegistry.WEIGHT.editorContrib(5),
713+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 5,
714714
kbExpr: EditorContextKeys.focus,
715715
primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.Enter
716716
}
@@ -721,7 +721,7 @@ registerEditorCommand(new FindCommand({
721721
precondition: CONTEXT_FIND_WIDGET_VISIBLE,
722722
handler: x => x.selectAllMatches(),
723723
kbOpts: {
724-
weight: KeybindingsRegistry.WEIGHT.editorContrib(5),
724+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 5,
725725
kbExpr: EditorContextKeys.focus,
726726
primary: KeyMod.Alt | KeyCode.Enter
727727
}

src/vs/editor/contrib/gotoError/gotoError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ registerEditorCommand(new MarkerCommand({
439439
precondition: CONTEXT_MARKERS_NAVIGATION_VISIBLE,
440440
handler: x => x.closeMarkersNavigation(),
441441
kbOpts: {
442-
weight: KeybindingsRegistry.WEIGHT.editorContrib(50),
442+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 50,
443443
kbExpr: EditorContextKeys.focus,
444444
primary: KeyCode.Escape,
445445
secondary: [KeyMod.Shift | KeyCode.Escape]

src/vs/editor/contrib/message/messageController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ registerEditorCommand(new MessageCommand({
114114
precondition: MessageController.MESSAGE_VISIBLE,
115115
handler: c => c.closeMessage(),
116116
kbOpts: {
117-
weight: KeybindingsRegistry.WEIGHT.editorContrib(30),
117+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 30,
118118
primary: KeyCode.Escape
119119
}
120120
}));

src/vs/editor/contrib/parameterHints/parameterHints.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class TriggerParameterHintsAction extends EditorAction {
8585
registerEditorContribution(ParameterHintsController);
8686
registerEditorAction(TriggerParameterHintsAction);
8787

88-
const weight = KeybindingsRegistry.WEIGHT.editorContrib(75);
88+
const weight = KeybindingsRegistry.WEIGHT.editorContrib() + 75;
8989

9090
const ParameterHintsCommand = EditorCommand.bindToContribution<ParameterHintsController>(ParameterHintsController.get);
9191

src/vs/editor/contrib/referenceSearch/referenceSearch.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ function withController(accessor: ServicesAccessor, fn: (controller: ReferencesC
193193

194194
KeybindingsRegistry.registerCommandAndKeybindingRule({
195195
id: 'goToNextReference',
196-
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(50),
196+
weight: KeybindingsRegistry.WEIGHT.workbenchContrib() + 50,
197197
primary: KeyCode.F4,
198198
when: ctxReferenceSearchVisible,
199199
handler(accessor) {
@@ -205,7 +205,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
205205

206206
KeybindingsRegistry.registerCommandAndKeybindingRule({
207207
id: 'goToNextReferenceFromEmbeddedEditor',
208-
weight: KeybindingsRegistry.WEIGHT.editorContrib(50),
208+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 50,
209209
primary: KeyCode.F4,
210210
when: PeekContext.inPeekEditor,
211211
handler(accessor) {
@@ -217,7 +217,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
217217

218218
KeybindingsRegistry.registerCommandAndKeybindingRule({
219219
id: 'goToPreviousReference',
220-
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(50),
220+
weight: KeybindingsRegistry.WEIGHT.workbenchContrib() + 50,
221221
primary: KeyMod.Shift | KeyCode.F4,
222222
when: ctxReferenceSearchVisible,
223223
handler(accessor) {
@@ -229,7 +229,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
229229

230230
KeybindingsRegistry.registerCommandAndKeybindingRule({
231231
id: 'goToPreviousReferenceFromEmbeddedEditor',
232-
weight: KeybindingsRegistry.WEIGHT.editorContrib(50),
232+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 50,
233233
primary: KeyMod.Shift | KeyCode.F4,
234234
when: PeekContext.inPeekEditor,
235235
handler(accessor) {
@@ -241,7 +241,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
241241

242242
KeybindingsRegistry.registerCommandAndKeybindingRule({
243243
id: 'closeReferenceSearch',
244-
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(50),
244+
weight: KeybindingsRegistry.WEIGHT.workbenchContrib() + 50,
245245
primary: KeyCode.Escape,
246246
secondary: [KeyMod.Shift | KeyCode.Escape],
247247
when: ContextKeyExpr.and(ctxReferenceSearchVisible, ContextKeyExpr.not('config.editor.stablePeek')),
@@ -250,7 +250,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
250250

251251
KeybindingsRegistry.registerCommandAndKeybindingRule({
252252
id: 'closeReferenceSearchEditor',
253-
weight: KeybindingsRegistry.WEIGHT.editorContrib(-101),
253+
weight: KeybindingsRegistry.WEIGHT.editorContrib() - 101,
254254
primary: KeyCode.Escape,
255255
secondary: [KeyMod.Shift | KeyCode.Escape],
256256
when: ContextKeyExpr.and(PeekContext.inPeekEditor, ContextKeyExpr.not('config.editor.stablePeek')),

src/vs/editor/contrib/rename/rename.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ registerEditorCommand(new RenameCommand({
270270
precondition: CONTEXT_RENAME_INPUT_VISIBLE,
271271
handler: x => x.acceptRenameInput(),
272272
kbOpts: {
273-
weight: KeybindingsRegistry.WEIGHT.editorContrib(99),
273+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 99,
274274
kbExpr: EditorContextKeys.focus,
275275
primary: KeyCode.Enter
276276
}
@@ -281,7 +281,7 @@ registerEditorCommand(new RenameCommand({
281281
precondition: CONTEXT_RENAME_INPUT_VISIBLE,
282282
handler: x => x.cancelRenameInput(),
283283
kbOpts: {
284-
weight: KeybindingsRegistry.WEIGHT.editorContrib(99),
284+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 99,
285285
kbExpr: EditorContextKeys.focus,
286286
primary: KeyCode.Escape,
287287
secondary: [KeyMod.Shift | KeyCode.Escape]

src/vs/editor/contrib/snippet/snippetController2.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ registerEditorCommand(new CommandCtor({
227227
precondition: ContextKeyExpr.and(SnippetController2.InSnippetMode, SnippetController2.HasNextTabstop),
228228
handler: ctrl => ctrl.next(),
229229
kbOpts: {
230-
weight: KeybindingsRegistry.WEIGHT.editorContrib(30),
230+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 30,
231231
kbExpr: EditorContextKeys.editorTextFocus,
232232
primary: KeyCode.Tab
233233
}
@@ -237,7 +237,7 @@ registerEditorCommand(new CommandCtor({
237237
precondition: ContextKeyExpr.and(SnippetController2.InSnippetMode, SnippetController2.HasPrevTabstop),
238238
handler: ctrl => ctrl.prev(),
239239
kbOpts: {
240-
weight: KeybindingsRegistry.WEIGHT.editorContrib(30),
240+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 30,
241241
kbExpr: EditorContextKeys.editorTextFocus,
242242
primary: KeyMod.Shift | KeyCode.Tab
243243
}
@@ -247,7 +247,7 @@ registerEditorCommand(new CommandCtor({
247247
precondition: SnippetController2.InSnippetMode,
248248
handler: ctrl => ctrl.cancel(),
249249
kbOpts: {
250-
weight: KeybindingsRegistry.WEIGHT.editorContrib(30),
250+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 30,
251251
kbExpr: EditorContextKeys.editorTextFocus,
252252
primary: KeyCode.Escape,
253253
secondary: [KeyMod.Shift | KeyCode.Escape]
@@ -259,7 +259,7 @@ registerEditorCommand(new CommandCtor({
259259
precondition: SnippetController2.InSnippetMode,
260260
handler: ctrl => ctrl.finish(),
261261
// kbOpts: {
262-
// weight: KeybindingsRegistry.WEIGHT.editorContrib(30),
262+
// weight: KeybindingsRegistry.WEIGHT.editorContrib() + 30,
263263
// kbExpr: EditorContextKeys.textFocus,
264264
// primary: KeyCode.Enter,
265265
// }

src/vs/editor/contrib/suggest/suggestController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export class TriggerSuggestAction extends EditorAction {
345345
registerEditorContribution(SuggestController);
346346
registerEditorAction(TriggerSuggestAction);
347347

348-
const weight = KeybindingsRegistry.WEIGHT.editorContrib(90);
348+
const weight = KeybindingsRegistry.WEIGHT.editorContrib() + 90;
349349

350350
const SuggestCommand = EditorCommand.bindToContribution<SuggestController>(SuggestController.get);
351351

src/vs/editor/standalone/browser/accessibilityHelp/accessibilityHelp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ registerEditorCommand(
367367
precondition: CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE,
368368
handler: x => x.hide(),
369369
kbOpts: {
370-
weight: KeybindingsRegistry.WEIGHT.editorContrib(100),
370+
weight: KeybindingsRegistry.WEIGHT.editorContrib() + 100,
371371
kbExpr: EditorContextKeys.focus,
372372
primary: KeyCode.Escape,
373373
secondary: [KeyMod.Shift | KeyCode.Escape]

src/vs/platform/keybinding/common/keybindingsRegistry.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ export const enum KeybindingRuleSource {
5757
Extension = 1
5858
}
5959

60+
export const enum KeybindingWeight {
61+
EditorCore = 0,
62+
EditorContrib = 100,
63+
WorkbenchContrib = 200,
64+
BuiltinExtension = 300,
65+
ExternalExtension = 400
66+
}
67+
6068
export interface ICommandAndKeybindingRule extends IKeybindingRule {
6169
handler: ICommandHandler;
6270
description?: ICommandHandlerDescription;
@@ -69,11 +77,11 @@ export interface IKeybindingsRegistry {
6977
getDefaultKeybindings(): IKeybindingItem[];
7078

7179
WEIGHT: {
72-
editorCore(importance?: number): number;
73-
editorContrib(importance?: number): number;
74-
workbenchContrib(importance?: number): number;
75-
builtinExtension(importance?: number): number;
76-
externalExtension(importance?: number): number;
80+
editorCore(): number;
81+
editorContrib(): number;
82+
workbenchContrib(): number;
83+
builtinExtension(): number;
84+
externalExtension(): number;
7785
};
7886
}
7987

@@ -83,20 +91,20 @@ class KeybindingsRegistryImpl implements IKeybindingsRegistry {
8391
private _keybindingsSorted: boolean;
8492

8593
public WEIGHT = {
86-
editorCore: (importance: number = 0): number => {
87-
return 0 + importance;
94+
editorCore: (): number => {
95+
return KeybindingWeight.EditorCore;
8896
},
89-
editorContrib: (importance: number = 0): number => {
90-
return 100 + importance;
97+
editorContrib: (): number => {
98+
return KeybindingWeight.EditorContrib;
9199
},
92-
workbenchContrib: (importance: number = 0): number => {
93-
return 200 + importance;
100+
workbenchContrib: (): number => {
101+
return KeybindingWeight.WorkbenchContrib;
94102
},
95-
builtinExtension: (importance: number = 0): number => {
96-
return 300 + importance;
103+
builtinExtension: (): number => {
104+
return KeybindingWeight.BuiltinExtension;
97105
},
98-
externalExtension: (importance: number = 0): number => {
99-
return 400 + importance;
106+
externalExtension: (): number => {
107+
return KeybindingWeight.ExternalExtension;
100108
}
101109
};
102110

0 commit comments

Comments
 (0)