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

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

A
Alex Dima 已提交
27
export class ExtHostCommands extends ExtHostCommandsShape {
E
Erich Gamma 已提交
28

J
Johannes Rieken 已提交
29
	private _commands = new Map<string, CommandHandler>();
30
	private _proxy: MainThreadCommandsShape;
A
Alex Dima 已提交
31
	private _extHostEditors: ExtHostEditors;
32
	private _converter: CommandsConverter;
E
Erich Gamma 已提交
33

34 35
	constructor(
		threadService: IThreadService,
36 37
		extHostEditors: ExtHostEditors,
		heapService: ExtHostHeapService
38
	) {
A
Alex Dima 已提交
39
		super();
40 41
		this._extHostEditors = extHostEditors;
		this._proxy = threadService.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
	}

49
	registerCommand(id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): extHostTypes.Disposable {
E
Erich Gamma 已提交
50 51 52 53 54

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

J
Johannes Rieken 已提交
55
		if (this._commands.has(id)) {
E
Erich Gamma 已提交
56 57 58
			throw new Error('command with id already exists');
		}

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

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

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

J
Johannes Rieken 已提交
71
		if (this._commands.has(id)) {
E
Erich Gamma 已提交
72 73
			// we stay inside the extension host and support
			// to pass any kind of parameters around
74
			return this.$executeContributedCommand(id, ...args);
E
Erich Gamma 已提交
75 76

		} else {
77 78
			// automagically convert some argument types

J
Johannes Rieken 已提交
79
			args = cloneAndChange(args, function (value) {
80 81 82 83 84 85 86 87 88 89 90 91 92
				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 已提交
93

94
			return this._proxy.$executeCommand(id, args);
E
Erich Gamma 已提交
95 96 97 98
		}

	}

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

		let {callback, thisArg, description} = command;

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

		try {
118
			let result = callback.apply(thisArg, args);
119
			return TPromise.as(result);
E
Erich Gamma 已提交
120
		} catch (err) {
121
			// console.log(err);
122 123 124 125 126
			// try {
			// 	console.log(toErrorMessage(err));
			// } catch (err) {
			// 	//
			// }
127
			return TPromise.wrapError<T>(`Running the contributed command:'${id}' failed.`);
E
Erich Gamma 已提交
128 129 130
		}
	}

131
	getCommands(filterUnderscoreCommands: boolean = false): Thenable<string[]> {
132
		return this._proxy.$getCommands().then(result => {
133 134 135 136 137
			if (filterUnderscoreCommands) {
				result = result.filter(command => command[0] !== '_');
			}
			return result;
		});
E
Erich Gamma 已提交
138
	}
139 140 141

	$getContributedCommandHandlerDescriptions(): TPromise<{ [id: string]: string | ICommandHandlerDescription }> {
		const result: { [id: string]: string | ICommandHandlerDescription } = Object.create(null);
J
Johannes Rieken 已提交
142 143
		this._commands.forEach((command, id) => {
			let {description} = command;
144 145 146
			if (description) {
				result[id] = description;
			}
J
Johannes Rieken 已提交
147
		});
148 149
		return TPromise.as(result);
	}
E
Erich Gamma 已提交
150
}
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168


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 已提交
169
			return undefined;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		}

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

		if (!isFalsyOrEmpty(command.arguments)) {
			// 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];
		}

		return result;
	}

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

		if (!command) {
M
Matt Bierner 已提交
194
			return undefined;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
		}

		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 已提交
210 211
	private _executeConvertedCommand(...args: any[]) {
		const actualCmd = this._heap.get<vscode.Command>(args[0]);
212 213 214
		return this._commands.executeCommand(actualCmd.command, ...actualCmd.arguments);
	}

J
Johannes Rieken 已提交
215
}