extHostLanguageFeatures.ts 37.4 KB
Newer Older
J
Johannes Rieken 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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';
J
Joao Moreno 已提交
9
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
10 11
import {Remotable, IThreadService} from 'vs/platform/thread/common/thread';
import * as vscode from 'vscode';
J
Johannes Rieken 已提交
12
import * as TypeConverters from 'vs/workbench/api/node/extHostTypeConverters';
13
import {Range, DocumentHighlightKind, Disposable, SignatureHelp, CompletionList} from 'vs/workbench/api/node/extHostTypes';
J
Johannes Rieken 已提交
14 15
import {IPosition, IRange, ISingleEditOperation} from 'vs/editor/common/editorCommon';
import * as modes from 'vs/editor/common/modes';
J
Johannes Rieken 已提交
16 17
import {ExtHostModelService} from 'vs/workbench/api/node/extHostDocuments';
import {ExtHostCommands} from 'vs/workbench/api/node/extHostCommands';
18
import {ExtHostDiagnostics} from 'vs/workbench/api/node/extHostDiagnostics';
J
Johannes Rieken 已提交
19
import {DeclarationRegistry} from 'vs/editor/contrib/goToDeclaration/common/goToDeclaration';
J
Johannes Rieken 已提交
20
import {ExtraInfoRegistry} from 'vs/editor/contrib/hover/common/hover';
21
import {OccurrencesRegistry} from 'vs/editor/contrib/wordHighlighter/common/wordHighlighter';
J
Johannes Rieken 已提交
22
import {QuickFixRegistry} from 'vs/editor/contrib/quickFix/common/quickFix';
J
Johannes Rieken 已提交
23
import {OutlineRegistry, IOutlineEntry, IOutlineSupport} from 'vs/editor/contrib/quickOpen/common/quickOpen';
B
Benjamin Pasero 已提交
24
import {NavigateTypesSupportRegistry, INavigateTypesSupport, ITypeBearing} from 'vs/workbench/parts/search/common/search';
J
Johannes Rieken 已提交
25 26 27 28 29
import {RenameRegistry} from 'vs/editor/contrib/rename/common/rename';
import {FormatRegistry, FormatOnTypeRegistry} from 'vs/editor/contrib/format/common/format';
import {CodeLensRegistry} from 'vs/editor/contrib/codelens/common/codelens';
import {ParameterHintsRegistry} from 'vs/editor/contrib/parameterHints/common/parameterHints';
import {SuggestRegistry} from 'vs/editor/contrib/suggest/common/suggest';
30
import {asWinJsPromise, ShallowCancelThenPromise} from 'vs/base/common/async';
J
Johannes Rieken 已提交
31 32 33

// --- adapter

34
class OutlineAdapter implements IOutlineSupport {
J
Johannes Rieken 已提交
35

36
	private _documents: ExtHostModelService;
J
Johannes Rieken 已提交
37 38
	private _provider: vscode.DocumentSymbolProvider;

39
	constructor(documents: ExtHostModelService, provider: vscode.DocumentSymbolProvider) {
J
Johannes Rieken 已提交
40 41 42 43 44
		this._documents = documents;
		this._provider = provider;
	}

	getOutline(resource: URI): TPromise<IOutlineEntry[]> {
45
		let doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
46 47
		return asWinJsPromise(token => this._provider.provideDocumentSymbols(doc, token)).then(value => {
			if (Array.isArray(value)) {
48
				return value.map(TypeConverters.SymbolInformation.toOutlineEntry);
J
Johannes Rieken 已提交
49 50 51 52 53
			}
		});
	}
}

54 55 56 57
interface CachedCodeLens {
	symbols: modes.ICodeLensSymbol[];
	lenses: vscode.CodeLens[];
	disposables: IDisposable[];
J
Johannes Rieken 已提交
58
}
59

60 61
class CodeLensAdapter implements modes.ICodeLensSupport {

62
	private _documents: ExtHostModelService;
63
	private _commands: ExtHostCommands;
64 65
	private _provider: vscode.CodeLensProvider;

66
	private _cache: { [uri: string]: { version: number; data: TPromise<CachedCodeLens>; } } = Object.create(null);
67

68
	constructor(documents: ExtHostModelService, commands: ExtHostCommands, provider: vscode.CodeLensProvider) {
69
		this._documents = documents;
70
		this._commands = commands;
71 72 73 74
		this._provider = provider;
	}

