forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathextension.ts
437 lines (389 loc) · 21.2 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
206
207
208
209
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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
426
427
428
429
430
431
432
433
434
435
436
437
'use strict';
// tslint:disable:no-var-requires no-require-imports
// This line should always be right on top.
// tslint:disable:no-any
if ((Reflect as any).metadata === undefined) {
require('reflect-metadata');
}
// Initialize source maps (this must never be moved up nor further down).
import { initialize } from './sourceMapSupport';
initialize(require('vscode'));
// Initialize the logger first.
require('./common/logger');
const durations: Record<string, number> = {};
import { StopWatch } from './common/utils/stopWatch';
// Do not move this line of code (used to measure extension load times).
const stopWatch = new StopWatch();
import { Container } from 'inversify';
import {
CodeActionKind,
debug,
DebugConfigurationProvider,
Disposable,
ExtensionContext,
extensions,
IndentAction,
languages,
Memento,
OutputChannel,
ProgressLocation,
ProgressOptions,
window
} from 'vscode';
import { registerTypes as activationRegisterTypes } from './activation/serviceRegistry';
import { IExtensionActivationManager, ILanguageServerExtension } from './activation/types';
import { buildApi, IExtensionApi } from './api';
import { registerTypes as appRegisterTypes } from './application/serviceRegistry';
import { IApplicationDiagnostics } from './application/types';
import { DebugService } from './common/application/debugService';
import { IApplicationShell, ICommandManager, IWorkspaceService } from './common/application/types';
import { Commands, isTestExecution, PYTHON, PYTHON_LANGUAGE, STANDARD_OUTPUT_CHANNEL } from './common/constants';
import { registerTypes as registerDotNetTypes } from './common/dotnet/serviceRegistry';
import { registerTypes as installerRegisterTypes } from './common/installer/serviceRegistry';
import { traceError } from './common/logger';
import { registerTypes as platformRegisterTypes } from './common/platform/serviceRegistry';
import { registerTypes as processRegisterTypes } from './common/process/serviceRegistry';
import { registerTypes as commonRegisterTypes } from './common/serviceRegistry';
import { ITerminalHelper } from './common/terminal/types';
import {
GLOBAL_MEMENTO,
IAsyncDisposableRegistry,
IConfigurationService,
IDisposableRegistry,
IExtensionContext,
IFeatureDeprecationManager,
IMemento,
IOutputChannel,
Resource,
WORKSPACE_MEMENTO
} from './common/types';
import { createDeferred } from './common/utils/async';
import { Common } from './common/utils/localize';
import { registerTypes as variableRegisterTypes } from './common/variables/serviceRegistry';
import { registerTypes as dataScienceRegisterTypes } from './datascience/serviceRegistry';
import { IDataScience } from './datascience/types';
import { DebuggerTypeName } from './debugger/constants';
import { DebugSessionEventDispatcher } from './debugger/extension/hooks/eventHandlerDispatcher';
import { IDebugSessionEventHandlers } from './debugger/extension/hooks/types';
import { registerTypes as debugConfigurationRegisterTypes } from './debugger/extension/serviceRegistry';
import { IDebugConfigurationService, IDebuggerBanner } from './debugger/extension/types';
import { registerTypes as formattersRegisterTypes } from './formatters/serviceRegistry';
import { AutoSelectionRule, IInterpreterAutoSelectionRule, IInterpreterAutoSelectionService } from './interpreter/autoSelection/types';
import { IInterpreterSelector } from './interpreter/configuration/types';
import {
ICondaService,
IInterpreterLocatorProgressService,
IInterpreterService,
InterpreterLocatorProgressHandler,
PythonInterpreter
} from './interpreter/contracts';
import { registerTypes as interpretersRegisterTypes } from './interpreter/serviceRegistry';
import { ServiceContainer } from './ioc/container';
import { ServiceManager } from './ioc/serviceManager';
import { IServiceContainer, IServiceManager } from './ioc/types';
import { LinterCommands } from './linters/linterCommands';
import { registerTypes as lintersRegisterTypes } from './linters/serviceRegistry';
import { ILintingEngine } from './linters/types';
import { PythonCodeActionProvider } from './providers/codeActionsProvider';
import { PythonFormattingEditProvider } from './providers/formatProvider';
import { LinterProvider } from './providers/linterProvider';
import { ReplProvider } from './providers/replProvider';
import { registerTypes as providersRegisterTypes } from './providers/serviceRegistry';
import { activateSimplePythonRefactorProvider } from './providers/simpleRefactorProvider';
import { TerminalProvider } from './providers/terminalProvider';
import { ISortImportsEditingProvider } from './providers/types';
import { activateUpdateSparkLibraryProvider } from './providers/updateSparkLibraryProvider';
import { sendTelemetryEvent } from './telemetry';
import { EventName } from './telemetry/constants';
import { EditorLoadTelemetry, IImportTracker } from './telemetry/types';
import { registerTypes as commonRegisterTerminalTypes } from './terminals/serviceRegistry';
import { ICodeExecutionManager, ITerminalAutoActivation } from './terminals/types';
import { TEST_OUTPUT_CHANNEL } from './testing/common/constants';
import { ITestContextService } from './testing/common/types';
import { ITestCodeNavigatorCommandHandler, ITestExplorerCommandHandler } from './testing/navigation/types';
import { registerTypes as unitTestsRegisterTypes } from './testing/serviceRegistry';
durations.codeLoadingTime = stopWatch.elapsedTime;
const activationDeferred = createDeferred<void>();
let activatedServiceContainer: ServiceContainer | undefined;
export async function activate(context: ExtensionContext): Promise<IExtensionApi> {
try {
return await activateUnsafe(context);
} catch (ex) {
handleError(ex);
throw ex; // re-raise
}
}
// tslint:disable-next-line:max-func-body-length
async function activateUnsafe(context: ExtensionContext): Promise<IExtensionApi> {
displayProgress(activationDeferred.promise);
durations.startActivateTime = stopWatch.elapsedTime;
const cont = new Container();
const serviceManager = new ServiceManager(cont);
const serviceContainer = new ServiceContainer(cont);
activatedServiceContainer = serviceContainer;
registerServices(context, serviceManager, serviceContainer);
await initializeServices(context, serviceManager, serviceContainer);
const manager = serviceContainer.get<IExtensionActivationManager>(IExtensionActivationManager);
context.subscriptions.push(manager);
const activationPromise = manager.activate();
serviceManager.get<ITerminalAutoActivation>(ITerminalAutoActivation).register();
const configuration = serviceManager.get<IConfigurationService>(IConfigurationService);
const pythonSettings = configuration.getSettings();
const standardOutputChannel = serviceContainer.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
activateSimplePythonRefactorProvider(context, standardOutputChannel, serviceContainer);
const sortImports = serviceContainer.get<ISortImportsEditingProvider>(ISortImportsEditingProvider);
sortImports.registerCommands();
serviceManager.get<ICodeExecutionManager>(ICodeExecutionManager).registerCommands();
// tslint:disable-next-line:no-suspicious-comment
// TODO: Move this down to right before durations.endActivateTime is set.
sendStartupTelemetry(Promise.all([activationDeferred.promise, activationPromise]), serviceContainer).ignoreErrors();
const workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
const interpreterManager = serviceContainer.get<IInterpreterService>(IInterpreterService);
interpreterManager.refresh(workspaceService.hasWorkspaceFolders ? workspaceService.workspaceFolders![0].uri : undefined)
.catch(ex => console.error('Python Extension: interpreterManager.refresh', ex));
const jupyterExtension = extensions.getExtension('donjayamanne.jupyter');
const lintingEngine = serviceManager.get<ILintingEngine>(ILintingEngine);
lintingEngine.linkJupyterExtension(jupyterExtension).ignoreErrors();
// Activate data science features
const dataScience = serviceManager.get<IDataScience>(IDataScience);
dataScience.activate().ignoreErrors();
// Activate import tracking
const importTracker = serviceManager.get<IImportTracker>(IImportTracker);
importTracker.activate().ignoreErrors();
context.subscriptions.push(new LinterCommands(serviceManager));
const linterProvider = new LinterProvider(context, serviceManager);
context.subscriptions.push(linterProvider);
// Enable indentAction
// tslint:disable-next-line:no-non-null-assertion
languages.setLanguageConfiguration(PYTHON_LANGUAGE, {
onEnterRules: [
{
beforeText: /^\s*(?:def|class|for|if|elif|else|while|try|with|finally|except|async)\b.*:\s*/,
action: { indentAction: IndentAction.Indent }
},
{
beforeText: /^(?!\s+\\)[^#\n]+\\\s*/,
action: { indentAction: IndentAction.Indent }
},
{
beforeText: /^\s*#.*/,
afterText: /.+$/,
action: { indentAction: IndentAction.None, appendText: '# ' }
},
{
beforeText: /^\s+(continue|break|return)\b.*/,
afterText: /\s+$/,
action: { indentAction: IndentAction.Outdent }
}
]
});
if (pythonSettings && pythonSettings.formatting && pythonSettings.formatting.provider !== 'internalConsole') {
const formatProvider = new PythonFormattingEditProvider(context, serviceContainer);
context.subscriptions.push(languages.registerDocumentFormattingEditProvider(PYTHON, formatProvider));
context.subscriptions.push(languages.registerDocumentRangeFormattingEditProvider(PYTHON, formatProvider));
}
const deprecationMgr = serviceContainer.get<IFeatureDeprecationManager>(IFeatureDeprecationManager);
deprecationMgr.initialize();
context.subscriptions.push(deprecationMgr);
context.subscriptions.push(activateUpdateSparkLibraryProvider());
context.subscriptions.push(new ReplProvider(serviceContainer));
context.subscriptions.push(new TerminalProvider(serviceContainer));
context.subscriptions.push(languages.registerCodeActionsProvider(PYTHON, new PythonCodeActionProvider(), { providedCodeActionKinds: [CodeActionKind.SourceOrganizeImports] }));
serviceContainer.getAll<DebugConfigurationProvider>(IDebugConfigurationService).forEach(debugConfigProvider => {
context.subscriptions.push(debug.registerDebugConfigurationProvider(DebuggerTypeName, debugConfigProvider));
});
serviceContainer.get<IDebuggerBanner>(IDebuggerBanner).initialize();
durations.endActivateTime = stopWatch.elapsedTime;
activationDeferred.resolve();
const api = buildApi(Promise.all([activationDeferred.promise, activationPromise]));
// In test environment return the DI Container.
if (isTestExecution()) {
// tslint:disable:no-any
(api as any).serviceContainer = serviceContainer;
(api as any).serviceManager = serviceManager;
// tslint:enable:no-any
}
return api;
}
export function deactivate(): Thenable<void> {
// Make sure to shutdown anybody who needs it.
if (activatedServiceContainer) {
const registry = activatedServiceContainer.get<IAsyncDisposableRegistry>(IAsyncDisposableRegistry);
if (registry) {
return registry.dispose();
}
}
return Promise.resolve();
}
// tslint:disable-next-line:no-any
function displayProgress(promise: Promise<any>) {
const progressOptions: ProgressOptions = { location: ProgressLocation.Window, title: Common.loadingExtension() };
window.withProgress(progressOptions, () => promise);
}
function registerServices(context: ExtensionContext, serviceManager: ServiceManager, serviceContainer: ServiceContainer) {
serviceManager.addSingletonInstance<IServiceContainer>(IServiceContainer, serviceContainer);
serviceManager.addSingletonInstance<IServiceManager>(IServiceManager, serviceManager);
serviceManager.addSingletonInstance<Disposable[]>(IDisposableRegistry, context.subscriptions);
serviceManager.addSingletonInstance<Memento>(IMemento, context.globalState, GLOBAL_MEMENTO);
serviceManager.addSingletonInstance<Memento>(IMemento, context.workspaceState, WORKSPACE_MEMENTO);
serviceManager.addSingletonInstance<IExtensionContext>(IExtensionContext, context);
const standardOutputChannel = window.createOutputChannel('Python');
const unitTestOutChannel = window.createOutputChannel('Python Test Log');
serviceManager.addSingletonInstance<OutputChannel>(IOutputChannel, standardOutputChannel, STANDARD_OUTPUT_CHANNEL);
serviceManager.addSingletonInstance<OutputChannel>(IOutputChannel, unitTestOutChannel, TEST_OUTPUT_CHANNEL);
activationRegisterTypes(serviceManager);
commonRegisterTypes(serviceManager);
registerDotNetTypes(serviceManager);
processRegisterTypes(serviceManager);
variableRegisterTypes(serviceManager);
unitTestsRegisterTypes(serviceManager);
lintersRegisterTypes(serviceManager);
interpretersRegisterTypes(serviceManager);
formattersRegisterTypes(serviceManager);
platformRegisterTypes(serviceManager);
installerRegisterTypes(serviceManager);
commonRegisterTerminalTypes(serviceManager);
dataScienceRegisterTypes(serviceManager);
debugConfigurationRegisterTypes(serviceManager);
appRegisterTypes(serviceManager);
providersRegisterTypes(serviceManager);
}
async function initializeServices(context: ExtensionContext, serviceManager: ServiceManager, serviceContainer: ServiceContainer) {
const selector = serviceContainer.get<IInterpreterSelector>(IInterpreterSelector);
selector.initialize();
context.subscriptions.push(selector);
const interpreterManager = serviceContainer.get<IInterpreterService>(IInterpreterService);
interpreterManager.initialize();
const handlers = serviceManager.getAll<IDebugSessionEventHandlers>(IDebugSessionEventHandlers);
const disposables = serviceManager.get<IDisposableRegistry>(IDisposableRegistry);
const dispatcher = new DebugSessionEventDispatcher(handlers, DebugService.instance, disposables);
dispatcher.registerEventHandlers();
const cmdManager = serviceContainer.get<ICommandManager>(ICommandManager);
const outputChannel = serviceManager.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
disposables.push(cmdManager.registerCommand(Commands.ViewOutput, () => outputChannel.show()));
// Display progress of interpreter refreshes only after extension has activated.
serviceContainer.get<InterpreterLocatorProgressHandler>(InterpreterLocatorProgressHandler).register();
serviceContainer.get<IInterpreterLocatorProgressService>(IInterpreterLocatorProgressService).register();
serviceContainer.get<IApplicationDiagnostics>(IApplicationDiagnostics).register();
serviceContainer.get<ITestCodeNavigatorCommandHandler>(ITestCodeNavigatorCommandHandler).register();
serviceContainer.get<ITestExplorerCommandHandler>(ITestExplorerCommandHandler).register();
serviceContainer.get<ILanguageServerExtension>(ILanguageServerExtension).register();
serviceContainer.get<ITestContextService>(ITestContextService).register();
}
// tslint:disable-next-line:no-any
async function sendStartupTelemetry(activatedPromise: Promise<any>, serviceContainer: IServiceContainer) {
try {
await activatedPromise;
durations.totalActivateTime = stopWatch.elapsedTime;
const props = await getActivationTelemetryProps(serviceContainer);
sendTelemetryEvent(EventName.EDITOR_LOAD, durations, props);
} catch (ex) {
traceError('sendStartupTelemetry() failed.', ex);
}
}
function isUsingGlobalInterpreterInWorkspace(currentPythonPath: string, serviceContainer: IServiceContainer): boolean {
const service = serviceContainer.get<IInterpreterAutoSelectionService>(IInterpreterAutoSelectionService);
const globalInterpreter = service.getAutoSelectedInterpreter(undefined);
if (!globalInterpreter) {
return false;
}
return currentPythonPath === globalInterpreter.path;
}
function hasUserDefinedPythonPath(resource: Resource, serviceContainer: IServiceContainer) {
const workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
const settings = workspaceService.getConfiguration('python', resource)!.inspect<string>('pythonPath')!;
return ((settings.workspaceFolderValue && settings.workspaceFolderValue !== 'python') ||
(settings.workspaceValue && settings.workspaceValue !== 'python') ||
(settings.globalValue && settings.globalValue !== 'python')) ? true : false;
}
function getPreferredWorkspaceInterpreter(resource: Resource, serviceContainer: IServiceContainer) {
const workspaceInterpreterSelector = serviceContainer.get<IInterpreterAutoSelectionRule>(IInterpreterAutoSelectionRule, AutoSelectionRule.workspaceVirtualEnvs);
const interpreter = workspaceInterpreterSelector.getPreviouslyAutoSelectedInterpreter(resource);
return interpreter ? interpreter.path : undefined;
}
/////////////////////////////
// telemetry
// tslint:disable-next-line:no-any
async function getActivationTelemetryProps(serviceContainer: IServiceContainer): Promise<EditorLoadTelemetry> {
// tslint:disable-next-line:no-suspicious-comment
// TODO: Not all of this data is showing up in the database...
// tslint:disable-next-line:no-suspicious-comment
// TODO: If any one of these parts fails we send no info. We should
// be able to partially populate as much as possible instead
// (through granular try-catch statements).
const terminalHelper = serviceContainer.get<ITerminalHelper>(ITerminalHelper);
const terminalShellType = terminalHelper.identifyTerminalShell();
const condaLocator = serviceContainer.get<ICondaService>(ICondaService);
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
const workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
const configurationService = serviceContainer.get<IConfigurationService>(IConfigurationService);
const mainWorkspaceUri = workspaceService.hasWorkspaceFolders ? workspaceService.workspaceFolders![0].uri : undefined;
const settings = configurationService.getSettings(mainWorkspaceUri);
const [condaVersion, interpreter, interpreters] = await Promise.all([
condaLocator.getCondaVersion().then(ver => ver ? ver.raw : '').catch<string>(() => ''),
interpreterService.getActiveInterpreter().catch<PythonInterpreter | undefined>(() => undefined),
interpreterService.getInterpreters(mainWorkspaceUri).catch<PythonInterpreter[]>(() => [])
]);
const workspaceFolderCount = workspaceService.hasWorkspaceFolders ? workspaceService.workspaceFolders!.length : 0;
const pythonVersion = interpreter && interpreter.version ? interpreter.version.raw : undefined;
const interpreterType = interpreter ? interpreter.type : undefined;
const usingUserDefinedInterpreter = hasUserDefinedPythonPath(mainWorkspaceUri, serviceContainer);
const preferredWorkspaceInterpreter = getPreferredWorkspaceInterpreter(mainWorkspaceUri, serviceContainer);
const usingGlobalInterpreter = isUsingGlobalInterpreterInWorkspace(settings.pythonPath, serviceContainer);
const usingAutoSelectedWorkspaceInterpreter = preferredWorkspaceInterpreter ? settings.pythonPath === getPreferredWorkspaceInterpreter(mainWorkspaceUri, serviceContainer) : false;
const hasPython3 = interpreters
.filter(item => item && item.version ? item.version.major === 3 : false)
.length > 0;
return {
condaVersion,
terminal: terminalShellType,
pythonVersion,
interpreterType,
workspaceFolderCount,
hasPython3,
usingUserDefinedInterpreter,
usingAutoSelectedWorkspaceInterpreter,
usingGlobalInterpreter
};
}
/////////////////////////////
// error handling
function handleError(ex: Error) {
notifyUser('Extension activation failed, run the \'Developer: Toggle Developer Tools\' command for more information.');
traceError('extension activation failed', ex);
sendErrorTelemetry(ex)
.ignoreErrors();
}
interface IAppShell {
showErrorMessage(string: string): Promise<void>;
}
function notifyUser(msg: string) {
try {
// tslint:disable-next-line:no-any
let appShell: IAppShell = (window as any as IAppShell);
if (activatedServiceContainer) {
// tslint:disable-next-line:no-any
appShell = activatedServiceContainer.get<IApplicationShell>(IApplicationShell) as any as IAppShell;
}
appShell.showErrorMessage(msg)
.ignoreErrors();
} catch (ex) {
// ignore
}
}
async function sendErrorTelemetry(ex: Error) {
try {
// tslint:disable-next-line:no-any
let props: any = {};
if (activatedServiceContainer) {
try {
props = await getActivationTelemetryProps(activatedServiceContainer);
} catch (ex) {
// ignore
}
}
sendTelemetryEvent(EventName.EDITOR_LOAD, durations, props, ex);
} catch (exc2) {
traceError('sendErrorTelemetry() failed.', exc2);
}
}