extHost.api.impl.ts 21.6 KB
Newer Older
E
Erich Gamma 已提交
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';

7
import {Emitter} from 'vs/base/common/event';
E
Erich Gamma 已提交
8
import {score} from 'vs/editor/common/modes/languageSelector';
9
import * as Platform from 'vs/base/common/platform';
10
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
E
Erich Gamma 已提交
11
import * as errors from 'vs/base/common/errors';
12
import product from 'vs/platform/product';
B
Benjamin Pasero 已提交
13
import pkg from 'vs/platform/package';
J
Johannes Rieken 已提交
14
import {ExtHostFileSystemEventService} from 'vs/workbench/api/node/extHostFileSystemEventService';
15
import {ExtHostDocuments} from 'vs/workbench/api/node/extHostDocuments';
J
Johannes Rieken 已提交
16 17 18 19
import {ExtHostConfiguration} from 'vs/workbench/api/node/extHostConfiguration';
import {ExtHostDiagnostics} from 'vs/workbench/api/node/extHostDiagnostics';
import {ExtHostWorkspace} from 'vs/workbench/api/node/extHostWorkspace';
import {ExtHostQuickOpen} from 'vs/workbench/api/node/extHostQuickOpen';
J
Johannes Rieken 已提交
20
import {ExtHostHeapService} from 'vs/workbench/api/node/extHostHeapService';
J
Johannes Rieken 已提交
21 22 23
import {ExtHostStatusBar} from 'vs/workbench/api/node/extHostStatusBar';
import {ExtHostCommands} from 'vs/workbench/api/node/extHostCommands';
import {ExtHostOutputService} from 'vs/workbench/api/node/extHostOutputService';
D
Daniel Imms 已提交
24
import {ExtHostTerminalService} from 'vs/workbench/api/node/extHostTerminalService';
J
Johannes Rieken 已提交
25 26 27 28
import {ExtHostMessageService} from 'vs/workbench/api/node/extHostMessageService';
import {ExtHostEditors} from 'vs/workbench/api/node/extHostEditors';
import {ExtHostLanguages} from 'vs/workbench/api/node/extHostLanguages';
import {ExtHostLanguageFeatures} from 'vs/workbench/api/node/extHostLanguageFeatures';
29
import * as ExtHostTypeConverters from 'vs/workbench/api/node/extHostTypeConverters';
J
Johannes Rieken 已提交
30 31
import {registerApiCommands} from 'vs/workbench/api/node/extHostApiCommands';
import * as extHostTypes from 'vs/workbench/api/node/extHostTypes';
E
Erich Gamma 已提交
32 33 34 35
import Modes = require('vs/editor/common/modes');
import URI from 'vs/base/common/uri';
import Severity from 'vs/base/common/severity';
import EditorCommon = require('vs/editor/common/editorCommon');
36
import {IExtensionDescription} from 'vs/platform/extensions/common/extensions';
37
import {ExtHostExtensionService} from 'vs/workbench/api/node/extHostExtensionService';
38
import {ExtensionsRegistry} from 'vs/platform/extensions/common/extensionsRegistry';
E
Erich Gamma 已提交
39 40 41 42 43
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 * as paths from 'vs/base/common/paths';
J
Johannes Rieken 已提交
44
import {ITelemetryService, ITelemetryInfo} from 'vs/platform/telemetry/common/telemetry';
45
import {MainContext, ExtHostContext, InstanceCollection} from './extHost.protocol';
E
Erich Gamma 已提交
46 47 48 49 50

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

	version: typeof vscode.version;
54
	env: typeof vscode.env;
E
Erich Gamma 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67
	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;
68
	EventEmitter: typeof vscode.EventEmitter;
E
Erich Gamma 已提交
69 70 71 72 73 74 75 76 77 78 79
	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;
80
	CompletionList: typeof vscode.CompletionList;
J
Johannes Rieken 已提交
81
	DocumentLink: typeof vscode.DocumentLink;
E
Erich Gamma 已提交
82 83 84
	IndentAction: typeof vscode.IndentAction;
	OverviewRulerLane: typeof vscode.OverviewRulerLane;
	TextEditorRevealType: typeof vscode.TextEditorRevealType;
