From 7668d2fa4b2da6f0bddbd10fa03b0fce459fb518 Mon Sep 17 00:00:00 2001 From: Colin Grant Date: Thu, 27 May 2021 12:18:47 -0500 Subject: [PATCH] Enforce some order on keybindings Signed-off-by: Colin Grant --- examples/api-tests/src/keybindings.spec.js | 2 +- packages/core/src/browser/keybinding.ts | 18 +++++++++++++++++- .../src/browser/editor-preview-contribution.ts | 2 +- .../plugin-vscode-commands-contribution.ts | 1 - 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/examples/api-tests/src/keybindings.spec.js b/examples/api-tests/src/keybindings.spec.js index 2087a697cfa15..5f862ee5cb3a9 100644 --- a/examples/api-tests/src/keybindings.spec.js +++ b/examples/api-tests/src/keybindings.spec.js @@ -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: () => { } diff --git a/packages/core/src/browser/keybinding.ts b/packages/core/src/browser/keybinding.ts index 06b22cca8362f..394841590f893 100644 --- a/packages/core/src/browser/keybinding.ts +++ b/packages/core/src/browser/keybinding.ts @@ -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) { @@ -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. */ diff --git a/packages/editor-preview/src/browser/editor-preview-contribution.ts b/packages/editor-preview/src/browser/editor-preview-contribution.ts index 0895e9e88ab6b..b5b870b7995f4 100644 --- a/packages/editor-preview/src/browser/editor-preview-contribution.ts +++ b/packages/editor-preview/src/browser/editor-preview-contribution.ts @@ -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' }; } diff --git a/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts b/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts index c471856b1906f..7b5ad6809ecfa 100755 --- a/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts +++ b/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts @@ -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