extHostApiCommands.ts 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  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 URI from 'vs/base/common/uri';
import {TPromise} from 'vs/base/common/winjs.base';
import {IDisposable} from 'vs/base/common/lifecycle';
import * as vscode from 'vscode';
J
Johannes Rieken 已提交
11 12
import * as typeConverters from 'vs/workbench/api/node/extHostTypeConverters';
import * as types from 'vs/workbench/api/node/extHostTypes';
13
import {ISingleEditOperation} from 'vs/editor/common/editorCommon';
14
import * as modes from 'vs/editor/common/modes';
15
import {ICommandHandlerDescription} from 'vs/platform/keybinding/common/keybindingService';
J
Johannes Rieken 已提交
16
import {ExtHostCommands} from 'vs/workbench/api/node/extHostCommands';
17
import {IQuickFix2} from 'vs/editor/contrib/quickFix/common/quickFix';
18
import {IOutline} from 'vs/editor/contrib/quickOpen/common/quickOpen';
B
Benjamin Pasero 已提交
19
import {ITypeBearing} from 'vs/workbench/parts/search/common/search';
J
Johannes Rieken 已提交
20
import {ICodeLensData} from 'vs/editor/contrib/codelens/common/codelens';
J
Johannes Rieken 已提交
21
import {IThreadService} from 'vs/platform/thread/common/thread';
J
Johannes Rieken 已提交
22

J
Johannes Rieken 已提交
23 24
export function registerApiCommands(threadService: IThreadService) {
	const commands = threadService.getRemotable(ExtHostCommands);
J
Johannes Rieken 已提交
25
	new ExtHostApiCommands(commands).registerCommands();
J
Johannes Rieken 已提交
26 27 28
}

class ExtHostApiCommands {
29

30
	private _commands: ExtHostCommands;
31 32
	private _disposables: IDisposable[] = [];

33
	constructor(commands: ExtHostCommands) {
34
		this._commands = commands;
J
Johannes Rieken 已提交
35
	}
36

J
Johannes Rieken 已提交
37
	registerCommands() {
38 39
		this._register('vscode.executeWorkspaceSymbolProvider', this._executeWorkspaceSymbolProvider, {
			description: 'Execute all workspace symbol provider.',
J
Johannes Rieken 已提交
40
			args: [{ name: 'query', description: 'Search string', constraint: String }],
J
Johannes Rieken 已提交
41 42
			returns: 'A promise that resolves to an array of SymbolInformation-instances.'

43 44 45
		});
		this._register('vscode.executeDefinitionProvider', this._executeDefinitionProvider, {
			description: 'Execute all definition provider.',
J
Johannes Rieken 已提交
46 47 48 49 50
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
				{ name: 'position', description: 'Position of a symbol', constraint: types.Position }
			],
			returns: 'A promise that resolves to an array of Location-instances.'
51 52
		});
		this._register('vscode.executeHoverProvider', this._executeHoverProvider, {
J
Johannes Rieken 已提交
53
			description: 'Execute all hover provider.',
J
Johannes Rieken 已提交
54 55 56 57 58
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
				{ name: 'position', description: 'Position of a symbol', constraint: types.Position }
			],
			returns: 'A promise that resolves to an array of Hover-instances.'
59 60 61
		});
		this._register('vscode.executeDocumentHighlights', this._executeDocumentHighlights, {
			description: 'Execute document highlight provider.',
J
Johannes Rieken 已提交
62 63 64 65 66
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
				{ name: 'position', description: 'Position in a text document', constraint: types.Position }
			],
			returns: 'A promise that resolves to an array of DocumentHighlight-instances.'
67
		});
68 69
		this._register('vscode.executeReferenceProvider', this._executeReferenceProvider, {
			description: 'Execute reference provider.',
J
Johannes Rieken 已提交
70 71 72 73 74
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
				{ name: 'position', description: 'Position in a text document', constraint: types.Position }
			],
			returns: 'A promise that resolves to an array of Location-instances.'
75 76 77
		});
		this._register('vscode.executeDocumentRenameProvider', this._executeDocumentRenameProvider, {
			description: 'Execute rename provider.',
J
Johannes Rieken 已提交
78 79 80 81 82 83
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
				{ name: 'position', description: 'Position in a text document', constraint: types.Position },
				{ name: 'newName', description: 'The new symbol name', constraint: String }
			],
			returns: 'A promise that resolves to a WorkspaceEdit.'
84 85 86
		});
		this._register('vscode.executeSignatureHelpProvider', this._executeSignatureHelpProvider, {
			description: 'Execute signature help provider.',
J
Johannes Rieken 已提交
87 88
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
89
				{ name: 'position', description: 'Position in a text document', constraint: types.Position },
90
				{ name: 'triggerCharacter', description: '(optional) Trigger signature help when the user types the character, like `,` or `(`', constraint: value => value === void 0 || typeof value === 'string' }
J
Johannes Rieken 已提交
91 92
			],
			returns: 'A promise that resolves to SignatureHelp.'
93 94 95
		});
		this._register('vscode.executeDocumentSymbolProvider', this._executeDocumentSymbolProvider, {
			description: 'Execute document symbol provider.',
J
Johannes Rieken 已提交
96 97 98 99
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI }
			],
			returns: 'A promise that resolves to an array of SymbolInformation-instances.'
