extHostLanguageFeatures.ts 36.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';
14
import {IModel, IEditorPosition, IPosition, IRange, ISingleEditOperation} from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
15
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';
B
Benjamin Pasero 已提交
19
import {NavigateTypesSupportRegistry, INavigateTypesSupport, ITypeBearing} from 'vs/workbench/parts/search/common/search';
20 21
import {asWinJsPromise, ShallowCancelThenPromise, wireCancellationToken} from 'vs/base/common/async';
import {CancellationToken} from 'vs/base/common/cancellation';
J
Johannes Rieken 已提交
22 23 24

// --- adapter

25
class OutlineAdapter implements modes.IOutlineSupport {
J
Johannes Rieken 已提交
26

27
	private _documents: ExtHostModelService;
J
Johannes Rieken 已提交
28 29
	private _provider: vscode.DocumentSymbolProvider;

30
	constructor(documents: ExtHostModelService, provider: vscode.DocumentSymbolProvider) {
J
Johannes Rieken 已提交
31 32 33 34
		this._documents = documents;
		this._provider = provider;
	}

35
	getOutline(resource: URI): TPromise<modes.IOutlineEntry[]> {
36
		let doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
37 38
		return asWinJsPromise(token => this._provider.provideDocumentSymbols(doc, token)).then(value => {
			if (Array.isArray(value)) {
39
				return value.map(TypeConverters.SymbolInformation.toOutlineEntry);
J
Johannes Rieken 已提交
40 41 42 43 44
			}
		});
	}
}

45 46 47 48
interface CachedCodeLens {
	symbols: modes.ICodeLensSymbol[];
	lenses: vscode.CodeLens[];
	disposables: IDisposable[];
J
Johannes Rieken 已提交
49
}
50

51 52
class CodeLensAdapter implements modes.ICodeLensSupport {

53
	private _documents: ExtHostModelService;
54
	private _commands: ExtHostCommands;
55 56
	private _provider: vscode.CodeLensProvider;

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

59
	constructor(documents: ExtHostModelService, commands: ExtHostCommands, provider: vscode.CodeLensProvider) {
60
		this._documents = documents;
61
		this._commands = commands;
62 63 64 65
		this._provider = provider;
	}

	findCodeLensSymbols(resource: URI): TPromise<modes.ICodeLensSymbol[]> {
66
		const doc = this._documents.getDocumentData(resource).document;
67 68
		const version = doc.version;
		const key = resource.toString();
69

70
		// from cache
71
		let entry = this._cache[key];
72
		if (entry && entry.version === version) {
73
			return new ShallowCancelThenPromise(entry.data.then(cached => cached.symbols));
74
		}
75

76 77
		const newCodeLensData = asWinJsPromise(token => this._provider.provideCodeLenses(doc, token)).then(lenses => {
			if (!Array.isArray(lenses)) {
78 79 80
				return;
			}

81 82 83 84
			const data: CachedCodeLens = {
				lenses,
				symbols: [],
				disposables: [],
J
Johannes Rieken 已提交
85
			};
86

87 88
			lenses.forEach((lens, i) => {
				data.symbols.push(<modes.ICodeLensSymbol>{
89
					id: String(i),
J
Johannes Rieken 已提交
90
					range: TypeConverters.fromRange(lens.range),
91
					command: TypeConverters.Command.from(lens.command, data.disposables)
92
				});
93
			});
94 95 96 97 98 99 100 101 102

			return data;
		});

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

103
		return new ShallowCancelThenPromise(newCodeLensData.then(newCached => {
104 105
			if (entry) {
				// only now dispose old commands et al
J
Joao Moreno 已提交
106
				entry.data.then(oldCached => dispose(oldCached.disposables));
107 108
			}
			return newCached && newCached.symbols;
109
		}));
110

111 112
	}

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

115 116
		const entry = this._cache[resource.toString()];
		if (!entry) {
117 118 119
			return;
		}

120
		return entry.data.then(cachedData => {
121

122 123 124
			if (!cachedData) {
				return;
			}
125

126 127 128 129 130 131 132 133 134 135
			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));
136
			}
J
Johannes Rieken 已提交
137

