keybindingService.ts 22.7 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
import { IJSONSchema } from 'vs/base/common/jsonSchema';
9
import { ResolvedKeybinding, Keybinding } from 'vs/base/common/keyCodes';
A
Alex Dima 已提交
10
import { OS, OperatingSystem } from 'vs/base/common/platform';
11
import { ExtensionMessageCollector, ExtensionsRegistry } from 'vs/workbench/services/extensions/common/extensionsRegistry';
J
Johannes Rieken 已提交
12
import { Extensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
13
import { AbstractKeybindingService } from 'vs/platform/keybinding/common/abstractKeybindingService';
J
Johannes Rieken 已提交
14
import { IStatusbarService } from 'vs/platform/statusbar/common/statusbar';
A
Alex Dima 已提交
15
import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver';
S
Sandeep Somavarapu 已提交
16
import { ICommandService } from 'vs/platform/commands/common/commands';
17
import { IKeybindingEvent, IUserFriendlyKeybinding, KeybindingSource, IKeyboardEvent } from 'vs/platform/keybinding/common/keybinding';
18
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
19
import { IKeybindingItem, KeybindingsRegistry, IKeybindingRule2, KeybindingRuleSource } from 'vs/platform/keybinding/common/keybindingsRegistry';
20
import { Registry } from 'vs/platform/registry/common/platform';
21 22
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { keybindingsTelemetry } from 'vs/platform/telemetry/common/telemetryUtils';
J
Johannes Rieken 已提交
23 24
import { ConfigWatcher } from 'vs/base/node/config';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
25 26
import * as dom from 'vs/base/browser/dom';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
27
import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem';
28
import { KeybindingIO, OutputBuilder, IUserKeybindingItem } from 'vs/workbench/services/keybinding/common/keybindingIO';
29
import * as nativeKeymap from 'native-keymap';
30
import { IKeyboardMapper, CachedKeyboardMapper } from 'vs/workbench/services/keybinding/common/keyboardMapper';
A
Alex Dima 已提交
31 32
import { WindowsKeyboardMapper, IWindowsKeyboardMapping, windowsKeyboardMappingEquals } from 'vs/workbench/services/keybinding/common/windowsKeyboardMapper';
import { IMacLinuxKeyboardMapping, MacLinuxKeyboardMapper, macLinuxKeyboardMappingEquals } from 'vs/workbench/services/keybinding/common/macLinuxKeyboardMapper';
33
import { MacLinuxFallbackKeyboardMapper } from 'vs/workbench/services/keybinding/common/macLinuxFallbackKeyboardMapper';
A
Alex Dima 已提交
34
import Event, { Emitter } from 'vs/base/common/event';
35 36
import { Extensions as ConfigExtensions, IConfigurationRegistry, IConfigurationNode } from 'vs/platform/configuration/common/configurationRegistry';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
B
Benjamin Pasero 已提交
37
import { onUnexpectedError } from 'vs/base/common/errors';
38
import { release } from 'os';
39
import { INotificationService } from 'vs/platform/notification/common/notification';
40

A
Alex Dima 已提交
41
export class KeyboardMapperFactory {
M
Matt Bierner 已提交
42
	public static readonly INSTANCE = new KeyboardMapperFactory();
A
Alex Dima 已提交
43 44 45 46

	private _layoutInfo: nativeKeymap.IKeyboardLayoutInfo;
	private _rawMapping: nativeKeymap.IKeyboardMapping;
	private _keyboardMapper: IKeyboardMapper;
A
Alex Dima 已提交
47
	private _initialized: boolean;
A
Alex Dima 已提交
48 49

	private _onDidChangeKeyboardMapper: Emitter<void> = new Emitter<void>();
50
	public readonly onDidChangeKeyboardMapper: Event<void> = this._onDidChangeKeyboardMapper.event;
A
Alex Dima 已提交
51 52 53 54 55

	private constructor() {
		this._layoutInfo = null;
		this._rawMapping = null;
		this._keyboardMapper = null;
A
Alex Dima 已提交
56
		this._initialized = false;
57
	}
A
Alex Dima 已提交
58

59
	public _onKeyboardLayoutChanged(): void {
A
Alex Dima 已提交
60
		if (this._initialized) {
61
			this._setKeyboardData(nativeKeymap.getCurrentKeyboardLayout(), nativeKeymap.getKeyMap());
A
Alex Dima 已提交
62 63 64
		}
	}

65
	public getKeyboardMapper(dispatchConfig: DispatchConfig): IKeyboardMapper {
A
Alex Dima 已提交
66
		if (!this._initialized) {
67
			this._setKeyboardData(nativeKeymap.getCurrentKeyboardLayout(), nativeKeymap.getKeyMap());
A
Alex Dima 已提交
68
		}
69 70 71 72
		if (dispatchConfig === DispatchConfig.KeyCode) {
			// Forcefully set to use keyCode
			return new MacLinuxFallbackKeyboardMapper(OS);
		}
A
Alex Dima 已提交
73 74 75
		return this._keyboardMapper;
	}

A
Alex Dima 已提交
76 77
	public getCurrentKeyboardLayout(): nativeKeymap.IKeyboardLayoutInfo {
		if (!this._initialized) {
78
			this._setKeyboardData(nativeKeymap.getCurrentKeyboardLayout(), nativeKeymap.getKeyMap());
A
Alex Dima 已提交
79 80 81 82
		}
		return this._layoutInfo;
	}

83
	private static _isUSStandard(_kbInfo: nativeKeymap.IKeyboardLayoutInfo): boolean {
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		if (OS === OperatingSystem.Linux) {
			const kbInfo = <nativeKeymap.ILinuxKeyboardLayoutInfo>_kbInfo;
			return (kbInfo && kbInfo.layout === 'us');
		}

		if (OS === OperatingSystem.Macintosh) {
			const kbInfo = <nativeKeymap.IMacKeyboardLayoutInfo>_kbInfo;
			return (kbInfo && kbInfo.id === 'com.apple.keylayout.US');
		}

		if (OS === OperatingSystem.Windows) {
			const kbInfo = <nativeKeymap.IWindowsKeyboardLayoutInfo>_kbInfo;
			return (kbInfo && kbInfo.name === '00000409');
		}

		return false;
	}

A
Alex Dima 已提交
102 103
	public getRawKeyboardMapping(): nativeKeymap.IKeyboardMapping {
		if (!this._initialized) {
104
			this._setKeyboardData(nativeKeymap.getCurrentKeyboardLayout(), nativeKeymap.getKeyMap());
A
Alex Dima 已提交
105 106 107 108
		}
		return this._rawMapping;
	}

109
	private _setKeyboardData(layoutInfo: nativeKeymap.IKeyboardLayoutInfo, rawMapping: nativeKeymap.IKeyboardMapping): void {
A
Alex Dima 已提交
110 111
		this._layoutInfo = layoutInfo;

112
		if (this._initialized && KeyboardMapperFactory._equals(this._rawMapping, rawMapping)) {
A
Alex Dima 已提交
113 114 115 116
			// nothing to do...
			return;
		}

A
Alex Dima 已提交
117
		this._initialized = true;
A
Alex Dima 已提交
118
		this._rawMapping = rawMapping;
119 120 121
		this._keyboardMapper = new CachedKeyboardMapper(
			KeyboardMapperFactory._createKeyboardMapper(this._layoutInfo, this._rawMapping)
		);
A
Alex Dima 已提交
122 123 124
		this._onDidChangeKeyboardMapper.fire();
	}

125
	private static _createKeyboardMapper(layoutInfo: nativeKeymap.IKeyboardLayoutInfo, rawMapping: nativeKeymap.IKeyboardMapping): IKeyboardMapper {
126
		const isUSStandard = KeyboardMapperFactory._isUSStandard(layoutInfo);
A
Alex Dima 已提交
127
		if (OS === OperatingSystem.Windows) {
128
			return new WindowsKeyboardMapper(isUSStandard, <IWindowsKeyboardMapping>rawMapping);
A
Alex Dima 已提交
129 130 131 132
		}

		if (Object.keys(rawMapping).length === 0) {
			// Looks like reading the mappings failed (most likely Mac + Japanese/Chinese keyboard layouts)
133
			return new MacLinuxFallbackKeyboardMapper(OS);
A
Alex Dima 已提交
134 135
		}

136 137 138 139 140 141 142 143
		if (OS === OperatingSystem.Macintosh) {
			const kbInfo = <nativeKeymap.IMacKeyboardLayoutInfo>layoutInfo;
			if (kbInfo.id === 'com.apple.keylayout.DVORAK-QWERTYCMD') {
				// Use keyCode based dispatching for DVORAK - QWERTY ⌘
				return new MacLinuxFallbackKeyboardMapper(OS);
			}
		}

144
		return new MacLinuxKeyboardMapper(isUSStandard, <IMacLinuxKeyboardMapping>rawMapping, OS);
A
Alex Dima 已提交
145 146 147 148 149 150 151 152
	}

	private static _equals(a: nativeKeymap.IKeyboardMapping, b: nativeKeymap.IKeyboardMapping): boolean {
		if (OS === OperatingSystem.Windows) {
			return windowsKeyboardMappingEquals(<IWindowsKeyboardMapping>a, <IWindowsKeyboardMapping>b);
		}

		return macLinuxKeyboardMappingEquals(<IMacLinuxKeyboardMapping>a, <IMacLinuxKeyboardMapping>b);
153
	}
154
}
E
Erich Gamma 已提交
155 156 157 158 159 160 161 162 163 164

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

165
function isContributedKeyBindingsArray(thing: ContributedKeyBinding | ContributedKeyBinding[]): thing is ContributedKeyBinding[] {
E
Erich Gamma 已提交
166 167 168 169 170 171 172 173 174
	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 已提交
175
		rejects.push(nls.localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'command'));
E
Erich Gamma 已提交
176 177 178
		return false;
	}
	if (typeof keyBinding.key !== 'string') {
B
Benjamin Pasero 已提交
179
		rejects.push(nls.localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'key'));