	findCodeLensSymbols(resource: URI): TPromise<modes.ICodeLensSymbol[]> {
75
		const doc = this._documents.getDocumentData(resource).document;
76 77
		const version = doc.version;
		const key = resource.toString();
78

79
		// from cache
80
		let entry = this._cache[key];
81
		if (entry && entry.version === version) {
82
			return new ShallowCancelThenPromise(entry.data.then(cached => cached.symbols));
83
		}
84

85 86
		const newCodeLensData = asWinJsPromise(token => this._provider.provideCodeLenses(doc, token)).then(lenses => {
			if (!Array.isArray(lenses)) {
87 88 89
				return;
			}

90 91 92 93
			const data: CachedCodeLens = {
				lenses,
				symbols: [],
				disposables: [],
J
Johannes Rieken 已提交
94
			};
95

96 97
			lenses.forEach((lens, i) => {
				data.symbols.push(<modes.ICodeLensSymbol>{
98
					id: String(i),
J
Johannes Rieken 已提交
99
					range: TypeConverters.fromRange(lens.range),
100 101
					command: TypeConverters.Command.from(lens.command, { commands: this._commands, disposables: data.disposables })
				});
102
			});
103 104 105 106 107 108 109 110 111

			return data;
		});

		this._cache[key] = {
			version,
			data: newCodeLensData
		};

112
		return new ShallowCancelThenPromise(newCodeLensData.then(newCached => {
113 114
			if (entry) {
				// only now dispose old commands et al
J
Joao Moreno 已提交
115
				entry.data.then(oldCached => dispose(oldCached.disposables));
116 117
			}
			return newCached && newCached.symbols;
118
		}));
119

120 121
	}

J
Johannes Rieken 已提交
122
	resolveCodeLensSymbol(resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICodeLensSymbol> {
123

124 125
		const entry = this._cache[resource.toString()];
		if (!entry) {
126 127 128
			return;
		}

129
		return entry.data.then(cachedData => {
130

131 132 133
			if (!cachedData) {
				return;
			}
134

135 136 137 138 139 140 141 142 143 144
			let lens = cachedData.lenses[Number(symbol.id)];
			if (!lens) {
				return;
			}

			let resolve: TPromise<vscode.CodeLens>;
			if (typeof this._provider.resolveCodeLens !== 'function' || lens.isResolved) {
				resolve = TPromise.as(lens);
			} else {
				resolve = asWinJsPromise(token => this._provider.resolveCodeLens(lens, token));
145
			}
J
Johannes Rieken 已提交
146

147 148 149 150 151 152 153 154 155 156 157 158 159
			return resolve.then(newLens => {
				lens = newLens || lens;
				let command = lens.command;
				if (!command) {
					command = {
						title: '<<MISSING COMMAND>>',
						command: 'missing',
					};
				}

				symbol.command = TypeConverters.Command.from(command, { commands: this._commands, disposables: cachedData.disposables });
				return symbol;
			});
160 161 162 163
		});
	}
}

J
Johannes Rieken 已提交
164 165
class DeclarationAdapter implements modes.IDeclarationSupport {

166
	private _documents: ExtHostModelService;
J
Johannes Rieken 已提交
167 168
	private _provider: vscode.DefinitionProvider;

169
	constructor(documents: ExtHostModelService, provider: vscode.DefinitionProvider) {
J
Johannes Rieken 已提交
170 171 172 173 174 175 176 177 178
		this._documents = documents;
		this._provider = provider;
	}

	canFindDeclaration() {
		return true;
	}

	findDeclaration(resource: URI, position: IPosition): TPromise<modes.IReference[]> {
179
		let doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
		let pos = TypeConverters.toPosition(position);
		return asWinJsPromise(token => this._provider.provideDefinition(doc, pos, token)).then(value => {
			if (Array.isArray(value)) {
				return value.map(DeclarationAdapter._convertLocation);
			} else if (value) {
				return DeclarationAdapter._convertLocation(value);
			}
		});
	}

	private static _convertLocation(location: vscode.Location): modes.IReference {
		if (!location) {
			return;
		}
		return <modes.IReference>{
			resource: location.uri,
			range: TypeConverters.fromRange(location.range)
		};
	}
}

J
Johannes Rieken 已提交
201 202
class ExtraInfoAdapter implements modes.IExtraInfoSupport {

203
	private _documents: ExtHostModelService;
J
Johannes Rieken 已提交
204 205
	private _provider: vscode.HoverProvider;

206
	constructor(documents: ExtHostModelService, provider: vscode.HoverProvider) {
J
Johannes Rieken 已提交
207 208 209 210 211 212
		this._documents = documents;
		this._provider = provider;
	}

	computeInfo(resource: URI, position: IPosition): TPromise<modes.IComputeExtraInfoResult> {

213
		let doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
214 215 216 217 218 219
		let pos = TypeConverters.toPosition(position);

		return asWinJsPromise(token => this._provider.provideHover(doc, pos, token)).then(value => {
			if (!value) {
				return;
			}
220 221
			if (!value.range) {
				value.range = doc.getWordRangeAtPosition(pos);
J
Johannes Rieken 已提交
222
			}
223 224
			if (!value.range) {
				value.range = new Range(pos, pos);
J
Johannes Rieken 已提交
225 226
			}

227
			return TypeConverters.fromHover(value);
J
Johannes Rieken 已提交
228 229 230 231
		});
	}
}

232 233
class OccurrencesAdapter implements modes.IOccurrencesSupport {

234
	private _documents: ExtHostModelService;
235 236
	private _provider: vscode.DocumentHighlightProvider;

237
	constructor(documents: ExtHostModelService, provider: vscode.DocumentHighlightProvider) {
238 239 240 241 242 243
		this._documents = documents;
		this._provider = provider;
	}

	findOccurrences(resource: URI, position: IPosition): TPromise<modes.IOccurence[]> {

244
		let doc = this._documents.getDocumentData(resource).document;
245 246 247 248 249 250 251 252 253 254 255 256 257
		let pos = TypeConverters.toPosition(position);

		return asWinJsPromise(token => this._provider.provideDocumentHighlights(doc, pos, token)).then(value => {
			if (Array.isArray(value)) {
				return value.map(OccurrencesAdapter._convertDocumentHighlight);
			}
		});
	}

	private static _convertDocumentHighlight(documentHighlight: vscode.DocumentHighlight): modes.IOccurence {
		return {
			range: TypeConverters.fromRange(documentHighlight.range),
			kind: DocumentHighlightKind[documentHighlight.kind].toString().toLowerCase()
B
Benjamin Pasero 已提交
258
		};
259 260 261
	}
}

J
Johannes Rieken 已提交
262 263
class ReferenceAdapter implements modes.IReferenceSupport {

264
	private _documents: ExtHostModelService;
J
Johannes Rieken 已提交
265 266
	private _provider: vscode.ReferenceProvider;

267
	constructor(documents: ExtHostModelService, provider: vscode.ReferenceProvider) {
J
Johannes Rieken 已提交
268 269 270 271
		this._documents = documents;
		this._provider = provider;
	}

272
	canFindReferences(): boolean {
B
Benjamin Pasero 已提交
273
		return true;
J
Johannes Rieken 已提交
274 275 276
	}