138 139 140 141 142 143 144 145 146 147
			return resolve.then(newLens => {
				lens = newLens || lens;
				let command = lens.command;
				if (!command) {
					command = {
						title: '<<MISSING COMMAND>>',
						command: 'missing',
					};
				}

148
				symbol.command = TypeConverters.Command.from(command, cachedData.disposables);
149 150
				return symbol;
			});
151 152 153 154
		});
	}
}

J
Johannes Rieken 已提交
155 156
class DeclarationAdapter implements modes.IDeclarationSupport {

157
	private _documents: ExtHostModelService;
J
Johannes Rieken 已提交
158 159
	private _provider: vscode.DefinitionProvider;

160
	constructor(documents: ExtHostModelService, provider: vscode.DefinitionProvider) {
J
Johannes Rieken 已提交
161 162 163 164 165 166 167 168 169
		this._documents = documents;
		this._provider = provider;
	}

	canFindDeclaration() {
		return true;
	}

	findDeclaration(resource: URI, position: IPosition): TPromise<modes.IReference[]> {
170
		let doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		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)
		};
	}
}

A
Alex Dima 已提交
192
class HoverProviderAdapter {
J
Johannes Rieken 已提交
193

194
	private _documents: ExtHostModelService;
J
Johannes Rieken 已提交
195 196
	private _provider: vscode.HoverProvider;

197
	constructor(documents: ExtHostModelService, provider: vscode.HoverProvider) {
J
Johannes Rieken 已提交
198 199 200 201
		this._documents = documents;
		this._provider = provider;
	}

202
	public provideHover(resource: URI, position: IPosition): TPromise<modes.Hover> {
J
Johannes Rieken 已提交
203

204
		let doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
205 206 207 208 209 210
		let pos = TypeConverters.toPosition(position);

		return asWinJsPromise(token => this._provider.provideHover(doc, pos, token)).then(value => {
			if (!value) {
				return;
			}
211 212
			if (!value.range) {
				value.range = doc.getWordRangeAtPosition(pos);
J
Johannes Rieken 已提交
213
			}
214 215
			if (!value.range) {
				value.range = new Range(pos, pos);
J
Johannes Rieken 已提交
216 217
			}

218
			return TypeConverters.fromHover(value);
J
Johannes Rieken 已提交
219 220 221 222
		});
	}
}

223 224
class OccurrencesAdapter implements modes.IOccurrencesSupport {

225
	private _documents: ExtHostModelService;
226 227
	private _provider: vscode.DocumentHighlightProvider;

228
	constructor(documents: ExtHostModelService, provider: vscode.DocumentHighlightProvider) {
229 230 231 232 233 234
		this._documents = documents;
		this._provider = provider;
	}

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

235
		let doc = this._documents.getDocumentData(resource).document;
236 237 238 239 240 241 242 243 244 245 246 247 248
		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 已提交
249
		};
250 251 252
	}
}

J
Johannes Rieken 已提交
253 254
class ReferenceAdapter implements modes.IReferenceSupport {

255
	private _documents: ExtHostModelService;
J
Johannes Rieken 已提交
256 257
	private _provider: vscode.ReferenceProvider;

258
	constructor(documents: ExtHostModelService, provider: vscode.ReferenceProvider) {
J
Johannes Rieken 已提交
259 260 261 262
		this._documents = documents;
		this._provider = provider;
	}

263
	canFindReferences(): boolean {
B
Benjamin Pasero 已提交
264
		return true;
J
Johannes Rieken 已提交
265 266 267
	}

	findReferences(resource: URI, position: IPosition, includeDeclaration: boolean): TPromise<modes.IReference[]> {
268
		let doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
		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 已提交
286 287
class QuickFixAdapter implements modes.IQuickFixSupport {

288 289
	private _documents: ExtHostModelService;
	private _commands: ExtHostCommands;
290
	private _diagnostics: ExtHostDiagnostics;
J
Johannes Rieken 已提交
291 292
	private _provider: vscode.CodeActionProvider;

293 294
	private _cachedCommands: IDisposable[] = [];

295
	constructor(documents: ExtHostModelService, commands: ExtHostCommands, diagnostics: ExtHostDiagnostics, provider: vscode.CodeActionProvider) {
J
Johannes Rieken 已提交
296 297
		this._documents = documents;
		this._commands = commands;
298
		this._diagnostics = diagnostics;
J
Johannes Rieken 已提交
299 300 301
		this._provider = provider;
	}

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

304
		const doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
305
		const ran = TypeConverters.toRange(range);
306 307 308 309 310 311 312 313 314 315
		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 已提交
316 317
		});

J
Joao Moreno 已提交
318
		this._cachedCommands = dispose(this._cachedCommands);
319

320
		return asWinJsPromise(token => this._provider.provideCodeActions(doc, ran, { diagnostics: allDiagnostics }, token)).then(commands => {
J
Johannes Rieken 已提交
321 322 323 324 325
			if (!Array.isArray(commands)) {
				return;
			}
			return commands.map((command, i) => {
				return <modes.IQuickFix> {
326
					command: TypeConverters.Command.from(command, this._cachedCommands),
327
					score: i
J
Johannes Rieken 已提交
328 329 330 331 332
				};
			});
		});
	}

