extHostCommands.ts 6.1 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
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';
J
Johannes Rieken 已提交
11 12
import * as extHostTypes from 'vs/workbench/api/node/extHostTypes';
import * as extHostTypeConverter from 'vs/workbench/api/node/extHostTypeConverters';
J
Johannes Rieken 已提交
13
import { cloneAndChange } from 'vs/base/common/objects';
14 15 16 17 18
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 已提交
19

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

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

J
Johannes Rieken 已提交
28
	private _commands = new Map<string, CommandHandler>();
29
	private _proxy: MainThreadCommandsShape;
30
	private _converter: CommandsConverter;
E
Erich Gamma 已提交
31

32 33
	constructor(
		threadService: IThreadService,
34
		heapService: ExtHostHeapService
35
	) {
A
Alex Dima 已提交
36
		super();
37
		this._proxy = threadService.get(MainContext.MainThreadCommands);
38 39 40 41 42
		this._converter = new CommandsConverter(this, heapService);
	}

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

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

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

J
Johannes Rieken 已提交
51
		if (this._commands.has(id)) {
E
Erich Gamma 已提交
52 53 54
			throw new Error('command with id already exists');
		}

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

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

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

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

		} else {
73 74
			// automagically convert some argument types

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

90
			return this._proxy.$executeCommand(id, args);
E
Erich Gamma 已提交
91 92 93 94
		}

	}

95
	$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
J
Johannes Rieken 已提交
96
		let command = this._commands.get(id);
E
Erich Gamma 已提交
97
		if (!command) {
98
			return TPromise.wrapError<T>(`Contributed command '${id}' does not exist.`);
E
Erich Gamma 已提交
99
		}
100

J
Johannes Rieken 已提交
101
		let { callback, thisArg, description } = command;
102 103 104 105

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

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

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

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


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

		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 已提交
190
			return undefined;
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
		}

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

J
Johannes Rieken 已提交
211
}