From 48900d65cfb60ae2bd9827f2ce0641b1d76f75be Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Fri, 10 Mar 2017 13:20:38 +0100 Subject: [PATCH] Consistent usage of Map in KeybindingResolver --- .../common/abstractKeybindingService.ts | 6 +-- .../keybinding/common/keybindingResolver.ts | 49 ++++++++++--------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/vs/platform/keybinding/common/abstractKeybindingService.ts b/src/vs/platform/keybinding/common/abstractKeybindingService.ts index acb4523a01b..895cb51a7d9 100644 --- a/src/vs/platform/keybinding/common/abstractKeybindingService.ts +++ b/src/vs/platform/keybinding/common/abstractKeybindingService.ts @@ -12,7 +12,7 @@ import { IDisposable, dispose } from 'vs/base/common/lifecycle'; import Severity from 'vs/base/common/severity'; import { isFalsyOrEmpty } from 'vs/base/common/arrays'; import { ICommandService, CommandsRegistry, ICommandHandlerDescription } from 'vs/platform/commands/common/commands'; -import { KeybindingResolver, IResolveResult, IBoundCommands } from 'vs/platform/keybinding/common/keybindingResolver'; +import { KeybindingResolver, IResolveResult } from 'vs/platform/keybinding/common/keybindingResolver'; import { IKeybindingEvent, IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { IContextKeyService, IContextKeyServiceTarget } from 'vs/platform/contextkey/common/contextkey'; import { IStatusbarService } from 'vs/platform/statusbar/common/statusbar'; @@ -130,7 +130,7 @@ export abstract class AbstractKeybindingService implements IKeybindingService { return out.toString(); } - private static _getAllCommandsAsComment(boundCommands: IBoundCommands): string { + private static _getAllCommandsAsComment(boundCommands: Map): string { const commands = CommandsRegistry.getCommands(); const unboundCommands: string[] = []; @@ -142,7 +142,7 @@ export abstract class AbstractKeybindingService implements IKeybindingService { && !isFalsyOrEmpty((commands[id].description).args)) { // command with args continue; } - if (boundCommands[id]) { + if (boundCommands.get(id) === true) { continue; } unboundCommands.push(id); diff --git a/src/vs/platform/keybinding/common/keybindingResolver.ts b/src/vs/platform/keybinding/common/keybindingResolver.ts index 3099092e453..805d95dd366 100644 --- a/src/vs/platform/keybinding/common/keybindingResolver.ts +++ b/src/vs/platform/keybinding/common/keybindingResolver.ts @@ -14,31 +14,24 @@ export interface IResolveResult { bubble: boolean; } -export interface IBoundCommands { - [commandId: string]: boolean; -} - -interface ICommandMap { - [keypress: string]: NormalizedKeybindingItem[]; -} - export class KeybindingResolver { private readonly _defaultKeybindings: NormalizedKeybindingItem[]; private readonly _shouldWarnOnConflict: boolean; - private readonly _defaultBoundCommands: IBoundCommands; - private readonly _map: ICommandMap; + private readonly _defaultBoundCommands: Map; + private readonly _map: Map; private readonly _lookupMap: Map; constructor(defaultKeybindings: NormalizedKeybindingItem[], overrides: NormalizedKeybindingItem[], shouldWarnOnConflict: boolean = true) { this._defaultKeybindings = defaultKeybindings; this._shouldWarnOnConflict = shouldWarnOnConflict; - this._defaultBoundCommands = Object.create(null); + this._defaultBoundCommands = new Map(); for (let i = 0, len = defaultKeybindings.length; i < len; i++) { - this._defaultBoundCommands[defaultKeybindings[i].command] = true; + const command = defaultKeybindings[i].command; + this._defaultBoundCommands.set(command, true); } - this._map = Object.create(null); + this._map = new Map(); this._lookupMap = new Map(); let allKeybindings = KeybindingResolver.combine(defaultKeybindings, overrides); @@ -103,15 +96,15 @@ export class KeybindingResolver { private _addKeyPress(keypress: string, item: NormalizedKeybindingItem): void { - if (!this._map[keypress]) { + const conflicts = this._map.get(keypress); + + if (typeof conflicts === 'undefined') { // There is no conflict so far - this._map[keypress] = [item]; + this._map.set(keypress, [item]); this._addToLookupMap(item); return; } - let conflicts = this._map[keypress]; - for (let i = conflicts.length - 1; i >= 0; i--) { let conflict = conflicts[i]; @@ -203,7 +196,7 @@ export class KeybindingResolver { return true; } - public getDefaultBoundCommands(): IBoundCommands { + public getDefaultBoundCommands(): Map { return this._defaultBoundCommands; } @@ -240,8 +233,14 @@ export class KeybindingResolver { if (currentChord !== null) { // Fetch all chord bindings for `currentChord` + + const candidates = this._map.get(currentChord); + if (typeof candidates === 'undefined') { + // No chords starting with `currentChord` + return null; + } + lookupMap = []; - let candidates = this._map[currentChord]; for (let i = 0, len = candidates.length; i < len; i++) { let candidate = candidates[i]; if (candidate.keypressChordPart === keypress) { @@ -249,7 +248,13 @@ export class KeybindingResolver { } } } else { - lookupMap = this._map[keypress]; + const candidates = this._map.get(keypress); + if (typeof candidates === 'undefined') { + // No bindings with `keypress` + return null; + } + + lookupMap = candidates; } let result = this._findCommand(context, lookupMap); @@ -275,10 +280,6 @@ export class KeybindingResolver { } private _findCommand(context: any, matches: NormalizedKeybindingItem[]): NormalizedKeybindingItem { - if (!matches) { - return null; - } - for (let i = matches.length - 1; i >= 0; i--) { let k = matches[i];