333
	runQuickFixAction(resource: URI, range: IRange, quickFix: modes.IQuickFix): any {
334 335
		let command = TypeConverters.Command.to(quickFix.command);
		return this._commands.executeCommand(command.command, ...command.arguments);
J
Johannes Rieken 已提交
336 337 338
	}
}

339 340
class DocumentFormattingAdapter implements modes.IFormattingSupport {

341
	private _documents: ExtHostModelService;
342 343
	private _provider: vscode.DocumentFormattingEditProvider;

344
	constructor(documents: ExtHostModelService, provider: vscode.DocumentFormattingEditProvider) {
345 346 347 348 349 350
		this._documents = documents;
		this._provider = provider;
	}

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

351
		let doc = this._documents.getDocumentData(resource).document;
352 353 354

		return asWinJsPromise(token => this._provider.provideDocumentFormattingEdits(doc, <any>options, token)).then(value => {
			if (Array.isArray(value)) {
355
				return value.map(TypeConverters.TextEdit.from);
356 357 358 359 360 361 362
			}
		});
	}
}

class RangeFormattingAdapter implements modes.IFormattingSupport {

363
	private _documents: ExtHostModelService;
364 365
	private _provider: vscode.DocumentRangeFormattingEditProvider;

366
	constructor(documents: ExtHostModelService, provider: vscode.DocumentRangeFormattingEditProvider) {
367 368 369 370 371 372
		this._documents = documents;
		this._provider = provider;
	}

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

373
		let doc = this._documents.getDocumentData(resource).document;
374 375 376 377
		let ran = TypeConverters.toRange(range);

		return asWinJsPromise(token => this._provider.provideDocumentRangeFormattingEdits(doc, ran, <any>options, token)).then(value => {
			if (Array.isArray(value)) {
378
				return value.map(TypeConverters.TextEdit.from);
379 380 381 382 383 384 385
			}
		});
	}
}

class OnTypeFormattingAdapter implements modes.IFormattingSupport {

386
	private _documents: ExtHostModelService;
387 388
	private _provider: vscode.OnTypeFormattingEditProvider;

389
	constructor(documents: ExtHostModelService, provider: vscode.OnTypeFormattingEditProvider) {
390 391 392 393
		this._documents = documents;
		this._provider = provider;
	}

394
	autoFormatTriggerCharacters: string[] = []; // not here
395 396 397

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

398
		let doc = this._documents.getDocumentData(resource).document;
399 400 401 402
		let pos = TypeConverters.toPosition(position);

		return asWinJsPromise(token => this._provider.provideOnTypeFormattingEdits(doc, pos, ch, <any> options, token)).then(value => {
			if (Array.isArray(value)) {
403
				return value.map(TypeConverters.TextEdit.from);
404 405 406 407 408
			}
		});
	}
}

409 410 411 412 413 414 415 416 417 418 419
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)) {
420
				return value.map(TypeConverters.fromSymbolInformation);
421 422 423 424 425
			}
		});
	}
}

J
Johannes Rieken 已提交
426 427
class RenameAdapter implements modes.IRenameSupport {

428
	private _documents: ExtHostModelService;
J
Johannes Rieken 已提交
429 430
	private _provider: vscode.RenameProvider;

431
	constructor(documents: ExtHostModelService, provider: vscode.RenameProvider) {
J
Johannes Rieken 已提交
432 433 434 435 436 437
		this._documents = documents;
		this._provider = provider;
	}

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

438
		let doc = this._documents.getDocumentData(resource).document;
J
Johannes Rieken 已提交
439 440 441 442 443 444 445 446 447 448 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
		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);
		});
	}
}

