Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/keybinding/common/keybindingIO.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 { KeybindingParser } from '../../../../base/common/keybindingParser.js';
7
import { Keybinding } from '../../../../base/common/keybindings.js';
8
import { ContextKeyExpr, ContextKeyExpression } from '../../../../platform/contextkey/common/contextkey.js';
9
import { ResolvedKeybindingItem } from '../../../../platform/keybinding/common/resolvedKeybindingItem.js';
10
11
export interface IUserKeybindingItem {
12
keybinding: Keybinding | null;
13
command: string | null;
14
commandArgs?: any;
15
when: ContextKeyExpression | undefined;
16
_sourceKey: string | undefined; /** captures `key` field from `keybindings.json`; `this.keybinding !== null` implies `_sourceKey !== null` */
17
}
18
19
export class KeybindingIO {
20
21
public static writeKeybindingItem(out: OutputBuilder, item: ResolvedKeybindingItem): void {
22
if (!item.resolvedKeybinding) {
23
return;
24
}
25
const quotedSerializedKeybinding = JSON.stringify(item.resolvedKeybinding.getUserSettingsLabel());
26
out.write(`{ "key": ${rightPaddedString(quotedSerializedKeybinding + ',', 25)} "command": `);
27
28
const quotedSerializedWhen = item.when ? JSON.stringify(item.when.serialize()) : '';
29
const quotedSerializeCommand = JSON.stringify(item.command);
30
if (quotedSerializedWhen.length > 0) {
31
out.write(`${quotedSerializeCommand},`);
32
out.writeLine();
33
out.write(` "when": ${quotedSerializedWhen}`);
34
} else {
35
out.write(`${quotedSerializeCommand}`);
36
}
37
if (item.commandArgs) {
38
out.write(',');
39
out.writeLine();
40
out.write(` "args": ${JSON.stringify(item.commandArgs)}`);
41
}
42
out.write(' }');
43
}
44
45
public static readUserKeybindingItem(input: Object): IUserKeybindingItem {
46
const keybinding = 'key' in input && typeof input.key === 'string'
47
? KeybindingParser.parseKeybinding(input.key)
48
: null;
49
const when = 'when' in input && typeof input.when === 'string'
50
? ContextKeyExpr.deserialize(input.when)
51
: undefined;
52
const command = 'command' in input && typeof input.command === 'string'
53
? input.command
54
: null;
55
const commandArgs = 'args' in input && typeof input.args !== 'undefined'
56
? input.args
57
: undefined;
58
return {
59
keybinding,
60
command,
61
commandArgs,
62
when,
63
_sourceKey: 'key' in input && typeof input.key === 'string' ? input.key : undefined,
64
};
65
}
66
}
67
68
function rightPaddedString(str: string, minChars: number): string {
69
if (str.length < minChars) {
70
return str + (new Array(minChars - str.length).join(' '));
71
}
72
return str;
73
}
74
75
export class OutputBuilder {
76
77
private _lines: string[] = [];
78
private _currentLine: string = '';
79
80
write(str: string): void {
81
this._currentLine += str;
82
}
83
84
writeLine(str: string = ''): void {
85
this._lines.push(this._currentLine + str);
86
this._currentLine = '';
87
}
88
89
toString(): string {
90
this.writeLine();
91
return this._lines.join('\n');
92
}
93
}
94
95