From 7e407b547294849e9a22b16e5aea28a56f88524c Mon Sep 17 00:00:00 2001 From: Sandeep Somavarapu Date: Mon, 25 May 2020 19:39:59 +0200 Subject: [PATCH] Fix #98489 --- .../common/keybindingsEditorModel.ts | 116 +++++++++--------- .../common/keybindingsEditorModel.test.ts | 42 ++++++- 2 files changed, 99 insertions(+), 59 deletions(-) diff --git a/src/vs/workbench/services/preferences/common/keybindingsEditorModel.ts b/src/vs/workbench/services/preferences/common/keybindingsEditorModel.ts index 362555816ad..fb0b72bd894 100644 --- a/src/vs/workbench/services/preferences/common/keybindingsEditorModel.ts +++ b/src/vs/workbench/services/preferences/common/keybindingsEditorModel.ts @@ -421,22 +421,6 @@ class KeybindingItemMatches { return this.wordMatchesMetaModifier(word); } - private wordMatchesMetaModifier(word: string): boolean { - if (matchesPrefix(this.modifierLabels.ui.metaKey, word)) { - return true; - } - if (matchesPrefix(this.modifierLabels.aria.metaKey, word)) { - return true; - } - if (matchesPrefix(this.modifierLabels.user.metaKey, word)) { - return true; - } - if (matchesPrefix(localize('meta', "meta"), word)) { - return true; - } - return false; - } - private matchesCtrlModifier(keybinding: ResolvedKeybindingPart | null, word: string): boolean { if (!keybinding) { return false; @@ -447,19 +431,6 @@ class KeybindingItemMatches { return this.wordMatchesCtrlModifier(word); } - private wordMatchesCtrlModifier(word: string): boolean { - if (matchesPrefix(this.modifierLabels.ui.ctrlKey, word)) { - return true; - } - if (matchesPrefix(this.modifierLabels.aria.ctrlKey, word)) { - return true; - } - if (matchesPrefix(this.modifierLabels.user.ctrlKey, word)) { - return true; - } - return false; - } - private matchesShiftModifier(keybinding: ResolvedKeybindingPart | null, word: string): boolean { if (!keybinding) { return false; @@ -470,19 +441,6 @@ class KeybindingItemMatches { return this.wordMatchesShiftModifier(word); } - private wordMatchesShiftModifier(word: string): boolean { - if (matchesPrefix(this.modifierLabels.ui.shiftKey, word)) { - return true; - } - if (matchesPrefix(this.modifierLabels.aria.shiftKey, word)) { - return true; - } - if (matchesPrefix(this.modifierLabels.user.shiftKey, word)) { - return true; - } - return false; - } - private matchesAltModifier(keybinding: ResolvedKeybindingPart | null, word: string): boolean { if (!keybinding) { return false; @@ -493,22 +451,6 @@ class KeybindingItemMatches { return this.wordMatchesAltModifier(word); } - private wordMatchesAltModifier(word: string): boolean { - if (matchesPrefix(this.modifierLabels.ui.altKey, word)) { - return true; - } - if (matchesPrefix(this.modifierLabels.aria.altKey, word)) { - return true; - } - if (matchesPrefix(this.modifierLabels.user.altKey, word)) { - return true; - } - if (matchesPrefix(localize('option', "option"), word)) { - return true; - } - return false; - } - private hasAnyMatch(keybindingMatch: KeybindingMatch): boolean { return !!keybindingMatch.altKey || !!keybindingMatch.ctrlKey || @@ -574,4 +516,62 @@ class KeybindingItemMatches { } return false; } + + private wordMatchesAltModifier(word: string): boolean { + if (strings.equalsIgnoreCase(this.modifierLabels.ui.altKey, word)) { + return true; + } + if (strings.equalsIgnoreCase(this.modifierLabels.aria.altKey, word)) { + return true; + } + if (strings.equalsIgnoreCase(this.modifierLabels.user.altKey, word)) { + return true; + } + if (strings.equalsIgnoreCase(localize('option', "option"), word)) { + return true; + } + return false; + } + + private wordMatchesCtrlModifier(word: string): boolean { + if (strings.equalsIgnoreCase(this.modifierLabels.ui.ctrlKey, word)) { + return true; + } + if (strings.equalsIgnoreCase(this.modifierLabels.aria.ctrlKey, word)) { + return true; + } + if (strings.equalsIgnoreCase(this.modifierLabels.user.ctrlKey, word)) { + return true; + } + return false; + } + + private wordMatchesMetaModifier(word: string): boolean { + if (strings.equalsIgnoreCase(this.modifierLabels.ui.metaKey, word)) { + return true; + } + if (strings.equalsIgnoreCase(this.modifierLabels.aria.metaKey, word)) { + return true; + } + if (strings.equalsIgnoreCase(this.modifierLabels.user.metaKey, word)) { + return true; + } + if (strings.equalsIgnoreCase(localize('meta', "meta"), word)) { + return true; + } + return false; + } + + private wordMatchesShiftModifier(word: string): boolean { + if (strings.equalsIgnoreCase(this.modifierLabels.ui.shiftKey, word)) { + return true; + } + if (strings.equalsIgnoreCase(this.modifierLabels.aria.shiftKey, word)) { + return true; + } + if (strings.equalsIgnoreCase(this.modifierLabels.user.shiftKey, word)) { + return true; + } + return false; + } } diff --git a/src/vs/workbench/services/preferences/test/common/keybindingsEditorModel.test.ts b/src/vs/workbench/services/preferences/test/common/keybindingsEditorModel.test.ts index 8a445d39a20..c6dcb02e4f3 100644 --- a/src/vs/workbench/services/preferences/test/common/keybindingsEditorModel.test.ts +++ b/src/vs/workbench/services/preferences/test/common/keybindingsEditorModel.test.ts @@ -34,7 +34,7 @@ class AnAction extends Action { } } -suite('KeybindingsEditorModel test', () => { +suite('KeybindingsEditorModel', () => { let instantiationService: TestInstantiationService; let testObject: KeybindingsEditorModel; @@ -568,6 +568,46 @@ suite('KeybindingsEditorModel test', () => { assert.deepEqual(actual[0].keybindingMatches!.firstPart, { keyCode: true }); }); + test('filter modifiers are not matched when not completely matched (prefix)', async () => { + testObject = instantiationService.createInstance(KeybindingsEditorModel, OperatingSystem.Macintosh); + const term = `alt.${uuid.generateUuid()}`; + const command = `command.${term}`; + const expected = aResolvedKeybindingItem({ command, firstPart: { keyCode: KeyCode.Escape }, isDefault: false }); + prepareKeybindingService(expected, aResolvedKeybindingItem({ command: 'some_command', firstPart: { keyCode: KeyCode.Escape, modifiers: { altKey: true } }, isDefault: false })); + + await testObject.resolve(new Map()); + const actual = testObject.fetch(term); + assert.equal(1, actual.length); + assert.equal(command, actual[0].keybindingItem.command); + assert.equal(1, actual[0].commandIdMatches?.length); + }); + + test('filter modifiers are not matched when not completely matched (includes)', async () => { + testObject = instantiationService.createInstance(KeybindingsEditorModel, OperatingSystem.Macintosh); + const term = `abcaltdef.${uuid.generateUuid()}`; + const command = `command.${term}`; + const expected = aResolvedKeybindingItem({ command, firstPart: { keyCode: KeyCode.Escape }, isDefault: false }); + prepareKeybindingService(expected, aResolvedKeybindingItem({ command: 'some_command', firstPart: { keyCode: KeyCode.Escape, modifiers: { altKey: true } }, isDefault: false })); + + await testObject.resolve(new Map()); + const actual = testObject.fetch(term); + assert.equal(1, actual.length); + assert.equal(command, actual[0].keybindingItem.command); + assert.equal(1, actual[0].commandIdMatches?.length); + }); + + test('filter modifiers are matched with complete term', async () => { + testObject = instantiationService.createInstance(KeybindingsEditorModel, OperatingSystem.Macintosh); + const command = `command.${uuid.generateUuid()}`; + const expected = aResolvedKeybindingItem({ command, firstPart: { keyCode: KeyCode.Escape, modifiers: { altKey: true } }, isDefault: false }); + prepareKeybindingService(expected, aResolvedKeybindingItem({ command: 'some_command', firstPart: { keyCode: KeyCode.Escape }, isDefault: false })); + + await testObject.resolve(new Map()); + const actual = testObject.fetch('alt').filter(element => element.keybindingItem.command === command); + assert.equal(1, actual.length); + assert.deepEqual(actual[0].keybindingMatches!.firstPart, { altKey: true }); + }); + function prepareKeybindingService(...keybindingItems: ResolvedKeybindingItem[]): ResolvedKeybindingItem[] { instantiationService.stub(IKeybindingService, 'getKeybindings', () => keybindingItems); instantiationService.stub(IKeybindingService, 'getDefaultKeybindings', () => keybindingItems); -- GitLab