	findReferences(resource: URI, position: IPosition, includeDeclaration: boolean): TPromise<modes.IReference[]> {
277
		let doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
		let pos = TypeConverters.toPosition(position);

		return asWinJsPromise(token => this._provider.provideReferences(doc, pos, { includeDeclaration }, token)).then(value => {
			if (Array.isArray(value)) {
				return value.map(ReferenceAdapter._convertLocation);
			}
		});
	}

	private static _convertLocation(location: vscode.Location): modes.IReference {
		return <modes.IReference>{
			resource: location.uri,
			range: TypeConverters.fromRange(location.range)
		};
	}
}

J
Johannes Rieken 已提交
295 296
class QuickFixAdapter implements modes.IQuickFixSupport {

297 298
	private _documents: ExtHostModelService;
	private _commands: ExtHostCommands;
299
	private _diagnostics: ExtHostDiagnostics;
J
Johannes Rieken 已提交
300 301
	private _provider: vscode.CodeActionProvider;

302 303
	private _cachedCommands: IDisposable[] = [];

304
	constructor(documents: ExtHostModelService, commands: ExtHostCommands, diagnostics: ExtHostDiagnostics, provider: vscode.CodeActionProvider) {
J
Johannes Rieken 已提交
305 306
		this._documents = documents;
		this._commands = commands;
307
		this._diagnostics = diagnostics;
J
Johannes Rieken 已提交
308 309 310
		this._provider = provider;
	}

311
	getQuickFixes(resource: URI, range: IRange): TPromise<modes.IQuickFix[]> {
J
Johannes Rieken 已提交
312

313
		const doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
314
		const ran = TypeConverters.toRange(range);
315 316 317 318 319 320 321 322 323 324
		const allDiagnostics: vscode.Diagnostic[] = [];

		this._diagnostics.forEach(collection => {
			if (collection.has(resource)) {
				for (let diagnostic of collection.get(resource)) {
					if (diagnostic.range.intersection(ran)) {
						allDiagnostics.push(diagnostic);
					}
				}
			}
J
Johannes Rieken 已提交
325 326
		});

J
Joao Moreno 已提交
327
		this._cachedCommands = dispose(this._cachedCommands);
328 329
		const ctx = { commands: this._commands, disposables: this._cachedCommands };

330
		return asWinJsPromise(token => this._provider.provideCodeActions(doc, ran, { diagnostics: allDiagnostics }, token)).then(commands => {
J
Johannes Rieken 已提交
331 332 333 334 335
			if (!Array.isArray(commands)) {
				return;
			}
			return commands.map((command, i) => {
				return <modes.IQuickFix> {
336
					command: TypeConverters.Command.from(command, ctx),
337
					score: i
J
Johannes Rieken 已提交
338 339 340 341 342
				};
			});
		});
	}

343
	runQuickFixAction(resource: URI, range: IRange, quickFix: modes.IQuickFix): any {
344 345
		let command = TypeConverters.Command.to(quickFix.command);
		return this._commands.executeCommand(command.command, ...command.arguments);
J
Johannes Rieken 已提交
346 347 348
	}
}

349 350
class DocumentFormattingAdapter implements modes.IFormattingSupport {

351
	private _documents: ExtHostModelService;
352 353
	private _provider: vscode.DocumentFormattingEditProvider;

354
	constructor(documents: ExtHostModelService, provider: vscode.DocumentFormattingEditProvider) {
355 356 357 358 359 360
		this._documents = documents;
		this._provider = provider;
	}

	formatDocument(resource: URI, options: modes.IFormattingOptions): TPromise<ISingleEditOperation[]> {

361
		let doc = this._documents.getDocumentData(resource).document;
362 363 364

		return asWinJsPromise(token => this._provider.provideDocumentFormattingEdits(doc, <any>options, token)).then(value => {
			if (Array.isArray(value)) {
365
				return value.map(TypeConverters.TextEdit.from);
366 367 368 369 370 371 372
			}
		});
	}
}

class RangeFormattingAdapter implements modes.IFormattingSupport {

373
	private _documents: ExtHostModelService;
374 375
	private _provider: vscode.DocumentRangeFormattingEditProvider;

376
	constructor(documents: ExtHostModelService, provider: vscode.DocumentRangeFormattingEditProvider) {
377 378 379 380 381 382
		this._documents = documents;
		this._provider = provider;
	}

	formatRange(resource: URI, range: IRange, options: modes.IFormattingOptions): TPromise<ISingleEditOperation[]> {

383
		let doc = this._documents.getDocumentData(resource).document;
384 385 386 387
		let ran = TypeConverters.toRange(range);

		return asWinJsPromise(token => this._provider.provideDocumentRangeFormattingEdits(doc, ran, <any>options, token)).then(value => {
			if (Array.isArray(value)) {
388
				return value.map(TypeConverters.TextEdit.from);
389 390 391 392 393 394 395
			}
		});
	}
}

class OnTypeFormattingAdapter implements modes.IFormattingSupport {

396
	private _documents: ExtHostModelService;
397 398
	private _provider: vscode.OnTypeFormattingEditProvider;

399
	constructor(documents: ExtHostModelService, provider: vscode.OnTypeFormattingEditProvider) {
400 401 402 403
		this._documents = documents;
		this._provider = provider;
	}

404
	autoFormatTriggerCharacters: string[] = []; // not here
405 406 407