E
Erich Gamma 已提交
180 181 182
		return false;
	}
	if (keyBinding.when && typeof keyBinding.when !== 'string') {
B
Benjamin Pasero 已提交
183
		rejects.push(nls.localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'when'));
E
Erich Gamma 已提交
184 185 186
		return false;
	}
	if (keyBinding.mac && typeof keyBinding.mac !== 'string') {
B
Benjamin Pasero 已提交
187
		rejects.push(nls.localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'mac'));
E
Erich Gamma 已提交
188 189 190
		return false;
	}
	if (keyBinding.linux && typeof keyBinding.linux !== 'string') {
B
Benjamin Pasero 已提交
191
		rejects.push(nls.localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'linux'));
E
Erich Gamma 已提交
192 193 194
		return false;
	}
	if (keyBinding.win && typeof keyBinding.win !== 'string') {
B
Benjamin Pasero 已提交
195
		rejects.push(nls.localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'win'));
E
Erich Gamma 已提交
196 197 198 199 200
		return false;
	}
	return true;
}

201
let keybindingType: IJSONSchema = {
E
Erich Gamma 已提交
202 203 204 205 206 207 208 209
	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: {
A
Alex Dima 已提交
210
			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).'),
E
Erich Gamma 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
			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 已提交
232
let keybindingsExtPoint = ExtensionsRegistry.registerExtensionPoint<ContributedKeyBinding | ContributedKeyBinding[]>('keybindings', [], {
E
Erich Gamma 已提交
233 234 235 236 237 238 239 240 241 242
	description: nls.localize('vscode.extension.contributes.keybindings', "Contributes keybindings."),
	oneOf: [
		keybindingType,
		{
			type: 'array',
			items: keybindingType
		}
	]
});

243 244 245 246 247 248
export const enum DispatchConfig {
	Code,
	KeyCode
}

function getDispatchConfig(configurationService: IConfigurationService): DispatchConfig {
249
	const keyboard = configurationService.getValue('keyboard');
250 251 252 253
	const r = (keyboard ? (<any>keyboard).dispatch : null);
	return (r === 'keyCode' ? DispatchConfig.KeyCode : DispatchConfig.Code);
}

254 255
export class WorkbenchKeybindingService extends AbstractKeybindingService {

256
	private _keyboardMapper: IKeyboardMapper;
257 258
	private _cachedResolver: KeybindingResolver;
	private _firstTimeComputingResolver: boolean;
259
	private userKeybindings: ConfigWatcher<IUserFriendlyKeybinding[]>;
E
Erich Gamma 已提交
260

261
	constructor(
B
Benjamin Pasero 已提交
262
		windowElement: Window,
263
		@IContextKeyService contextKeyService: IContextKeyService,
264
		@ICommandService commandService: ICommandService,
265
		@ITelemetryService telemetryService: ITelemetryService,
266
		@INotificationService notificationService: INotificationService,
267
		@IEnvironmentService environmentService: IEnvironmentService,
268
		@IStatusbarService statusBarService: IStatusbarService,
269
		@IConfigurationService configurationService: IConfigurationService
270
	) {
271
		super(contextKeyService, commandService, telemetryService, notificationService, statusBarService);
272

273
		let dispatchConfig = getDispatchConfig(configurationService);
274
		configurationService.onDidChangeConfiguration((e) => {
275 276 277 278 279 280 281 282 283 284 285
			let newDispatchConfig = getDispatchConfig(configurationService);
			if (dispatchConfig === newDispatchConfig) {
				return;
			}

			dispatchConfig = newDispatchConfig;
			this._keyboardMapper = KeyboardMapperFactory.INSTANCE.getKeyboardMapper(dispatchConfig);
			this.updateResolver({ source: KeybindingSource.Default });
		});

		this._keyboardMapper = KeyboardMapperFactory.INSTANCE.getKeyboardMapper(dispatchConfig);
A
Alex Dima 已提交
286
		KeyboardMapperFactory.INSTANCE.onDidChangeKeyboardMapper(() => {
287
			this._keyboardMapper = KeyboardMapperFactory.INSTANCE.getKeyboardMapper(dispatchConfig);
A
Alex Dima 已提交
288 289
			this.updateResolver({ source: KeybindingSource.Default });
		});
290

291 292 293
		this._cachedResolver = null;
		this._firstTimeComputingResolver = true;

294
		this.userKeybindings = new ConfigWatcher(environmentService.appKeybindingsPath, { defaultConfig: [], onError: error => onUnexpectedError(error) });
B
Benjamin Pasero 已提交
295
		this.toDispose.push(this.userKeybindings);
296

E
Erich Gamma 已提交
297 298 299 300 301 302 303 304
		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 已提交
305
				this.updateResolver({ source: KeybindingSource.Default });
E
Erich Gamma 已提交
306 307
			}
		});
