extHost.api.impl.ts 24.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import {MainInplaceReplaceSupport, ReplaceSupport, IBracketElectricCharacterContribution} from 'vs/editor/common/modes/supports';
import {score} from 'vs/editor/common/modes/languageSelector';
import {Remotable, IThreadService} from 'vs/platform/thread/common/thread';
import * as errors from 'vs/base/common/errors';
11 12 13 14 15 16 17 18
import {ExtHostFileSystemEventService} from 'vs/workbench/api/common/extHostFileSystemEventService';
import {ExtHostModelService, setWordDefinitionFor} from 'vs/workbench/api/common/extHostDocuments';
import {ExtHostConfiguration} from 'vs/workbench/api/common/extHostConfiguration';
import {ExtHostDiagnostics} from 'vs/workbench/api/common/extHostDiagnostics';
import {ExtHostWorkspace} from 'vs/workbench/api/common/extHostWorkspace';
import {ExtHostQuickOpen} from 'vs/workbench/api/browser/extHostQuickOpen';
import {ExtHostStatusBar} from 'vs/workbench/api/browser/extHostStatusBar';
import {ExtHostCommands} from 'vs/workbench/api/common/extHostCommands';
19
import {ExtHostOutputService} from 'vs/workbench/api/common/extHostOutputService';
20 21
import {ExtHostMessageService} from 'vs/workbench/api/common/extHostMessageService';
import {ExtHostEditors} from 'vs/workbench/api/common/extHostEditors';
E
Erich Gamma 已提交
22
import {ExtHostLanguages} from 'vs/workbench/api/common/extHostLanguages';
J
Johannes Rieken 已提交
23
import {ExtHostLanguageFeatures} from 'vs/workbench/api/common/extHostLanguageFeatures';
J
Johannes Rieken 已提交
24
import {ExtHostApiCommands} from 'vs/workbench/api/common/extHostApiCommands';
25
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
J
Johannes Rieken 已提交
26
import 'vs/workbench/api/common/extHostTypes.marshalling';
27
import * as TypeConverters from 'vs/workbench/api/common/extHostTypeConverters';
E
Erich Gamma 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
import {wrapAsWinJSPromise} from 'vs/base/common/async';
import Modes = require('vs/editor/common/modes');
import {IModelService} from 'vs/editor/common/services/modelService';
import {IModeService} from 'vs/editor/common/services/modeService';
import {IDeclarationContribution, ISuggestContribution, IReferenceContribution, ICommentsSupportContribution, ITokenTypeClassificationSupportContribution} from 'vs/editor/common/modes/supports';
import {IOnEnterSupportOptions} from 'vs/editor/common/modes/supports/onEnter';
import URI from 'vs/base/common/uri';
import Severity from 'vs/base/common/severity';
import {IDisposable} from 'vs/base/common/lifecycle';
import EditorCommon = require('vs/editor/common/editorCommon');
import {IPluginService, IPluginDescription} from 'vs/platform/plugins/common/plugins';
import {PluginsRegistry} from 'vs/platform/plugins/common/pluginsRegistry';
import {relative} from 'path';
import {TPromise} from 'vs/base/common/winjs.base';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import {CancellationTokenSource} from 'vs/base/common/cancellation';
import vscode = require('vscode');
import {TextEditorRevealType} from 'vs/workbench/api/common/mainThreadEditors';
import * as paths from 'vs/base/common/paths';

/**
 * This class implements the API described in vscode.d.ts,
 * for the case of the extensionHost host process
 */
52
export class ExtHostAPIImplementation {
E
Erich Gamma 已提交
53 54 55

	private static _LAST_REGISTER_TOKEN = 0;
	private static generateDisposeToken(): string {
56
		return String(++ExtHostAPIImplementation._LAST_REGISTER_TOKEN);
E
Erich Gamma 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	}

	private _threadService: IThreadService;
	private _proxy: MainProcessVSCodeAPIHelper;
	private _pluginService: IPluginService;

