commandService.ts 1.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import {TPromise} from 'vs/base/common/winjs.base';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {ICommandService, CommandsRegistry} from 'vs/platform/commands/common/commands';
import {IExtensionService} from 'vs/platform/extensions/common/extensions';

export class CommandService implements ICommandService {

	serviceId = ICommandService;

	constructor(
		@IInstantiationService private _instantiationService: IInstantiationService,
		@IExtensionService private _extensionService: IExtensionService
	) {
		//
	}

	executeCommand<T>(id: string, ...args: any[]): TPromise<T> {

		return this._extensionService.activateByEvent(`onCommand:${id}`).then(_ => {
26 27 28 29 30 31

			const command = CommandsRegistry.getCommand(id);
			if (!command) {
				return TPromise.wrapError(new Error(`command '${id}' not found`));
			}

32 33 34 35 36 37 38 39
			try {
				const result = this._instantiationService.invokeFunction.apply(this._instantiationService, [command.handler].concat(args));
				return TPromise.as(result);
			} catch (err) {
				return TPromise.wrapError(err);
			}
		});
	}
40 41 42 43 44 45 46 47 48 49

	isKnownCommand(commandId: string): boolean {
		const command = CommandsRegistry.getCommand(commandId);

		if (!command) {
			return false;
		}

		return true;
	}
50
}