extHostCommands.ts 9.2 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';
J
Johannes Rieken 已提交
11 12
import { MainContext, MainThreadCommandsShape, ExtHostCommandsShape, ObjectIdentifier, IMainContext, CommandDto } from './extHost.protocol';
import { ExtHostHeapService } from 'vs/workbench/api/common/extHostHeapService';
13
import { isNonEmptyArray } from 'vs/base/common/arrays';
14 15
import * as modes from 'vs/editor/common/modes';
import * as vscode from 'vscode';
J
Joao Moreno 已提交
16
import { ILogService } from 'vs/platform/log/common/log';
17
import { revive } from 'vs/base/common/marshalling';
18 19 20
import { Range } from 'vs/editor/common/core/range';
import { Position } from 'vs/editor/common/core/position';
import { URI } from 'vs/base/common/uri';
21 22
import { Event, Emitter } from 'vs/base/common/event';
import { IDisposable } from 'vs/base/common/lifecycle';
E
Erich Gamma 已提交
23

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

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

34
export class ExtHostCommands implements ExtHostCommandsShape {
H
Harry Hedger 已提交
35
	private readonly _onDidExecuteCommand: Emitter<ICommandEvent>;
H
Harry Hedger 已提交
36
	readonly onDidExecuteCommandEmitter: Event<ICommandEvent>;
E
Erich Gamma 已提交
37

38 39 40 41 42
	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 已提交
43

44
	constructor(
45
		mainContext: IMainContext,
J
Joao Moreno 已提交
46
		heapService: ExtHostHeapService,
47
		logService: ILogService
48
	) {
49
		this._proxy = mainContext.getProxy(MainContext.MainThreadCommands);
H
Harry Hedger 已提交
50 51
		this._onDidExecuteCommand = new Emitter<ICommandEvent>({
			onFirstListenerDidAdd: this._proxy.$onDidExecuteCommand,
52
			onLastListenerRemove: this._proxy.$disposeListener,
H
Harry Hedger 已提交
53 54
		});
		this.onDidExecuteCommandEmitter = this._onDidExecuteCommand.event;
55
		this._logService = logService;
J
Johannes Rieken 已提交
56
		this._converter = new CommandsConverter(this, heapService);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		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;
						}
					});
				}
			}
		];
84 85 86 87
	}

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

J
Joao Moreno 已提交
90 91 92 93
	registerArgumentProcessor(processor: ArgumentProcessor): void {
		this._argumentProcessors.push(processor);
	}

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

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

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

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

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

119 120 121 122 123 124 125 126
	onDidExecuteCommand(listener: (command: ICommandEvent) => any, thisArgs?: any, disposables?: IDisposable[]) {
		return this.onDidExecuteCommandEmitter(listener, thisArgs, disposables);
	}

	$handleDidExecuteCommand(command: ICommandEvent): void {
		this._onDidExecuteCommand.fire(command);
	}

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

J
Johannes Rieken 已提交
130
		if (this._commands.has(id)) {
E
Erich Gamma 已提交
131 132
			// we stay inside the extension host and support
			// to pass any kind of parameters around
133
			return this._executeContributedCommand<T>(id, args);
E
Erich Gamma 已提交
134 135

		} else {
136 137
			// automagically convert some argument types

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

H
Harry Hedger 已提交
153
			return this._proxy.$executeCommand<T>(id, args).then(result => revive(result, 0));
E
Erich Gamma 已提交
154 155 156
		}
	}

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

		try {
174
			const result = callback.apply(thisArg, args);
H
Harry Hedger 已提交
175
			this._onDidExecuteCommand.fire({ commandId: id, args });
176
			return Promise.resolve(result);
E
Erich Gamma 已提交
177
		} catch (err) {
178
			this._logService.error(err, id);
179
			return Promise.reject(new Error(`Running the contributed command: '${id}' failed.`));
E
Erich Gamma 已提交
180 181 182
		}
	}

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

186 187 188 189 190 191 192 193
		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 已提交
194
	getCommands(filterUnderscoreCommands: boolean = false): Promise<string[]> {
195
		this._logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands);
J
Joao Moreno 已提交
196

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

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


export class CommandsConverter {

220
	private readonly _delegatingCommandId: string;
221 222 223 224 225
	private _commands: ExtHostCommands;
	private _heap: ExtHostHeapService;

	// --- conversion between internal and api commands
	constructor(commands: ExtHostCommands, heap: ExtHostHeapService) {
226
		this._delegatingCommandId = `_internal_command_delegation_${Date.now()}`;
227 228
		this._commands = commands;
		this._heap = heap;
229
		this._commands.registerCommand(true, this._delegatingCommandId, this._executeConvertedCommand, this);
230 231
	}

232 233 234
	toInternal(command: vscode.Command): CommandDto;
	toInternal(command: undefined): undefined;
	toInternal(command: vscode.Command | undefined): CommandDto | undefined;
235
	toInternal(command: vscode.Command | undefined): CommandDto | undefined {
236 237

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

241 242
		const result: CommandDto = {
			$ident: undefined,
243
			id: command.command,
244
			title: command.title,
245 246
		};

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

			const id = this._heap.keep(command);
252
			result.$ident = id;
253

254
			result.id = this._delegatingCommandId;
255 256 257
			result.arguments = [id];
		}

258 259 260 261
		if (command.tooltip) {
			result.tooltip = command.tooltip;
		}

262 263 264
		return result;
	}

265
	fromInternal(command: modes.Command): vscode.Command {
266 267 268 269 270 271 272 273 274 275 276 277 278 279

		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
			};
		}
	}

J
Johannes Rieken 已提交
280
	private _executeConvertedCommand<R>(...args: any[]): Promise<R> {
J
Johannes Rieken 已提交
281
		const actualCmd = this._heap.get<vscode.Command>(args[0]);
282
		return this._commands.executeCommand(actualCmd.command, ...(actualCmd.arguments || []));
283 284
	}

J
Johannes Rieken 已提交
285
}