Path: blob/main/src/vs/workbench/services/keybinding/common/keybindingIO.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 { KeybindingParser } from '../../../../base/common/keybindingParser.js';6import { Keybinding } from '../../../../base/common/keybindings.js';7import { ContextKeyExpr, ContextKeyExpression } from '../../../../platform/contextkey/common/contextkey.js';8import { ResolvedKeybindingItem } from '../../../../platform/keybinding/common/resolvedKeybindingItem.js';910export interface IUserKeybindingItem {11keybinding: Keybinding | null;12command: string | null;13commandArgs?: any;14when: ContextKeyExpression | undefined;15_sourceKey: string | undefined; /** captures `key` field from `keybindings.json`; `this.keybinding !== null` implies `_sourceKey !== null` */16}1718export class KeybindingIO {1920public static writeKeybindingItem(out: OutputBuilder, item: ResolvedKeybindingItem): void {21if (!item.resolvedKeybinding) {22return;23}24const quotedSerializedKeybinding = JSON.stringify(item.resolvedKeybinding.getUserSettingsLabel());25out.write(`{ "key": ${rightPaddedString(quotedSerializedKeybinding + ',', 25)} "command": `);2627const quotedSerializedWhen = item.when ? JSON.stringify(item.when.serialize()) : '';28const quotedSerializeCommand = JSON.stringify(item.command);29if (quotedSerializedWhen.length > 0) {30out.write(`${quotedSerializeCommand},`);31out.writeLine();32out.write(` "when": ${quotedSerializedWhen}`);33} else {34out.write(`${quotedSerializeCommand}`);35}36if (item.commandArgs) {37out.write(',');38out.writeLine();39out.write(` "args": ${JSON.stringify(item.commandArgs)}`);40}41out.write(' }');42}4344public static readUserKeybindingItem(input: Object): IUserKeybindingItem {45const keybinding = 'key' in input && typeof input.key === 'string'46? KeybindingParser.parseKeybinding(input.key)47: null;48const when = 'when' in input && typeof input.when === 'string'49? ContextKeyExpr.deserialize(input.when)50: undefined;51const command = 'command' in input && typeof input.command === 'string'52? input.command53: null;54const commandArgs = 'args' in input && typeof input.args !== 'undefined'55? input.args56: undefined;57return {58keybinding,59command,60commandArgs,61when,62_sourceKey: 'key' in input && typeof input.key === 'string' ? input.key : undefined,63};64}65}6667function rightPaddedString(str: string, minChars: number): string {68if (str.length < minChars) {69return str + (new Array(minChars - str.length).join(' '));70}71return str;72}7374export class OutputBuilder {7576private _lines: string[] = [];77private _currentLine: string = '';7879write(str: string): void {80this._currentLine += str;81}8283writeLine(str: string = ''): void {84this._lines.push(this._currentLine + str);85this._currentLine = '';86}8788toString(): string {89this.writeLine();90return this._lines.join('\n');91}92}939495