extHostCommands.ts 9.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

J
Johannes Rieken 已提交
6
import { validateConstraint } from 'vs/base/common/types';
7
import { ICommandHandlerDescription, ICommandEvent } from 'vs/platform/commands/common/commands';
J
Johannes Rieken 已提交
8 9
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
import * as extHostTypeConverter from 'vs/workbench/api/common/extHostTypeConverters';
J
Johannes Rieken 已提交
10
import { cloneAndChange } from 'vs/base/common/objects';
11
import { MainContext, MainThreadCommandsShape, ExtHostCommandsShape, ObjectIdentifier, ICommandDto } from './extHost.protocol';
12
import { isNonEmptyArray } from 'vs/base/common/arrays';
13 14
import * as modes from 'vs/editor/common/modes';
import * as vscode from 'vscode';
J
Joao Moreno 已提交
15
import { ILogService } from 'vs/platform/log/common/log';
16
import { revive } from 'vs/base/common/marshalling';
17 18 19
import { Range } from 'vs/editor/common/core/range';
import { Position } from 'vs/editor/common/core/position';
import { URI } from 'vs/base/common/uri';
20
import { Event, Emitter } from 'vs/base/common/event';
21
import { DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
22
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
23
import { IExtHostRpcService } from 'vs/workbench/api/common/rpcService';
E
Erich Gamma 已提交
24

25 26 27
interface CommandHandler {
	callback: Function;
	thisArg: any;
M
Matt Bierner 已提交
28
	description?: ICommandHandlerDescription;
29 30
}

J
Joao Moreno 已提交
31 32 33 34
export interface ArgumentProcessor {
	processArgument(arg: any): any;
}

35
export class ExtHostCommands implements ExtHostCommandsShape {
J
Johannes Rieken 已提交
36

37 38
	readonly _serviceBrand: any;

J
Johannes Rieken 已提交
39 40
	private readonly _onDidExecuteCommand: Emitter<vscode.CommandExecutionEvent>;
	readonly onDidExecuteCommand: Event<vscode.CommandExecutionEvent>;
E
Erich Gamma 已提交
41

42 43 44 45 46
	private readonly _commands = new Map<string, CommandHandler>();
	private readonly _proxy: MainThreadCommandsShape;
	private readonly _converter: CommandsConverter;
	private readonly _logService: ILogService;
	private readonly _argumentProcessors: ArgumentProcessor[];
E
Erich Gamma 已提交
47

48
	constructor(
49
		@IExtHostRpcService extHostRpc: IExtHostRpcService,
50
		@ILogService logService: ILogService
51
	) {
52
		this._proxy = extHostRpc.getProxy(MainContext.MainThreadCommands);
J
Johannes Rieken 已提交
53
		this._onDidExecuteCommand = new Emitter<vscode.CommandExecutionEvent>({
54 55
			onFirstListenerDidAdd: () => this._proxy.$registerCommandListener(),
			onLastListenerRemove: () => this._proxy.$unregisterCommandListener(),
H
Harry Hedger 已提交
56
		});
57
		this.onDidExecuteCommand = Event.filter(this._onDidExecuteCommand.event, e => e.command[0] !== '_'); // filter 'private' commands
58
		this._logService = logService;
59
		this._converter = new CommandsConverter(this);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
		this._argumentProcessors = [
			{
				processArgument(a) {
					// URI, Regex
					return revive(a, 0);
				}
			},
			{
				processArgument(arg) {
					return cloneAndChange(arg, function (obj) {
						// Reverse of https://github.com/Microsoft/vscode/blob/1f28c5fc681f4c01226460b6d1c7e91b8acb4a5b/src/vs/workbench/api/node/extHostCommands.ts#L112-L127
						if (Range.isIRange(obj)) {
							return extHostTypeConverter.Range.to(obj);
						}
						if (Position.isIPosition(obj)) {
							return extHostTypeConverter.Position.to(obj);
						}
						if (Range.isIRange((obj as modes.Location).range) && URI.isUri((obj as modes.Location).uri)) {
							return extHostTypeConverter.location.to(obj);
						}
						if (!Array.isArray(obj)) {
							return obj;
						}
					});
				}
			}
		];
87 88 89 90
	}

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

J
Joao Moreno 已提交
93 94 95 96
	registerArgumentProcessor(processor: ArgumentProcessor): void {
		this._argumentProcessors.push(processor);
	}

97
	registerCommand(global: boolean, id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): extHostTypes.Disposable {
98
		this._logService.trace('ExtHostCommands#registerCommand', id);
E
Erich Gamma 已提交
99 100 101 102 103

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

J
Johannes Rieken 已提交
104
		if (this._commands.has(id)) {
105
			throw new Error(`command '${id}' already exists`);
E
Erich Gamma 已提交
106 107
		}

J
Johannes Rieken 已提交
108
		this._commands.set(id, { callback, thisArg, description });
109 110 111
		if (global) {
			this._proxy.$registerCommand(id);
		}
E
Erich Gamma 已提交
112

J
Johannes Rieken 已提交
113
		return new extHostTypes.Disposable(() => {
J
Johannes Rieken 已提交
114
			if (this._commands.delete(id)) {
115 116 117
				if (global) {
					this._proxy.$unregisterCommand(id);
				}
J
Johannes Rieken 已提交
118 119
			}
		});
E
Erich Gamma 已提交
120 121
	}

122
	$handleDidExecuteCommand(command: ICommandEvent): void {
123 124 125 126
		this._onDidExecuteCommand.fire({
			command: command.commandId,
			arguments: command.args.map(arg => this._argumentProcessors.reduce((r, p) => p.processArgument(r), arg))
		});
127 128
	}

J
Johannes Rieken 已提交
129
	executeCommand<T>(id: string, ...args: any[]): Promise<T> {
130
		this._logService.trace('ExtHostCommands#executeCommand', id);
E
Erich Gamma 已提交
131

J
Johannes Rieken 已提交
132
		if (this._commands.has(id)) {
E
Erich Gamma 已提交
133 134
			// we stay inside the extension host and support
			// to pass any kind of parameters around
J
Johannes Rieken 已提交
135 136 137
			const res = this._executeContributedCommand<T>(id, args);
			this._onDidExecuteCommand.fire({ command: id, arguments: args });
			return res;
E
Erich Gamma 已提交
138 139

		} else {
140 141
			// automagically convert some argument types

J
Johannes Rieken 已提交
142
			args = cloneAndChange(args, function (value) {
143
				if (value instanceof extHostTypes.Position) {
144
					return extHostTypeConverter.Position.from(value);
145 146
				}
				if (value instanceof extHostTypes.Range) {
147
					return extHostTypeConverter.Range.from(value);
148 149 150 151 152 153 154 155
				}
				if (value instanceof extHostTypes.Location) {
					return extHostTypeConverter.location.from(value);
				}
				if (!Array.isArray(value)) {
					return value;
				}
			});
E
Erich Gamma 已提交
156

H
Harry Hedger 已提交
157
			return this._proxy.$executeCommand<T>(id, args).then(result => revive(result, 0));
E
Erich Gamma 已提交
158 159 160
		}
	}

J
Johannes Rieken 已提交
161
	private _executeContributedCommand<T>(id: string, args: any[]): Promise<T> {
162 163 164 165 166
		const command = this._commands.get(id);
		if (!command) {
			throw new Error('Unknown command');
		}
		let { callback, thisArg, description } = command;
167 168 169
		if (description) {
			for (let i = 0; i < description.args.length; i++) {
				try {
J
Johannes Rieken 已提交
170
					validateConstraint(args[i], description.args[i].constraint);
171
				} catch (err) {
172
					return Promise.reject(new Error(`Running the contributed command: '${id}' failed. Illegal argument '${description.args[i].name}' - ${description.args[i].description}`));
173 174
				}
			}
175 176 177
		}

		try {
178
			const result = callback.apply(thisArg, args);
179
			return Promise.resolve(result);
E
Erich Gamma 已提交
180
		} catch (err) {
181
			this._logService.error(err, id);
182
			return Promise.reject(new Error(`Running the contributed command: '${id}' failed.`));
E
Erich Gamma 已提交
183 184 185
		}
	}

J
Johannes Rieken 已提交
186
	$executeContributedCommand<T>(id: string, ...args: any[]): Promise<T> {
187 188
		this._logService.trace('ExtHostCommands#$executeContributedCommand', id);

189 190 191 192 193 194 195 196
		if (!this._commands.has(id)) {
			return Promise.reject(new Error(`Contributed command '${id}' does not exist.`));
		} else {
			args = args.map(arg => this._argumentProcessors.reduce((r, p) => p.processArgument(r), arg));
			return this._executeContributedCommand(id, args);
		}
	}

J
Johannes Rieken 已提交
197
	getCommands(filterUnderscoreCommands: boolean = false): Promise<string[]> {
198
		this._logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands);
J
Joao Moreno 已提交
199

200
		return this._proxy.$getCommands().then(result => {
201 202 203 204 205
			if (filterUnderscoreCommands) {
				result = result.filter(command => command[0] !== '_');
			}
			return result;
		});
E
Erich Gamma 已提交
206
	}
207

J
Johannes Rieken 已提交
208
	$getContributedCommandHandlerDescriptions(): Promise<{ [id: string]: string | ICommandHandlerDescription }> {
209
		const result: { [id: string]: string | ICommandHandlerDescription } = Object.create(null);
J
Johannes Rieken 已提交
210
		this._commands.forEach((command, id) => {
J
Johannes Rieken 已提交
211
			let { description } = command;
212 213 214
			if (description) {
				result[id] = description;
			}
J
Johannes Rieken 已提交
215
		});
216
		return Promise.resolve(result);
217
	}
E
Erich Gamma 已提交
218
}
219 220 221 222


