menusExtensionPoint.ts 12.9 KB
Newer Older
J
Johannes Rieken 已提交
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 URI from 'vs/base/common/uri';
8 9
import { createCSSRule } from 'vs/base/browser/dom';
import { localize } from 'vs/nls';
10
import { isFalsyOrWhitespace } from 'vs/base/common/strings';
11
import { join } from 'path';
12 13 14
import { IdGenerator } from 'vs/base/common/idGenerator';
import { IJSONSchema } from 'vs/base/common/jsonSchema';
import { forEach } from 'vs/base/common/collections';
15
import { IExtensionPointUser, ExtensionMessageCollector, ExtensionsRegistry } from 'vs/platform/extensions/common/extensionsRegistry';
16
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
17
import { MenuId, MenuRegistry, ILocalizedString } from 'vs/platform/actions/common/actions';
18

J
Johannes Rieken 已提交
19 20
namespace schema {

21 22
	// --- menus contribution point

23 24 25 26 27 28 29
	export interface IUserFriendlyMenuItem {
		command: string;
		alt?: string;
		when?: string;
		group?: string;
	}

J
Johannes Rieken 已提交
30
	export function parseMenuId(value: string): MenuId {
31
		switch (value) {
J
Joao Moreno 已提交
32
			case 'commandPalette': return MenuId.CommandPalette;
33
			case 'editor/title': return MenuId.EditorTitle;
J
Johannes Rieken 已提交
34
			case 'editor/context': return MenuId.EditorContext;
35
			case 'explorer/context': return MenuId.ExplorerContext;
36
			case 'editor/title/context': return MenuId.EditorTitleContext;
37
			case 'debug/callstack/context': return MenuId.DebugCallStackContext;
J
Joao Moreno 已提交
38 39
			case 'scm/title': return MenuId.SCMTitle;
			case 'scm/resourceGroup/context': return MenuId.SCMResourceGroupContext;
40
			case 'scm/resourceState/context': return MenuId.SCMResourceContext;
41
			case 'view/title': return MenuId.ViewTitle;
42
			case 'view/item/context': return MenuId.ViewItemContext;
J
Joao Moreno 已提交
43 44
		}

J
Joao Moreno 已提交
45
		return void 0;
46 47
	}

48
	export function isValidMenuItems(menu: IUserFriendlyMenuItem[], collector: ExtensionMessageCollector): boolean {
49
		if (!Array.isArray(menu)) {
D
Dirk Baeumer 已提交
50
			collector.error(localize('requirearray', "menu items must be an array"));
51
			return false;
J
Johannes Rieken 已提交
52 53
		}

54 55 56 57 58 59 60 61 62 63 64 65 66
		for (let item of menu) {
			if (typeof item.command !== 'string') {
				collector.error(localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'command'));
				return false;
			}
			if (item.alt && typeof item.alt !== 'string') {
				collector.error(localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'alt'));
				return false;
			}
			if (item.when && typeof item.when !== 'string') {
				collector.error(localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'when'));
				return false;
			}
67 68 69 70
			if (item.group && typeof item.group !== 'string') {
				collector.error(localize('optstring', "property `{0}` can be omitted or must be of type `string`", 'group'));
				return false;
			}
71
		}
J
Johannes Rieken 已提交
72 73 74 75

		return true;
	}

76 77 78 79
	const menuItem: IJSONSchema = {
		type: 'object',
		properties: {
			command: {
J
Johannes Rieken 已提交
80
				description: localize('vscode.extension.contributes.menuItem.command', 'Identifier of the command to execute. The command must be declared in the \'commands\'-section'),
81 82 83
				type: 'string'
			},
			alt: {
J
Johannes Rieken 已提交
84
				description: localize('vscode.extension.contributes.menuItem.alt', 'Identifier of an alternative command to execute. The command must be declared in the \'commands\'-section'),
85 86 87 88 89
				type: 'string'
			},
			when: {
				description: localize('vscode.extension.contributes.menuItem.when', 'Condition which must be true to show this item'),
				type: 'string'
90 91 92 93
			},
			group: {
				description: localize('vscode.extension.contributes.menuItem.group', 'Group into which this command belongs'),
				type: 'string'
94 95 96
			}
		}
	};
J
Johannes Rieken 已提交
97

