Skip to content

Commit

Permalink
[vscode] fix eclipse-theia#4175: tolerate mistyped keybindings
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Feb 1, 2019
1 parent 57b8add commit 0628cb3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 11 additions & 3 deletions packages/core/src/browser/keybinding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ describe('keybindings', () => {

const bindings = keybindingRegistry.getKeybindingsForCommand(TEST_COMMAND2.id);
if (bindings) {
expect(bindings.length).to.be.equal(2);
expect(bindings.length).to.be.equal(1);
const keyCode = KeyCode.parse(bindings[0].keybinding);
expect(keyCode.key).to.be.equal(Key.F1);
expect(keyCode.ctrl).to.be.true;
expect(keyCode.key).to.be.equal(Key.F3);
expect(keyCode.ctrl).to.be.false;
}
});

Expand Down Expand Up @@ -516,6 +516,14 @@ describe('keys api', () => {
const normalized = keyCode.normalizeToUsLayout();
expect(normalized).to.be.deep.equal(KeyCode.parse('ctrlcmd+/'));
});

it('parse bogus keybinding', () => {
const [first, second] = KeySequence.parse(' CtrlCmd+sHiFt+F10 b ');
expect(first.ctrl).to.be.true;
expect(first.shift).to.be.true;
expect(first.key).is.equal(Key.F10);
expect(second.key).is.equal(Key.KEY_B);
});
});

const TEST_COMMAND: Command = {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/browser/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export namespace KeySequence {

export function parse(keybinding: string): KeySequence {
const keyCodes = [];
const rawKeyCodes = keybinding.split(' ');
const rawKeyCodes = keybinding.trim().split(/\s+/g);
for (const rawKeyCode of rawKeyCodes) {
const keyCode = KeyCode.parse(rawKeyCode);
if (keyCode !== undefined) {
Expand Down Expand Up @@ -258,7 +258,7 @@ export class KeyCode {
}

const sequence: string[] = [];
const keys = keybinding.split('+');
const keys = keybinding.trim().toLowerCase().split('+');
/* If duplicates i.e ctrl+ctrl+a or alt+alt+b or b+alt+b it is invalid */
if (keys.length !== new Set(keys).size) {
throw new Error(`Can't parse keybinding ${keybinding} Duplicate modifiers`);
Expand Down

0 comments on commit 0628cb3

Please sign in to comment.