1
>
/*---------------------------------------------------------------------------------------------
keybindingResolver.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { ContextKeyExpression, ContextKeyExprType, IContext, IContextKeyService, implies } from '../../contextkey/common/contextkey.js';
7
>
import { ResolvedKeybindingItem } from './resolvedKeybindingItem.js';
8
>
9
>
//#region resolution-result
10
>
11
>
export const enum ResultKind {
12
>
/** No keybinding found this sequence of chords */
13
>
NoMatchingKb,
14
>
15
>
/** There're several keybindings that have the given sequence of chords as a prefix */
16
>
MoreChordsNeeded,
17
>
18
>
/** A single keybinding found to be dispatched/invoked */
19
>
KbFound
20
>
}
21
>
22
>
export type ResolutionResult =
23
>
| { kind: ResultKind.NoMatchingKb }
24
>
| { kind: ResultKind.MoreChordsNeeded }
25
>
| { kind: ResultKind.KbFound; commandId: string | null; commandArgs: any; isBubble: boolean };
26
>
27
>
28
>
// util definitions to make working with the above types easier within this module:
29
>
30
>
export const NoMatchingKb: ResolutionResult = { kind: ResultKind.NoMatchingKb };
31
>
const MoreChordsNeeded: ResolutionResult = { kind: ResultKind.MoreChordsNeeded };
32
function KbFound(commandId: string | null, commandArgs: any, isBubble: boolean): ResolutionResult {
33
return { kind: ResultKind.KbFound, commandId, commandArgs, isBubble };
34
}
36
>
//#endregion
37
>
38
>
/**
39
>
* Stores mappings from keybindings to commands and from commands to keybindings.
40
>
* Given a sequence of chords, `resolve`s which keybinding it matches
41
>
*/
42
>
export class KeybindingResolver {
43
>
private readonly _log: (str: string) => void;
44
>
private readonly _defaultKeybindings: ResolvedKeybindingItem[];
45
>
private readonly _keybindings: ResolvedKeybindingItem[];
46
>
private readonly _defaultBoundCommands: Map</* commandId */ string, boolean>;
47
>
private readonly _map: Map</* 1st chord's keypress */ string, ResolvedKeybindingItem[]>;
48
>
private readonly _lookupMap: Map</* commandId */ string, ResolvedKeybindingItem[]>;
49
>
50
>
constructor(
51
/** built-in and extension-provided keybindings */
52
defaultKeybindings: ResolvedKeybindingItem[],