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

import {Remotable, IThreadService} from 'vs/platform/thread/common/thread';
B
Benjamin Pasero 已提交
8
import {validateConstraint} from 'vs/base/common/types';
E
Erich Gamma 已提交
9
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
10
import {IKeybindingService, ICommandHandlerDescription} from 'vs/platform/keybinding/common/keybindingService';
E
Erich Gamma 已提交
11
import {TPromise} from 'vs/base/common/winjs.base';
J
Johannes Rieken 已提交
12 13 14
import {ExtHostEditors} from 'vs/workbench/api/node/extHostEditors';
import * as extHostTypes from 'vs/workbench/api/node/extHostTypes';
import * as extHostTypeConverter from 'vs/workbench/api/node/extHostTypeConverters';
15
import {cloneAndChange} from 'vs/base/common/objects';
E
Erich Gamma 已提交
16

17 18 19 20 21 22
interface CommandHandler {
	callback: Function;
	thisArg: any;
	description: ICommandHandlerDescription;
}

A
Alex Dima 已提交
23
@Remotable.ExtHostContext('ExtHostCommands')
24
export class ExtHostCommands {
E
Erich Gamma 已提交
25

26
	private _commands: { [n: string]: CommandHandler } = Object.create(null);
E
Erich Gamma 已提交
27
	private _proxy: MainThreadCommands;
A
Alex Dima 已提交
28
	private _extHostEditors: ExtHostEditors;
E
Erich Gamma 已提交
29 30

	constructor(@IThreadService threadService: IThreadService) {
A
Alex Dima 已提交
31
		this._extHostEditors = threadService.getRemotable(ExtHostEditors);
E
Erich Gamma 已提交
32 33 34
		this._proxy = threadService.getRemotable(MainThreadCommands);
	}

35
	registerCommand(id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): extHostTypes.Disposable {
E
Erich Gamma 已提交
36 37 38 39 40 41 42 43 44

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

		if (this._commands[id]) {
			throw new Error('command with id already exists');
		}

45 46
		this._commands[id] = { callback, thisArg, description };
		this._proxy.$registerCommand(id);
E
Erich Gamma 已提交
47

48
		return new extHostTypes.Disposable(() => delete this._commands[id]);
E
Erich Gamma 已提交
49 50 51 52 53 54 55
	}

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

		if (this._commands[id]) {
			// we stay inside the extension host and support
			// to pass any kind of parameters around
56
			return this.$executeContributedCommand(id, ...args);
E
Erich Gamma 已提交
57 58

		} else {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
			// automagically convert some argument types

			args = cloneAndChange(args, function(value) {
				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 已提交
75

76
			return this._proxy.$executeCommand(id, args);
E
Erich Gamma 已提交
77 78 79 80
		}

	}

81
	$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
E
Erich Gamma 已提交
82 83
		let command = this._commands[id];
		if (!command) {
84
			return Promise.reject<T>(`Contributed command '${id}' does not exist.`);
E
Erich Gamma 已提交
85 86
		}
		try {
87 88
			let {callback, thisArg, description} = command;
			if (description) {
J
Johannes Rieken 已提交
89 90
				for (let i = 0; i < description.args.length; i++) {
					validateConstraint(args[i], description.args[i].constraint);
91 92
				}
			}
93
			let result = callback.apply(thisArg, args);
E
Erich Gamma 已提交
94 95
			return Promise.resolve(result);
		} catch (err) {
A
Alex Dima 已提交
96
			console.log(err);
97 98 99 100 101
			// try {
			// 	console.log(toErrorMessage(err));
			// } catch (err) {
			// 	//
			// }
E
Erich Gamma 已提交
102 103 104 105
			return Promise.reject<T>(`Running the contributed command:'${id}' failed.`);
		}
	}

106
	getCommands(filterUnderscoreCommands: boolean = false): Thenable<string[]> {
107
		return this._proxy.$getCommands().then(result => {
108 109 110 111 112
			if (filterUnderscoreCommands) {
				result = result.filter(command => command[0] !== '_');
			}
			return result;
		});
E
Erich Gamma 已提交
113
	}
114 115 116 117 118 119 120 121 122 123 124

	$getContributedCommandHandlerDescriptions(): TPromise<{ [id: string]: string | ICommandHandlerDescription }> {
		const result: { [id: string]: string | ICommandHandlerDescription } = Object.create(null);
		for (let id in this._commands) {
			let {description} = this._commands[id];
			if (description) {
				result[id] = description;
			}
		}
		return TPromise.as(result);
	}
E
Erich Gamma 已提交
125 126 127 128 129 130 131
}

@Remotable.MainContext('MainThreadCommands')
export class MainThreadCommands {

	private _threadService: IThreadService;
	private _keybindingService: IKeybindingService;
132
	private _proxy: ExtHostCommands;
E
Erich Gamma 已提交
133 134 135 136

	constructor( @IThreadService threadService: IThreadService, @IKeybindingService keybindingService: IKeybindingService) {
		this._threadService = threadService;
		this._keybindingService = keybindingService;
137
		this._proxy = this._threadService.getRemotable(ExtHostCommands);
E
Erich Gamma 已提交
138 139
	}

140
	$registerCommand(id: string): TPromise<any> {
E
Erich Gamma 已提交
141 142 143 144

		KeybindingsRegistry.registerCommandDesc({
			id,
			handler: (serviceAccessor, ...args: any[]) => {
145
				return this._proxy.$executeContributedCommand(id, ...args);
E
Erich Gamma 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158
			},
			weight: undefined,
			context: undefined,
			win: undefined,
			mac: undefined,
			linux: undefined,
			primary: undefined,
			secondary: undefined
		});

		return undefined;
	}

159
	$executeCommand<T>(id: string, args: any[]): Thenable<T> {
160
		return this._keybindingService.executeCommand(id, args);
E
Erich Gamma 已提交
161 162
	}

163
	$getCommands(): Thenable<string[]> {
E
Erich Gamma 已提交
164 165
		return TPromise.as(Object.keys(KeybindingsRegistry.getCommands()));
	}
166
}
167

168 169 170 171 172 173 174 175 176

// --- command doc

KeybindingsRegistry.registerCommandDesc({
	id: '_generateCommandsDocumentation',
	handler: function(accessor) {
		return accessor.get(IThreadService).getRemotable(ExtHostCommands).$getContributedCommandHandlerDescriptions().then(result => {

			// add local commands
177 178 179 180 181 182 183
			const commands = KeybindingsRegistry.getCommands();
			for (let id in commands) {
				let {description} = commands[id];
				if (description) {
					result[id] = description;
				}
			}
184

185
			// print all as markdown
186 187
			const all: string[] = [];
			for (let id in result) {
B
Benjamin Pasero 已提交
188
				all.push('`' + id + '` - ' + _generateMarkdown(result[id]));
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
			}
			console.log(all.join('\n'));
		});
	},
	context: undefined,
	weight: KeybindingsRegistry.WEIGHT.builtinExtension(0),
	primary: undefined
});

function _generateMarkdown(description: string | ICommandHandlerDescription): string {
	if (typeof description === 'string') {
		return description;
	} else {
		let parts = [description.description];
		parts.push('\n\n');
J
Johannes Rieken 已提交
204 205 206
		if (description.args) {
			for (let arg of description.args) {
				parts.push(`* _${arg.name}_ ${arg.description || ''}\n`);
207 208
			}
		}
J
Johannes Rieken 已提交
209 210
		if (description.returns) {
			parts.push(`* _(returns)_ ${description.returns}`);
211 212 213 214
		}
		parts.push('\n\n');
		return parts.join('');
	}
215
}