extHostCommands.ts 6.8 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';

J
Johannes Rieken 已提交
7 8 9
import { validateConstraint } from 'vs/base/common/types';
import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
import { TPromise } from 'vs/base/common/winjs.base';
J
Johannes Rieken 已提交
10 11
import * as extHostTypes from 'vs/workbench/api/node/extHostTypes';
import * as extHostTypeConverter from 'vs/workbench/api/node/extHostTypeConverters';
J
Johannes Rieken 已提交
12
import { cloneAndChange } from 'vs/base/common/objects';
13
import { MainContext, MainThreadCommandsShape, ExtHostCommandsShape, ObjectIdentifier, IMainContext } from './extHost.protocol';
14 15 16 17
import { ExtHostHeapService } from 'vs/workbench/api/node/extHostHeapService';
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
import * as modes from 'vs/editor/common/modes';
import * as vscode from 'vscode';
J
Joao Moreno 已提交
18
import { log, LogLevel } from 'vs/platform/log/common/log';
E
Erich Gamma 已提交
19

20 21 22 23 24 25
interface CommandHandler {
	callback: Function;
	thisArg: any;
	description: ICommandHandlerDescription;
}

J
Joao Moreno 已提交
26 27 28 29
export interface ArgumentProcessor {
	processArgument(arg: any): any;
}

30
export class ExtHostCommands implements ExtHostCommandsShape {
E
Erich Gamma 已提交
31

J
Johannes Rieken 已提交
32
	private _commands = new Map<string, CommandHandler>();
33
	private _proxy: MainThreadCommandsShape;
34
	private _converter: CommandsConverter;
J
Joao Moreno 已提交
35
	private _argumentProcessors: ArgumentProcessor[] = [];
E
Erich Gamma 已提交
36

37
	constructor(
38
		mainContext: IMainContext,
J
Joao Moreno 已提交
39
		heapService: ExtHostHeapService
40
	) {
41
		this._proxy = mainContext.get(MainContext.MainThreadCommands);
42 43 44 45 46
		this._converter = new CommandsConverter(this, heapService);
	}