	version: typeof vscode.version;
	Uri: typeof vscode.Uri;
	Location: typeof vscode.Location;
	Diagnostic: typeof vscode.Diagnostic;
	DiagnosticSeverity: typeof vscode.DiagnosticSeverity;
	Disposable: typeof vscode.Disposable;
	TextEdit: typeof vscode.TextEdit;
	WorkspaceEdit: typeof vscode.WorkspaceEdit;
	ViewColumn: typeof vscode.ViewColumn;
	StatusBarAlignment: typeof vscode.StatusBarAlignment;
	Position: typeof vscode.Position;
	Range: typeof vscode.Range;
	Selection: typeof vscode.Selection;
	CancellationTokenSource: typeof vscode.CancellationTokenSource;
	Hover: typeof vscode.Hover;
	DocumentHighlightKind: typeof vscode.DocumentHighlightKind;
	DocumentHighlight: typeof vscode.DocumentHighlight;
	SymbolKind: typeof vscode.SymbolKind;
	SymbolInformation: typeof vscode.SymbolInformation;
	CodeLens: typeof vscode.CodeLens;
	ParameterInformation: typeof vscode.ParameterInformation;
	SignatureInformation: typeof vscode.SignatureInformation;
	SignatureHelp: typeof vscode.SignatureHelp;
	CompletionItem: typeof vscode.CompletionItem;
	CompletionItemKind: typeof vscode.CompletionItemKind;
	IndentAction: typeof vscode.IndentAction;
	OverviewRulerLane: typeof vscode.OverviewRulerLane;
	TextEditorRevealType: typeof vscode.TextEditorRevealType;
	commands: typeof vscode.commands;
	window: typeof vscode.window;
	workspace: typeof vscode.workspace;
	languages: typeof vscode.languages;
	extensions: typeof vscode.extensions;