	formatAfterKeystroke(resource: URI, position: IPosition, ch: string, options: modes.IFormattingOptions): TPromise<ISingleEditOperation[]> {

408
		let doc = this._documents.getDocumentData(resource).document;
409 410 411 412
		let pos = TypeConverters.toPosition(position);

		return asWinJsPromise(token => this._provider.provideOnTypeFormattingEdits(doc, pos, ch, <any> options, token)).then(value => {
			if (Array.isArray(value)) {
413
				return value.map(TypeConverters.TextEdit.from);
414 415 416 417 418
			}
		});
	}
}

419 420 421 422 423 424 425 426 427 428 429
class NavigateTypeAdapter implements INavigateTypesSupport {

	private _provider: vscode.WorkspaceSymbolProvider;

	constructor(provider: vscode.WorkspaceSymbolProvider) {
		this._provider = provider;
	}

	getNavigateToItems(search: string): TPromise<ITypeBearing[]> {
		return asWinJsPromise(token => this._provider.provideWorkspaceSymbols(search, token)).then(value => {
			if (Array.isArray(value)) {
430
				return value.map(TypeConverters.fromSymbolInformation);
431 432 433 434 435
			}
		});
	}
}

J
Johannes Rieken 已提交
436 437
class RenameAdapter implements modes.IRenameSupport {

438
	private _documents: ExtHostModelService;
J
Johannes Rieken 已提交
439 440
	private _provider: vscode.RenameProvider;

441
	constructor(documents: ExtHostModelService, provider: vscode.RenameProvider) {
J
Johannes Rieken 已提交
442 443 444 445 446 447
		this._documents = documents;
		this._provider = provider;
	}

	rename(resource: URI, position: IPosition, newName: string): TPromise<modes.IRenameResult> {

448
		let doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
		let pos = TypeConverters.toPosition(position);

		return asWinJsPromise(token => this._provider.provideRenameEdits(doc, pos, newName, token)).then(value => {

			if (!value) {
				return;
			}

			let result = <modes.IRenameResult>{
				currentName: undefined,
				edits: []
			};

			for (let entry of value.entries()) {
				let [uri, textEdits] = entry;
				for (let textEdit of textEdits) {
					result.edits.push({
						resource: <URI>uri,
						newText: textEdit.newText,
						range: TypeConverters.fromRange(textEdit.range)
					});
				}
			}
			return result;
		}, err => {
			if (typeof err === 'string') {
				return <modes.IRenameResult>{
					currentName: undefined,
					edits: undefined,
					rejectReason: err
				};
			}
			return TPromise.wrapError(err);
		});
	}
}

486 487 488 489
interface ISuggestion2 extends modes.ISuggestion {
	id: string;
}

490 491
class SuggestAdapter implements modes.ISuggestSupport {

492
	private _documents: ExtHostModelService;
493
	private _provider: vscode.CompletionItemProvider;
494
	private _cache: { [key: string]: CompletionList } = Object.create(null);
495

496
	constructor(documents: ExtHostModelService, provider: vscode.CompletionItemProvider) {
497 498 499 500
		this._documents = documents;
		this._provider = provider;
	}

501
	suggest(resource: URI, position: IPosition): TPromise<modes.ISuggestResult[]> {
502

503
		const doc = this._documents.getDocumentData(resource).document;
504 505 506 507 508 509
		const pos = TypeConverters.toPosition(position);
		const ran = doc.getWordRangeAtPosition(pos);

		const key = resource.toString();
		delete this._cache[key];

510
		return asWinJsPromise<vscode.CompletionItem[]|vscode.CompletionList>(token => this._provider.provideCompletionItems(doc, pos, token)).then(value => {
511

512
			let defaultSuggestions: modes.ISuggestResult = {
513
				suggestions: [],
514
				currentWord: ran ? doc.getText(new Range(ran.start.line, ran.start.character, pos.line, pos.character)) : '',
515
			};
516
			let allSuggestions: modes.ISuggestResult[] = [defaultSuggestions];
517

518 519 520 521 522 523 524 525 526
			let list: CompletionList;
			if (Array.isArray(value)) {
				list = new CompletionList(value);
			} else if (value instanceof CompletionList) {
				list = value;
				defaultSuggestions.incomplete = list.isIncomplete;
			} else {
				return;
			}
527

528 529 530
			for (let i = 0; i < list.items.length; i++) {
				const item = list.items[i];
				const suggestion = <ISuggestion2> TypeConverters.Suggest.from(<any>item);
531 532 533 534 535 536 537 538 539 540 541 542 543 544

				if (item.textEdit) {

					let editRange = item.textEdit.range;

					// invalid text edit
					if (!editRange.isSingleLine || editRange.start.line !== pos.line) {
						console.warn('INVALID text edit, must be single line and on the same line');
						continue;
					}

					// insert the text of the edit and create a dedicated
					// suggestion-container with overwrite[Before|After]
					suggestion.codeSnippet = item.textEdit.newText;
B
Benjamin Pasero 已提交
545 546
					suggestion.overwriteBefore = pos.character - editRange.start.character;
					suggestion.overwriteAfter = editRange.end.character - pos.character;
547 548 549

					allSuggestions.push({
						currentWord: doc.getText(<any>editRange),
550 551
						suggestions: [suggestion],
						incomplete: list.isIncomplete
552 553 554 555 556 557 558 559 560 561 562
					});

				} else {
					defaultSuggestions.suggestions.push(suggestion);
				}

				// assign identifier to suggestion
				suggestion.id = String(i);
			}

			// cache for details call
563
			this._cache[key] = list;
564 565 566 567 568 569 570 571 572

			return allSuggestions;
		});
	}

