keybindingService.ts 19.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import * as nls from 'vs/nls';
J
Johannes Rieken 已提交
8 9
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
import { IJSONSchema } from 'vs/base/common/jsonSchema';
A
Renames  
Alex Dima 已提交
10
import { ResolvedKeybinding, KeyCode, USER_SETTINGS, Keybinding, KeybindingType, SimpleKeybinding, KeyCodeUtils } from 'vs/base/common/keyCodes';
A
Alex Dima 已提交
11
import { UILabelProvider, AriaLabelProvider, UserSettingsLabelProvider, ElectronAcceleratorLabelProvider } from 'vs/platform/keybinding/common/keybindingLabels';
A
Alex Dima 已提交
12
import { OS, OperatingSystem } from 'vs/base/common/platform';
J
Johannes Rieken 已提交
13
import { toDisposable } from 'vs/base/common/lifecycle';
14
import { ExtensionMessageCollector, ExtensionsRegistry } from 'vs/platform/extensions/common/extensionsRegistry';
J
Johannes Rieken 已提交
15
import { Extensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
16
import { AbstractKeybindingService } from 'vs/platform/keybinding/common/abstractKeybindingService';
J
Johannes Rieken 已提交
17
import { IStatusbarService } from 'vs/platform/statusbar/common/statusbar';
A
Alex Dima 已提交
18
import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver';
S
Sandeep Somavarapu 已提交
19
import { ICommandService } from 'vs/platform/commands/common/commands';
20
import { IKeybindingEvent, IUserFriendlyKeybinding, KeybindingSource, IKeyboardEvent } from 'vs/platform/keybinding/common/keybinding';
21
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
A
Renames  
Alex Dima 已提交
22
import { IKeybindingRule, IKeybindingItem, KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
J
Johannes Rieken 已提交
23
import { Registry } from 'vs/platform/platform';
24 25
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { keybindingsTelemetry } from 'vs/platform/telemetry/common/telemetryUtils';
A
Alex Dima 已提交
26
import { getCurrentKeyboardLayout, getNativeLabelProviderRemaps } from 'vs/workbench/services/keybinding/electron-browser/nativeKeymap';
J
Johannes Rieken 已提交
27 28 29
import { IMessageService } from 'vs/platform/message/common/message';
import { ConfigWatcher } from 'vs/base/node/config';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
30 31
import * as dom from 'vs/base/browser/dom';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
32
import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem';
A
Alex Dima 已提交
33
import { KeybindingIO, OutputBuilder } from 'vs/workbench/services/keybinding/common/keybindingIO';
E
Erich Gamma 已提交
34 35 36 37 38 39 40 41 42 43

interface ContributedKeyBinding {
	command: string;
	key: string;
	when?: string;
	mac?: string;
	linux?: string;
	win?: string;
}

44
function isContributedKeyBindingsArray(thing: ContributedKeyBinding | ContributedKeyBinding[]): thing is ContributedKeyBinding[] {
E
Erich Gamma 已提交
45 46 47 48 49 50 51 52 53
	return Array.isArray(thing);
}

function isValidContributedKeyBinding(keyBinding: ContributedKeyBinding, rejects: string[]): boolean {
	if (!keyBinding) {
		rejects.push(nls.localize('nonempty', "expected non-empty value."));
		return false;
	}
	if (typeof keyBinding.command !== 'string') {
B
Benjamin Pasero 已提交
54
		rejects.push(nls.localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'command'));
E
Erich Gamma 已提交
55 56 57
		return false;
	}
	if (typeof keyBinding.key !== 'string') {
B
Benjamin Pasero 已提交
58
		rejects.push(nls.localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'key'));
E
Erich Gamma 已提交
59 60 61
		return false;
	}
	if (keyBinding.when && typeof keyBinding.when !== 'string') {
B
Benjamin Pasero 已提交
62
		rejects.push(nls.localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'when'));
E
Erich Gamma 已提交
63 64 65
		return false;
	}
	if (keyBinding.mac && typeof keyBinding.mac !== 'string') {
B
Benjamin Pasero 已提交
66
		rejects.push(nls.localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'mac'));
E
Erich Gamma 已提交
67 68 69
		return false;
	}
	if (keyBinding.linux && typeof keyBinding.linux !== 'string') {
B
Benjamin Pasero 已提交
70
		rejects.push(nls.localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'linux'));