export class CommandsConverter {

223
	private readonly _delegatingCommandId: string;
224 225 226
	private readonly _commands: ExtHostCommands;
	private readonly _cache = new Map<number, vscode.Command>();
	private _cachIdPool = 0;
227 228

	// --- conversion between internal and api commands
229
	constructor(commands: ExtHostCommands) {
230
		this._delegatingCommandId = `_vscode_delegate_cmd_${Date.now().toString(36)}`;
231
		this._commands = commands;
232
		this._commands.registerCommand(true, this._delegatingCommandId, this._executeConvertedCommand, this);
233 234
	}

J
Johannes Rieken 已提交
235
	toInternal(command: vscode.Command | undefined, disposables: DisposableStore): ICommandDto | undefined {
236 237

		if (!command) {
M
Matt Bierner 已提交
238
			return undefined;
239 240
		}

J
Johannes Rieken 已提交
241
		const result: ICommandDto = {
242
			$ident: undefined,
243
			id: command.command,
244
			title: command.title,
245
			tooltip: command.tooltip
246 247
		};

248
		if (command.command && isNonEmptyArray(command.arguments)) {
249 250 251
			// we have a contributed command with arguments. that
			// means we don't want to send the arguments around

252 253 254
			const id = ++this._cachIdPool;
			this._cache.set(id, command);
			disposables.add(toDisposable(() => this._cache.delete(id)));
255
			result.$ident = id;
256

257
			result.id = this._delegatingCommandId;
258 259
			result.arguments = [id];

260 261
		}

262 263 264
		return result;
	}

265
	fromInternal(command: modes.Command): vscode.Command | undefined {
266 267 268

		const id = ObjectIdentifier.of(command);
		if (typeof id === 'number') {
269
			return this._cache.get(id);
270 271 272 273 274 275 276 277 278 279

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

J
Johannes Rieken 已提交
280
	private _executeConvertedCommand<R>(...args: any[]): Promise<R> {
281 282 283 284
		const actualCmd = this._cache.get(args[0]);
		if (!actualCmd) {
			return Promise.reject('actual command NOT FOUND');
		}
285
		return this._commands.executeCommand(actualCmd.command, ...(actualCmd.arguments || []));
286 287
	}

J
Johannes Rieken 已提交
288
}
289 290 291

export interface IExtHostCommands extends ExtHostCommands { }
export const IExtHostCommands = createDecorator<IExtHostCommands>('IExtHostCommands');