mainThreadLanguageFeatures.ts 15.1 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

J
Johannes Rieken 已提交
7 8
import { TPromise } from 'vs/base/common/winjs.base';
import { IDisposable } from 'vs/base/common/lifecycle';
9
import { Emitter } from 'vs/base/common/event';
10
import * as vscode from 'vscode';
J
Johannes Rieken 已提交
11
import { IReadOnlyModel, ISingleEditOperation } from 'vs/editor/common/editorCommon';
12
import * as modes from 'vs/editor/common/modes';
13
import { WorkspaceSymbolProviderRegistry, IWorkspaceSymbolProvider } from 'vs/workbench/parts/search/common/search';
J
Johannes Rieken 已提交
14 15 16 17
import { wireCancellationToken } from 'vs/base/common/async';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Position as EditorPosition } from 'vs/editor/common/core/position';
import { Range as EditorRange } from 'vs/editor/common/core/range';
18
import { ExtHostContext, MainThreadLanguageFeaturesShape, ExtHostLanguageFeaturesShape, IRawColorFormatMap, MainContext, IExtHostContext } from '../node/extHost.protocol';
19 20
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
import { LanguageConfiguration } from 'vs/editor/common/modes/languageConfiguration';
21
import { IHeapService } from './mainThreadHeapService';
A
Alex Dima 已提交
22
import { IModeService } from 'vs/editor/common/services/modeService';
J
Joao Moreno 已提交
23
import { ColorFormatter, CombinedColorFormatter } from 'vs/editor/contrib/colorPicker/common/colorFormatter';
24
import { extHostNamedCustomer } from "vs/workbench/api/electron-browser/extHostCustomers";
25

26
@extHostNamedCustomer(MainContext.MainThreadLanguageFeatures)
27
export class MainThreadLanguageFeatures implements MainThreadLanguageFeaturesShape {
28

29
	private _proxy: ExtHostLanguageFeaturesShape;
30
	private _heapService: IHeapService;
A
Alex Dima 已提交
31
	private _modeService: IModeService;
32
	private _registrations: { [handle: number]: IDisposable; } = Object.create(null);
J
Joao Moreno 已提交
33
	private _formatters: Map<number, ColorFormatter>;
34

35
	constructor(
36
		extHostContext: IExtHostContext,
A
Alex Dima 已提交
37 38
		@IHeapService heapService: IHeapService,
		@IModeService modeService: IModeService,
39
	) {
40
		this._proxy = extHostContext.get(ExtHostContext.ExtHostLanguageFeatures);
41
		this._heapService = heapService;
A
Alex Dima 已提交
42
		this._modeService = modeService;
J
Joao Moreno 已提交
43
		this._formatters = new Map<number, ColorFormatter>();
44 45
	}

46 47 48 49 50 51
	dispose(): void {
		for (const key in this._registrations) {
			this._registrations[key].dispose();
		}
	}

52 53 54 55 56 57 58 59 60 61 62 63 64
	$unregister(handle: number): TPromise<any> {
		let registration = this._registrations[handle];
		if (registration) {
			registration.dispose();
			delete this._registrations[handle];
		}
		return undefined;
	}

	// --- outline

	$registerOutlineSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.DocumentSymbolProviderRegistry.register(selector, <modes.DocumentSymbolProvider>{
J
Johannes Rieken 已提交
65
			provideDocumentSymbols: (model: IReadOnlyModel, token: CancellationToken): Thenable<modes.SymbolInformation[]> => {
66 67 68 69 70 71 72 73
				return wireCancellationToken(token, this._proxy.$provideDocumentSymbols(handle, model.uri));
			}
		});
		return undefined;
	}

	// --- code lens