	getSuggestionDetails(resource: URI, position: IPosition, suggestion: modes.ISuggestion): TPromise<modes.ISuggestion> {
		if (typeof this._provider.resolveCompletionItem !== 'function') {
			return TPromise.as(suggestion);
		}
573 574
		let list = this._cache[resource.toString()];
		if (!list) {
575 576
			return TPromise.as(suggestion);
		}
577
		let item = list.items[Number((<ISuggestion2> suggestion).id)];
578 579 580 581
		if (!item) {
			return TPromise.as(suggestion);
		}
		return asWinJsPromise(token => this._provider.resolveCompletionItem(item, token)).then(resolvedItem => {
582
			return TypeConverters.Suggest.from(resolvedItem || item);
583 584 585 586 587 588 589 590 591 592 593 594 595 596
		});
	}

	getTriggerCharacters(): string[] {
		throw new Error('illegal state');
	}
	shouldShowEmptySuggestionList(): boolean {
		throw new Error('illegal state');
	}
	shouldAutotriggerSuggest(context: modes.ILineContext, offset: number, triggeredByCharacter: string): boolean {
		throw new Error('illegal state');
	}
}

597 598
class ParameterHintsAdapter implements modes.IParameterHintsSupport {

599
	private _documents: ExtHostModelService;
600 601
	private _provider: vscode.SignatureHelpProvider;

602
	constructor(documents: ExtHostModelService, provider: vscode.SignatureHelpProvider) {
603 604 605 606 607 608
		this._documents = documents;
		this._provider = provider;
	}

	getParameterHints(resource: URI, position: IPosition, triggerCharacter?: string): TPromise<modes.IParameterHints> {

609
		const doc = this._documents.getDocumentData(resource).document;
610 611 612 613
		const pos = TypeConverters.toPosition(position);

		return asWinJsPromise(token => this._provider.provideSignatureHelp(doc, pos, token)).then(value => {
			if (value instanceof SignatureHelp) {
614
				return TypeConverters.SignatureHelp.from(value);
615 616 617 618 619 620 621 622 623 624 625 626 627
			}
		});
	}

	getParameterHintsTriggerCharacters(): string[] {
		throw new Error('illegal state');
	}

	shouldTriggerParameterHints(context: modes.ILineContext, offset: number): boolean {
		throw new Error('illegal state');
	}
}

628 629 630
type Adapter = OutlineAdapter | CodeLensAdapter | DeclarationAdapter | ExtraInfoAdapter
	| OccurrencesAdapter | ReferenceAdapter | QuickFixAdapter | DocumentFormattingAdapter
	| RangeFormattingAdapter | OnTypeFormattingAdapter | NavigateTypeAdapter | RenameAdapter
631
	| SuggestAdapter | ParameterHintsAdapter;
J
Johannes Rieken 已提交
632

A
Alex Dima 已提交
633
@Remotable.ExtHostContext('ExtHostLanguageFeatures')
J
Johannes Rieken 已提交
634 635
export class ExtHostLanguageFeatures {

636
	private static _handlePool: number = 0;
J
Johannes Rieken 已提交
637 638

	private _proxy: MainThreadLanguageFeatures;
639 640
	private _documents: ExtHostModelService;
	private _commands: ExtHostCommands;
641
	private _diagnostics: ExtHostDiagnostics;
J
Johannes Rieken 已提交
642 643 644 645
	private _adapter: { [handle: number]: Adapter } = Object.create(null);

	constructor( @IThreadService threadService: IThreadService) {
		this._proxy = threadService.getRemotable(MainThreadLanguageFeatures);
646 647
		this._documents = threadService.getRemotable(ExtHostModelService);
		this._commands = threadService.getRemotable(ExtHostCommands);
648
		this._diagnostics = threadService.getRemotable(ExtHostDiagnostics);
J
Johannes Rieken 已提交
649 650 651 652 653 654 655 656 657
	}

	private _createDisposable(handle: number): Disposable {
		return new Disposable(() => {
			delete this._adapter[handle];
			this._proxy.$unregister(handle);
		});
	}

658 659 660 661
	private _nextHandle(): number {
		return ExtHostLanguageFeatures._handlePool++;
	}

J
Johannes Rieken 已提交
662 663 664 665 666 667
	private _withAdapter<A, R>(handle: number, ctor: { new (...args: any[]): A }, callback: (adapter: A) => TPromise<R>): TPromise<R> {
		let adapter = this._adapter[handle];
		if (!(adapter instanceof ctor)) {
			return TPromise.wrapError(new Error('no adapter found'));
		}
		return callback(<any> adapter);
668 669
	}

J
Johannes Rieken 已提交
670 671 672
	// --- outline

	registerDocumentSymbolProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentSymbolProvider): vscode.Disposable {
673 674
		const handle = this._nextHandle();
		this._adapter[handle] = new OutlineAdapter(this._documents, provider);
675
		this._proxy.$registerOutlineSupport(handle, selector);
J
Johannes Rieken 已提交
676 677 678
		return this._createDisposable(handle);
	}

679
	$getOutline(handle: number, resource: URI): TPromise<IOutlineEntry[]> {
J
Johannes Rieken 已提交
680
		return this._withAdapter(handle, OutlineAdapter, adapter => adapter.getOutline(resource));
J
Johannes Rieken 已提交
681
	}
682 683 684 685 686

	// --- code lens