308

C
Christof Marti 已提交
309 310 311 312
		this.toDispose.push(this.userKeybindings.onDidUpdateConfiguration(event => this.updateResolver({
			source: KeybindingSource.User,
			keybindings: event.config
		})));
313

B
Benjamin Pasero 已提交
314
		this.toDispose.push(dom.addDisposableListener(windowElement, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
315
			let keyEvent = new StandardKeyboardEvent(e);
316
			let shouldPreventDefault = this._dispatch(keyEvent, keyEvent.target);
317 318 319 320 321
			if (shouldPreventDefault) {
				keyEvent.preventDefault();
			}
		}));

C
Christof Marti 已提交
322
		keybindingsTelemetry(telemetryService, this);
A
Alex Dima 已提交
323
		let data = KeyboardMapperFactory.INSTANCE.getCurrentKeyboardLayout();
K
kieferrm 已提交
324
		/* __GDPR__
K
kieferrm 已提交
325 326 327 328
			"keyboardLayout" : {
				"currentKeyboardLayout": { "${inline}": [ "${IKeyboardLayoutInfo}" ] }
			}
		*/
A
Alex Dima 已提交
329 330 331
		telemetryService.publicLog('keyboardLayout', {
			currentKeyboardLayout: data
		});
E
Erich Gamma 已提交
332 333
	}