	constructor(
		@IThreadService threadService: IThreadService,
		@IPluginService pluginService: IPluginService,
		@IWorkspaceContextService contextService: IWorkspaceContextService
	) {
		this._pluginService = pluginService;
		this._threadService = threadService;
		this._proxy = threadService.getRemotable(MainProcessVSCodeAPIHelper);

		this.version = contextService.getConfiguration().env.version;
		this.Uri = URI;
		this.Location = extHostTypes.Location;
		this.Diagnostic = <any> extHostTypes.Diagnostic;
		this.DiagnosticSeverity = <any> extHostTypes.DiagnosticSeverity;
		this.Disposable = extHostTypes.Disposable;
		this.TextEdit = extHostTypes.TextEdit;
		this.WorkspaceEdit = extHostTypes.WorkspaceEdit;
		this.Position = extHostTypes.Position;
		this.Range = extHostTypes.Range;
		this.Selection = extHostTypes.Selection;
		this.CancellationTokenSource = CancellationTokenSource;
		this.Hover = extHostTypes.Hover;
		this.SymbolKind = <any>extHostTypes.SymbolKind;
		this.SymbolInformation = <any>extHostTypes.SymbolInformation;
		this.DocumentHighlightKind = <any>extHostTypes.DocumentHighlightKind;
		this.DocumentHighlight = <any>extHostTypes.DocumentHighlight;
		this.CodeLens = extHostTypes.CodeLens;
		this.ParameterInformation = extHostTypes.ParameterInformation;
		this.SignatureInformation = extHostTypes.SignatureInformation;
		this.SignatureHelp = extHostTypes.SignatureHelp;
		this.CompletionItem = <any>extHostTypes.CompletionItem;
		this.CompletionItemKind = <any>extHostTypes.CompletionItemKind;
		this.ViewColumn = <any>extHostTypes.ViewColumn;
		this.StatusBarAlignment = <any>extHostTypes.StatusBarAlignment;
		this.IndentAction = <any>Modes.IndentAction;
		this.OverviewRulerLane = <any>EditorCommon.OverviewRulerLane;
		this.TextEditorRevealType = <any>TextEditorRevealType;

		errors.setUnexpectedErrorHandler((err) => {
			this._proxy.onUnexpectedPluginHostError(errors.transformErrorForSerialization(err));
		});

139
		const pluginHostCommands = this._threadService.getRemotable(ExtHostCommands);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
		this.commands = {
			registerCommand<T>(id: string, command: <T>(...args: any[]) => T | Thenable<T>, thisArgs?: any): vscode.Disposable {
				return pluginHostCommands.registerCommand(id, command, thisArgs);
			},
			registerTextEditorCommand(commandId: string, callback: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => void, thisArg?: any): vscode.Disposable {
				return pluginHostCommands.registerTextEditorCommand(commandId, callback, thisArg);
			},
			executeCommand<T>(id: string, ...args: any[]): Thenable<T> {
				return pluginHostCommands.executeCommand(id, args);
			},
			getCommands(filterInternal: boolean = false):Thenable<string[]> {
				return pluginHostCommands.getCommands(filterInternal);
			}
		};

155 156 157 158
		const pluginHostEditors = this._threadService.getRemotable(ExtHostEditors);
		const pluginHostMessageService = new ExtHostMessageService(this._threadService, this.commands);
		const pluginHostQuickOpen = new ExtHostQuickOpen(this._threadService);
		const pluginHostStatusBar = new ExtHostStatusBar(this._threadService);
E
Erich Gamma 已提交
159 160 161 162 163 164 165 166 167 168 169 170 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
		const extHostOutputService = new ExtHostOutputService(this._threadService);
		this.window = {
			get activeTextEditor() {
				return pluginHostEditors.getActiveTextEditor();
			},
			get visibleTextEditors() {
				return pluginHostEditors.getVisibleTextEditors();
			},
			showTextDocument(document: vscode.TextDocument, column: vscode.ViewColumn): TPromise<vscode.TextEditor> {
				return pluginHostEditors.showTextDocument(document, column);
			},
			createTextEditorDecorationType(options:vscode.DecorationRenderOptions): vscode.TextEditorDecorationType {
				return pluginHostEditors.createTextEditorDecorationType(options);
			},
			onDidChangeActiveTextEditor: pluginHostEditors.onDidChangeActiveTextEditor.bind(pluginHostEditors),
			onDidChangeTextEditorSelection: (listener: (e: vscode.TextEditorSelectionChangeEvent) => any, thisArgs?: any, disposables?: extHostTypes.Disposable[]) => {
				return pluginHostEditors.onDidChangeTextEditorSelection(listener, thisArgs, disposables);
			},
			onDidChangeTextEditorOptions: (listener: (e: vscode.TextEditorOptionsChangeEvent) => any, thisArgs?: any, disposables?: extHostTypes.Disposable[]) => {
				return pluginHostEditors.onDidChangeTextEditorOptions(listener, thisArgs, disposables);
			},
			showInformationMessage: (message, ...items) => {
				return pluginHostMessageService.showMessage(Severity.Info, message, items);
			},
			showWarningMessage: (message, ...items) => {
				return pluginHostMessageService.showMessage(Severity.Warning, message, items);
			},
			showErrorMessage: (message, ...items) => {
				return pluginHostMessageService.showMessage(Severity.Error, message, items);
			},
			showQuickPick: (items: any, options: vscode.QuickPickOptions) => {
				return pluginHostQuickOpen.show(items, options);
			},
			showInputBox: pluginHostQuickOpen.input.bind(pluginHostQuickOpen),

			createStatusBarItem(position?: vscode.StatusBarAlignment, priority?: number): vscode.StatusBarItem {
				return pluginHostStatusBar.createStatusBarEntry(<number>position, priority);
			},
			setStatusBarMessage(text: string, timeoutOrThenable?: number | Thenable<any>): vscode.Disposable {
				return pluginHostStatusBar.setStatusBarMessage(text, timeoutOrThenable);
			},
			createOutputChannel(name: string): vscode.OutputChannel {
				return extHostOutputService.createOutputChannel(name);
			}
		};

		//
206
		const workspacePath = contextService.getWorkspace() ? contextService.getWorkspace().resource.fsPath : undefined;
207 208 209
		const pluginHostFileSystemEvent = threadService.getRemotable(ExtHostFileSystemEventService);
		const pluginHostWorkspace = new ExtHostWorkspace(this._threadService, workspacePath);
		const pluginHostDocuments = this._threadService.getRemotable(ExtHostModelService);
E
Erich Gamma 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
		this.workspace = Object.freeze({
			get rootPath() {
				return pluginHostWorkspace.getPath();
			},
			set rootPath(value) {
				throw errors.readonly();
			},
			asRelativePath: (pathOrUri) => {
				return pluginHostWorkspace.getRelativePath(pathOrUri);
			},
			findFiles: (include, exclude, maxResults?) => {
				return pluginHostWorkspace.findFiles(include, exclude, maxResults);
			},
			saveAll: (includeUntitled?) => {
				return pluginHostWorkspace.saveAll(includeUntitled);
			},
			applyEdit(edit: vscode.WorkspaceEdit): TPromise<boolean> {
				return pluginHostWorkspace.appyEdit(edit);
			},
			createFileSystemWatcher: (pattern, ignoreCreate, ignoreChange, ignoreDelete): vscode.FileSystemWatcher => {
				return pluginHostFileSystemEvent.createFileSystemWatcher(pattern, ignoreCreate, ignoreChange, ignoreDelete);
			},
			get textDocuments() {
				return pluginHostDocuments.getDocuments();
			},
			set textDocuments(value) {
				throw errors.readonly();
			},
			// createTextDocument(text: string, fileName?: string, language?: string): Thenable<vscode.TextDocument> {
			// 	return pluginHostDocuments.createDocument(text, fileName, language);
			// },
			openTextDocument(uriOrFileName:vscode.Uri | string) {
				return pluginHostDocuments.openDocument(uriOrFileName);
			},
			onDidOpenTextDocument: (listener, thisArgs?, disposables?) => {
				return pluginHostDocuments.onDidAddDocument(listener, thisArgs, disposables);
			},
			onDidCloseTextDocument: (listener, thisArgs?, disposables?) => {
				return pluginHostDocuments.onDidRemoveDocument(listener, thisArgs, disposables);
			},
			onDidChangeTextDocument: (listener, thisArgs?, disposables?) => {
				return pluginHostDocuments.onDidChangeDocument(listener, thisArgs, disposables);
			},
			onDidSaveTextDocument: (listener, thisArgs?, disposables?) => {
				return pluginHostDocuments.onDidSaveDocument(listener, thisArgs, disposables);
			},
			onDidChangeConfiguration: (listener: () => any, thisArgs?: any, disposables?: extHostTypes.Disposable[]) => {
				return pluginHostConfiguration.onDidChangeConfiguration(listener, thisArgs, disposables);
			},
			getConfiguration: (section?: string):vscode.WorkspaceConfiguration => {
				return pluginHostConfiguration.getConfiguration(section);
			}
		});

		//
		const languages = new ExtHostLanguages(this._threadService);
266
		const pluginHostDiagnostics = new ExtHostDiagnostics(this._threadService);
J
Johannes Rieken 已提交
267
		const languageFeatures = threadService.getRemotable(ExtHostLanguageFeatures);
268
		const languageFeatureCommand = new ExtHostApiCommands(threadService.getRemotable(ExtHostCommands));
E
Erich Gamma 已提交
269 270 271 272 273 274 275 276 277

		this.languages = {
			createDiagnosticCollection(name?: string): vscode.DiagnosticCollection {
				return pluginHostDiagnostics.createDiagnosticCollection(name);
			},
			getLanguages(): TPromise<string[]> {
				return languages.getLanguages();
			},
			match(selector: vscode.DocumentSelector, document: vscode.TextDocument): number {
278
				return score(selector, <any> document.uri, document.languageId);
E
Erich Gamma 已提交
279 280
			},
			registerCodeActionsProvider(selector: vscode.DocumentSelector, provider: vscode.CodeActionProvider): vscode.Disposable {
J
Johannes Rieken 已提交
281
				return languageFeatures.registerCodeActionProvider(selector, provider);
E
Erich Gamma 已提交
282 283
			},
			registerCodeLensProvider(selector: vscode.DocumentSelector, provider: vscode.CodeLensProvider): vscode.Disposable {
284
				return languageFeatures.registerCodeLensProvider(selector, provider);
E
Erich Gamma 已提交
285 286
			},
			registerDefinitionProvider(selector: vscode.DocumentSelector, provider: vscode.DefinitionProvider): vscode.Disposable {
J
Johannes Rieken 已提交
287
				return languageFeatures.registerDefinitionProvider(selector, provider);
E
Erich Gamma 已提交
288 289
			},
			registerHoverProvider(selector: vscode.DocumentSelector, provider: vscode.HoverProvider): vscode.Disposable {
J
Johannes Rieken 已提交
290
				return languageFeatures.registerHoverProvider(selector, provider);
E
Erich Gamma 已提交
291 292
			},
			registerDocumentHighlightProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentHighlightProvider): vscode.Disposable {
293
				return languageFeatures.registerDocumentHighlightProvider(selector, provider);
E
Erich Gamma 已提交
294 295
			},
			registerReferenceProvider(selector: vscode.DocumentSelector, provider: vscode.ReferenceProvider): vscode.Disposable {
J
Johannes Rieken 已提交
296
				return languageFeatures.registerReferenceProvider(selector, provider);
E
Erich Gamma 已提交
297 298
			},
			registerRenameProvider(selector: vscode.DocumentSelector, provider: vscode.RenameProvider): vscode.Disposable {
J
Johannes Rieken 已提交
299
				return languageFeatures.registerRenameProvider(selector, provider);
E
Erich Gamma 已提交
300 301
			},
			registerDocumentSymbolProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentSymbolProvider): vscode.Disposable {
J
Johannes Rieken 已提交
302
				return languageFeatures.registerDocumentSymbolProvider(selector, provider);
E
Erich Gamma 已提交
303 304
			},
			registerWorkspaceSymbolProvider(provider: vscode.WorkspaceSymbolProvider): vscode.Disposable {
305
				return languageFeatures.registerWorkspaceSymbolProvider(provider);
E
Erich Gamma 已提交
306 307
			},
			registerDocumentFormattingEditProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentFormattingEditProvider): vscode.Disposable {
308
				return languageFeatures.registerDocumentFormattingEditProvider(selector, provider);
E
Erich Gamma 已提交
309 310
			},
			registerDocumentRangeFormattingEditProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentRangeFormattingEditProvider): vscode.Disposable {
311
				return languageFeatures.registerDocumentRangeFormattingEditProvider(selector, provider);
E
Erich Gamma 已提交
312 313
			},
			registerOnTypeFormattingEditProvider(selector: vscode.DocumentSelector, provider: vscode.OnTypeFormattingEditProvider, firstTriggerCharacter: string, ...moreTriggerCharacters: string[]): vscode.Disposable {
314
				return languageFeatures.registerOnTypeFormattingEditProvider(selector, provider, [firstTriggerCharacter].concat(moreTriggerCharacters));
E
Erich Gamma 已提交
315 316
			},
			registerSignatureHelpProvider(selector: vscode.DocumentSelector, provider: vscode.SignatureHelpProvider, ...triggerCharacters: string[]): vscode.Disposable {
317
				return languageFeatures.registerSignatureHelpProvider(selector, provider, triggerCharacters);
E
Erich Gamma 已提交
318 319
			},
			registerCompletionItemProvider(selector: vscode.DocumentSelector, provider: vscode.CompletionItemProvider, ...triggerCharacters: string[]): vscode.Disposable {
320
				return languageFeatures.registerCompletionItemProvider(selector, provider, triggerCharacters);
E
Erich Gamma 已提交
321 322 323 324 325 326
			},
			setLanguageConfiguration: (language: string, configuration: vscode.LanguageConfiguration):vscode.Disposable => {
				return this._setLanguageConfiguration(language, configuration);
			}
		};