	registerCodeLensProvider(selector: vscode.DocumentSelector, provider: vscode.CodeLensProvider): vscode.Disposable {
		const handle = this._nextHandle();
687
		this._adapter[handle] = new CodeLensAdapter(this._documents, this._commands, provider);
688 689 690 691
		this._proxy.$registerCodeLensSupport(handle, selector);
		return this._createDisposable(handle);
	}

J
Johannes Rieken 已提交
692 693
	$findCodeLensSymbols(handle: number, resource: URI): TPromise<modes.ICodeLensSymbol[]> {
		return this._withAdapter(handle, CodeLensAdapter, adapter => adapter.findCodeLensSymbols(resource));
694 695
	}

696
	$resolveCodeLensSymbol(handle: number, resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICodeLensSymbol> {
J
Johannes Rieken 已提交
697 698 699 700 701 702 703 704 705 706
		return this._withAdapter(handle, CodeLensAdapter, adapter => adapter.resolveCodeLensSymbol(resource, symbol));
	}

	// --- declaration

	registerDefinitionProvider(selector: vscode.DocumentSelector, provider: vscode.DefinitionProvider): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new DeclarationAdapter(this._documents, provider);
		this._proxy.$registerDeclaractionSupport(handle, selector);
		return this._createDisposable(handle);
707 708
	}

J
Johannes Rieken 已提交
709 710 711
	$findDeclaration(handle: number, resource: URI, position: IPosition): TPromise<modes.IReference[]> {
		return this._withAdapter(handle, DeclarationAdapter, adapter => adapter.findDeclaration(resource, position));
	}
J
Johannes Rieken 已提交
712 713 714 715 716 717 718 719 720 721

	// --- extra info

	registerHoverProvider(selector: vscode.DocumentSelector, provider: vscode.HoverProvider): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new ExtraInfoAdapter(this._documents, provider);
		this._proxy.$registerExtraInfoSupport(handle, selector);
		return this._createDisposable(handle);
	}

722
	$computeInfo(handle: number, resource: URI, position: IPosition): TPromise<modes.IComputeExtraInfoResult> {
J
Johannes Rieken 已提交
723 724
		return this._withAdapter(handle, ExtraInfoAdapter, adpater => adpater.computeInfo(resource, position));
	}
725

J
Johannes Rieken 已提交
726
	// --- occurrences
727 728 729 730 731 732 733 734

	registerDocumentHighlightProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentHighlightProvider): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new OccurrencesAdapter(this._documents, provider);
		this._proxy.$registerOccurrencesSupport(handle, selector);
		return this._createDisposable(handle);
	}

735
	$findOccurrences(handle: number, resource: URI, position: IPosition): TPromise<modes.IOccurence[]> {
736 737
		return this._withAdapter(handle, OccurrencesAdapter, adapter => adapter.findOccurrences(resource, position));
	}
J
Johannes Rieken 已提交
738 739 740 741 742 743 744 745 746 747 748 749 750 751

	// --- references

	registerReferenceProvider(selector: vscode.DocumentSelector, provider: vscode.ReferenceProvider): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new ReferenceAdapter(this._documents, provider);
		this._proxy.$registerReferenceSupport(handle, selector);
		return this._createDisposable(handle);
	}

	$findReferences(handle: number, resource: URI, position: IPosition, includeDeclaration: boolean): TPromise<modes.IReference[]> {
		return this._withAdapter(handle, ReferenceAdapter, adapter => adapter.findReferences(resource, position, includeDeclaration));
	}

J
Johannes Rieken 已提交
752 753 754 755
	// --- quick fix

	registerCodeActionProvider(selector: vscode.DocumentSelector, provider: vscode.CodeActionProvider): vscode.Disposable {
		const handle = this._nextHandle();
756
		this._adapter[handle] = new QuickFixAdapter(this._documents, this._commands, this._diagnostics, provider);
J
Johannes Rieken 已提交
757 758 759 760
		this._proxy.$registerQuickFixSupport(handle, selector);
		return this._createDisposable(handle);
	}

761 762
	$getQuickFixes(handle: number, resource: URI, range: IRange): TPromise<modes.IQuickFix[]> {
		return this._withAdapter(handle, QuickFixAdapter, adapter => adapter.getQuickFixes(resource, range));
J
Johannes Rieken 已提交
763 764
	}

765 766
	$runQuickFixAction(handle: number, resource: URI, range: IRange, quickFix: modes.IQuickFix): any {
		return this._withAdapter(handle, QuickFixAdapter, adapter => adapter.runQuickFixAction(resource, range, quickFix));
J
Johannes Rieken 已提交
767
	}
768 769 770 771 772 773 774 775 776 777

	// --- formatting

	registerDocumentFormattingEditProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentFormattingEditProvider): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new DocumentFormattingAdapter(this._documents, provider);
		this._proxy.$registerDocumentFormattingSupport(handle, selector);
		return this._createDisposable(handle);
	}

778
	$formatDocument(handle: number, resource: URI, options: modes.IFormattingOptions): TPromise<ISingleEditOperation[]> {
779 780 781 782 783 784 785 786 787 788
		return this._withAdapter(handle, DocumentFormattingAdapter, adapter => adapter.formatDocument(resource, options));
	}

	registerDocumentRangeFormattingEditProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentRangeFormattingEditProvider): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new RangeFormattingAdapter(this._documents, provider);
		this._proxy.$registerRangeFormattingSupport(handle, selector);
		return this._createDisposable(handle);
	}

789
	$formatRange(handle: number, resource: URI, range: IRange, options: modes.IFormattingOptions): TPromise<ISingleEditOperation[]> {
790 791 792 793 794 795 796 797 798 799
		return this._withAdapter(handle, RangeFormattingAdapter, adapter => adapter.formatRange(resource, range, options));
	}

	registerOnTypeFormattingEditProvider(selector: vscode.DocumentSelector, provider: vscode.OnTypeFormattingEditProvider, triggerCharacters: string[]): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new OnTypeFormattingAdapter(this._documents, provider);
		this._proxy.$registerOnTypeFormattingSupport(handle, selector, triggerCharacters);
		return this._createDisposable(handle);
	}