A
Alex Dima 已提交
334
	public dumpDebugInfo(): string {
A
Alex Dima 已提交
335 336 337 338
		const layoutInfo = JSON.stringify(KeyboardMapperFactory.INSTANCE.getCurrentKeyboardLayout(), null, '\t');
		const mapperInfo = this._keyboardMapper.dumpDebugInfo();
		const rawMapping = JSON.stringify(KeyboardMapperFactory.INSTANCE.getRawKeyboardMapping(), null, '\t');
		return `Layout info:\n${layoutInfo}\n${mapperInfo}\n\nRaw mapping:\n${rawMapping}`;
A
Alex Dima 已提交
339 340
	}

341 342 343 344 345 346 347 348
	private _safeGetConfig(): IUserFriendlyKeybinding[] {
		let rawConfig = this.userKeybindings.getConfig();
		if (Array.isArray(rawConfig)) {
			return rawConfig;
		}
		return [];
	}

349
	public customKeybindingsCount(): number {
350
		let userKeybindings = this._safeGetConfig();
351 352

		return userKeybindings.length;
353 354
	}

355 356 357 358 359 360 361
	private updateResolver(event: IKeybindingEvent): void {
		this._cachedResolver = null;
		this._onDidUpdateKeybindings.fire(event);
	}

	protected _getResolver(): KeybindingResolver {
		if (!this._cachedResolver) {
362 363
			const defaults = this._resolveKeybindingItems(KeybindingsRegistry.getDefaultKeybindings(), true);
			const overrides = this._resolveUserKeybindingItems(this._getExtraKeybindings(this._firstTimeComputingResolver), false);
364
			this._cachedResolver = new KeybindingResolver(defaults, overrides);
365 366 367
			this._firstTimeComputingResolver = false;
		}
		return this._cachedResolver;
368 369
	}