E
Erich Gamma 已提交
71 72 73
		return false;
	}
	if (keyBinding.win && typeof keyBinding.win !== 'string') {
B
Benjamin Pasero 已提交
74
		rejects.push(nls.localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'win'));
E
Erich Gamma 已提交
75 76 77 78 79
		return false;
	}
	return true;
}

80
let keybindingType: IJSONSchema = {
E
Erich Gamma 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
	type: 'object',
	default: { command: '', key: '' },
	properties: {
		command: {
			description: nls.localize('vscode.extension.contributes.keybindings.command', 'Identifier of the command to run when keybinding is triggered.'),
			type: 'string'
		},
		key: {
			description: nls.localize('vscode.extension.contributes.keybindings.key', 'Key or key sequence (separate keys with plus-sign and sequences with space, e.g Ctrl+O and Ctrl+L L for a chord'),
			type: 'string'
		},
		mac: {
			description: nls.localize('vscode.extension.contributes.keybindings.mac', 'Mac specific key or key sequence.'),
			type: 'string'
		},
		linux: {
			description: nls.localize('vscode.extension.contributes.keybindings.linux', 'Linux specific key or key sequence.'),
			type: 'string'
		},
		win: {
			description: nls.localize('vscode.extension.contributes.keybindings.win', 'Windows specific key or key sequence.'),
			type: 'string'
		},
		when: {
			description: nls.localize('vscode.extension.contributes.keybindings.when', 'Condition when the key is active.'),
			type: 'string'
		}
	}
};

A
Alex Dima 已提交
111
let keybindingsExtPoint = ExtensionsRegistry.registerExtensionPoint<ContributedKeyBinding | ContributedKeyBinding[]>('keybindings', [], {
E
Erich Gamma 已提交
112 113 114 115 116 117 118 119 120 121
	description: nls.localize('vscode.extension.contributes.keybindings', "Contributes keybindings."),
	oneOf: [
		keybindingType,
		{
			type: 'array',
			items: keybindingType
		}
	]
});

A
Alex Dima 已提交
122 123
export class FancyResolvedKeybinding extends ResolvedKeybinding {

A
Alex Dima 已提交
124 125
	private readonly _firstPart: SimpleKeybinding;
	private readonly _chordPart: SimpleKeybinding;
A
Alex Dima 已提交
126

A
Renames  
Alex Dima 已提交
127
	constructor(actual: Keybinding) {
A
Alex Dima 已提交
128
		super();
A
Alex Dima 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		if (actual === null) {
			this._firstPart = null;
			this._chordPart = null;
		} else if (actual.type === KeybindingType.Chord) {
			this._firstPart = actual.firstPart;
			this._chordPart = actual.chordPart;
		} else {
			this._firstPart = actual;
			this._chordPart = null;
		}
	}

	private _keyCodeToUILabel(keyCode: KeyCode): string {
		if (OS === OperatingSystem.Macintosh) {
			switch (keyCode) {
				case KeyCode.LeftArrow:
					return '';
				case KeyCode.UpArrow:
					return '';
				case KeyCode.RightArrow:
					return '';
				case KeyCode.DownArrow:
					return '';
			}
		}
		let remaps = getNativeLabelProviderRemaps();
		if (remaps[keyCode] !== null) {
			return remaps[keyCode].render();
		}

		return KeyCodeUtils.toString(keyCode);
A
Alex Dima 已提交
160 161 162
	}

	public getLabel(): string {
A
Alex Dima 已提交
163 164 165 166
		let firstPart = this._firstPart ? this._keyCodeToUILabel(this._firstPart.keyCode) : null;
		let chordPart = this._chordPart ? this._keyCodeToUILabel(this._chordPart.keyCode) : null;
		return UILabelProvider.toLabel(this._firstPart, firstPart, this._chordPart, chordPart, OS);
	}
A
Alex Dima 已提交
167

A
Alex Dima 已提交
168 169 170 171 172 173
	private _keyCodeToAriaLabel(keyCode: KeyCode): string {
		let remaps = getNativeLabelProviderRemaps();
		if (remaps[keyCode] !== null) {
			return remaps[keyCode].render();
		}
		return KeyCodeUtils.toString(keyCode);
A
Alex Dima 已提交
174 175 176
	}