100 101 102
		});
		this._register('vscode.executeCompletionItemProvider', this._executeCompletionItemProvider, {
			description: 'Execute completion item provider.',
J
Johannes Rieken 已提交
103 104
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
105
				{ name: 'position', description: 'Position in a text document', constraint: types.Position },
106
				{ name: 'triggerCharacter', description: '(optional) Trigger completion when the user types the character, like `,` or `(`', constraint: value => value === void 0 || typeof value === 'string' }
J
Johannes Rieken 已提交
107
			],
108
			returns: 'A promise that resolves to a CompletionList-instance.'
109 110 111
		});
		this._register('vscode.executeCodeActionProvider', this._executeCodeActionProvider, {
			description: 'Execute code action provider.',
J
Johannes Rieken 已提交
112 113 114 115
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
				{ name: 'range', description: 'Range in a text document', constraint: types.Range }
			],
X
xzper 已提交
116
			returns: 'A promise that resolves to an array of Command-instances.'
117 118 119
		});
		this._register('vscode.executeCodeLensProvider', this._executeCodeLensProvider, {
			description: 'Execute completion item provider.',
J
Johannes Rieken 已提交
120 121 122
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI }
			],
X
xzper 已提交
123
			returns: 'A promise that resolves to an array of CodeLens-instances.'
124 125 126
		});
		this._register('vscode.executeFormatDocumentProvider', this._executeFormatDocumentProvider, {
			description: 'Execute document format provider.',
J
Johannes Rieken 已提交
127 128 129 130 131
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
				{ name: 'options', description: 'Formatting options' }
			],
			returns: 'A promise that resolves to an array of TextEdits.'
132 133 134
		});
		this._register('vscode.executeFormatRangeProvider', this._executeFormatRangeProvider, {
			description: 'Execute range format provider.',
J
Johannes Rieken 已提交
135 136 137 138 139 140
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
				{ name: 'range', description: 'Range in a text document', constraint: types.Range },
				{ name: 'options', description: 'Formatting options' }
			],
			returns: 'A promise that resolves to an array of TextEdits.'
141 142 143
		});
		this._register('vscode.executeFormatOnTypeProvider', this._executeFormatOnTypeProvider, {
			description: 'Execute document format provider.',
J
Johannes Rieken 已提交
144 145 146 147 148 149 150
			args: [
				{ name: 'uri', description: 'Uri of a text document', constraint: URI },
				{ name: 'position', description: 'Position in a text document', constraint: types.Position },
				{ name: 'ch', description: 'Character that got typed', constraint: String },
				{ name: 'options', description: 'Formatting options' }
			],
			returns: 'A promise that resolves to an array of TextEdits.'
151
		});
152 153 154 155 156 157 158


		this._register('vscode.previewHtml', (uri: URI, position?: vscode.ViewColumn) => {
			return this._commands.executeCommand('_workbench.previewHtml', uri,
				typeof position === 'number' ? typeConverters.fromViewColumn(position) : void 0);

		}, {
159 160 161 162 163 164
				description: `
					Render the html of the resource in an editor view.

					Links contained in the document will be handled by VS Code whereby it supports file-resources and virtual resources
					as well as triggering commands using the 'command'-scheme.
				`,
165
			args: [
166
				{ name: 'uri', description: 'Uri of the resource to preview.', constraint: value => value instanceof URI || typeof value === 'string' },
167 168 169
				{ name: 'column', description: '(optional) Column in which to preview.' },
			]
		});
170