370
	private _resolveKeybindingItems(items: IKeybindingItem[], isDefault: boolean): ResolvedKeybindingItem[] {
371
		let result: ResolvedKeybindingItem[] = [], resultLen = 0;
372 373 374
		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 已提交
375
			const keybinding = item.keybinding;
376 377 378 379 380 381 382 383 384
			if (!keybinding) {
				// This might be a removal keybinding item in user settings => accept it
				result[resultLen++] = new ResolvedKeybindingItem(null, item.command, item.commandArgs, when, isDefault);
			} else {
				const resolvedKeybindings = this.resolveKeybinding(keybinding);
				for (let j = 0; j < resolvedKeybindings.length; j++) {
					result[resultLen++] = new ResolvedKeybindingItem(resolvedKeybindings[j], item.command, item.commandArgs, when, isDefault);
				}
			}
385 386 387
		}

		return result;
388 389
	}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	private _resolveUserKeybindingItems(items: IUserKeybindingItem[], isDefault: boolean): ResolvedKeybindingItem[] {
		let result: ResolvedKeybindingItem[] = [], resultLen = 0;
		for (let i = 0, len = items.length; i < len; i++) {
			const item = items[i];
			const when = (item.when ? item.when.normalize() : null);
			const firstPart = item.firstPart;
			const chordPart = item.chordPart;
			if (!firstPart) {
				// This might be a removal keybinding item in user settings => accept it
				result[resultLen++] = new ResolvedKeybindingItem(null, item.command, item.commandArgs, when, isDefault);
			} else {
				const resolvedKeybindings = this._keyboardMapper.resolveUserBinding(firstPart, chordPart);
				for (let j = 0; j < resolvedKeybindings.length; j++) {
					result[resultLen++] = new ResolvedKeybindingItem(resolvedKeybindings[j], item.command, item.commandArgs, when, isDefault);
				}
			}
		}

		return result;
	}

	private _getExtraKeybindings(isFirstTime: boolean): IUserKeybindingItem[] {
412
		let extraUserKeybindings: IUserFriendlyKeybinding[] = this._safeGetConfig();
413 414
		if (!isFirstTime) {
			let cnt = extraUserKeybindings.length;
415

K
kieferrm 已提交
416
			/* __GDPR__
K
kieferrm 已提交
417
				"customKeybindingsChanged" : {
R
Ramya Achutha Rao 已提交
418
					"keyCount" : { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true }
K
kieferrm 已提交
419 420
				}
			*/
421
			this._telemetryService.publicLog('customKeybindingsChanged', {
422 423
				keyCount: cnt
			});
424
		}
425

426
		return extraUserKeybindings.map((k) => KeybindingIO.readUserKeybindingItem(k, OS));
427 428
	}

429
	public resolveKeybinding(kb: Keybinding): ResolvedKeybinding[] {
430
		return this._keyboardMapper.resolveKeybinding(kb);
A
Alex Dima 已提交
431 432
	}

433
	public resolveKeyboardEvent(keyboardEvent: IKeyboardEvent): ResolvedKeybinding {
434
		return this._keyboardMapper.resolveKeyboardEvent(keyboardEvent);
435 436
	}

437 438 439 440 441
	public resolveUserBinding(userBinding: string): ResolvedKeybinding[] {
		const [firstPart, chordPart] = KeybindingIO._readUserBinding(userBinding);
		return this._keyboardMapper.resolveUserBinding(firstPart, chordPart);
	}

442
	private _handleKeybindingsExtensionPointUser(isBuiltin: boolean, keybindings: ContributedKeyBinding | ContributedKeyBinding[], collector: ExtensionMessageCollector): boolean {
E
Erich Gamma 已提交
443 444 445 446 447 448 449 450 451 452 453
		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);
		}
	}

454
	private _handleKeybinding(isBuiltin: boolean, idx: number, keybindings: ContributedKeyBinding, collector: ExtensionMessageCollector): boolean {
E
Erich Gamma 已提交
455 456 457 458 459 460 461

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

		if (isValidContributedKeyBinding(keybindings, rejects)) {
			let rule = this._asCommandRule(isBuiltin, idx++, keybindings);
			if (rule) {
462
				KeybindingsRegistry.registerKeybindingRule2(rule, KeybindingRuleSource.Extension);
E
Erich Gamma 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
				commandAdded = true;
			}
		}

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

		return commandAdded;
	}