74 75 76
	$registerCodeLensSupport(handle: number, selector: vscode.DocumentSelector, eventHandle: number): TPromise<any> {

		const provider = <modes.CodeLensProvider>{
J
Johannes Rieken 已提交
77
			provideCodeLenses: (model: IReadOnlyModel, token: CancellationToken): modes.ICodeLensSymbol[] | Thenable<modes.ICodeLensSymbol[]> => {
78
				return this._heapService.trackRecursive(wireCancellationToken(token, this._proxy.$provideCodeLenses(handle, model.uri)));
79
			},
J
Johannes Rieken 已提交
80
			resolveCodeLens: (model: IReadOnlyModel, codeLens: modes.ICodeLensSymbol, token: CancellationToken): modes.ICodeLensSymbol | Thenable<modes.ICodeLensSymbol> => {
81
				return this._heapService.trackRecursive(wireCancellationToken(token, this._proxy.$resolveCodeLens(handle, model.uri, codeLens)));
82
			}
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		};

		if (typeof eventHandle === 'number') {
			const emitter = new Emitter<modes.CodeLensProvider>();
			this._registrations[eventHandle] = emitter;
			provider.onDidChange = emitter.event;
		}

		this._registrations[handle] = modes.CodeLensProviderRegistry.register(selector, provider);
		return undefined;
	}

	$emitCodeLensEvent(eventHandle: number, event?: any): TPromise<any> {
		const obj = this._registrations[eventHandle];
		if (obj instanceof Emitter) {
			obj.fire(event);
		}
100 101 102 103 104 105 106 107 108 109 110 111 112 113
		return undefined;
	}

	// --- declaration

	$registerDeclaractionSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.DefinitionProviderRegistry.register(selector, <modes.DefinitionProvider>{
			provideDefinition: (model, position, token): Thenable<modes.Definition> => {
				return wireCancellationToken(token, this._proxy.$provideDefinition(handle, model.uri, position));
			}
		});
		return undefined;
	}

114
	$registerImplementationSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
M
Matt Bierner 已提交
115 116 117
		this._registrations[handle] = modes.ImplementationProviderRegistry.register(selector, <modes.ImplementationProvider>{
			provideImplementation: (model, position, token): Thenable<modes.Definition> => {
				return wireCancellationToken(token, this._proxy.$provideImplementation(handle, model.uri, position));
118 119 120 121 122
			}
		});
		return undefined;
	}

123 124 125 126 127 128 129 130 131
	$registerTypeDefinitionSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.TypeDefinitionProviderRegistry.register(selector, <modes.TypeDefinitionProvider>{
			provideTypeDefinition: (model, position, token): Thenable<modes.Definition> => {
				return wireCancellationToken(token, this._proxy.$provideTypeDefinition(handle, model.uri, position));
			}
		});
		return undefined;
	}