98
	export const menusContribtion: IJSONSchema = {
J
Johannes Rieken 已提交
99
		description: localize('vscode.extension.contributes.menus', "Contributes menu items to the editor"),
100 101
		type: 'object',
		properties: {
J
Joao Moreno 已提交
102 103 104 105 106
			'commandPalette': {
				description: localize('menus.commandPalette', "The Command Palette"),
				type: 'array',
				items: menuItem
			},
107
			'editor/title': {
J
Johannes Rieken 已提交
108
				description: localize('menus.editorTitle', "The editor title menu"),
109 110
				type: 'array',
				items: menuItem
111
			},
J
Johannes Rieken 已提交
112 113
			'editor/context': {
				description: localize('menus.editorContext', "The editor context menu"),
114 115
				type: 'array',
				items: menuItem
J
Johannes Rieken 已提交
116
			},
117 118 119 120 121
			'explorer/context': {
				description: localize('menus.explorerContext', "The file explorer context menu"),
				type: 'array',
				items: menuItem
			},
B
Benjamin Pasero 已提交
122
			'editor/title/context': {
B
Benjamin Pasero 已提交
123 124 125 126
				description: localize('menus.editorTabContext', "The editor tabs context menu"),
				type: 'array',
				items: menuItem
			},
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
			'debug/callstack/context': {
				description: localize('menus.debugCallstackContext', "The debug callstack context menu"),
				type: 'array',
				items: menuItem
			},
			'scm/title': {
				description: localize('menus.scmTitle', "The Source Control title menu"),
				type: 'array',
				items: menuItem
			},
			'scm/resourceGroup/context': {
				description: localize('menus.resourceGroupContext', "The Source Control resource group context menu"),
				type: 'array',
				items: menuItem
			},
			'scm/resourceState/context': {
				description: localize('menus.resourceStateContext', "The Source Control resource state context menu"),
J
Johannes Rieken 已提交
144 145
				type: 'array',
				items: menuItem
146 147 148 149 150 151 152 153 154 155
			},
			'view/title': {
				description: localize('view.viewTitle', "The contributed view title menu"),
				type: 'array',
				items: menuItem
			},
			'view/item/context': {
				description: localize('view.itemContext', "The contributed view item context menu"),
				type: 'array',
				items: menuItem
J
Johannes Rieken 已提交
156 157
			}
		}
158 159 160
	};

	// --- commands contribution point
J
Johannes Rieken 已提交
161

162 163
	export interface IUserFriendlyCommand {
		command: string;
164 165
		title: string | ILocalizedString;
		category?: string | ILocalizedString;
166 167 168 169 170
		icon?: IUserFriendlyIcon;
	}

	export type IUserFriendlyIcon = string | { light: string; dark: string; };

171
	export function isValidCommand(command: IUserFriendlyCommand, collector: ExtensionMessageCollector): boolean {
172 173 174 175
		if (!command) {
			collector.error(localize('nonempty', "expected non-empty value."));
			return false;
		}
176
		if (isFalsyOrWhitespace(command.command)) {
177 178 179
			collector.error(localize('requirestring', "property `{0}` is mandatory and must be of type `string`", 'command'));
			return false;
		}
180
		if (!isValidLocalizedString(command.title, collector, 'title')) {
181 182
			return false;
		}
183
		if (command.category && !isValidLocalizedString(command.category, collector, 'category')) {
184 185 186 187 188
			return false;
		}
		if (!isValidIcon(command.icon, collector)) {
			return false;
		}
J
Johannes Rieken 已提交
189 190 191
		return true;
	}

192
	function isValidIcon(icon: IUserFriendlyIcon, collector: ExtensionMessageCollector): boolean {
193 194 195 196 197 198 199 200 201 202 203
		if (typeof icon === 'undefined') {
			return true;
		}
		if (typeof icon === 'string') {
			return true;
		} else if (typeof icon.dark === 'string' && typeof icon.light === 'string') {
			return true;
		}
		collector.error(localize('opticon', "property `icon` can be omitted or must be either a string or a literal like `{dark, light}`"));
		return false;
	}
J
Johannes Rieken 已提交
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	function isValidLocalizedString(localized: string | ILocalizedString, collector: ExtensionMessageCollector, propertyName: string): boolean {
		if (typeof localized === 'undefined') {
			collector.error(localize('requireStringOrObject', "property `{0}` is mandatory and must be of type `string` or `object`", propertyName));
			return false;
		} else if (typeof localized === 'string' && isFalsyOrWhitespace(localized)) {
			collector.error(localize('requirestring', "property `{0}` is mandatory and must be of type `string`", propertyName));
			return false;
		} else if (typeof localized !== 'string' && (isFalsyOrWhitespace(localized.original) || isFalsyOrWhitespace(localized.value))) {
			collector.error(localize('requirestrings', "properties `{0}` and `{1}` are mandatory and must be of type `string`", `${propertyName}.value`, `${propertyName}.original`));
			return false;
		}

		return true;
	}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	const commandType: IJSONSchema = {
		type: 'object',
		properties: {
			command: {
				description: localize('vscode.extension.contributes.commandType.command', 'Identifier of the command to execute'),
				type: 'string'
			},
			title: {
				description: localize('vscode.extension.contributes.commandType.title', 'Title by which the command is represented in the UI'),
				type: 'string'
			},
			category: {
				description: localize('vscode.extension.contributes.commandType.category', '(Optional) Category string by the command is grouped in the UI'),
				type: 'string'
			},
			icon: {
				description: localize('vscode.extension.contributes.commandType.icon', '(Optional) Icon which is used to represent the command in the UI. Either a file path or a themable configuration'),
				anyOf: [
					'string',
					{
						type: 'object',
						properties: {
							light: {
								description: localize('vscode.extension.contributes.commandType.icon.light', 'Icon path when a light theme is used'),
								type: 'string'
							},
							dark: {
								description: localize('vscode.extension.contributes.commandType.icon.dark', 'Icon path when a dark theme is used'),
								type: 'string'
							}
						}
					}
				]
			}
		}
J
Johannes Rieken 已提交
255 256
	};