800
	$formatAfterKeystroke(handle: number, resource: URI, position: IPosition, ch: string, options: modes.IFormattingOptions): TPromise<ISingleEditOperation[]> {
801 802 803
		return this._withAdapter(handle, OnTypeFormattingAdapter, adapter => adapter.formatAfterKeystroke(resource, position, ch, options));
	}

804 805 806 807 808 809 810 811 812 813 814 815
	// --- navigate types

	registerWorkspaceSymbolProvider(provider: vscode.WorkspaceSymbolProvider): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new NavigateTypeAdapter(provider);
		this._proxy.$registerNavigateTypeSupport(handle);
		return this._createDisposable(handle);
	}

	$getNavigateToItems(handle: number, search: string): TPromise<ITypeBearing[]> {
		return this._withAdapter(handle, NavigateTypeAdapter, adapter => adapter.getNavigateToItems(search));
	}
J
Johannes Rieken 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828

	// --- rename

	registerRenameProvider(selector: vscode.DocumentSelector, provider: vscode.RenameProvider): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new RenameAdapter(this._documents, provider);
		this._proxy.$registerRenameSupport(handle, selector);
		return this._createDisposable(handle);
	}

	$rename(handle: number, resource: URI, position: IPosition, newName: string): TPromise<modes.IRenameResult> {
		return this._withAdapter(handle, RenameAdapter, adapter => adapter.rename(resource, position, newName));
	}
829 830 831 832 833 834 835 836 837 838

	// --- suggestion

	registerCompletionItemProvider(selector: vscode.DocumentSelector, provider: vscode.CompletionItemProvider, triggerCharacters: string[]): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new SuggestAdapter(this._documents, provider);
		this._proxy.$registerSuggestSupport(handle, selector, triggerCharacters);
		return this._createDisposable(handle);
	}

839
	$suggest(handle: number, resource: URI, position: IPosition): TPromise<modes.ISuggestResult[]> {
840 841 842 843 844 845
		return this._withAdapter(handle, SuggestAdapter, adapter => adapter.suggest(resource, position));
	}

	$getSuggestionDetails(handle: number, resource: URI, position: IPosition, suggestion: modes.ISuggestion): TPromise<modes.ISuggestion> {
		return this._withAdapter(handle, SuggestAdapter, adapter => adapter.getSuggestionDetails(resource, position, suggestion));
	}
846 847 848 849 850 851 852 853 854 855 856 857 858

	// --- parameter hints

	registerSignatureHelpProvider(selector: vscode.DocumentSelector, provider: vscode.SignatureHelpProvider, triggerCharacters: string[]): vscode.Disposable {
		const handle = this._nextHandle();
		this._adapter[handle] = new ParameterHintsAdapter(this._documents, provider);
		this._proxy.$registerParameterHintsSupport(handle, selector, triggerCharacters);
		return this._createDisposable(handle);
	}

	$getParameterHints(handle: number, resource: URI, position: IPosition, triggerCharacter?: string): TPromise<modes.IParameterHints> {
		return this._withAdapter(handle, ParameterHintsAdapter, adapter => adapter.getParameterHints(resource, position, triggerCharacter));
	}
J
Johannes Rieken 已提交
859 860 861 862 863 864
}

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

	private _proxy: ExtHostLanguageFeatures;
865
	private _registrations: { [handle: number]: IDisposable; } = Object.create(null);
J
Johannes Rieken 已提交
866

867
	constructor( @IThreadService threadService: IThreadService) {
J
Johannes Rieken 已提交
868 869 870 871
		this._proxy = threadService.getRemotable(ExtHostLanguageFeatures);
	}

	$unregister(handle: number): TPromise<any> {
872 873 874 875
		let registration = this._registrations[handle];
		if (registration) {
			registration.dispose();
			delete this._registrations[handle];
J
Johannes Rieken 已提交
876 877 878 879 880 881 882
		}
		return undefined;
	}

	// --- outline

	$registerOutlineSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
883
		this._registrations[handle] = OutlineRegistry.register(selector, <IOutlineSupport>{
J
Johannes Rieken 已提交
884 885 886 887
			getOutline: (resource: URI): TPromise<IOutlineEntry[]> => {
				return this._proxy.$getOutline(handle, resource);
			}
		});
888 889 890 891 892 893 894 895 896 897
		return undefined;
	}

	// --- code lens

	$registerCodeLensSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = CodeLensRegistry.register(selector, <modes.ICodeLensSupport>{
			findCodeLensSymbols: (resource: URI): TPromise<modes.ICodeLensSymbol[]> => {
				return this._proxy.$findCodeLensSymbols(handle, resource);
			},
J
Johannes Rieken 已提交
898
			resolveCodeLensSymbol: (resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICodeLensSymbol> => {
899 900 901
				return this._proxy.$resolveCodeLensSymbol(handle, resource, symbol);
			}
		});
J
Johannes Rieken 已提交
902 903
		return undefined;
	}
J
Johannes Rieken 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917

	// --- declaration

	$registerDeclaractionSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = DeclarationRegistry.register(selector, <modes.IDeclarationSupport>{
			canFindDeclaration() {
				return true;
			},
			findDeclaration: (resource: URI, position: IPosition): TPromise<modes.IReference[]> => {
				return this._proxy.$findDeclaration(handle, resource, position);
			}
		});
		return undefined;
	}