476 477 478 479
interface ISuggestion2 extends modes.ISuggestion {
	id: string;
}

480 481
class SuggestAdapter implements modes.ISuggestSupport {

482
	private _documents: ExtHostModelService;
483
	private _provider: vscode.CompletionItemProvider;
484
	private _cache: { [key: string]: CompletionList } = Object.create(null);
485

486
	constructor(documents: ExtHostModelService, provider: vscode.CompletionItemProvider) {
487 488 489 490
		this._documents = documents;
		this._provider = provider;
	}

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

493
		const doc = this._documents.getDocumentData(resource).document;
494 495 496 497 498 499
		const pos = TypeConverters.toPosition(position);
		const ran = doc.getWordRangeAtPosition(pos);

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

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

502
			let defaultSuggestions: modes.ISuggestResult = {
503
				suggestions: [],
504
				currentWord: ran ? doc.getText(new Range(ran.start.line, ran.start.character, pos.line, pos.character)) : '',
505
			};
506
			let allSuggestions: modes.ISuggestResult[] = [defaultSuggestions];
507

508 509 510 511 512 513
			let list: CompletionList;
			if (Array.isArray(value)) {
				list = new CompletionList(value);
			} else if (value instanceof CompletionList) {
				list = value;
				defaultSuggestions.incomplete = list.isIncomplete;
514 515 516
			} else if (!value) {
				// undefined and null are valid results
				return;
517
			} else {
518 519
				// warn about everything else
				console.warn('INVALID result from completion provider. expected CompletionItem-array or CompletionList but got:', value);
520 521
				return;
			}
522

523 524
			for (let i = 0; i < list.items.length; i++) {
				const item = list.items[i];
525
				const suggestion = <ISuggestion2> TypeConverters.Suggest.from(item);
526 527 528 529 530 531 532 533 534 535 536 537 538 539

				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 已提交
540 541
					suggestion.overwriteBefore = pos.character - editRange.start.character;
					suggestion.overwriteAfter = editRange.end.character - pos.character;
542 543

					allSuggestions.push({
544
						currentWord: doc.getText(editRange),
545 546
						suggestions: [suggestion],
						incomplete: list.isIncomplete
547 548 549 550 551 552 553 554 555 556 557
					});

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

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

			// cache for details call
558
			this._cache[key] = list;
559 560 561 562 563 564 565 566 567

			return allSuggestions;
		});
	}

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

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

589
class ParameterHintsAdapter {
590

591
	private _documents: ExtHostModelService;
592 593
	private _provider: vscode.SignatureHelpProvider;

594
	constructor(documents: ExtHostModelService, provider: vscode.SignatureHelpProvider) {
595 596 597 598
		this._documents = documents;
		this._provider = provider;
	}

599
	provideSignatureHelp(resource: URI, position: IPosition): TPromise<modes.SignatureHelp> {
600

601
		const doc = this._documents.getDocumentData(resource).document;
602 603 604 605
		const pos = TypeConverters.toPosition(position);

		return asWinJsPromise(token => this._provider.provideSignatureHelp(doc, pos, token)).then(value => {
			if (value instanceof SignatureHelp) {
606
				return TypeConverters.SignatureHelp.from(value);
607 608 609 610 611
			}
		});
	}
}

A
Alex Dima 已提交
612
type Adapter = OutlineAdapter | CodeLensAdapter | DeclarationAdapter | HoverProviderAdapter
613 614
	| OccurrencesAdapter | ReferenceAdapter | QuickFixAdapter | DocumentFormattingAdapter
	| RangeFormattingAdapter | OnTypeFormattingAdapter | NavigateTypeAdapter | RenameAdapter
615
	| SuggestAdapter | ParameterHintsAdapter;
J
Johannes Rieken 已提交
616

A
Alex Dima 已提交
617
@Remotable.ExtHostContext('ExtHostLanguageFeatures')
J
Johannes Rieken 已提交
618 619
export class ExtHostLanguageFeatures {

620
	private static _handlePool: number = 0;
J
Johannes Rieken 已提交
621 622