	public getAriaLabel(): string {
A
Alex Dima 已提交
177 178 179
		let firstPart = this._firstPart ? this._keyCodeToAriaLabel(this._firstPart.keyCode) : null;
		let chordPart = this._chordPart ? this._keyCodeToAriaLabel(this._chordPart.keyCode) : null;
		return AriaLabelProvider.toLabel(this._firstPart, firstPart, this._chordPart, chordPart, OS);
A
Alex Dima 已提交
180 181 182
	}

	public getHTMLLabel(): IHTMLContentElement[] {
A
Alex Dima 已提交
183 184 185
		let firstPart = this._firstPart ? this._keyCodeToUILabel(this._firstPart.keyCode) : null;
		let chordPart = this._chordPart ? this._keyCodeToUILabel(this._chordPart.keyCode) : null;
		return UILabelProvider.toHTMLLabel(this._firstPart, firstPart, this._chordPart, chordPart, OS);
A
Alex Dima 已提交
186 187
	}

A
Alex Dima 已提交
188 189 190 191 192
	private _keyCodeToElectronAccelerator(keyCode: KeyCode): string {
		if (keyCode >= KeyCode.NUMPAD_0 && keyCode <= KeyCode.NUMPAD_DIVIDE) {
			// Electron cannot handle numpad keys
			return null;
		}
A
Alex Dima 已提交
193

A
Alex Dima 已提交
194 195 196 197 198 199 200 201 202
		switch (keyCode) {
			case KeyCode.UpArrow:
				return 'Up';
			case KeyCode.DownArrow:
				return 'Down';
			case KeyCode.LeftArrow:
				return 'Left';
			case KeyCode.RightArrow:
				return 'Right';
A
Alex Dima 已提交
203 204
		}

A
Alex Dima 已提交
205 206 207 208 209 210 211 212 213 214 215
		let remaps = getNativeLabelProviderRemaps();

		if (remaps[keyCode] === null) {
			return KeyCodeUtils.toString(keyCode);
		}

		let remapped = remaps[keyCode].render();
		if (OS === OperatingSystem.Windows) {
			// electron menus always do the correct rendering on Windows
			return remapped;
		} else {
A
Alex Dima 已提交
216 217
			// electron menus are incorrect in rendering (linux) and in rendering and interpreting (mac)
			// for non US standard keyboard layouts
A
Alex Dima 已提交
218 219 220 221
			if (remapped !== KeyCodeUtils.toString(keyCode)) {
				return null;
			}
			return remapped;
A
Alex Dima 已提交
222 223
		}
	}
224

A
Alex Dima 已提交
225 226 227 228 229 230 231 232
	public getElectronAccelerator(): string {
		if (this._chordPart !== null) {
			// Electron cannot handle chords
			return null;
		}

		let firstPart = this._firstPart ? this._keyCodeToElectronAccelerator(this._firstPart.keyCode) : null;
		return ElectronAcceleratorLabelProvider.toLabel(this._firstPart, firstPart, null, null, OS);
233 234
	}

235
	public getUserSettingsLabel(): string {
A
Alex Dima 已提交
236 237 238
		let firstPart = this._firstPart ? USER_SETTINGS.fromKeyCode(this._firstPart.keyCode) : null;
		let chordPart = this._chordPart ? USER_SETTINGS.fromKeyCode(this._chordPart.keyCode) : null;
		let result = UserSettingsLabelProvider.toLabel(this._firstPart, firstPart, this._chordPart, chordPart, OS);
239
		return result.toLowerCase();
240
	}
241 242

	public isChord(): boolean {
A
Alex Dima 已提交
243
		return (this._chordPart ? true : false);
244 245 246
	}

	public hasCtrlModifier(): boolean {
A
Alex Dima 已提交
247
		if (this._chordPart) {
248 249
			return false;
		}
A
Alex Dima 已提交
250
		return this._firstPart.ctrlKey;
251 252 253
	}

	public hasShiftModifier(): boolean {
A
Alex Dima 已提交
254
		if (this._chordPart) {
255 256
			return false;
		}
A
Alex Dima 已提交
257
		return this._firstPart.shiftKey;
258 259 260
	}

	public hasAltModifier(): boolean {
A
Alex Dima 已提交
261
		if (this._chordPart) {
262 263
			return false;
		}
A
Alex Dima 已提交
264
		return this._firstPart.altKey;
265 266 267
	}