327
		var pluginHostConfiguration = threadService.getRemotable(ExtHostConfiguration);
E
Erich Gamma 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

		//
		this.extensions = {
			getExtension(extensionId: string):Extension<any> {
				let desc = PluginsRegistry.getPluginDescription(extensionId);
				if (desc) {
					return new Extension(pluginService, desc);
				}
			},
			get all():Extension<any>[] {
				return PluginsRegistry.getAllPluginDescriptions().map((desc) => new Extension(pluginService, desc));
			}
		}

		// Intentionally calling a function for typechecking purposes
		defineAPI(this);
	}

	private _disposableFromToken(disposeToken:string): IDisposable {
		return new extHostTypes.Disposable(() => this._proxy.disposeByToken(disposeToken));
	}

	private _setLanguageConfiguration(modeId: string, configuration: vscode.LanguageConfiguration): vscode.Disposable {

		let disposables: IDisposable[] = [];
		let {comments, wordPattern} = configuration;

		// comment configuration
		if (comments) {
			let lineCommentToken = comments.lineComment;

			let contrib: ICommentsSupportContribution = { commentsConfiguration: {} };
			if (comments.lineComment) {
				contrib.commentsConfiguration.lineCommentTokens = [comments.lineComment];
			}
			if (comments.blockComment) {
				let [blockStart, blockEnd] = comments.blockComment;
				contrib.commentsConfiguration.blockCommentStartToken = blockStart;
				contrib.commentsConfiguration.blockCommentEndToken = blockEnd;
			}
			let d = this.Modes_CommentsSupport_register(modeId, contrib);
			disposables.push(d);
		}

		// word definition
		if (wordPattern) {
			setWordDefinitionFor(modeId, wordPattern);
			let d = this.Modes_TokenTypeClassificationSupport_register(modeId, {
				wordDefinition: wordPattern
			});
			disposables.push(d);

		} else {
			setWordDefinitionFor(modeId, null);
		}

		// on enter
		let onEnter: IOnEnterSupportOptions = {};
		let empty = true;
		let {brackets, indentationRules, onEnterRules} = configuration;

		if (brackets) {
			empty = false;
			onEnter.brackets = brackets.map(pair => {
				let [open, close] = pair;
				return { open, close };
			});
		}
		if (indentationRules) {
			empty = false;
			onEnter.indentationRules = indentationRules;
		}
		if (onEnterRules) {
			empty = false;
			onEnter.regExpRules = <any>onEnterRules;
		}

		if (!empty) {
			let d = this.Modes_OnEnterSupport_register(modeId, onEnter);
			disposables.push(d);
		}

		if (configuration.__electricCharacterSupport) {
			disposables.push(
				this.Modes_ElectricCharacterSupport_register(modeId, configuration.__electricCharacterSupport)
			);
		}

		if (configuration.__characterPairSupport) {
			disposables.push(
				this.Modes_CharacterPairSupport_register(modeId, configuration.__characterPairSupport)
			);
		}

		return extHostTypes.Disposable.from(...disposables);
	}

	private Modes_CommentsSupport_register(modeId: string, commentsSupport: ICommentsSupportContribution): IDisposable {
426
		let disposeToken = ExtHostAPIImplementation.generateDisposeToken();
E
Erich Gamma 已提交
427 428 429 430 431
		this._proxy.Modes_CommentsSupport_register(disposeToken, modeId, commentsSupport);
		return this._disposableFromToken(disposeToken);
	}

	private Modes_TokenTypeClassificationSupport_register(modeId: string, tokenTypeClassificationSupport:ITokenTypeClassificationSupportContribution): IDisposable {
432
		let disposeToken = ExtHostAPIImplementation.generateDisposeToken();
E
Erich Gamma 已提交
433 434 435 436 437
		this._proxy.Modes_TokenTypeClassificationSupport_register(disposeToken, modeId, tokenTypeClassificationSupport);
		return this._disposableFromToken(disposeToken);
	}

	private Modes_ElectricCharacterSupport_register(modeId: string, electricCharacterSupport:IBracketElectricCharacterContribution): IDisposable {
438
		let disposeToken = ExtHostAPIImplementation.generateDisposeToken();
E
Erich Gamma 已提交
439 440 441 442 443
		this._proxy.Modes_ElectricCharacterSupport_register(disposeToken, modeId, electricCharacterSupport);
		return this._disposableFromToken(disposeToken);
	}

	private Modes_CharacterPairSupport_register(modeId: string, characterPairSupport:Modes.ICharacterPairContribution): IDisposable {
444
		let disposeToken = ExtHostAPIImplementation.generateDisposeToken();
E
Erich Gamma 已提交
445 446 447 448 449
		this._proxy.Modes_CharacterPairSupport_register(disposeToken, modeId, characterPairSupport);
		return this._disposableFromToken(disposeToken);
	}

	private Modes_OnEnterSupport_register(modeId: string, opts: IOnEnterSupportOptions): IDisposable {
450
		let disposeToken = ExtHostAPIImplementation.generateDisposeToken();
E
Erich Gamma 已提交
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 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
		this._proxy.Modes_OnEnterSupport_register(disposeToken, modeId, opts);
		return this._disposableFromToken(disposeToken);
	}
}