	get converter(): CommandsConverter {
		return this._converter;
E
Erich Gamma 已提交
47 48
	}

J
Joao Moreno 已提交
49 50 51 52
	registerArgumentProcessor(processor: ArgumentProcessor): void {
		this._argumentProcessors.push(processor);
	}

J
Joao Moreno 已提交
53
	@log(LogLevel.TRACE, 'ExtHostCommands', (msg, id) => `${msg}(${id})`)
54
	registerCommand(id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): extHostTypes.Disposable {
E
Erich Gamma 已提交
55 56 57 58 59

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

J
Johannes Rieken 已提交
60
		if (this._commands.has(id)) {
61
			throw new Error(`command '${id}' already exists`);
E
Erich Gamma 已提交
62 63
		}

J
Johannes Rieken 已提交
64
		this._commands.set(id, { callback, thisArg, description });
65
		this._proxy.$registerCommand(id);
E
Erich Gamma 已提交
66

J
Johannes Rieken 已提交
67
		return new extHostTypes.Disposable(() => {
J
Johannes Rieken 已提交
68
			if (this._commands.delete(id)) {
J
Johannes Rieken 已提交
69 70 71
				this._proxy.$unregisterCommand(id);
			}
		});
E
Erich Gamma 已提交
72 73
	}

J
Joao Moreno 已提交
74
	@log(LogLevel.TRACE, 'ExtHostCommands', (msg, id) => `${msg}(${id})`)
E
Erich Gamma 已提交
75 76
	executeCommand<T>(id: string, ...args: any[]): Thenable<T> {

J
Johannes Rieken 已提交
77
		if (this._commands.has(id)) {
E
Erich Gamma 已提交
78 79
			// we stay inside the extension host and support
			// to pass any kind of parameters around
J
Johannes Rieken 已提交
80
			return this.$executeContributedCommand<T>(id, ...args);
E
Erich Gamma 已提交
81 82

		} else {
83 84
			// automagically convert some argument types

J
Johannes Rieken 已提交
85
			args = cloneAndChange(args, function (value) {
86 87 88 89 90 91 92 93 94 95 96 97 98
				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 已提交
99

100
			return this._proxy.$executeCommand<T>(id, args);
E
Erich Gamma 已提交
101
		}
J
Johannes Rieken 已提交
102

E
Erich Gamma 已提交
103 104
	}

J
Johannes Rieken 已提交
105
	$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
J
Johannes Rieken 已提交
106
		let command = this._commands.get(id);
E
Erich Gamma 已提交
107
		if (!command) {
108
			return TPromise.wrapError<T>(new Error(`Contributed command '${id}' does not exist.`));
E
Erich Gamma 已提交
109
		}
110

J
Johannes Rieken 已提交
111
		let { callback, thisArg, description } = command;
112 113 114 115

		if (description) {
			for (let i = 0; i < description.args.length; i++) {
				try {
J
Johannes Rieken 已提交
116
					validateConstraint(args[i], description.args[i].constraint);
117
				} catch (err) {
118
					return TPromise.wrapError<T>(new Error(`Running the contributed command:'${id}' failed. Illegal argument '${description.args[i].name}' - ${description.args[i].description}`));
119 120
				}
			}
121 122
		}

J
Joao Moreno 已提交
123 124
		args = args.map(arg => this._argumentProcessors.reduce((r, p) => p.processArgument(r), arg));

125
		try {
126
			let result = callback.apply(thisArg, args);
127
			return TPromise.as(result);
E
Erich Gamma 已提交
128
		} catch (err) {
129
			// console.log(err);
130 131 132 133 134
			// try {
			// 	console.log(toErrorMessage(err));
			// } catch (err) {
			// 	//
			// }
135
			return TPromise.wrapError<T>(new Error(`Running the contributed command:'${id}' failed.`));
E
Erich Gamma 已提交
136 137 138
		}
	}

J
Joao Moreno 已提交
139
	@log(LogLevel.TRACE, 'ExtHostCommands', (msg, filterUnderscoreCommands) => `${msg}(${filterUnderscoreCommands})`)
140
	getCommands(filterUnderscoreCommands: boolean = false): Thenable<string[]> {
141
		return this._proxy.$getCommands().then(result => {
142 143 144 145 146
			if (filterUnderscoreCommands) {
				result = result.filter(command => command[0] !== '_');
			}
			return result;
		});
E
Erich Gamma 已提交
147
	}
148 149 150

	$getContributedCommandHandlerDescriptions(): TPromise<{ [id: string]: string | ICommandHandlerDescription }> {
		const result: { [id: string]: string | ICommandHandlerDescription } = Object.create(null);
J
Johannes Rieken 已提交
151
		this._commands.forEach((command, id) => {
J
Johannes Rieken 已提交
152
			let { description } = command;
153 154 155
			if (description) {
				result[id] = description;
			}
J
Johannes Rieken 已提交
156
		});
157 158
		return TPromise.as(result);
	}
E
Erich Gamma 已提交
159
}
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177


export class CommandsConverter {

	private _commands: ExtHostCommands;
	private _heap: ExtHostHeapService;

	// --- conversion between internal and api commands
	constructor(commands: ExtHostCommands, heap: ExtHostHeapService) {

		this._commands = commands;
		this._heap = heap;
		this._commands.registerCommand('_internal_command_delegation', this._executeConvertedCommand, this);
	}

	toInternal(command: vscode.Command): modes.Command {

		if (!command) {
M
Matt Bierner 已提交
178
			return undefined;
179 180 181 182 183 184 185
		}

		const result: modes.Command = {
			id: command.command,
			title: command.title
		};

186
		if (command.command && !isFalsyOrEmpty(command.arguments)) {
187 188 189 190 191 192 193 194 195 196
			// we have a contributed command with arguments. that
			// means we don't want to send the arguments around

			const id = this._heap.keep(command);
			ObjectIdentifier.mixin(result, id);

			result.id = '_internal_command_delegation';
			result.arguments = [id];
		}

197 198 199 200
		if (command.tooltip) {
			result.tooltip = command.tooltip;
		}

201 202 203 204 205 206
		return result;
	}

	fromInternal(command: modes.Command): vscode.Command {

		if (!command) {
M
Matt Bierner 已提交
207
			return undefined;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
		}

		const id = ObjectIdentifier.of(command);
		if (typeof id === 'number') {
			return this._heap.get<vscode.Command>(id);

		} else {
			return {
				command: command.id,
				title: command.title,
				arguments: command.arguments
			};
		}
	}

223
	private _executeConvertedCommand<R>(...args: any[]): Thenable<R> {
J
Johannes Rieken 已提交
224
		const actualCmd = this._heap.get<vscode.Command>(args[0]);
225 226 227
		return this._commands.executeCommand(actualCmd.command, ...actualCmd.arguments);
	}

J
Johannes Rieken 已提交
228
}