From e4bc90752f6c7a412943e79f43c8d0d93c89f8ed Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Wed, 22 Mar 2017 18:34:36 +0100 Subject: [PATCH] Improve KeybindingIO.readKeybinding --- .../keybinding/common/keybindingIO.ts | 109 ++++++++---------- 1 file changed, 51 insertions(+), 58 deletions(-) diff --git a/src/vs/workbench/services/keybinding/common/keybindingIO.ts b/src/vs/workbench/services/keybinding/common/keybindingIO.ts index 3921c1ec8a6..f407d8196b8 100644 --- a/src/vs/workbench/services/keybinding/common/keybindingIO.ts +++ b/src/vs/workbench/services/keybinding/common/keybindingIO.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import { KeyMod, USER_SETTINGS, createKeybinding, Keybinding, SimpleKeybinding, KeybindingType, ChordKeybinding } from 'vs/base/common/keyCodes'; +import { USER_SETTINGS, Keybinding, SimpleKeybinding, ChordKeybinding } from 'vs/base/common/keyCodes'; import { OperatingSystem } from 'vs/base/common/platform'; import { IUserFriendlyKeybinding } from 'vs/platform/keybinding/common/keybinding'; import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; @@ -81,99 +81,92 @@ export class KeybindingIO { return this._cachedKeybindingRegex; } - public static readKeybinding(input: string, OS: OperatingSystem): Keybinding { - if (!input) { - return null; - } + private static _readModifiers(input: string) { input = input.toLowerCase().trim(); - let ctrlCmd = false, - shift = false, - alt = false, - winCtrl = false, - key: string = ''; + let ctrl = false; + let shift = false; + let alt = false; + let meta = false; + + let matchedModifier: boolean; - while (/^(ctrl|shift|alt|meta|win|cmd)(\+|\-)/.test(input)) { + do { + matchedModifier = false; if (/^ctrl(\+|\-)/.test(input)) { - if (OS === OperatingSystem.Macintosh) { - winCtrl = true; - } else { - ctrlCmd = true; - } + ctrl = true; input = input.substr('ctrl-'.length); + matchedModifier = true; } if (/^shift(\+|\-)/.test(input)) { shift = true; input = input.substr('shift-'.length); + matchedModifier = true; } if (/^alt(\+|\-)/.test(input)) { alt = true; input = input.substr('alt-'.length); + matchedModifier = true; } if (/^meta(\+|\-)/.test(input)) { - if (OS === OperatingSystem.Macintosh) { - ctrlCmd = true; - } else { - winCtrl = true; - } + meta = true; input = input.substr('meta-'.length); + matchedModifier = true; } if (/^win(\+|\-)/.test(input)) { - if (OS === OperatingSystem.Macintosh) { - ctrlCmd = true; - } else { - winCtrl = true; - } + meta = true; input = input.substr('win-'.length); + matchedModifier = true; } if (/^cmd(\+|\-)/.test(input)) { - if (OS === OperatingSystem.Macintosh) { - ctrlCmd = true; - } else { - winCtrl = true; - } + meta = true; input = input.substr('cmd-'.length); + matchedModifier = true; } - } + } while (matchedModifier); - let chord: SimpleKeybinding = null; + let key: string; - let firstSpaceIdx = input.indexOf(' '); + const firstSpaceIdx = input.indexOf(' '); if (firstSpaceIdx > 0) { key = input.substring(0, firstSpaceIdx); - const readChord = KeybindingIO.readKeybinding(input.substring(firstSpaceIdx), OS); - if (readChord) { - if (readChord.type === KeybindingType.Chord) { - chord = readChord.firstPart; - } else { - chord = readChord; - } - } + input = input.substring(firstSpaceIdx); } else { key = input; + input = ''; } - let keyCode = USER_SETTINGS.toKeyCode(key); + return { + remains: input, + ctrl, + shift, + alt, + meta, + key + }; + } + + private static _readSimpleKeybinding(input: string, OS: OperatingSystem): [SimpleKeybinding, string] { + const mods = this._readModifiers(input); + const keyCode = USER_SETTINGS.toKeyCode(mods.key); + return [new SimpleKeybinding(mods.ctrl, mods.shift, mods.alt, mods.meta, keyCode), mods.remains]; + } - let result = 0; - if (ctrlCmd) { - result |= KeyMod.CtrlCmd; - } - if (shift) { - result |= KeyMod.Shift; - } - if (alt) { - result |= KeyMod.Alt; + public static readKeybinding(input: string, OS: OperatingSystem): Keybinding { + if (!input) { + return null; } - if (winCtrl) { - result |= KeyMod.WinCtrl; + + let [firstPart, remains] = this._readSimpleKeybinding(input, OS); + let chordPart: SimpleKeybinding = null; + if (remains.length > 0) { + [chordPart] = this._readSimpleKeybinding(remains, OS); } - result |= keyCode; - if (chord) { - return new ChordKeybinding(createKeybinding(result, OS), chord); + if (chordPart) { + return new ChordKeybinding(firstPart, chordPart); } - return createKeybinding(result, OS); + return firstPart; } } -- GitLab