Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/keybinding/test/node/keyboardMapperTestUtils.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 * as fs from 'fs';
7
import assert from 'assert';
8
import * as path from '../../../../../base/common/path.js';
9
import { SingleModifierChord, ResolvedKeybinding, Keybinding } from '../../../../../base/common/keybindings.js';
10
import { Promises } from '../../../../../base/node/pfs.js';
11
import { IKeyboardEvent } from '../../../../../platform/keybinding/common/keybinding.js';
12
import { IKeyboardMapper } from '../../../../../platform/keyboardLayout/common/keyboardMapper.js';
13
import { FileAccess } from '../../../../../base/common/network.js';
14
15
export interface IResolvedKeybinding {
16
label: string | null;
17
ariaLabel: string | null;
18
electronAccelerator: string | null;
19
userSettingsLabel: string | null;
20
isWYSIWYG: boolean;
21
isMultiChord: boolean;
22
dispatchParts: (string | null)[];
23
singleModifierDispatchParts: (SingleModifierChord | null)[];
24
}
25
26
function toIResolvedKeybinding(kb: ResolvedKeybinding): IResolvedKeybinding {
27
return {
28
label: kb.getLabel(),
29
ariaLabel: kb.getAriaLabel(),
30
electronAccelerator: kb.getElectronAccelerator(),
31
userSettingsLabel: kb.getUserSettingsLabel(),
32
isWYSIWYG: kb.isWYSIWYG(),
33
isMultiChord: kb.hasMultipleChords(),
34
dispatchParts: kb.getDispatchChords(),
35
singleModifierDispatchParts: kb.getSingleModifierDispatchChords()
36
};
37
}
38
39
export function assertResolveKeyboardEvent(mapper: IKeyboardMapper, keyboardEvent: IKeyboardEvent, expected: IResolvedKeybinding): void {
40
const actual = toIResolvedKeybinding(mapper.resolveKeyboardEvent(keyboardEvent));
41
assert.deepStrictEqual(actual, expected);
42
}
43
44
export function assertResolveKeybinding(mapper: IKeyboardMapper, keybinding: Keybinding, expected: IResolvedKeybinding[]): void {
45
const actual: IResolvedKeybinding[] = mapper.resolveKeybinding(keybinding).map(toIResolvedKeybinding);
46
assert.deepStrictEqual(actual, expected);
47
}
48
49
export function readRawMapping<T>(file: string): Promise<T> {
50
return fs.promises.readFile(FileAccess.asFileUri(`vs/workbench/services/keybinding/test/node/${file}.js`).fsPath).then((buff) => {
51
const contents = buff.toString();
52
const func = new Function('define', contents);// CodeQL [SM01632] This is used in tests and we read the files as JS to avoid slowing down TS compilation
53
let rawMappings: T | null = null;
54
func(function (value: T) {
55
rawMappings = value;
56
});
57
return rawMappings!;
58
});
59
}
60
61
export function assertMapping(writeFileIfDifferent: boolean, mapper: IKeyboardMapper, file: string): Promise<void> {
62
const filePath = path.normalize(FileAccess.asFileUri(`vs/workbench/services/keybinding/test/node/${file}`).fsPath);
63
64
return fs.promises.readFile(filePath).then((buff) => {
65
const expected = buff.toString().replace(/\r\n/g, '\n');
66
const actual = mapper.dumpDebugInfo().replace(/\r\n/g, '\n');
67
if (actual !== expected && writeFileIfDifferent) {
68
const destPath = filePath.replace(/[\/\\]out[\/\\]vs[\/\\]workbench/, '/src/vs/workbench');
69
Promises.writeFile(destPath, actual);
70
}
71
assert.deepStrictEqual(actual, expected);
72
});
73
}
74
75