171 172 173 174 175 176 177 178
		this._register('vscode.openFolder', (uri?: URI, newWindow?: boolean) => {
			if (!uri) {
				return this._commands.executeCommand('_workbench.ipc', 'vscode:openFolderPicker', [newWindow]);
			}

			return this._commands.executeCommand('_workbench.ipc', 'vscode:windowOpen', [[uri.fsPath], newWindow]);
		}, {
			description: 'Open a folder in the current window or new window depending on the newWindow argument. Note that opening in the same window will shutdown the current extension host process and start a new one on the given folder unless the newWindow parameter is set to true.',
179
			args: [
180 181
				{ name: 'uri', description: '(optional) Uri of the folder to open. If not provided, a native dialog will ask the user for the folder', constraint: value => value === void 0 || value instanceof URI },
				{ name: 'newWindow', description: '(optional) Wether to open the folder in a new window or the same. Defaults to opening in the same window.', constraint: value => value === void 0 || typeof value === 'boolean' }
182 183
			]
		});
184 185 186 187
	}

	// --- command impl

188 189 190
	private _register(id: string, handler: (...args: any[]) => any, description?: ICommandHandlerDescription): void {
		let disposable = this._commands.registerCommand(id, handler, this, description);
		this._disposables.push(disposable);
J
Johannes Rieken 已提交
191 192
	}

193 194 195 196 197 198
	/**
	 * Execute workspace symbol provider.
	 *
	 * @param query Search string to match query symbol names
	 * @return A promise that resolves to an array of symbol information.
	 */
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	private _executeWorkspaceSymbolProvider(query: string): Thenable<types.SymbolInformation[]> {
		return this._commands.executeCommand<ITypeBearing[]>('_executeWorkspaceSymbolProvider', { query }).then(value => {
			if (Array.isArray(value)) {
				return value.map(typeConverters.toSymbolInformation);
			}
		});
	}

	private _executeDefinitionProvider(resource: URI, position: types.Position): Thenable<types.Location[]> {
		const args = {
			resource,
			position: position && typeConverters.fromPosition(position)
		};
		return this._commands.executeCommand<modes.IReference[]>('_executeDefinitionProvider', args).then(value => {
			if (Array.isArray(value)) {
214
				return value.map(typeConverters.location.to);
215 216 217 218 219 220 221 222 223 224 225
			}
		});
	}

	private _executeHoverProvider(resource: URI, position: types.Position): Thenable<types.Hover[]> {
		const args = {
			resource,
			position: position && typeConverters.fromPosition(position)
		};
		return this._commands.executeCommand<modes.IComputeExtraInfoResult[]>('_executeHoverProvider', args).then(value => {
			if (Array.isArray(value)) {
B
Benjamin Pasero 已提交
226
				return value.map(typeConverters.toHover);
227 228 229
			}
		});
	}
230 231 232 233 234 235 236 237

	private _executeDocumentHighlights(resource: URI, position: types.Position): Thenable<types.DocumentHighlight[]> {
		const args = {
			resource,
			position: position && typeConverters.fromPosition(position)
		};
		return this._commands.executeCommand<modes.IOccurence[]>('_executeDocumentHighlights', args).then(value => {
			if (Array.isArray(value)) {
B
Benjamin Pasero 已提交
238
				return value.map(typeConverters.toDocumentHighlight);
239 240 241
			}
		});
	}
J
Johannes Rieken 已提交
242 243 244 245 246 247

	private _executeReferenceProvider(resource: URI, position: types.Position): Thenable<types.Location[]> {
		const args = {
			resource,
			position: position && typeConverters.fromPosition(position)
		};
248
		return this._commands.executeCommand<modes.IReference[]>('_executeReferenceProvider', args).then(value => {
J
Johannes Rieken 已提交
249
			if (Array.isArray(value)) {
250
				return value.map(typeConverters.location.to);
J
Johannes Rieken 已提交
251 252 253
			}
		});
	}
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

	private _executeDocumentRenameProvider(resource: URI, position: types.Position, newName: string): Thenable<types.WorkspaceEdit> {
		const args = {
			resource,
			position: position && typeConverters.fromPosition(position),
			newName
		};
		return this._commands.executeCommand<modes.IRenameResult>('_executeDocumentRenameProvider', args).then(value => {
			if (!value) {
				return;
			}
			if (value.rejectReason) {
				return TPromise.wrapError(value.rejectReason);
			}
			let workspaceEdit = new types.WorkspaceEdit();
			for (let edit of value.edits) {
				workspaceEdit.replace(edit.resource, typeConverters.toRange(edit.range), edit.newText);
			}
			return workspaceEdit;
		});
	}

	private _executeSignatureHelpProvider(resource: URI, position: types.Position, triggerCharacter: string): Thenable<types.SignatureHelp> {
		const args = {
			resource,
			position: position && typeConverters.fromPosition(position),
			triggerCharacter
		};
		return this._commands.executeCommand<modes.IParameterHints>('_executeSignatureHelpProvider', args).then(value => {
			if (value) {
				return typeConverters.SignatureHelp.to(value);
			}
		});
	}