257 258 259 260 261 262 263 264 265 266
	export const commandsContribution: IJSONSchema = {
		description: localize('vscode.extension.contributes.commands', "Contributes commands to the command palette."),
		oneOf: [
			commandType,
			{
				type: 'array',
				items: commandType
			}
		]
	};
J
Johannes Rieken 已提交
267 268
}

A
Alex Dima 已提交
269
ExtensionsRegistry.registerExtensionPoint<schema.IUserFriendlyCommand | schema.IUserFriendlyCommand[]>('commands', [], schema.commandsContribution).setHandler(extensions => {
J
Johannes Rieken 已提交
270

271
	const ids = new IdGenerator('contrib-cmd-icon-');
J
Johannes Rieken 已提交
272

273
	function handleCommand(userFriendlyCommand: schema.IUserFriendlyCommand, extension: IExtensionPointUser<any>) {
274 275

		if (!schema.isValidCommand(userFriendlyCommand, extension.collector)) {
276 277
			return;
		}
J
Johannes Rieken 已提交
278

J
Joao Moreno 已提交
279
		let { icon, category, title, command } = userFriendlyCommand;
280 281 282 283 284
		let iconClass: string;
		if (icon) {
			iconClass = ids.nextId();
			if (typeof icon === 'string') {
				const path = join(extension.description.extensionFolderPath, icon);
285
				createCSSRule(`.icon.${iconClass}`, `background-image: url("${URI.file(path).toString()}")`);
286 287 288
			} else {
				const light = join(extension.description.extensionFolderPath, icon.light);
				const dark = join(extension.description.extensionFolderPath, icon.dark);
289
				createCSSRule(`.icon.${iconClass}`, `background-image: url("${URI.file(light).toString()}")`);
290
				createCSSRule(`.vs-dark .icon.${iconClass}, .hc-black .icon.${iconClass}`, `background-image: url("${URI.file(dark).toString()}")`);
291
			}
292 293
		}

294
		if (MenuRegistry.addCommand({ id: command, title, category, iconClass })) {
295
			extension.collector.info(localize('dup', "Command `{0}` appears multiple times in the `commands` section.", userFriendlyCommand.command));
J
Johannes Rieken 已提交
296 297 298
		}
	}

299
	for (let extension of extensions) {
J
Joao Moreno 已提交
300
		const { value } = extension;
301
		if (Array.isArray<schema.IUserFriendlyCommand>(value)) {
302 303 304 305 306 307
			for (let command of value) {
				handleCommand(command, extension);
			}
		} else {
			handleCommand(value, extension);
		}
J
Johannes Rieken 已提交
308
	}
309
});
J
Johannes Rieken 已提交
310

311

A
Alex Dima 已提交
312
ExtensionsRegistry.registerExtensionPoint<{ [loc: string]: schema.IUserFriendlyMenuItem[] }>('menus', [], schema.menusContribtion).setHandler(extensions => {
313
	for (let extension of extensions) {
J
Joao Moreno 已提交
314
		const { value, collector } = extension;
315 316 317 318 319 320 321 322 323 324 325 326 327

		forEach(value, entry => {
			if (!schema.isValidMenuItems(entry.value, collector)) {
				return;
			}

			const menu = schema.parseMenuId(entry.key);
			if (!menu) {
				collector.warn(localize('menuId.invalid', "`{0}` is not a valid menu identifier", entry.key));
				return;
			}

			for (let item of entry.value) {
328 329 330 331
				let command = MenuRegistry.getCommand(item.command);
				let alt = item.alt && MenuRegistry.getCommand(item.alt);

				if (!command) {
J
Johannes Rieken 已提交
332
					collector.warn(localize('missing.command', "Menu item references a command `{0}` which is not defined in the 'commands' section.", item.command));
333
				}
334
				if (item.alt && !alt) {
J
Johannes Rieken 已提交
335
					collector.warn(localize('missing.altCommand', "Menu item references an alt-command `{0}` which is not defined in the 'commands' section.", item.alt));
336
				}
337
				if (item.command === item.alt) {
338 339 340
					collector.info(localize('dupe.command', "Menu item references the same command as default and alt-command"));
				}

341 342
				if (item.alt && menu !== MenuId.EditorTitle && item.group !== 'navigation') {
					collector.info(localize('nosupport.altCommand', "Sorry, but currently only the 'navigation' group of the 'editor/title' menu supports alt-commands"));
343 344
				}

345 346 347 348 349 350 351 352 353 354 355 356
				let group: string;
				let order: number;
				if (item.group) {
					const idx = item.group.lastIndexOf('@');
					if (idx > 0) {
						group = item.group.substr(0, idx);
						order = Number(item.group.substr(idx + 1)) || undefined;
					} else {
						group = item.group;
					}
				}

357 358 359
				MenuRegistry.appendMenuItem(menu, {
					command,
					alt,
360 361
					group,
					order,
A
Alex Dima 已提交
362
					when: ContextKeyExpr.deserialize(item.when)
363 364
				});
			}
365 366 367
		});
	}
});