85
	EndOfLine: typeof vscode.EndOfLine;
A
Alex Dima 已提交
86
	TextEditorCursorStyle: typeof vscode.TextEditorCursorStyle;
87
	TextEditorSelectionChangeKind: typeof vscode.TextEditorSelectionChangeKind;
E
Erich Gamma 已提交
88 89 90 91 92 93 94
	commands: typeof vscode.commands;
	window: typeof vscode.window;
	workspace: typeof vscode.workspace;
	languages: typeof vscode.languages;
	extensions: typeof vscode.extensions;

	constructor(
95 96 97 98
		threadService: IThreadService,
		extensionService: ExtHostExtensionService,
		contextService: IWorkspaceContextService,
		telemetryService: ITelemetryService
E
Erich Gamma 已提交
99
	) {
100 101 102
		// Addressable instances
		const col = new InstanceCollection();

J
Johannes Rieken 已提交
103
		const extHostHeapMonitor = col.define(ExtHostContext.ExtHostHeapService).set<ExtHostHeapService>(new ExtHostHeapService());
104 105 106
		const extHostDocuments = col.define(ExtHostContext.ExtHostDocuments).set<ExtHostDocuments>(new ExtHostDocuments(threadService));
		const extHostEditors = col.define(ExtHostContext.ExtHostEditors).set<ExtHostEditors>(new ExtHostEditors(threadService, extHostDocuments));
		const extHostCommands = col.define(ExtHostContext.ExtHostCommands).set<ExtHostCommands>(new ExtHostCommands(threadService, extHostEditors));
J
Johannes Rieken 已提交
107
		const extHostConfiguration = col.define(ExtHostContext.ExtHostConfiguration).set<ExtHostConfiguration>(new ExtHostConfiguration(threadService.get(MainContext.MainThreadConfiguration)));
108
		const extHostDiagnostics = col.define(ExtHostContext.ExtHostDiagnostics).set<ExtHostDiagnostics>(new ExtHostDiagnostics(threadService));
109
		const languageFeatures = col.define(ExtHostContext.ExtHostLanguageFeatures).set<ExtHostLanguageFeatures>(new ExtHostLanguageFeatures(threadService, extHostDocuments, extHostCommands, extHostHeapMonitor, extHostDiagnostics));
110 111
		const extHostFileSystemEvent = col.define(ExtHostContext.ExtHostFileSystemEventService).set<ExtHostFileSystemEventService>(new ExtHostFileSystemEventService());
		const extHostQuickOpen = col.define(ExtHostContext.ExtHostQuickOpen).set<ExtHostQuickOpen>(new ExtHostQuickOpen(threadService));
112 113 114 115 116
		col.define(ExtHostContext.ExtHostExtensionService).set(extensionService);

		col.finish(false, threadService);

		// Others
117
		const mainThreadErrors = threadService.get(MainContext.MainThreadErrors);
118
		errors.setUnexpectedErrorHandler((err) => {
119
			mainThreadErrors.onUnexpectedExtHostError(errors.transformErrorForSerialization(err));
120 121 122 123 124
		});

		const extHostMessageService = new ExtHostMessageService(threadService);
		const extHostStatusBar = new ExtHostStatusBar(threadService);
		const extHostOutputService = new ExtHostOutputService(threadService);
D
Daniel Imms 已提交
125
		const extHostTerminalService = new ExtHostTerminalService(threadService);
126 127 128 129 130 131 132 133 134
		const workspacePath = contextService.getWorkspace() ? contextService.getWorkspace().resource.fsPath : undefined;
		const extHostWorkspace = new ExtHostWorkspace(threadService, workspacePath);
		const languages = new ExtHostLanguages(threadService);

		// the converter might create delegate commands to avoid sending args
		// around all the time
		ExtHostTypeConverters.Command.initialize(extHostCommands);
		registerApiCommands(extHostCommands);

E
Erich Gamma 已提交
135

B
Benjamin Pasero 已提交
136
		this.version = pkg.version;
E
Erich Gamma 已提交
137 138
		this.Uri = URI;
		this.Location = extHostTypes.Location;
139 140
		this.Diagnostic = extHostTypes.Diagnostic;
		this.DiagnosticSeverity = extHostTypes.DiagnosticSeverity;
141
		this.EventEmitter = Emitter;
E
Erich Gamma 已提交
142 143 144 145 146 147 148 149
		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;
150 151 152 153
		this.SymbolKind = extHostTypes.SymbolKind;
		this.SymbolInformation = extHostTypes.SymbolInformation;
		this.DocumentHighlightKind = extHostTypes.DocumentHighlightKind;
		this.DocumentHighlight = extHostTypes.DocumentHighlight;
E
Erich Gamma 已提交
154 155 156 157
		this.CodeLens = extHostTypes.CodeLens;
		this.ParameterInformation = extHostTypes.ParameterInformation;
		this.SignatureInformation = extHostTypes.SignatureInformation;
		this.SignatureHelp = extHostTypes.SignatureHelp;
158 159
		this.CompletionItem = extHostTypes.CompletionItem;
		this.CompletionItemKind = extHostTypes.CompletionItemKind;
160
		this.CompletionList = extHostTypes.CompletionList;
J
Johannes Rieken 已提交
161
		this.DocumentLink = extHostTypes.DocumentLink;
162 163 164 165
		this.ViewColumn = extHostTypes.ViewColumn;
		this.StatusBarAlignment = extHostTypes.StatusBarAlignment;
		this.IndentAction = Modes.IndentAction;
		this.OverviewRulerLane = EditorCommon.OverviewRulerLane;
166
		this.TextEditorRevealType = extHostTypes.TextEditorRevealType;
167
		this.EndOfLine = extHostTypes.EndOfLine;
A
Alex Dima 已提交
168
		this.TextEditorCursorStyle = EditorCommon.TextEditorCursorStyle;
169
		this.TextEditorSelectionChangeKind = extHostTypes.TextEditorSelectionChangeKind;
E
Erich Gamma 已提交
170

171
		// env namespace
J
Johannes Rieken 已提交
172 173
		let telemetryInfo: ITelemetryInfo;
		this.env = Object.freeze({
J
Johannes Rieken 已提交
174 175
			get machineId() { return telemetryInfo.machineId; },
			get sessionId() { return telemetryInfo.sessionId; },
176
			get language() { return Platform.language; },
177
			get appName() { return product.nameLong; }
J
Johannes Rieken 已提交
178 179
		});
		telemetryService.getTelemetryInfo().then(info => telemetryInfo = info, errors.onUnexpectedError);
180

181
		// commands namespace
182 183
		this.commands = {
			registerCommand<T>(id: string, command: <T>(...args: any[]) => T | Thenable<T>, thisArgs?: any): vscode.Disposable {
A
Alex Dima 已提交
184
				return extHostCommands.registerCommand(id, command, thisArgs);
185
			},
J
Johannes Rieken 已提交
186 187
			registerTextEditorCommand(id: string, callback: (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, ...args: any[]) => void, thisArg?: any): vscode.Disposable {
				return extHostCommands.registerCommand(id, (...args: any[]) => {
A
Alex Dima 已提交
188
					let activeTextEditor = extHostEditors.getActiveTextEditor();
189 190 191 192 193
					if (!activeTextEditor) {
						console.warn('Cannot execute ' + id + ' because there is no active text editor.');
						return;
					}

194
					return activeTextEditor.edit((edit: vscode.TextEditorEdit) => {
J
Johannes Rieken 已提交
195 196 197
						args.unshift(activeTextEditor, edit);
						callback.apply(thisArg, args);

198 199 200 201 202 203 204 205
					}).then((result) => {
						if (!result) {
							console.warn('Edits from command ' + id + ' were not applied.');
						}
					}, (err) => {
						console.warn('An error occured while running command ' + id, err);
					});
				});
206 207
			},
			executeCommand<T>(id: string, ...args: any[]): Thenable<T> {
A
Alex Dima 已提交
208
				return extHostCommands.executeCommand(id, ...args);
209
			},
210
			getCommands(filterInternal: boolean = false): Thenable<string[]> {
A
Alex Dima 已提交
211
				return extHostCommands.getCommands(filterInternal);
212 213 214
			}
		};

E
Erich Gamma 已提交
215 216
		this.window = {
			get activeTextEditor() {
A
Alex Dima 已提交
217
				return extHostEditors.getActiveTextEditor();
E
Erich Gamma 已提交
218 219
			},
			get visibleTextEditors() {
A
Alex Dima 已提交
220
				return extHostEditors.getVisibleTextEditors();
E
Erich Gamma 已提交
221
			},
222
			showTextDocument(document: vscode.TextDocument, column?: vscode.ViewColumn, preserveFocus?: boolean): TPromise<vscode.TextEditor> {
A
Alex Dima 已提交
223
				return extHostEditors.showTextDocument(document, column, preserveFocus);
E
Erich Gamma 已提交
224 225
			},
			createTextEditorDecorationType(options:vscode.DecorationRenderOptions): vscode.TextEditorDecorationType {
A
Alex Dima 已提交
226
				return extHostEditors.createTextEditorDecorationType(options);
E
Erich Gamma 已提交
227
			},
A
Alex Dima 已提交
228
			onDidChangeActiveTextEditor: extHostEditors.onDidChangeActiveTextEditor.bind(extHostEditors),
E
Erich Gamma 已提交
229
			onDidChangeTextEditorSelection: (listener: (e: vscode.TextEditorSelectionChangeEvent) => any, thisArgs?: any, disposables?: extHostTypes.Disposable[]) => {
A
Alex Dima 已提交
230
				return extHostEditors.onDidChangeTextEditorSelection(listener, thisArgs, disposables);
E
Erich Gamma 已提交
231 232
			},
			onDidChangeTextEditorOptions: (listener: (e: vscode.TextEditorOptionsChangeEvent) => any, thisArgs?: any, disposables?: extHostTypes.Disposable[]) => {
A
Alex Dima 已提交
233
				return extHostEditors.onDidChangeTextEditorOptions(listener, thisArgs, disposables);
E
Erich Gamma 已提交
234
			},
235
			onDidChangeTextEditorViewColumn(listener, thisArg?, disposables?) {
A
Alex Dima 已提交
236
				return extHostEditors.onDidChangeTextEditorViewColumn(listener, thisArg, disposables);
237
			},
E
Erich Gamma 已提交
238
			showInformationMessage: (message, ...items) => {
A
Alex Dima 已提交
239
				return extHostMessageService.showMessage(Severity.Info, message, items);
E
Erich Gamma 已提交
240 241
			},
			showWarningMessage: (message, ...items) => {
A
Alex Dima 已提交
242
				return extHostMessageService.showMessage(Severity.Warning, message, items);
E
Erich Gamma 已提交
243 244
			},
			showErrorMessage: (message, ...items) => {
A
Alex Dima 已提交
245
				return extHostMessageService.showMessage(Severity.Error, message, items);
E
Erich Gamma 已提交
246
			},
247 248
			showQuickPick: (items: any, options: vscode.QuickPickOptions, token?: vscode.CancellationToken) => {
				return extHostQuickOpen.showQuickPick(items, options, token);
E
Erich Gamma 已提交
249
			},
250 251
			showInputBox(options?: vscode.InputBoxOptions, token?: vscode.CancellationToken) {
				return extHostQuickOpen.showInput(options, token);
252
			},
E
Erich Gamma 已提交
253 254

			createStatusBarItem(position?: vscode.StatusBarAlignment, priority?: number): vscode.StatusBarItem {
A
Alex Dima 已提交
255
				return extHostStatusBar.createStatusBarEntry(<number>position, priority);
E
Erich Gamma 已提交
256 257
			},
			setStatusBarMessage(text: string, timeoutOrThenable?: number | Thenable<any>): vscode.Disposable {
A
Alex Dima 已提交
258
				return extHostStatusBar.setStatusBarMessage(text, timeoutOrThenable);
E
Erich Gamma 已提交
259
			},
260 261
			createOutputChannel(name: string): vscode.OutputChannel {
				return extHostOutputService.createOutputChannel(name);
D
Daniel Imms 已提交
262
			},
263 264
			createTerminal(name?: string, shellPath?: string): vscode.Terminal {
				return extHostTerminalService.createTerminal(name, shellPath);
E
Erich Gamma 已提交
265 266 267 268 269
			}
		};

		this.workspace = Object.freeze({
			get rootPath() {
A
Alex Dima 已提交
270
				return extHostWorkspace.getPath();
E
Erich Gamma 已提交
271 272 273 274 275
			},
			set rootPath(value) {
				throw errors.readonly();
			},
			asRelativePath: (pathOrUri) => {
A
Alex Dima 已提交
276
				return extHostWorkspace.getRelativePath(pathOrUri);
E
Erich Gamma 已提交
277
			},
278
			findFiles: (include, exclude, maxResults?, token?) => {
A
Alex Dima 已提交
279
				return extHostWorkspace.findFiles(include, exclude, maxResults, token);
E
Erich Gamma 已提交
280 281
			},
			saveAll: (includeUntitled?) => {
A
Alex Dima 已提交
282
				return extHostWorkspace.saveAll(includeUntitled);
E
Erich Gamma 已提交
283 284
			},
			applyEdit(edit: vscode.WorkspaceEdit): TPromise<boolean> {
A
Alex Dima 已提交
285
				return extHostWorkspace.appyEdit(edit);
E
Erich Gamma 已提交
286 287
			},
			createFileSystemWatcher: (pattern, ignoreCreate, ignoreChange, ignoreDelete): vscode.FileSystemWatcher => {
A
Alex Dima 已提交
288
				return extHostFileSystemEvent.createFileSystemWatcher(pattern, ignoreCreate, ignoreChange, ignoreDelete);
E
Erich Gamma 已提交
289 290
			},
			get textDocuments() {
A
Alex Dima 已提交
291
				return extHostDocuments.getAllDocumentData().map(data => data.document);
E
Erich Gamma 已提交
292 293 294 295
			},
			set textDocuments(value) {
				throw errors.readonly();
			},
296 297 298 299 300 301 302 303 304
			openTextDocument(uriOrFileName: vscode.Uri | string) {
				let uri: URI;
				if (typeof uriOrFileName === 'string') {
					uri = URI.file(uriOrFileName);
				} else if (uriOrFileName instanceof URI) {
					uri = <URI>uriOrFileName;
				} else {
					throw new Error('illegal argument - uriOrFileName');
				}
A
Alex Dima 已提交
305 306
				return extHostDocuments.ensureDocumentData(uri).then(() => {
					const data = extHostDocuments.getDocumentData(uri);
307 308
					return data && data.document;
				});
E
Erich Gamma 已提交
309
			},
J
Johannes Rieken 已提交
310
			registerTextDocumentContentProvider(scheme: string, provider: vscode.TextDocumentContentProvider) {
A
Alex Dima 已提交
311
				return extHostDocuments.registerTextDocumentContentProvider(scheme, provider);
J
Johannes Rieken 已提交
312
			},
E
Erich Gamma 已提交
313
			onDidOpenTextDocument: (listener, thisArgs?, disposables?) => {
A
Alex Dima 已提交
314
				return extHostDocuments.onDidAddDocument(listener, thisArgs, disposables);
E
Erich Gamma 已提交
315 316
			},
			onDidCloseTextDocument: (listener, thisArgs?, disposables?) => {
A
Alex Dima 已提交
317
				return extHostDocuments.onDidRemoveDocument(listener, thisArgs, disposables);
E
Erich Gamma 已提交
318 319
			},
			onDidChangeTextDocument: (listener, thisArgs?, disposables?) => {
A
Alex Dima 已提交
320
				return extHostDocuments.onDidChangeDocument(listener, thisArgs, disposables);
E
Erich Gamma 已提交
321 322
			},
			onDidSaveTextDocument: (listener, thisArgs?, disposables?) => {
A
Alex Dima 已提交
323
				return extHostDocuments.onDidSaveDocument(listener, thisArgs, disposables);
E
Erich Gamma 已提交
324 325
			},
			onDidChangeConfiguration: (listener: () => any, thisArgs?: any, disposables?: extHostTypes.Disposable[]) => {
A
Alex Dima 已提交
326
				return extHostConfiguration.onDidChangeConfiguration(listener, thisArgs, disposables);
E
Erich Gamma 已提交
327 328
			},
			getConfiguration: (section?: string):vscode.WorkspaceConfiguration => {
A
Alex Dima 已提交
329
				return extHostConfiguration.getConfiguration(section);
E
Erich Gamma 已提交
330 331 332 333 334
			}
		});

		this.languages = {
			createDiagnosticCollection(name?: string): vscode.DiagnosticCollection {
A
Alex Dima 已提交
335
				return extHostDiagnostics.createDiagnosticCollection(name);
E
Erich Gamma 已提交
336 337 338 339 340
			},
			getLanguages(): TPromise<string[]> {
				return languages.getLanguages();
			},
			match(selector: vscode.DocumentSelector, document: vscode.TextDocument): number {
341
				return score(selector, <any> document.uri, document.languageId);
E
Erich Gamma 已提交
342 343
			},
			registerCodeActionsProvider(selector: vscode.DocumentSelector, provider: vscode.CodeActionProvider): vscode.Disposable {
J
Johannes Rieken 已提交
344
				return languageFeatures.registerCodeActionProvider(selector, provider);
E
Erich Gamma 已提交
345 346
			},
			registerCodeLensProvider(selector: vscode.DocumentSelector, provider: vscode.CodeLensProvider): vscode.Disposable {
347
				return languageFeatures.registerCodeLensProvider(selector, provider);
E
Erich Gamma 已提交
348 349
			},
			registerDefinitionProvider(selector: vscode.DocumentSelector, provider: vscode.DefinitionProvider): vscode.Disposable {
J
Johannes Rieken 已提交
350
				return languageFeatures.registerDefinitionProvider(selector, provider);
E
Erich Gamma 已提交
351 352
			},
			registerHoverProvider(selector: vscode.DocumentSelector, provider: vscode.HoverProvider): vscode.Disposable {
J
Johannes Rieken 已提交
353
				return languageFeatures.registerHoverProvider(selector, provider);
E
Erich Gamma 已提交
354 355
			},
			registerDocumentHighlightProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentHighlightProvider): vscode.Disposable {
356
				return languageFeatures.registerDocumentHighlightProvider(selector, provider);
E
Erich Gamma 已提交
357 358
			},
			registerReferenceProvider(selector: vscode.DocumentSelector, provider: vscode.ReferenceProvider): vscode.Disposable {
J
Johannes Rieken 已提交
359
				return languageFeatures.registerReferenceProvider(selector, provider);
E
Erich Gamma 已提交
360 361
			},
			registerRenameProvider(selector: vscode.DocumentSelector, provider: vscode.RenameProvider): vscode.Disposable {
J
Johannes Rieken 已提交
362
				return languageFeatures.registerRenameProvider(selector, provider);
E
Erich Gamma 已提交
363 364
			},
			registerDocumentSymbolProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentSymbolProvider): vscode.Disposable {
J
Johannes Rieken 已提交
365
				return languageFeatures.registerDocumentSymbolProvider(selector, provider);
E
Erich Gamma 已提交
366 367
			},
			registerWorkspaceSymbolProvider(provider: vscode.WorkspaceSymbolProvider): vscode.Disposable {
368
				return languageFeatures.registerWorkspaceSymbolProvider(provider);
E
Erich Gamma 已提交
369 370
			},
			registerDocumentFormattingEditProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentFormattingEditProvider): vscode.Disposable {
371
				return languageFeatures.registerDocumentFormattingEditProvider(selector, provider);
E
Erich Gamma 已提交
372 373
			},
			registerDocumentRangeFormattingEditProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentRangeFormattingEditProvider): vscode.Disposable {
374
				return languageFeatures.registerDocumentRangeFormattingEditProvider(selector, provider);
E
Erich Gamma 已提交
375 376
			},
			registerOnTypeFormattingEditProvider(selector: vscode.DocumentSelector, provider: vscode.OnTypeFormattingEditProvider, firstTriggerCharacter: string, ...moreTriggerCharacters: string[]): vscode.Disposable {
377
				return languageFeatures.registerOnTypeFormattingEditProvider(selector, provider, [firstTriggerCharacter].concat(moreTriggerCharacters));
E
Erich Gamma 已提交
378 379
			},
			registerSignatureHelpProvider(selector: vscode.DocumentSelector, provider: vscode.SignatureHelpProvider, ...triggerCharacters: string[]): vscode.Disposable {
380
				return languageFeatures.registerSignatureHelpProvider(selector, provider, triggerCharacters);
E
Erich Gamma 已提交
381 382
			},
			registerCompletionItemProvider(selector: vscode.DocumentSelector, provider: vscode.CompletionItemProvider, ...triggerCharacters: string[]): vscode.Disposable {
383
				return languageFeatures.registerCompletionItemProvider(selector, provider, triggerCharacters);
E
Erich Gamma 已提交
384
			},
