Skip to content

Commit

Permalink
Enforce some order on keybindings
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Grant <[email protected]>
  • Loading branch information
colin-grant-work committed May 27, 2021
1 parent 0053b56 commit 7668d2f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/api-tests/src/keybindings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('Keybindings', function () {
assert.notEqual(executedCommand, id);
});

it('later registered keybinding should has higher priority', async () => {
it('later registered keybinding should have higher priority', async () => {
const id = '__test:keybindings.copy';
toTearDown.push(commands.registerCommand({ id }, {
execute: () => { }
Expand Down
18 changes: 17 additions & 1 deletion packages/core/src/browser/keybinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class KeybindingRegistry {
try {
this.resolveKeybinding(binding);
const scoped = Object.assign(binding, { scope });
this.keymaps[scope].unshift(scoped);
this.insertBindingIntoScope(scoped, scope);
return Disposable.create(() => {
const index = this.keymaps[scope].indexOf(scoped);
if (index !== -1) {
Expand All @@ -245,6 +245,22 @@ export class KeybindingRegistry {
}
}

/**
* Ensures that keybindings are inserted in order of increasing length of binding to ensure that if a
* user triggers a short keybinding (e.g. ctrl+k), the UI won't wait for a longer one (e.g. ctrl+k enter)
*/
protected insertBindingIntoScope(item: common.Keybinding & { scope: KeybindingScope; }, scope: KeybindingScope): void {
const scopedKeymap = this.keymaps[scope];
const getNumberOfKeystrokes = (binding: common.Keybinding): number => (binding.keybinding.trim().match(/\s/)?.length ?? 0) + 1;
const numberOfKeystrokesInBinding = getNumberOfKeystrokes(item);
const indexOfFirstItemWithEqualStrokes = scopedKeymap.findIndex(existingBinding => getNumberOfKeystrokes(existingBinding) === numberOfKeystrokesInBinding);
if (indexOfFirstItemWithEqualStrokes > -1) {
scopedKeymap.splice(indexOfFirstItemWithEqualStrokes, 0, item);
} else {
scopedKeymap.push(item);
}
}

/**
* Ensure that the `resolved` property of the given binding is set by calling the KeyboardLayoutService.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { EditorPreviewWidget } from './editor-preview-widget';

export namespace EditorPreviewCommands {
export const PIN_PREVIEW_COMMAND: Command = {
id: 'editor-preview-pin-editor',
id: 'workbench.action.keepEditor',
label: 'Keep Open'
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ export class PluginVscodeCommandsContribution implements CommandContribution {

/**
* TODO:
* Keep Open: workbench.action.keepEditor
* Open Next: workbench.action.openNextRecentlyUsedEditorInGroup
* Open Previous: workbench.action.openPreviousRecentlyUsedEditorInGroup
* Copy Path of Active File: workbench.action.files.copyPathOfActiveFile
Expand Down

0 comments on commit 7668d2f

Please sign in to comment.