J
Johannes Rieken 已提交
918 919 920 921 922 923 924 925 926 927 928

	// --- extra info

	$registerExtraInfoSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = ExtraInfoRegistry.register(selector, <modes.IExtraInfoSupport>{
			computeInfo: (resource: URI, position: IPosition): TPromise<modes.IComputeExtraInfoResult> => {
				return this._proxy.$computeInfo(handle, resource, position);
			}
		});
		return undefined;
	}
929 930 931 932 933 934 935 936 937 938 939

	// --- occurrences

	$registerOccurrencesSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = OccurrencesRegistry.register(selector, <modes.IOccurrencesSupport>{
			findOccurrences: (resource: URI, position: IPosition): TPromise<modes.IOccurence[]> => {
				return this._proxy.$findOccurrences(handle, resource, position);
			}
		});
		return undefined;
	}
J
Johannes Rieken 已提交
940 941 942 943

	// --- references

	$registerReferenceSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
944
		this._registrations[handle] = modes.ReferenceSearchRegistry.register(selector, <modes.IReferenceSupport>{
J
Johannes Rieken 已提交
945 946 947 948 949 950 951 952 953
			canFindReferences() {
				return true;
			},
			findReferences: (resource: URI, position: IPosition, includeDeclaration: boolean): TPromise<modes.IReference[]> => {
				return this._proxy.$findReferences(handle, resource, position, includeDeclaration);
			}
		});
		return undefined;
	}
J
Johannes Rieken 已提交
954 955 956 957 958 959

	// --- quick fix

	$registerQuickFixSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = QuickFixRegistry.register(selector, <modes.IQuickFixSupport>{
			getQuickFixes: (resource: URI, range: IRange): TPromise<modes.IQuickFix[]> => {
960
				return this._proxy.$getQuickFixes(handle, resource, range);
J
Johannes Rieken 已提交
961
			},
962 963
			runQuickFixAction: (resource: URI, range: IRange, quickFix: modes.IQuickFix) => {
				return this._proxy.$runQuickFixAction(handle, resource, range, quickFix);
J
Johannes Rieken 已提交
964 965 966 967
			}
		});
		return undefined;
	}
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

	// --- formatting

	$registerDocumentFormattingSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = FormatRegistry.register(selector, <modes.IFormattingSupport>{
			formatDocument: (resource: URI, options: modes.IFormattingOptions): TPromise <ISingleEditOperation[] > => {
				return this._proxy.$formatDocument(handle, resource, options);
			}
		});
		return undefined;
	}

	$registerRangeFormattingSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = FormatRegistry.register(selector, <modes.IFormattingSupport>{
			formatRange: (resource: URI, range: IRange, options: modes.IFormattingOptions): TPromise <ISingleEditOperation[] > => {
				return this._proxy.$formatRange(handle, resource, range, options);
			}
		});
		return undefined;
	}

	$registerOnTypeFormattingSupport(handle: number, selector: vscode.DocumentSelector, autoFormatTriggerCharacters: string[]): TPromise<any> {
		this._registrations[handle] = FormatOnTypeRegistry.register(selector, <modes.IFormattingSupport>{

			autoFormatTriggerCharacters,

			formatAfterKeystroke: (resource: URI, position: IPosition, ch: string, options: modes.IFormattingOptions): TPromise<ISingleEditOperation[]> => {
				return this._proxy.$formatAfterKeystroke(handle, resource, position, ch, options);
			}
		});
		return undefined;
	}
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

	// --- navigate type

	$registerNavigateTypeSupport(handle: number): TPromise<any> {
		this._registrations[handle] = NavigateTypesSupportRegistry.register(<INavigateTypesSupport>{
			getNavigateToItems: (search: string): TPromise<ITypeBearing[]> => {
				return this._proxy.$getNavigateToItems(handle, search);
			}
		});
		return undefined;
	}
J
Johannes Rieken 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

	// --- rename

	$registerRenameSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = RenameRegistry.register(selector, <modes.IRenameSupport>{
			rename: (resource: URI, position: IPosition, newName: string): TPromise<modes.IRenameResult> => {
				return this._proxy.$rename(handle, resource, position, newName);
			}
		});
		return undefined;
	}
1022 1023 1024 1025 1026

	// --- suggest

	$registerSuggestSupport(handle: number, selector: vscode.DocumentSelector, triggerCharacters: string[]): TPromise<any> {
		this._registrations[handle] = SuggestRegistry.register(selector, <modes.ISuggestSupport>{
1027
			suggest: (resource: URI, position: IPosition, triggerCharacter?: string): TPromise<modes.ISuggestResult[]> => {
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
				return this._proxy.$suggest(handle, resource, position);
			},
			getSuggestionDetails: (resource: URI, position: IPosition, suggestion: modes.ISuggestion): TPromise<modes.ISuggestion> => {
				return this._proxy.$getSuggestionDetails(handle, resource, position, suggestion);
			},
			getTriggerCharacters(): string[] {
				return triggerCharacters;
			},
			shouldShowEmptySuggestionList(): boolean {
				return true;
			},
			shouldAutotriggerSuggest(): boolean {
				return true;
			}
		});
		return undefined;
	}
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

	// --- parameter hints

	$registerParameterHintsSupport(handle: number, selector: vscode.DocumentSelector, triggerCharacter: string[]): TPromise<any> {
		this._registrations[handle] = ParameterHintsRegistry.register(selector, <modes.IParameterHintsSupport>{
			getParameterHints: (resource: URI, position: IPosition, triggerCharacter?: string): TPromise<modes.IParameterHints> => {
				return this._proxy.$getParameterHints(handle, resource, position, triggerCharacter);
			},
			getParameterHintsTriggerCharacters(): string[] {
				return triggerCharacter;
			},
			shouldTriggerParameterHints(context: modes.ILineContext, offset: number): boolean {
				return true;
			}
		});
		return undefined;
	}
1062
}