class Extension<T> implements vscode.Extension<T> {

	private _pluginService: IPluginService;

	public id: string;
	public extensionPath: string;
	public packageJSON: any;

	constructor(pluginService:IPluginService, description:IPluginDescription) {
		this._pluginService = pluginService;
		this.id = description.id;
		this.extensionPath = paths.normalize(description.extensionFolderPath, true);
		this.packageJSON = description;
	}

	get isActive(): boolean {
		return this._pluginService.isActivated(this.id);
	}

	get exports(): T {
		return this._pluginService.get(this.id);
	}

	activate(): Thenable<T> {
		return this._pluginService.activateAndGet<T>(this.id);
	}
}

function defineAPI(impl: typeof vscode) {
	var node_module = <any>require.__$__nodeRequire('module');
	var original = node_module._load;
	node_module._load = function load(request, parent, isMain) {
		if (request === 'vscode') {
			return impl;
		}
		return original.apply(this, arguments);
	};
	define('vscode', [], impl);
}

@Remotable.MainContext('MainProcessVSCodeAPIHelper')
export class MainProcessVSCodeAPIHelper {
	protected _modeService: IModeService;
	private _token2Dispose: {
		[token:string]: IDisposable;
	};

	constructor(
		@IModeService modeService: IModeService
	) {
		this._modeService = modeService;
		this._token2Dispose = {};
	}