J
Johannes Rieken 已提交
385 386 387
			registerDocumentLinkProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentLinkProvider): vscode.Disposable {
				return languageFeatures.registerDocumentLinkProvider(selector, provider);
			},
E
Erich Gamma 已提交
388
			setLanguageConfiguration: (language: string, configuration: vscode.LanguageConfiguration):vscode.Disposable => {
389
				return languageFeatures.setLanguageConfiguration(language, configuration);
E
Erich Gamma 已提交
390 391 392 393 394
			}
		};

		this.extensions = {
			getExtension(extensionId: string):Extension<any> {
A
Alex Dima 已提交
395
				let desc = ExtensionsRegistry.getExtensionDescription(extensionId);
E
Erich Gamma 已提交
396
				if (desc) {
A
Alex Dima 已提交
397
					return new Extension(<ExtHostExtensionService> extensionService, desc);
E
Erich Gamma 已提交
398 399 400
				}
			},
			get all():Extension<any>[] {
A
Alex Dima 已提交
401
				return ExtensionsRegistry.getAllExtensionDescriptions().map((desc) => new Extension(<ExtHostExtensionService> extensionService, desc));
E
Erich Gamma 已提交
402
			}
B
Benjamin Pasero 已提交
403
		};
E
Erich Gamma 已提交
404 405 406 407 408
	}
}

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