	public hasMetaModifier(): boolean {
A
Alex Dima 已提交
268
		if (this._chordPart) {
269 270
			return false;
		}
A
Alex Dima 已提交
271
		return this._firstPart.metaKey;
272
	}
273 274

	public getDispatchParts(): [string, string] {
A
Alex Dima 已提交
275 276 277
		let firstPart = this._firstPart ? this._getDispatchStr(this._firstPart) : null;
		let chordPart = this._chordPart ? this._getDispatchStr(this._chordPart) : null;
		return [firstPart, chordPart];
278
	}
279

A
Alex Dima 已提交
280
	private _getDispatchStr(keybinding: SimpleKeybinding): string {
281 282 283
		if (keybinding.isModifierKey()) {
			return null;
		}
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
		let result = '';

		if (keybinding.ctrlKey) {
			result += 'ctrl+';
		}
		if (keybinding.shiftKey) {
			result += 'shift+';
		}
		if (keybinding.altKey) {
			result += 'alt+';
		}
		if (keybinding.metaKey) {
			result += 'meta+';
		}
		result += KeyCodeUtils.toString(keybinding.keyCode);

		return result;
	}
A
Alex Dima 已提交
302 303
}

304 305 306 307
export class WorkbenchKeybindingService extends AbstractKeybindingService {

	private _cachedResolver: KeybindingResolver;
	private _firstTimeComputingResolver: boolean;
308
	private userKeybindings: ConfigWatcher<IUserFriendlyKeybinding[]>;
E
Erich Gamma 已提交
309

310
	constructor(
B
Benjamin Pasero 已提交
311
		windowElement: Window,
312
		@IContextKeyService contextKeyService: IContextKeyService,
313
		@ICommandService commandService: ICommandService,
314
		@ITelemetryService private telemetryService: ITelemetryService,
315
		@IMessageService messageService: IMessageService,
316
		@IEnvironmentService environmentService: IEnvironmentService,
317
		@IStatusbarService statusBarService: IStatusbarService
318
	) {
319
		super(contextKeyService, commandService, messageService, statusBarService);
320

321 322 323
		this._cachedResolver = null;
		this._firstTimeComputingResolver = true;

324
		this.userKeybindings = new ConfigWatcher(environmentService.appKeybindingsPath, { defaultConfig: [] });
B
Benjamin Pasero 已提交
325
		this.toDispose.push(toDisposable(() => this.userKeybindings.dispose()));
326

E
Erich Gamma 已提交
327 328 329 330 331 332 333 334
		keybindingsExtPoint.setHandler((extensions) => {
			let commandAdded = false;

			for (let extension of extensions) {
				commandAdded = this._handleKeybindingsExtensionPointUser(extension.description.isBuiltin, extension.value, extension.collector) || commandAdded;
			}

			if (commandAdded) {
C
Christof Marti 已提交
335
				this.updateResolver({ source: KeybindingSource.Default });
E
Erich Gamma 已提交
336 337
			}
		});
338

C
Christof Marti 已提交
339 340 341 342
		this.toDispose.push(this.userKeybindings.onDidUpdateConfiguration(event => this.updateResolver({
			source: KeybindingSource.User,
			keybindings: event.config
		})));
343

B
Benjamin Pasero 已提交
344
		this.toDispose.push(dom.addDisposableListener(windowElement, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
345
			let keyEvent = new StandardKeyboardEvent(e);
346
			let shouldPreventDefault = this._dispatch(keyEvent, keyEvent.target);
347 348 349 350 351
			if (shouldPreventDefault) {
				keyEvent.preventDefault();
			}
		}));

C
Christof Marti 已提交
352
		keybindingsTelemetry(telemetryService, this);
A
Alex Dima 已提交
353
		let data = getCurrentKeyboardLayout();
A
Alex Dima 已提交
354 355 356
		telemetryService.publicLog('keyboardLayout', {
			currentKeyboardLayout: data
		});
E
Erich Gamma 已提交
357 358
	}

359 360 361 362 363 364 365 366
	private _safeGetConfig(): IUserFriendlyKeybinding[] {
		let rawConfig = this.userKeybindings.getConfig();
		if (Array.isArray(rawConfig)) {
			return rawConfig;
		}
		return [];
	}