132 133 134 135
	// --- extra info

	$registerHoverProvider(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.HoverProviderRegistry.register(selector, <modes.HoverProvider>{
J
Johannes Rieken 已提交
136
			provideHover: (model: IReadOnlyModel, position: EditorPosition, token: CancellationToken): Thenable<modes.Hover> => {
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
				return wireCancellationToken(token, this._proxy.$provideHover(handle, model.uri, position));
			}
		});
		return undefined;
	}

	// --- occurrences

	$registerDocumentHighlightProvider(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.DocumentHighlightProviderRegistry.register(selector, <modes.DocumentHighlightProvider>{
			provideDocumentHighlights: (model: IReadOnlyModel, position: EditorPosition, token: CancellationToken): Thenable<modes.DocumentHighlight[]> => {
				return wireCancellationToken(token, this._proxy.$provideDocumentHighlights(handle, model.uri, position));
			}
		});
		return undefined;
	}

	// --- references

	$registerReferenceSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.ReferenceProviderRegistry.register(selector, <modes.ReferenceProvider>{
J
Johannes Rieken 已提交
158
			provideReferences: (model: IReadOnlyModel, position: EditorPosition, context: modes.ReferenceContext, token: CancellationToken): Thenable<modes.Location[]> => {
159 160 161 162 163 164 165 166 167 168
				return wireCancellationToken(token, this._proxy.$provideReferences(handle, model.uri, position, context));
			}
		});
		return undefined;
	}

	// --- quick fix

	$registerQuickFixSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.CodeActionProviderRegistry.register(selector, <modes.CodeActionProvider>{
J
Johannes Rieken 已提交
169
			provideCodeActions: (model: IReadOnlyModel, range: EditorRange, token: CancellationToken): Thenable<modes.Command[]> => {
170
				return this._heapService.trackRecursive(wireCancellationToken(token, this._proxy.$provideCodeActions(handle, model.uri, range)));
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
			}
		});
		return undefined;
	}

	// --- formatting

	$registerDocumentFormattingSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.DocumentFormattingEditProviderRegistry.register(selector, <modes.DocumentFormattingEditProvider>{
			provideDocumentFormattingEdits: (model: IReadOnlyModel, options: modes.FormattingOptions, token: CancellationToken): Thenable<ISingleEditOperation[]> => {
				return wireCancellationToken(token, this._proxy.$provideDocumentFormattingEdits(handle, model.uri, options));
			}
		});
		return undefined;
	}

	$registerRangeFormattingSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.DocumentRangeFormattingEditProviderRegistry.register(selector, <modes.DocumentRangeFormattingEditProvider>{
			provideDocumentRangeFormattingEdits: (model: IReadOnlyModel, range: EditorRange, options: modes.FormattingOptions, token: CancellationToken): Thenable<ISingleEditOperation[]> => {
				return wireCancellationToken(token, this._proxy.$provideDocumentRangeFormattingEdits(handle, model.uri, range, options));
			}
		});
		return undefined;
	}

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

			autoFormatTriggerCharacters,

			provideOnTypeFormattingEdits: (model: IReadOnlyModel, position: EditorPosition, ch: string, options: modes.FormattingOptions, token: CancellationToken): Thenable<ISingleEditOperation[]> => {
				return wireCancellationToken(token, this._proxy.$provideOnTypeFormattingEdits(handle, model.uri, position, ch, options));
			}
		});
		return undefined;
	}

	// --- navigate type

	$registerNavigateTypeSupport(handle: number): TPromise<any> {
211
		this._registrations[handle] = WorkspaceSymbolProviderRegistry.register(<IWorkspaceSymbolProvider>{
212
			provideWorkspaceSymbols: (search: string): TPromise<modes.SymbolInformation[]> => {
213
				return this._heapService.trackRecursive(this._proxy.$provideWorkspaceSymbols(handle, search));
214
			},
215
			resolveWorkspaceSymbol: (item: modes.SymbolInformation): TPromise<modes.SymbolInformation> => {
216
				return this._proxy.$resolveWorkspaceSymbol(handle, item);
217 218 219 220 221 222 223 224 225
			}
		});
		return undefined;
	}

	// --- rename

	$registerRenameSupport(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.RenameProviderRegistry.register(selector, <modes.RenameProvider>{
J
Johannes Rieken 已提交
226
			provideRenameEdits: (model: IReadOnlyModel, position: EditorPosition, newName: string, token: CancellationToken): Thenable<modes.WorkspaceEdit> => {
227 228 229 230 231 232 233 234 235 236
				return wireCancellationToken(token, this._proxy.$provideRenameEdits(handle, model.uri, position, newName));
			}
		});
		return undefined;
	}

	// --- suggest

	$registerSuggestSupport(handle: number, selector: vscode.DocumentSelector, triggerCharacters: string[]): TPromise<any> {
		this._registrations[handle] = modes.SuggestRegistry.register(selector, <modes.ISuggestSupport>{
237
			triggerCharacters,
J
Johannes Rieken 已提交
238
			provideCompletionItems: (model: IReadOnlyModel, position: EditorPosition, token: CancellationToken): Thenable<modes.ISuggestResult> => {
239
				return this._heapService.trackRecursive(wireCancellationToken(token, this._proxy.$provideCompletionItems(handle, model.uri, position)));
240
			},
J
Johannes Rieken 已提交
241
			resolveCompletionItem: (model: IReadOnlyModel, position: EditorPosition, suggestion: modes.ISuggestion, token: CancellationToken): Thenable<modes.ISuggestion> => {
242 243 244 245 246 247 248 249 250 251 252 253 254
				return wireCancellationToken(token, this._proxy.$resolveCompletionItem(handle, model.uri, position, suggestion));
			}
		});
		return undefined;
	}

	// --- parameter hints

	$registerSignatureHelpProvider(handle: number, selector: vscode.DocumentSelector, triggerCharacter: string[]): TPromise<any> {
		this._registrations[handle] = modes.SignatureHelpProviderRegistry.register(selector, <modes.SignatureHelpProvider>{

			signatureHelpTriggerCharacters: triggerCharacter,

J
Johannes Rieken 已提交
255
			provideSignatureHelp: (model: IReadOnlyModel, position: EditorPosition, token: CancellationToken): Thenable<modes.SignatureHelp> => {
256 257 258 259 260 261 262
				return wireCancellationToken(token, this._proxy.$provideSignatureHelp(handle, model.uri, position));
			}

		});
		return undefined;
	}