	public onUnexpectedPluginHostError(err: any): void {
		errors.onUnexpectedError(err);
	}

	public disposeByToken(disposeToken:string): void {
		if (this._token2Dispose[disposeToken]) {
			this._token2Dispose[disposeToken].dispose();
			delete this._token2Dispose[disposeToken];
		}
	}

	public Modes_CommentsSupport_register(disposeToken:string, modeId: string, commentsSupport: ICommentsSupportContribution): void {
		this._token2Dispose[disposeToken] = this._modeService.registerDeclarativeCommentsSupport(modeId, commentsSupport);
	}

	public Modes_TokenTypeClassificationSupport_register(disposeToken:string, modeId: string, tokenTypeClassificationSupport:ITokenTypeClassificationSupportContribution): void {
		this._token2Dispose[disposeToken] = this._modeService.registerDeclarativeTokenTypeClassificationSupport(modeId, tokenTypeClassificationSupport);
	}

	public Modes_ElectricCharacterSupport_register(disposeToken:string, modeId: string, electricCharacterSupport:IBracketElectricCharacterContribution): void {
		this._token2Dispose[disposeToken] = this._modeService.registerDeclarativeElectricCharacterSupport(modeId, electricCharacterSupport);
	}

	public Modes_CharacterPairSupport_register(disposeToken:string, modeId: string, characterPairSupport:Modes.ICharacterPairContribution): void {
		this._token2Dispose[disposeToken] = this._modeService.registerDeclarativeCharacterPairSupport(modeId, characterPairSupport);
	}

	public Modes_OnEnterSupport_register(disposeToken:string, modeId: string, opts:IOnEnterSupportOptions): void {
		this._token2Dispose[disposeToken] = this._modeService.registerDeclarativeOnEnterSupport(modeId, <any>opts);
	}
}