Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/keybinding/test/common/mockKeybindingService.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
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 { Event } from '../../../../base/common/event.js';
7
import { KeyCodeChord, Keybinding, ResolvedKeybinding } from '../../../../base/common/keybindings.js';
8
import { Disposable } from '../../../../base/common/lifecycle.js';
9
import { OS } from '../../../../base/common/platform.js';
10
import { ContextKeyExpression, ContextKeyValue, IContextKey, IContextKeyChangeEvent, IContextKeyService, IContextKeyServiceTarget, IScopedContextKeyService } from '../../../contextkey/common/contextkey.js';
11
import { IKeybindingService, IKeyboardEvent } from '../../common/keybinding.js';
12
import { NoMatchingKb, ResolutionResult } from '../../common/keybindingResolver.js';
13
import { ResolvedKeybindingItem } from '../../common/resolvedKeybindingItem.js';
14
import { USLayoutResolvedKeybinding } from '../../common/usLayoutResolvedKeybinding.js';
15
16
class MockKeybindingContextKey<T extends ContextKeyValue = ContextKeyValue> implements IContextKey<T> {
17
private _defaultValue: T | undefined;
18
private _value: T | undefined;
19
20
constructor(defaultValue: T | undefined) {
21
this._defaultValue = defaultValue;
22
this._value = this._defaultValue;
23
}
24
25
public set(value: T | undefined): void {
26
this._value = value;
27
}
28
29
public reset(): void {
30
this._value = this._defaultValue;
31
}
32
33
public get(): T | undefined {
34
return this._value;
35
}
36
}
37
38
export class MockContextKeyService implements IContextKeyService {
39
40
public _serviceBrand: undefined;
41
private _keys = new Map<string, IContextKey<any>>();
42
43
public dispose(): void {
44
//
45
}
46
public createKey<T extends ContextKeyValue = ContextKeyValue>(key: string, defaultValue: T | undefined): IContextKey<T> {
47
const ret = new MockKeybindingContextKey(defaultValue);
48
this._keys.set(key, ret);
49
return ret;
50
}
51
public contextMatchesRules(rules: ContextKeyExpression): boolean {
52
return false;
53
}
54
public get onDidChangeContext(): Event<IContextKeyChangeEvent> {
55
return Event.None;
56
}
57
public bufferChangeEvents(callback: () => void) { callback(); }
58
public getContextKeyValue(key: string) {
59
const value = this._keys.get(key);
60
if (value) {
61
return value.get();
62
}
63
}
64
public getContext(domNode: HTMLElement): any {
65
return null;
66
}
67
public createScoped(domNode: HTMLElement): IScopedContextKeyService {
68
return this;
69
}
70
public createOverlay(): IContextKeyService {
71
return this;
72
}
73
updateParent(_parentContextKeyService: IContextKeyService): void {
74
// no-op
75
}
76
}
77
78
export class MockScopableContextKeyService extends MockContextKeyService {
79
/**
80
* Don't implement this for all tests since we rarely depend on this behavior and it isn't implemented fully
81
*/
82
public override createScoped(domNote: HTMLElement): IScopedContextKeyService {
83
return new MockScopableContextKeyService();
84
}
85
}
86
87
export class MockKeybindingService implements IKeybindingService {
88
public _serviceBrand: undefined;
89
90
public readonly inChordMode: boolean = false;
91
92
public get onDidUpdateKeybindings(): Event<void> {
93
return Event.None;
94
}
95
96
public getDefaultKeybindingsContent(): string {
97
return '';
98
}
99
100
public getDefaultKeybindings(): ResolvedKeybindingItem[] {
101
return [];
102
}
103
104
public getKeybindings(): ResolvedKeybindingItem[] {
105
return [];
106
}
107
108
public resolveKeybinding(keybinding: Keybinding): ResolvedKeybinding[] {
109
return USLayoutResolvedKeybinding.resolveKeybinding(keybinding, OS);
110
}
111
112
public resolveKeyboardEvent(keyboardEvent: IKeyboardEvent): ResolvedKeybinding {
113
const chord = new KeyCodeChord(
114
keyboardEvent.ctrlKey,
115
keyboardEvent.shiftKey,
116
keyboardEvent.altKey,
117
keyboardEvent.metaKey,
118
keyboardEvent.keyCode
119
);
120
return this.resolveKeybinding(chord.toKeybinding())[0];
121
}
122
123
public resolveUserBinding(userBinding: string): ResolvedKeybinding[] {
124
return [];
125
}
126
127
public lookupKeybindings(commandId: string): ResolvedKeybinding[] {
128
return [];
129
}
130
131
public lookupKeybinding(commandId: string): ResolvedKeybinding | undefined {
132
return undefined;
133
}
134
135
public customKeybindingsCount(): number {
136
return 0;
137
}
138
139
public softDispatch(keybinding: IKeyboardEvent, target: IContextKeyServiceTarget): ResolutionResult {
140
return NoMatchingKb;
141
}
142
143
public dispatchByUserSettingsLabel(userSettingsLabel: string, target: IContextKeyServiceTarget): void {
144
145
}
146
147
public dispatchEvent(e: IKeyboardEvent, target: IContextKeyServiceTarget): boolean {
148
return false;
149
}
150
151
public enableKeybindingHoldMode(commandId: string): undefined {
152
return undefined;
153
}
154
155
public mightProducePrintableCharacter(e: IKeyboardEvent): boolean {
156
return false;
157
}
158
159
public toggleLogging(): boolean {
160
return false;
161
}
162
163
public _dumpDebugInfo(): string {
164
return '';
165
}
166
167
public _dumpDebugInfoJSON(): string {
168
return '';
169
}
170
171
public registerSchemaContribution() {
172
return Disposable.None;
173
}
174
}
175
176