Path: blob/main/src/vs/platform/keybinding/test/common/mockKeybindingService.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Event } from '../../../../base/common/event.js';6import { KeyCodeChord, Keybinding, ResolvedKeybinding } from '../../../../base/common/keybindings.js';7import { Disposable } from '../../../../base/common/lifecycle.js';8import { OS } from '../../../../base/common/platform.js';9import { ContextKeyExpression, ContextKeyValue, IContextKey, IContextKeyChangeEvent, IContextKeyService, IContextKeyServiceTarget, IScopedContextKeyService } from '../../../contextkey/common/contextkey.js';10import { IKeybindingService, IKeyboardEvent } from '../../common/keybinding.js';11import { NoMatchingKb, ResolutionResult } from '../../common/keybindingResolver.js';12import { ResolvedKeybindingItem } from '../../common/resolvedKeybindingItem.js';13import { USLayoutResolvedKeybinding } from '../../common/usLayoutResolvedKeybinding.js';1415class MockKeybindingContextKey<T extends ContextKeyValue = ContextKeyValue> implements IContextKey<T> {16private _defaultValue: T | undefined;17private _value: T | undefined;1819constructor(defaultValue: T | undefined) {20this._defaultValue = defaultValue;21this._value = this._defaultValue;22}2324public set(value: T | undefined): void {25this._value = value;26}2728public reset(): void {29this._value = this._defaultValue;30}3132public get(): T | undefined {33return this._value;34}35}3637export class MockContextKeyService implements IContextKeyService {3839public _serviceBrand: undefined;40private _keys = new Map<string, IContextKey<any>>();4142public dispose(): void {43//44}45public createKey<T extends ContextKeyValue = ContextKeyValue>(key: string, defaultValue: T | undefined): IContextKey<T> {46const ret = new MockKeybindingContextKey(defaultValue);47this._keys.set(key, ret);48return ret;49}50public contextMatchesRules(rules: ContextKeyExpression): boolean {51return false;52}53public get onDidChangeContext(): Event<IContextKeyChangeEvent> {54return Event.None;55}56public bufferChangeEvents(callback: () => void) { callback(); }57public getContextKeyValue(key: string) {58const value = this._keys.get(key);59if (value) {60return value.get();61}62}63public getContext(domNode: HTMLElement): any {64return null;65}66public createScoped(domNode: HTMLElement): IScopedContextKeyService {67return this;68}69public createOverlay(): IContextKeyService {70return this;71}72updateParent(_parentContextKeyService: IContextKeyService): void {73// no-op74}75}7677export class MockScopableContextKeyService extends MockContextKeyService {78/**79* Don't implement this for all tests since we rarely depend on this behavior and it isn't implemented fully80*/81public override createScoped(domNote: HTMLElement): IScopedContextKeyService {82return new MockScopableContextKeyService();83}84}8586export class MockKeybindingService implements IKeybindingService {87public _serviceBrand: undefined;8889public readonly inChordMode: boolean = false;9091public get onDidUpdateKeybindings(): Event<void> {92return Event.None;93}9495public getDefaultKeybindingsContent(): string {96return '';97}9899public getDefaultKeybindings(): ResolvedKeybindingItem[] {100return [];101}102103public getKeybindings(): ResolvedKeybindingItem[] {104return [];105}106107public resolveKeybinding(keybinding: Keybinding): ResolvedKeybinding[] {108return USLayoutResolvedKeybinding.resolveKeybinding(keybinding, OS);109}110111public resolveKeyboardEvent(keyboardEvent: IKeyboardEvent): ResolvedKeybinding {112const chord = new KeyCodeChord(113keyboardEvent.ctrlKey,114keyboardEvent.shiftKey,115keyboardEvent.altKey,116keyboardEvent.metaKey,117keyboardEvent.keyCode118);119return this.resolveKeybinding(chord.toKeybinding())[0];120}121122public resolveUserBinding(userBinding: string): ResolvedKeybinding[] {123return [];124}125126public lookupKeybindings(commandId: string): ResolvedKeybinding[] {127return [];128}129130public lookupKeybinding(commandId: string): ResolvedKeybinding | undefined {131return undefined;132}133134public customKeybindingsCount(): number {135return 0;136}137138public softDispatch(keybinding: IKeyboardEvent, target: IContextKeyServiceTarget): ResolutionResult {139return NoMatchingKb;140}141142public dispatchByUserSettingsLabel(userSettingsLabel: string, target: IContextKeyServiceTarget): void {143144}145146public dispatchEvent(e: IKeyboardEvent, target: IContextKeyServiceTarget): boolean {147return false;148}149150public enableKeybindingHoldMode(commandId: string): undefined {151return undefined;152}153154public mightProducePrintableCharacter(e: IKeyboardEvent): boolean {155return false;156}157158public toggleLogging(): boolean {159return false;160}161162public _dumpDebugInfo(): string {163return '';164}165166public _dumpDebugInfoJSON(): string {167return '';168}169170public registerSchemaContribution() {171return Disposable.None;172}173}174175176