extHostCommands.ts 6.1 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';
12
import {ExtHostEditors} from 'vs/workbench/api/common/extHostEditors';
13
import {Disposable} from 'vs/workbench/api/common/extHostTypes';
E
Erich Gamma 已提交
14

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

21 22
@Remotable.PluginHostContext('ExtHostCommands')
export class ExtHostCommands {
E
Erich Gamma 已提交
23

24
	private _commands: { [n: string]: CommandHandler } = Object.create(null);
E
Erich Gamma 已提交
25
	private _proxy: MainThreadCommands;
26
	private _pluginHostEditors: ExtHostEditors;
E
Erich Gamma 已提交
27 28

	constructor(@IThreadService threadService: IThreadService) {
29
		this._pluginHostEditors = threadService.getRemotable(ExtHostEditors);
E
Erich Gamma 已提交
30 31 32
		this._proxy = threadService.getRemotable(MainThreadCommands);
	}

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

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

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

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

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

	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
54
			return this.$executeContributedCommand(id, ...args);
E
Erich Gamma 已提交
55 56

		} else {
57 58 59 60 61 62 63
			// // check that we can get all parameters over to
			// // the other side
			// for (let i = 0; i < args.length; i++) {
			// 	if (args[i] !== null && typeof args[i] === 'object' && !canSerialize(args[i])) {
			// 		throw new Error('illegal argument - can not serialize argument number: ' + i)
			// 	}
			// }
E
Erich Gamma 已提交
64

65
			return this._proxy.$executeCommand(id, args);
E
Erich Gamma 已提交
66 67 68 69
		}

	}

70
	$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
E
Erich Gamma 已提交
71 72 73 74 75
		let command = this._commands[id];
		if (!command) {
			return Promise.reject<T>(id);
		}
		try {
76 77
			let {callback, thisArg, description} = command;
			if (description) {
J
Johannes Rieken 已提交
78 79
				for (let i = 0; i < description.args.length; i++) {
					validateConstraint(args[i], description.args[i].constraint);
80 81
				}
			}
82
			let result = callback.apply(thisArg, args);
E
Erich Gamma 已提交
83 84
			return Promise.resolve(result);
		} catch (err) {
85 86 87 88 89 90
			// try {
			// 	console.log(toErrorMessage(err));
			// 	console.log(err);
			// } catch (err) {
			// 	//
			// }
E
Erich Gamma 已提交
91 92 93 94
			return Promise.reject<T>(`Running the contributed command:'${id}' failed.`);
		}
	}

95
	getCommands(filterUnderscoreCommands: boolean = false): Thenable<string[]> {
96
		return this._proxy.$getCommands().then(result => {
97 98 99 100 101
			if (filterUnderscoreCommands) {
				result = result.filter(command => command[0] !== '_');
			}
			return result;
		});
E
Erich Gamma 已提交
102
	}
103 104 105 106 107 108 109 110 111 112 113

	$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 已提交
114 115 116 117 118 119 120
}

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

	private _threadService: IThreadService;
	private _keybindingService: IKeybindingService;
121
	private _proxy: ExtHostCommands;
E
Erich Gamma 已提交
122 123 124 125

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

129
	$registerCommand(id: string): TPromise<any> {
E
Erich Gamma 已提交
130 131 132 133

		KeybindingsRegistry.registerCommandDesc({
			id,
			handler: (serviceAccessor, ...args: any[]) => {
134
				return this._proxy.$executeContributedCommand(id, ...args);
E
Erich Gamma 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147
			},
			weight: undefined,
			context: undefined,
			win: undefined,
			mac: undefined,
			linux: undefined,
			primary: undefined,
			secondary: undefined
		});

		return undefined;
	}

148
	$executeCommand<T>(id: string, args: any[]): Thenable<T> {
149
		return this._keybindingService.executeCommand(id, args);
E
Erich Gamma 已提交
150 151
	}

152
	$getCommands(): Thenable<string[]> {
E
Erich Gamma 已提交
153 154
		return TPromise.as(Object.keys(KeybindingsRegistry.getCommands()));
	}
155
}
156

157 158 159 160 161 162 163 164 165

// --- command doc

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

			// add local commands
166 167 168 169 170 171 172
			const commands = KeybindingsRegistry.getCommands();
			for (let id in commands) {
				let {description} = commands[id];
				if (description) {
					result[id] = description;
				}
			}
173

174
			// print all as markdown
175 176
			const all: string[] = [];
			for (let id in result) {
B
Benjamin Pasero 已提交
177
				all.push('`' + id + '` - ' + _generateMarkdown(result[id]));
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
			}
			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 已提交
193 194 195
		if (description.args) {
			for (let arg of description.args) {
				parts.push(`* _${arg.name}_ ${arg.description || ''}\n`);
196 197
			}
		}
J
Johannes Rieken 已提交
198 199
		if (description.returns) {
			parts.push(`* _(returns)_ ${description.returns}`);
200 201 202 203
		}
		parts.push('\n\n');
		return parts.join('');
	}
204
}