	private _proxy: MainThreadLanguageFeatures;
623 624
	private _documents: ExtHostModelService;
	private _commands: ExtHostCommands;
625
	private _diagnostics: ExtHostDiagnostics;
J
Johannes Rieken 已提交
626 627 628 629
	private _adapter: { [handle: number]: Adapter } = Object.create(null);

	constructor( @IThreadService threadService: IThreadService) {
		this._proxy = threadService.getRemotable(MainThreadLanguageFeatures);
630 631
		this._documents = threadService.getRemotable(ExtHostModelService);
		this._commands = threadService.getRemotable(ExtHostCommands);
632
		this._diagnostics = threadService.getRemotable(ExtHostDiagnostics);
J
Johannes Rieken 已提交
633 634 635 636 637 638 639 640 641
	}

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

642 643 644 645
	private _nextHandle(): number {
		return ExtHostLanguageFeatures._handlePool++;
	}

J
Johannes Rieken 已提交
646 647 648 649 650 651
	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);
652 653
	}

J
Johannes Rieken 已提交
654 655 656
	// --- outline

	registerDocumentSymbolProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentSymbolProvider): vscode.Disposable {
657 658
		const handle = this._nextHandle();
		this._adapter[handle] = new OutlineAdapter(this._documents, provider);
659
		this._proxy.$registerOutlineSupport(handle, selector);
J
Johannes Rieken 已提交
660 661 662
		return this._createDisposable(handle);
	}

663
	$getOutline(handle: number, resource: URI): TPromise<modes.IOutlineEntry[]> {
J
Johannes Rieken 已提交
664
		return this._withAdapter(handle, OutlineAdapter, adapter => adapter.getOutline(resource));
J
Johannes Rieken 已提交
665
	}
666 667 668 669 670

	// --- code lens

	registerCodeLensProvider(selector: vscode.DocumentSelector, provider: vscode.CodeLensProvider): vscode.Disposable {
		const handle = this._nextHandle();
671
		this._adapter[handle] = new CodeLensAdapter(this._documents, this._commands, provider);
672 673 674 675
		this._proxy.$registerCodeLensSupport(handle, selector);
		return this._createDisposable(handle);
	}

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

680
	$resolveCodeLensSymbol(handle: number, resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICodeLensSymbol> {
J
Johannes Rieken 已提交
681 682 683 684 685 686 687 688 689 690
		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);
691 692
	}

J
Johannes Rieken 已提交
693 694 695
	$findDeclaration(handle: number, resource: URI, position: IPosition): TPromise<modes.IReference[]> {
		return this._withAdapter(handle, DeclarationAdapter, adapter => adapter.findDeclaration(resource, position));
	}
J
Johannes Rieken 已提交
696 697 698 699 700

	// --- extra info

	registerHoverProvider(selector: vscode.DocumentSelector, provider: vscode.HoverProvider): vscode.Disposable {
		const handle = this._nextHandle();
A
Alex Dima 已提交
701 702
		this._adapter[handle] = new HoverProviderAdapter(this._documents, provider);
		this._proxy.$registerHoverProvider(handle, selector);
J
Johannes Rieken 已提交
703 704 705
		return this._createDisposable(handle);
	}

706
	$provideHover(handle: number, resource: URI, position: IPosition): TPromise<modes.Hover> {
A
Alex Dima 已提交
707
		return this._withAdapter(handle, HoverProviderAdapter, adpater => adpater.provideHover(resource, position));
J
Johannes Rieken 已提交
708
	}
709

J
Johannes Rieken 已提交
710
	// --- occurrences
711 712 713 714 715 716 717 718

	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);
	}

719
	$findOccurrences(handle: number, resource: URI, position: IPosition): TPromise<modes.IOccurence[]> {
720 721
		return this._withAdapter(handle, OccurrencesAdapter, adapter => adapter.findOccurrences(resource, position));
	}
J
Johannes Rieken 已提交
722 723 724 725 726 727 728 729 730 731 732 733 734 735

	// --- 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 已提交
736 737 738 739
	// --- quick fix

	registerCodeActionProvider(selector: vscode.DocumentSelector, provider: vscode.CodeActionProvider): vscode.Disposable {
		const handle = this._nextHandle();
740
		this._adapter[handle] = new QuickFixAdapter(this._documents, this._commands, this._diagnostics, provider);
J
Johannes Rieken 已提交
741 742 743 744
		this._proxy.$registerQuickFixSupport(handle, selector);
		return this._createDisposable(handle);
	}

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