A
Alex Dima 已提交
409
	private _extensionService: ExtHostExtensionService;
E
Erich Gamma 已提交
410 411 412 413 414

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

A
Alex Dima 已提交
415
	constructor(extensionService:ExtHostExtensionService, description:IExtensionDescription) {
A
Alex Dima 已提交
416
		this._extensionService = extensionService;
E
Erich Gamma 已提交
417 418 419 420 421 422
		this.id = description.id;
		this.extensionPath = paths.normalize(description.extensionFolderPath, true);
		this.packageJSON = description;
	}

	get isActive(): boolean {
A
Alex Dima 已提交
423
		return this._extensionService.isActivated(this.id);
E
Erich Gamma 已提交
424 425 426
	}

	get exports(): T {
A
Alex Dima 已提交
427
		return <T>this._extensionService.get(this.id);
E
Erich Gamma 已提交
428 429 430
	}

	activate(): Thenable<T> {
A
Alex Dima 已提交
431
		return this._extensionService.activateById(this.id).then(() => this.exports);
E
Erich Gamma 已提交
432 433 434
	}
}

435
export function defineAPI(impl: typeof vscode) {
B
Benjamin Pasero 已提交
436 437
	let node_module = <any>require.__$__nodeRequire('module');
	let original = node_module._load;
E
Erich Gamma 已提交
438 439 440 441 442 443 444 445
	node_module._load = function load(request, parent, isMain) {
		if (request === 'vscode') {
			return impl;
		}
		return original.apply(this, arguments);
	};
	define('vscode', [], impl);
}