J
Johannes Rieken 已提交
263 264 265 266 267
	// --- links

	$registerDocumentLinkProvider(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
		this._registrations[handle] = modes.LinkProviderRegistry.register(selector, <modes.LinkProvider>{
			provideLinks: (model, token) => {
J
Johannes Rieken 已提交
268
				return this._heapService.trackRecursive(wireCancellationToken(token, this._proxy.$provideDocumentLinks(handle, model.uri)));
269 270 271
			},
			resolveLink: (link, token) => {
				return wireCancellationToken(token, this._proxy.$resolveDocumentLink(handle, link));
J
Johannes Rieken 已提交
272 273 274 275 276
			}
		});
		return undefined;
	}

277 278 279
	// --- colors

	$registerDocumentColorProvider(handle: number, selector: vscode.DocumentSelector): TPromise<any> {
280
		const proxy = this._proxy;
281

J
Joao Moreno 已提交
282
		this._registrations[handle] = modes.ColorProviderRegistry.register(selector, <modes.ColorRangeProvider>{
J
Joao Moreno 已提交
283
			provideColorRanges: (model, token) => {
284
				return wireCancellationToken(token, proxy.$provideDocumentColors(handle, model.uri))
J
Joao Moreno 已提交
285 286
					.then(documentColors => {
						return documentColors.map(documentColor => {
J
Joao Moreno 已提交
287 288 289 290 291 292 293
							const formatters = documentColor.availableFormats.map(f => {
								if (typeof f === 'number') {
									return this._formatters.get(f);
								} else {
									return new CombinedColorFormatter(this._formatters.get(f[0]), this._formatters.get(f[1]));
								}
							});
294

J
Joao Moreno 已提交
295
							const [red, green, blue, alpha] = documentColor.color;
J
Joao Moreno 已提交
296 297 298 299 300 301 302
							const color = {
								red: red / 255.0,
								green: green / 255.0,
								blue: blue / 255.0,
								alpha
							};

303
							return {
J
Joao Moreno 已提交
304
								color,
J
Joao Moreno 已提交
305 306
								formatters,
								range: documentColor.range
307 308 309
							};
						});
					});
310 311
			}
		});
J
Joao Moreno 已提交
312 313

		return TPromise.as(null);
314 315
	}

J
Joao Moreno 已提交
316 317
	$registerColorFormats(formats: IRawColorFormatMap): TPromise<any> {
		formats.forEach(f => this._formatters.set(f[0], new ColorFormatter(f[1])));
J
Joao Moreno 已提交
318
		return TPromise.as(null);
319 320
	}

321 322
	// --- configuration

A
Alex Dima 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	$setLanguageConfiguration(handle: number, languageId: string, _configuration: vscode.LanguageConfiguration): TPromise<any> {

		let configuration: LanguageConfiguration = {
			comments: _configuration.comments,
			brackets: _configuration.brackets,
			wordPattern: _configuration.wordPattern,
			indentationRules: _configuration.indentationRules,
			onEnterRules: _configuration.onEnterRules,

			autoClosingPairs: null,
			surroundingPairs: null,
			__electricCharacterSupport: null
		};

		if (_configuration.__characterPairSupport) {
			// backwards compatibility
			configuration.autoClosingPairs = _configuration.__characterPairSupport.autoClosingPairs;
		}
341

A
Alex Dima 已提交
342 343 344 345 346 347 348
		if (_configuration.__electricCharacterSupport && _configuration.__electricCharacterSupport.docComment) {
			configuration.__electricCharacterSupport = {
				docComment: {
					open: _configuration.__electricCharacterSupport.docComment.open,
					close: _configuration.__electricCharacterSupport.docComment.close
				}
			};
349 350
		}

A
Alex Dima 已提交
351 352 353 354 355
		let languageIdentifier = this._modeService.getLanguageIdentifier(languageId);
		if (languageIdentifier) {
			this._registrations[handle] = LanguageConfigurationRegistry.register(languageIdentifier, configuration);
		}

356 357 358 359
		return undefined;
	}

}