749 750
	$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 已提交
751
	}
752 753 754 755 756 757 758 759 760 761

	// --- 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);
	}

762
	$formatDocument(handle: number, resource: URI, options: modes.IFormattingOptions): TPromise<ISingleEditOperation[]> {
763 764 765 766 767 768 769 770 771 772
		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);
	}

773
	$formatRange(handle: number, resource: URI, range: IRange, options: modes.IFormattingOptions): TPromise<ISingleEditOperation[]> {
774 775 776 777 778 779 780 781 782 783
		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);
	}

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

788 789 790 791 792 793 794 795 796 797 798 799
	// --- 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 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812

	// --- 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));
	}
813 814 815 816 817 818 819 820 821 822

	// --- 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);
	}

823
	$suggest(handle: number, resource: URI, position: IPosition): TPromise<modes.ISuggestResult[]> {
824 825 826 827 828 829
		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));
	}
830 831 832 833 834 835 836 837 838 839

	// --- 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);
	}

840 841
	$provideSignatureHelp(handle: number, resource: URI, position: IPosition): TPromise<modes.SignatureHelp> {
		return this._withAdapter(handle, ParameterHintsAdapter, adapter => adapter.provideSignatureHelp(resource, position));
842
	}
J
Johannes Rieken 已提交
843 844 845 846 847 848
}

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

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

851
	constructor( @IThreadService threadService: IThreadService) {
J
Johannes Rieken 已提交
852 853 854 855
		this._proxy = threadService.getRemotable(ExtHostLanguageFeatures);
	}

	$unregister(handle: number): TPromise<any> {
856 857 858 859
		let registration = this._registrations[handle];
		if (registration) {
			registration.dispose();
			delete this._registrations[handle];
J
Johannes Rieken 已提交
860 861 862 863 864 865 866
		}
		return undefined;
	}

	// --- outline

	$registerOutlineSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
867 868
		this._registrations[handle] = modes.OutlineRegistry.register(selector, <modes.IOutlineSupport>{
			getOutline: (resource: URI): TPromise<modes.IOutlineEntry[]> => {
J
Johannes Rieken 已提交
869 870 871
				return this._proxy.$getOutline(handle, resource);
			}
		});
872 873 874 875 876 877
		return undefined;
	}

	// --- code lens

	$registerCodeLensSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
878
		this._registrations[handle] = modes.CodeLensRegistry.register(selector, <modes.ICodeLensSupport>{
879 880 881
			findCodeLensSymbols: (resource: URI): TPromise<modes.ICodeLensSymbol[]> => {
				return this._proxy.$findCodeLensSymbols(handle, resource);
			},
J
Johannes Rieken 已提交
882
			resolveCodeLensSymbol: (resource: URI, symbol: modes.ICodeLensSymbol): TPromise<modes.ICodeLensSymbol> => {
883 884 885
				return this._proxy.$resolveCodeLensSymbol(handle, resource, symbol);
			}
		});
J
Johannes Rieken 已提交
886 887
		return undefined;
	}
J
Johannes Rieken 已提交
888 889 890 891

	// --- declaration

	$registerDeclaractionSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
892
		this._registrations[handle] = modes.DeclarationRegistry.register(selector, <modes.IDeclarationSupport>{
J
Johannes Rieken 已提交
893 894 895 896 897 898 899 900 901
			canFindDeclaration() {
				return true;
			},
			findDeclaration: (resource: URI, position: IPosition): TPromise<modes.IReference[]> => {
				return this._proxy.$findDeclaration(handle, resource, position);
			}
		});
		return undefined;
	}
J
Johannes Rieken 已提交
902 903 904

	// --- extra info

A
Alex Dima 已提交
905 906
	$registerHoverProvider(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.HoverProviderRegistry.register(selector, <modes.HoverProvider>{
907 908
			provideHover: (model:IModel, position:IEditorPosition, cancellationToken:CancellationToken): Thenable<modes.Hover> => {
				return wireCancellationToken(cancellationToken, this._proxy.$provideHover(handle, model.getAssociatedResource(), position));
J
Johannes Rieken 已提交
909 910 911 912
			}
		});
		return undefined;
	}