367
	public customKeybindingsCount(): number {
368
		let userKeybindings = this._safeGetConfig();
369 370

		return userKeybindings.length;
371 372
	}

373 374 375 376 377 378 379
	private updateResolver(event: IKeybindingEvent): void {
		this._cachedResolver = null;
		this._onDidUpdateKeybindings.fire(event);
	}

	protected _getResolver(): KeybindingResolver {
		if (!this._cachedResolver) {
380 381
			const defaults = this._toNormalizedKeybindingItems(KeybindingsRegistry.getDefaultKeybindings(), true);
			const overrides = this._toNormalizedKeybindingItems(this._getExtraKeybindings(this._firstTimeComputingResolver), false);
382
			this._cachedResolver = new KeybindingResolver(defaults, overrides);
383 384 385
			this._firstTimeComputingResolver = false;
		}
		return this._cachedResolver;
386 387
	}

388 389
	private _toNormalizedKeybindingItems(items: IKeybindingItem[], isDefault: boolean): ResolvedKeybindingItem[] {
		let result: ResolvedKeybindingItem[] = [], resultLen = 0;
390 391 392
		for (let i = 0, len = items.length; i < len; i++) {
			const item = items[i];
			const when = (item.when ? item.when.normalize() : null);
A
Alex Dima 已提交
393
			const keybinding = item.keybinding;
394
			const resolvedKeybinding = (keybinding ? this.resolveKeybinding(keybinding) : null);
395

396
			result[resultLen++] = new ResolvedKeybindingItem(resolvedKeybinding, item.command, item.commandArgs, when, isDefault);
397 398 399
		}

		return result;
400 401 402
	}

	private _getExtraKeybindings(isFirstTime: boolean): IKeybindingItem[] {
403
		let extraUserKeybindings: IUserFriendlyKeybinding[] = this._safeGetConfig();
404 405
		if (!isFirstTime) {
			let cnt = extraUserKeybindings.length;
406

407 408 409
			this.telemetryService.publicLog('customKeybindingsChanged', {
				keyCount: cnt
			});
410
		}
411

A
Alex Dima 已提交
412
		return extraUserKeybindings.map((k, i) => KeybindingIO.readKeybindingItem(k, i, OS));
413 414
	}

415
	public resolveKeybinding(kb: Keybinding): ResolvedKeybinding {
A
Alex Dima 已提交
416
		return new FancyResolvedKeybinding(kb);
A
Alex Dima 已提交
417 418
	}

419 420 421 422 423 424 425 426 427 428 429
	public resolveKeyboardEvent(keyboardEvent: IKeyboardEvent): ResolvedKeybinding {
		let keybinding = new SimpleKeybinding(
			keyboardEvent.ctrlKey,
			keyboardEvent.shiftKey,
			keyboardEvent.altKey,
			keyboardEvent.metaKey,
			keyboardEvent.keyCode
		);
		return this.resolveKeybinding(keybinding);
	}

430
	private _handleKeybindingsExtensionPointUser(isBuiltin: boolean, keybindings: ContributedKeyBinding | ContributedKeyBinding[], collector: ExtensionMessageCollector): boolean {
E
Erich Gamma 已提交
431 432 433 434 435 436 437 438 439 440 441
		if (isContributedKeyBindingsArray(keybindings)) {
			let commandAdded = false;
			for (let i = 0, len = keybindings.length; i < len; i++) {
				commandAdded = this._handleKeybinding(isBuiltin, i + 1, keybindings[i], collector) || commandAdded;
			}
			return commandAdded;
		} else {
			return this._handleKeybinding(isBuiltin, 1, keybindings, collector);
		}
	}

442
	private _handleKeybinding(isBuiltin: boolean, idx: number, keybindings: ContributedKeyBinding, collector: ExtensionMessageCollector): boolean {
E
Erich Gamma 已提交
443 444 445 446 447 448 449

		let rejects: string[] = [];
		let commandAdded = false;

		if (isValidContributedKeyBinding(keybindings, rejects)) {
			let rule = this._asCommandRule(isBuiltin, idx++, keybindings);
			if (rule) {
A
Alex Dima 已提交
450
				KeybindingsRegistry.registerKeybindingRule(rule);
E
Erich Gamma 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
				commandAdded = true;
			}
		}

		if (rejects.length > 0) {
			collector.error(nls.localize(
				'invalid.keybindings',
				"Invalid `contributes.{0}`: {1}",
				keybindingsExtPoint.name,
				rejects.join('\n')
			));
		}

		return commandAdded;
	}