288

289
	private _executeCompletionItemProvider(resource: URI, position: types.Position, triggerCharacter: string): Thenable<types.CompletionItem[]|types.CompletionList> {
290 291 292 293 294
		const args = {
			resource,
			position: position && typeConverters.fromPosition(position),
			triggerCharacter
		};
295
		return this._commands.executeCommand<modes.ISuggestResult[]>('_executeCompletionItemProvider', args).then(value => {
296 297
			if (value) {
				let items: types.CompletionItem[] = [];
298
				let incomplete: boolean;
299 300 301 302 303
				for (let suggestions of value) {
					incomplete = suggestions.incomplete || incomplete;
					for (let suggestion of suggestions.suggestions) {
						const item = typeConverters.Suggest.to(suggestions, position, suggestion);
						items.push(item);
304 305
					}
				}
306
				return new types.CompletionList(<any>items, incomplete);
307 308 309 310
			}
		});
	}

311 312 313 314 315 316 317 318 319 320
	private _executeDocumentSymbolProvider(resource: URI): Thenable<types.SymbolInformation[]> {
		const args = {
			resource
		};
		return this._commands.executeCommand<IOutline>('_executeDocumentSymbolProvider', args).then(value => {
			if (value && Array.isArray(value.entries)) {
				return value.entries.map(typeConverters.SymbolInformation.fromOutlineEntry);
			}
		});
	}
321 322 323 324 325 326 327

	private _executeCodeActionProvider(resource: URI, range: types.Range): Thenable<vscode.Command[]> {
		const args = {
			resource,
			range: typeConverters.fromRange(range)
		};
		return this._commands.executeCommand<IQuickFix2[]>('_executeCodeActionProvider', args).then(value => {
328 329
			if (!Array.isArray(value)) {
				return;
330
			}
331
			return value.map(quickFix => typeConverters.Command.to(quickFix.command));
332 333
		});
	}
J
Johannes Rieken 已提交
334

J
Johannes Rieken 已提交
335
	private _executeCodeLensProvider(resource: URI): Thenable<vscode.CodeLens[]> {
336
		const args = { resource };
J
Johannes Rieken 已提交
337
		return this._commands.executeCommand<ICodeLensData[]>('_executeCodeLensProvider', args).then(value => {
J
Johannes Rieken 已提交
338
			if (Array.isArray(value)) {
J
Johannes Rieken 已提交
339
				return value.map(item => {
340 341
					return new types.CodeLens(
						typeConverters.toRange(item.symbol.range),
J
Johannes Rieken 已提交
342 343
						typeConverters.Command.to(item.symbol.command));
				});
J
Johannes Rieken 已提交
344 345 346
			}
		});
	}
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

	private _executeFormatDocumentProvider(resource: URI, options: vscode.FormattingOptions): Thenable<vscode.TextEdit[]> {
		const args = {
			resource,
			options
		};
		return this._commands.executeCommand<ISingleEditOperation[]>('_executeFormatDocumentProvider', args).then(value => {
			if (Array.isArray(value)) {
				return value.map(edit => new types.TextEdit(typeConverters.toRange(edit.range), edit.text));
			}
		});
	}

	private _executeFormatRangeProvider(resource: URI, range: types.Range, options: vscode.FormattingOptions): Thenable<vscode.TextEdit[]> {
		const args = {
			resource,
			range: typeConverters.fromRange(range),
			options
		};
		return this._commands.executeCommand<ISingleEditOperation[]>('_executeFormatRangeProvider', args).then(value => {
			if (Array.isArray(value)) {
				return value.map(edit => new types.TextEdit(typeConverters.toRange(edit.range), edit.text));
			}
		});
	}
372

J
Johannes Rieken 已提交
373
	private _executeFormatOnTypeProvider(resource: URI, position: types.Position, ch: string, options: vscode.FormattingOptions): Thenable<vscode.TextEdit[]> {
374 375 376 377 378 379 380 381 382 383 384 385
		const args = {
			resource,
			position: typeConverters.fromPosition(position),
			ch,
			options
		};
		return this._commands.executeCommand<ISingleEditOperation[]>('_executeFormatOnTypeProvider', args).then(value => {
			if (Array.isArray(value)) {
				return value.map(edit => new types.TextEdit(typeConverters.toRange(edit.range), edit.text));
			}
		});
	}
386
}