extHostCommands.ts 4.4 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 {IThreadService} from 'vs/workbench/services/thread/common/threadService';
B
Benjamin Pasero 已提交
8
import {validateConstraint} from 'vs/base/common/types';
9
import {ICommandHandlerDescription} from 'vs/platform/commands/common/commands';
E
Erich Gamma 已提交
10
import {TPromise} from 'vs/base/common/winjs.base';
J
Johannes Rieken 已提交
11 12 13
import {ExtHostEditors} from 'vs/workbench/api/node/extHostEditors';
import * as extHostTypes from 'vs/workbench/api/node/extHostTypes';
import * as extHostTypeConverter from 'vs/workbench/api/node/extHostTypeConverters';
14
import {cloneAndChange} from 'vs/base/common/objects';
A
Alex Dima 已提交
15
import {MainContext, MainThreadCommandsShape, ExtHostCommandsShape} from './extHost.protocol';
E
Erich Gamma 已提交
16

17 18 19 20 21 22
interface CommandHandler {
	callback: Function;
	thisArg: any;
	description: ICommandHandlerDescription;
}

A
Alex Dima 已提交
23
export class ExtHostCommands extends ExtHostCommandsShape {
E
Erich Gamma 已提交
24

25
	private _commands: { [n: string]: CommandHandler } = Object.create(null);
26
	private _proxy: MainThreadCommandsShape;
A
Alex Dima 已提交
27
	private _extHostEditors: ExtHostEditors;
E
Erich Gamma 已提交
28

29 30 31 32
	constructor(
		threadService: IThreadService,
		extHostEditors:ExtHostEditors
	) {
A
Alex Dima 已提交
33
		super();
34 35
		this._extHostEditors = extHostEditors;
		this._proxy = threadService.get(MainContext.MainThreadCommands);
E
Erich Gamma 已提交
36 37
	}

38
	registerCommand(id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): extHostTypes.Disposable {
E
Erich Gamma 已提交
39 40 41 42 43 44 45 46 47

		if (!id.trim().length) {
			throw new Error('invalid id');
		}

		if (this._commands[id]) {
			throw new Error('command with id already exists');
		}

48 49
		this._commands[id] = { callback, thisArg, description };
		this._proxy.$registerCommand(id);
E
Erich Gamma 已提交
50

J
Johannes Rieken 已提交
51 52 53 54 55
		return new extHostTypes.Disposable(() => {
			if (delete this._commands[id]) {
				this._proxy.$unregisterCommand(id);
			}
		});
E
Erich Gamma 已提交
56 57 58 59 60 61 62
	}

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

		if (this._commands[id]) {
			// we stay inside the extension host and support
			// to pass any kind of parameters around
63
			return this.$executeContributedCommand(id, ...args);
E
Erich Gamma 已提交
64 65

		} else {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
			// automagically convert some argument types

			args = cloneAndChange(args, function(value) {
				if (value instanceof extHostTypes.Position) {
					return extHostTypeConverter.fromPosition(value);
				}
				if (value instanceof extHostTypes.Range) {
					return extHostTypeConverter.fromRange(value);
				}
				if (value instanceof extHostTypes.Location) {
					return extHostTypeConverter.location.from(value);
				}
				if (!Array.isArray(value)) {
					return value;
				}
			});
E
Erich Gamma 已提交
82

83
			return this._proxy.$executeCommand(id, args);
E
Erich Gamma 已提交
84 85 86 87
		}

	}

88
	$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
E
Erich Gamma 已提交
89 90
		let command = this._commands[id];
		if (!command) {
91
			return Promise.reject<T>(`Contributed command '${id}' does not exist.`);
E
Erich Gamma 已提交
92
		}
93 94 95 96 97 98

		let {callback, thisArg, description} = command;

		if (description) {
			for (let i = 0; i < description.args.length; i++) {
				try {
J
Johannes Rieken 已提交
99
					validateConstraint(args[i], description.args[i].constraint);
100 101
				} catch (err) {
					return Promise.reject<T>(`Running the contributed command:'${id}' failed. Illegal argument '${description.args[i].name}' - ${description.args[i].description}`);
102 103
				}
			}
104 105 106
		}

		try {
107
			let result = callback.apply(thisArg, args);
E
Erich Gamma 已提交
108 109
			return Promise.resolve(result);
		} catch (err) {
110
			// console.log(err);
111 112 113 114 115
			// try {
			// 	console.log(toErrorMessage(err));
			// } catch (err) {
			// 	//
			// }
E
Erich Gamma 已提交
116 117 118 119
			return Promise.reject<T>(`Running the contributed command:'${id}' failed.`);
		}
	}

120
	getCommands(filterUnderscoreCommands: boolean = false): Thenable<string[]> {
121
		return this._proxy.$getCommands().then(result => {
122 123 124 125 126
			if (filterUnderscoreCommands) {
				result = result.filter(command => command[0] !== '_');
			}
			return result;
		});
E
Erich Gamma 已提交
127
	}
128 129 130 131 132 133 134 135 136 137 138

	$getContributedCommandHandlerDescriptions(): TPromise<{ [id: string]: string | ICommandHandlerDescription }> {
		const result: { [id: string]: string | ICommandHandlerDescription } = Object.create(null);
		for (let id in this._commands) {
			let {description} = this._commands[id];
			if (description) {
				result[id] = description;
			}
		}
		return TPromise.as(result);
	}
E
Erich Gamma 已提交
139
}