479
	private _asCommandRule(isBuiltin: boolean, idx: number, binding: ContributedKeyBinding): IKeybindingRule2 {
E
Erich Gamma 已提交
480

481
		let { command, when, key, mac, linux, win } = binding;
E
Erich Gamma 已提交
482 483 484 485 486 487 488 489 490 491

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

		let desc = {
			id: command,
492
			when: ContextKeyExpr.deserialize(when),
E
Erich Gamma 已提交
493
			weight: weight,
A
Alex Dima 已提交
494 495 496 497
			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 已提交
498
		};
E
Erich Gamma 已提交
499 500

		if (!desc.primary && !desc.mac && !desc.linux && !desc.win) {
501
			return undefined;
E
Erich Gamma 已提交
502 503 504 505
		}

		return desc;
	}
A
Alex Dima 已提交
506

S
Sandeep Somavarapu 已提交
507
	public getDefaultKeybindingsContent(): string {
A
Alex Dima 已提交
508 509 510 511 512 513 514 515 516 517
		const resolver = this._getResolver();
		const defaultKeybindings = resolver.getDefaultKeybindings();
		const boundCommands = resolver.getDefaultBoundCommands();
		return (
			WorkbenchKeybindingService._getDefaultKeybindings(defaultKeybindings)
			+ '\n\n'
			+ WorkbenchKeybindingService._getAllCommandsAsComment(boundCommands)
		);
	}

518
	private static _getDefaultKeybindings(defaultKeybindings: ResolvedKeybindingItem[]): string {
A
Alex Dima 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
		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 已提交
536
		const unboundCommands = KeybindingResolver.getAllUnboundCommands(boundCommands);
A
Alex Dima 已提交
537 538 539
		let pretty = unboundCommands.sort().join('\n// - ');
		return '// ' + nls.localize('unboundCommands', "Here are other available commands: ") + '\n// - ' + pretty;
	}
540
}
541

542
let schemaId = 'vscode://schemas/keybindings';
543
let schema: IJSONSchema = {
544 545 546 547 548 549
	'id': schemaId,
	'type': 'array',
	'title': nls.localize('keybindings.json.title', "Keybindings configuration"),
	'items': {
		'required': ['key'],
		'type': 'object',
550
		'defaultSnippets': [{ 'body': { 'key': '$1', 'command': '$2', 'when': '$3' } }],
551 552 553
		'properties': {
			'key': {
				'type': 'string',
554
				'description': nls.localize('keybindings.json.key', "Key or key sequence (separated by space)"),
555 556
			},
			'command': {
557
				'description': nls.localize('keybindings.json.command', "Name of the command to execute"),
558 559 560
			},
			'when': {
				'type': 'string',
561 562 563 564
				'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.")
565 566 567 568 569
			}
		}
	}
};

570
let schemaRegistry = <IJSONContributionRegistry>Registry.as(Extensions.JSONContribution);
571
schemaRegistry.registerSchema(schemaId, schema);
572

573 574 575 576 577 578 579 580 581 582 583 584 585
const configurationRegistry = <IConfigurationRegistry>Registry.as(ConfigExtensions.Configuration);
const keyboardConfiguration: IConfigurationNode = {
	'id': 'keyboard',
	'order': 15,
	'type': 'object',
	'title': nls.localize('keyboardConfigurationTitle', "Keyboard"),
	'overridable': true,
	'properties': {
		'keyboard.dispatch': {
			'type': 'string',
			'enum': ['code', 'keyCode'],
			'default': 'code',
			'description': nls.localize('dispatch', "Controls the dispatching logic for key presses to use either `code` (recommended) or `keyCode`."),
586
			'included': OS === OperatingSystem.Macintosh || OS === OperatingSystem.Linux
587 588 589 590
		},
		'keyboard.touchbar.enabled': {
			'type': 'boolean',
			'default': true,
B
Benjamin Pasero 已提交
591
			'description': nls.localize('touchbar.enabled', "Enables the macOS touchbar buttons on the keyboard if available."),
592
			'included': OS === OperatingSystem.Macintosh && parseFloat(release()) >= 16 // Minimum: macOS Sierra (10.12.x = darwin 16.x)
593
		}
594 595
	}
};
596

597
configurationRegistry.registerConfiguration(keyboardConfiguration);