A
Alex Dima 已提交
467
	private _asCommandRule(isBuiltin: boolean, idx: number, binding: ContributedKeyBinding): IKeybindingRule {
E
Erich Gamma 已提交
468 469 470 471 472 473 474 475 476 477 478 479

		let {command, when, key, mac, linux, win} = binding;

		let weight: number;
		if (isBuiltin) {
			weight = KeybindingsRegistry.WEIGHT.builtinExtension(idx);
		} else {
			weight = KeybindingsRegistry.WEIGHT.externalExtension(idx);
		}

		let desc = {
			id: command,
480
			when: ContextKeyExpr.deserialize(when),
E
Erich Gamma 已提交
481
			weight: weight,
A
Alex Dima 已提交
482 483 484 485
			primary: KeybindingIO.readKeybinding(key, OS),
			mac: mac && { primary: KeybindingIO.readKeybinding(mac, OS) },
			linux: linux && { primary: KeybindingIO.readKeybinding(linux, OS) },
			win: win && { primary: KeybindingIO.readKeybinding(win, OS) }
B
Benjamin Pasero 已提交
486
		};
E
Erich Gamma 已提交
487 488

		if (!desc.primary && !desc.mac && !desc.linux && !desc.win) {
489
			return undefined;
E
Erich Gamma 已提交
490 491 492 493
		}

		return desc;
	}
A
Alex Dima 已提交
494 495 496 497 498 499 500 501 502 503 504 505

	public getDefaultKeybindings(): string {
		const resolver = this._getResolver();
		const defaultKeybindings = resolver.getDefaultKeybindings();
		const boundCommands = resolver.getDefaultBoundCommands();
		return (
			WorkbenchKeybindingService._getDefaultKeybindings(defaultKeybindings)
			+ '\n\n'
			+ WorkbenchKeybindingService._getAllCommandsAsComment(boundCommands)
		);
	}

506
	private static _getDefaultKeybindings(defaultKeybindings: ResolvedKeybindingItem[]): string {
A
Alex Dima 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
		let out = new OutputBuilder();
		out.writeLine('[');

		let lastIndex = defaultKeybindings.length - 1;
		defaultKeybindings.forEach((k, index) => {
			KeybindingIO.writeKeybindingItem(out, k, OS);
			if (index !== lastIndex) {
				out.writeLine(',');
			} else {
				out.writeLine();
			}
		});
		out.writeLine(']');
		return out.toString();
	}

	private static _getAllCommandsAsComment(boundCommands: Map<string, boolean>): string {
S
Sandeep Somavarapu 已提交
524
		const unboundCommands = KeybindingResolver.getAllUnboundCommands(boundCommands);
A
Alex Dima 已提交
525 526 527
		let pretty = unboundCommands.sort().join('\n// - ');
		return '// ' + nls.localize('unboundCommands', "Here are other available commands: ") + '\n// - ' + pretty;
	}
528
}
529

530
let schemaId = 'vscode://schemas/keybindings';
531
let schema: IJSONSchema = {
532 533 534 535 536 537
	'id': schemaId,
	'type': 'array',
	'title': nls.localize('keybindings.json.title', "Keybindings configuration"),
	'items': {
		'required': ['key'],
		'type': 'object',
538
		'defaultSnippets': [{ 'body': { 'key': '$1', 'command': '$2', 'when': '$3' } }],
539 540 541
		'properties': {
			'key': {
				'type': 'string',
542
				'description': nls.localize('keybindings.json.key', "Key or key sequence (separated by space)"),
543 544
			},
			'command': {
545
				'description': nls.localize('keybindings.json.command', "Name of the command to execute"),
546 547 548
			},
			'when': {
				'type': 'string',
549 550 551 552
				'description': nls.localize('keybindings.json.when', "Condition when the key is active.")
			},
			'args': {
				'description': nls.localize('keybindings.json.args', "Arguments to pass to the command to execute.")
553 554 555 556 557
			}
		}
	}
};

558
let schemaRegistry = <IJSONContributionRegistry>Registry.as(Extensions.JSONContribution);
559
schemaRegistry.registerSchema(schemaId, schema);