913 914 915 916

	// --- occurrences

	$registerOccurrencesSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
917
		this._registrations[handle] = modes.OccurrencesRegistry.register(selector, <modes.IOccurrencesSupport>{
918 919 920 921 922 923
			findOccurrences: (resource: URI, position: IPosition): TPromise<modes.IOccurence[]> => {
				return this._proxy.$findOccurrences(handle, resource, position);
			}
		});
		return undefined;
	}
J
Johannes Rieken 已提交
924 925 926 927

	// --- references

	$registerReferenceSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
928
		this._registrations[handle] = modes.ReferenceSearchRegistry.register(selector, <modes.IReferenceSupport>{
J
Johannes Rieken 已提交
929 930 931 932 933 934 935 936 937
			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 已提交
938 939 940 941

	// --- quick fix

	$registerQuickFixSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
942
		this._registrations[handle] = modes.QuickFixRegistry.register(selector, <modes.IQuickFixSupport>{
J
Johannes Rieken 已提交
943
			getQuickFixes: (resource: URI, range: IRange): TPromise<modes.IQuickFix[]> => {
944
				return this._proxy.$getQuickFixes(handle, resource, range);
J
Johannes Rieken 已提交
945
			},
946 947
			runQuickFixAction: (resource: URI, range: IRange, quickFix: modes.IQuickFix) => {
				return this._proxy.$runQuickFixAction(handle, resource, range, quickFix);
J
Johannes Rieken 已提交
948 949 950 951
			}
		});
		return undefined;
	}
952 953 954 955

	// --- formatting

	$registerDocumentFormattingSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
956
		this._registrations[handle] = modes.FormatRegistry.register(selector, <modes.IFormattingSupport>{
957 958 959 960 961 962 963 964
			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> {
965
		this._registrations[handle] = modes.FormatRegistry.register(selector, <modes.IFormattingSupport>{
966 967 968 969 970 971 972 973
			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> {
974
		this._registrations[handle] = modes.FormatOnTypeRegistry.register(selector, <modes.IFormattingSupport>{
975 976 977 978 979 980 981 982 983

			autoFormatTriggerCharacters,

			formatAfterKeystroke: (resource: URI, position: IPosition, ch: string, options: modes.IFormattingOptions): TPromise<ISingleEditOperation[]> => {
				return this._proxy.$formatAfterKeystroke(handle, resource, position, ch, options);
			}
		});
		return undefined;
	}
984 985 986 987 988 989 990 991 992 993 994

	// --- 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 已提交
995 996 997 998

	// --- rename

	$registerRenameSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
999
		this._registrations[handle] = modes.RenameRegistry.register(selector, <modes.IRenameSupport>{
J
Johannes Rieken 已提交
1000 1001 1002 1003 1004 1005
			rename: (resource: URI, position: IPosition, newName: string): TPromise<modes.IRenameResult> => {
				return this._proxy.$rename(handle, resource, position, newName);
			}
		});
		return undefined;
	}
1006 1007 1008 1009

	// --- suggest

	$registerSuggestSupport(handle: number, selector: vscode.DocumentSelector, triggerCharacters: string[]): TPromise<any> {
1010
		this._registrations[handle] = modes.SuggestRegistry.register(selector, <modes.ISuggestSupport>{
1011
			suggest: (resource: URI, position: IPosition, triggerCharacter?: string): TPromise<modes.ISuggestResult[]> => {
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
				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;
			},
			shouldAutotriggerSuggest(): boolean {
				return true;
			}
		});
		return undefined;
	}
1026 1027 1028 1029

	// --- parameter hints

	$registerParameterHintsSupport(handle: number, selector: vscode.DocumentSelector, triggerCharacter: string[]): TPromise<any> {
1030
		this._registrations[handle] = modes.ParameterHintsRegistry.register(selector, <modes.IParameterHintsSupport>{
1031 1032 1033 1034 1035

			parameterHintsTriggerCharacters: triggerCharacter,

			provideSignatureHelp: (model:IModel, position:IEditorPosition, cancellationToken:CancellationToken): Thenable<modes.SignatureHelp> => {
				return wireCancellationToken(cancellationToken, this._proxy.$provideSignatureHelp(handle, model.getAssociatedResource(), position));
1036
			}
1037

1038 1039 1040
		});
		return undefined;
	}
1041
}