|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// tslint:disable:max-func-body-length |
| 7 | + |
| 8 | +import { CodeActionKind, debug, DebugConfigurationProvider, languages, OutputChannel, window } from 'vscode'; |
| 9 | + |
| 10 | +import { registerTypes as activationRegisterTypes } from './activation/serviceRegistry'; |
| 11 | +import { IExtensionActivationManager, ILanguageServerExtension } from './activation/types'; |
| 12 | +import { registerTypes as appRegisterTypes } from './application/serviceRegistry'; |
| 13 | +import { IApplicationDiagnostics } from './application/types'; |
| 14 | +import { DebugService } from './common/application/debugService'; |
| 15 | +import { ICommandManager, IWorkspaceService } from './common/application/types'; |
| 16 | +import { Commands, PYTHON, PYTHON_LANGUAGE, STANDARD_OUTPUT_CHANNEL } from './common/constants'; |
| 17 | +import { registerTypes as installerRegisterTypes } from './common/installer/serviceRegistry'; |
| 18 | +import { traceError } from './common/logger'; |
| 19 | +import { registerTypes as platformRegisterTypes } from './common/platform/serviceRegistry'; |
| 20 | +import { registerTypes as processRegisterTypes } from './common/process/serviceRegistry'; |
| 21 | +import { registerTypes as commonRegisterTypes } from './common/serviceRegistry'; |
| 22 | +import { |
| 23 | + IConfigurationService, |
| 24 | + IDisposableRegistry, |
| 25 | + IExperimentsManager, |
| 26 | + IExtensionContext, |
| 27 | + IFeatureDeprecationManager, |
| 28 | + IOutputChannel |
| 29 | +} from './common/types'; |
| 30 | +import { OutputChannelNames } from './common/utils/localize'; |
| 31 | +import { registerTypes as variableRegisterTypes } from './common/variables/serviceRegistry'; |
| 32 | +import { JUPYTER_OUTPUT_CHANNEL } from './datascience/constants'; |
| 33 | +import { registerTypes as dataScienceRegisterTypes } from './datascience/serviceRegistry'; |
| 34 | +import { IDataScience } from './datascience/types'; |
| 35 | +import { DebuggerTypeName } from './debugger/constants'; |
| 36 | +import { DebugSessionEventDispatcher } from './debugger/extension/hooks/eventHandlerDispatcher'; |
| 37 | +import { IDebugSessionEventHandlers } from './debugger/extension/hooks/types'; |
| 38 | +import { registerTypes as debugConfigurationRegisterTypes } from './debugger/extension/serviceRegistry'; |
| 39 | +import { IDebugConfigurationService, IDebuggerBanner } from './debugger/extension/types'; |
| 40 | +import { registerTypes as formattersRegisterTypes } from './formatters/serviceRegistry'; |
| 41 | +import { IInterpreterSelector } from './interpreter/configuration/types'; |
| 42 | +import { |
| 43 | + IInterpreterLocatorProgressHandler, |
| 44 | + IInterpreterLocatorProgressService, |
| 45 | + IInterpreterService |
| 46 | +} from './interpreter/contracts'; |
| 47 | +import { registerTypes as interpretersRegisterTypes } from './interpreter/serviceRegistry'; |
| 48 | +import { IServiceContainer, IServiceManager } from './ioc/types'; |
| 49 | +import { getLanguageConfiguration } from './language/languageConfiguration'; |
| 50 | +import { LinterCommands } from './linters/linterCommands'; |
| 51 | +import { registerTypes as lintersRegisterTypes } from './linters/serviceRegistry'; |
| 52 | +import { PythonCodeActionProvider } from './providers/codeActionProvider/pythonCodeActionProvider'; |
| 53 | +import { PythonFormattingEditProvider } from './providers/formatProvider'; |
| 54 | +import { ReplProvider } from './providers/replProvider'; |
| 55 | +import { registerTypes as providersRegisterTypes } from './providers/serviceRegistry'; |
| 56 | +import { activateSimplePythonRefactorProvider } from './providers/simpleRefactorProvider'; |
| 57 | +import { TerminalProvider } from './providers/terminalProvider'; |
| 58 | +import { ISortImportsEditingProvider } from './providers/types'; |
| 59 | +import { registerTypes as commonRegisterTerminalTypes } from './terminals/serviceRegistry'; |
| 60 | +import { ICodeExecutionManager, ITerminalAutoActivation } from './terminals/types'; |
| 61 | +import { TEST_OUTPUT_CHANNEL } from './testing/common/constants'; |
| 62 | +import { ITestContextService } from './testing/common/types'; |
| 63 | +import { ITestCodeNavigatorCommandHandler, ITestExplorerCommandHandler } from './testing/navigation/types'; |
| 64 | +import { registerTypes as unitTestsRegisterTypes } from './testing/serviceRegistry'; |
| 65 | + |
| 66 | +export async function activateComponents( |
| 67 | + context: IExtensionContext, |
| 68 | + serviceManager: IServiceManager, |
| 69 | + serviceContainer: IServiceContainer |
| 70 | +) { |
| 71 | + // We will be pulling code over from activateLegacy(). |
| 72 | + |
| 73 | + return activateLegacy(context, serviceManager, serviceContainer); |
| 74 | +} |
| 75 | + |
| 76 | +///////////////////////////// |
| 77 | +// old activation code |
| 78 | + |
| 79 | +// tslint:disable-next-line:no-suspicious-comment |
| 80 | +// TODO(GH-10454): Gradually move simple initialization |
| 81 | +// and DI registration currently in this function over |
| 82 | +// to initializeComponents(). Likewise with complex |
| 83 | +// init and activation: move them to activateComponents(). |
| 84 | + |
| 85 | +async function activateLegacy( |
| 86 | + context: IExtensionContext, |
| 87 | + serviceManager: IServiceManager, |
| 88 | + serviceContainer: IServiceContainer |
| 89 | +) { |
| 90 | + // register "services" |
| 91 | + |
| 92 | + const standardOutputChannel = window.createOutputChannel(OutputChannelNames.python()); |
| 93 | + const unitTestOutChannel = window.createOutputChannel(OutputChannelNames.pythonTest()); |
| 94 | + const jupyterOutputChannel = window.createOutputChannel(OutputChannelNames.jupyter()); |
| 95 | + serviceManager.addSingletonInstance<OutputChannel>(IOutputChannel, standardOutputChannel, STANDARD_OUTPUT_CHANNEL); |
| 96 | + serviceManager.addSingletonInstance<OutputChannel>(IOutputChannel, unitTestOutChannel, TEST_OUTPUT_CHANNEL); |
| 97 | + serviceManager.addSingletonInstance<OutputChannel>(IOutputChannel, jupyterOutputChannel, JUPYTER_OUTPUT_CHANNEL); |
| 98 | + |
| 99 | + commonRegisterTypes(serviceManager); |
| 100 | + processRegisterTypes(serviceManager); |
| 101 | + variableRegisterTypes(serviceManager); |
| 102 | + unitTestsRegisterTypes(serviceManager); |
| 103 | + lintersRegisterTypes(serviceManager); |
| 104 | + interpretersRegisterTypes(serviceManager); |
| 105 | + formattersRegisterTypes(serviceManager); |
| 106 | + platformRegisterTypes(serviceManager); |
| 107 | + installerRegisterTypes(serviceManager); |
| 108 | + commonRegisterTerminalTypes(serviceManager); |
| 109 | + dataScienceRegisterTypes(serviceManager); |
| 110 | + debugConfigurationRegisterTypes(serviceManager); |
| 111 | + |
| 112 | + const configuration = serviceManager.get<IConfigurationService>(IConfigurationService); |
| 113 | + const languageServerType = configuration.getSettings().languageServer; |
| 114 | + |
| 115 | + appRegisterTypes(serviceManager, languageServerType); |
| 116 | + providersRegisterTypes(serviceManager); |
| 117 | + activationRegisterTypes(serviceManager, languageServerType); |
| 118 | + |
| 119 | + // "initialize" "services" |
| 120 | + |
| 121 | + const abExperiments = serviceContainer.get<IExperimentsManager>(IExperimentsManager); |
| 122 | + await abExperiments.activate(); |
| 123 | + const selector = serviceContainer.get<IInterpreterSelector>(IInterpreterSelector); |
| 124 | + selector.initialize(); |
| 125 | + context.subscriptions.push(selector); |
| 126 | + |
| 127 | + const interpreterManager = serviceContainer.get<IInterpreterService>(IInterpreterService); |
| 128 | + interpreterManager.initialize(); |
| 129 | + |
| 130 | + const handlers = serviceManager.getAll<IDebugSessionEventHandlers>(IDebugSessionEventHandlers); |
| 131 | + const disposables = serviceManager.get<IDisposableRegistry>(IDisposableRegistry); |
| 132 | + const dispatcher = new DebugSessionEventDispatcher(handlers, DebugService.instance, disposables); |
| 133 | + dispatcher.registerEventHandlers(); |
| 134 | + |
| 135 | + const cmdManager = serviceContainer.get<ICommandManager>(ICommandManager); |
| 136 | + const outputChannel = serviceManager.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL); |
| 137 | + disposables.push(cmdManager.registerCommand(Commands.ViewOutput, () => outputChannel.show())); |
| 138 | + |
| 139 | + // Display progress of interpreter refreshes only after extension has activated. |
| 140 | + serviceContainer.get<IInterpreterLocatorProgressHandler>(IInterpreterLocatorProgressHandler).register(); |
| 141 | + serviceContainer.get<IInterpreterLocatorProgressService>(IInterpreterLocatorProgressService).register(); |
| 142 | + serviceContainer.get<IApplicationDiagnostics>(IApplicationDiagnostics).register(); |
| 143 | + serviceContainer.get<ITestCodeNavigatorCommandHandler>(ITestCodeNavigatorCommandHandler).register(); |
| 144 | + serviceContainer.get<ITestExplorerCommandHandler>(ITestExplorerCommandHandler).register(); |
| 145 | + serviceContainer.get<ILanguageServerExtension>(ILanguageServerExtension).register(); |
| 146 | + serviceContainer.get<ITestContextService>(ITestContextService).register(); |
| 147 | + |
| 148 | + // "activate" everything else |
| 149 | + |
| 150 | + const manager = serviceContainer.get<IExtensionActivationManager>(IExtensionActivationManager); |
| 151 | + context.subscriptions.push(manager); |
| 152 | + const activationPromise = manager.activate(); |
| 153 | + |
| 154 | + serviceManager.get<ITerminalAutoActivation>(ITerminalAutoActivation).register(); |
| 155 | + const pythonSettings = configuration.getSettings(); |
| 156 | + |
| 157 | + activateSimplePythonRefactorProvider(context, standardOutputChannel, serviceContainer); |
| 158 | + |
| 159 | + const sortImports = serviceContainer.get<ISortImportsEditingProvider>(ISortImportsEditingProvider); |
| 160 | + sortImports.registerCommands(); |
| 161 | + |
| 162 | + serviceManager.get<ICodeExecutionManager>(ICodeExecutionManager).registerCommands(); |
| 163 | + |
| 164 | + const workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService); |
| 165 | + interpreterManager |
| 166 | + .refresh(workspaceService.hasWorkspaceFolders ? workspaceService.workspaceFolders![0].uri : undefined) |
| 167 | + .catch(ex => traceError('Python Extension: interpreterManager.refresh', ex)); |
| 168 | + |
| 169 | + // Activate data science features |
| 170 | + const dataScience = serviceManager.get<IDataScience>(IDataScience); |
| 171 | + dataScience.activate().ignoreErrors(); |
| 172 | + |
| 173 | + context.subscriptions.push(new LinterCommands(serviceManager)); |
| 174 | + |
| 175 | + languages.setLanguageConfiguration(PYTHON_LANGUAGE, getLanguageConfiguration()); |
| 176 | + |
| 177 | + if (pythonSettings && pythonSettings.formatting && pythonSettings.formatting.provider !== 'internalConsole') { |
| 178 | + const formatProvider = new PythonFormattingEditProvider(context, serviceContainer); |
| 179 | + context.subscriptions.push(languages.registerDocumentFormattingEditProvider(PYTHON, formatProvider)); |
| 180 | + context.subscriptions.push(languages.registerDocumentRangeFormattingEditProvider(PYTHON, formatProvider)); |
| 181 | + } |
| 182 | + |
| 183 | + const deprecationMgr = serviceContainer.get<IFeatureDeprecationManager>(IFeatureDeprecationManager); |
| 184 | + deprecationMgr.initialize(); |
| 185 | + context.subscriptions.push(deprecationMgr); |
| 186 | + |
| 187 | + context.subscriptions.push(new ReplProvider(serviceContainer)); |
| 188 | + |
| 189 | + const terminalProvider = new TerminalProvider(serviceContainer); |
| 190 | + terminalProvider.initialize(window.activeTerminal).ignoreErrors(); |
| 191 | + context.subscriptions.push(terminalProvider); |
| 192 | + |
| 193 | + context.subscriptions.push( |
| 194 | + languages.registerCodeActionsProvider(PYTHON, new PythonCodeActionProvider(), { |
| 195 | + providedCodeActionKinds: [CodeActionKind.SourceOrganizeImports] |
| 196 | + }) |
| 197 | + ); |
| 198 | + |
| 199 | + serviceContainer.getAll<DebugConfigurationProvider>(IDebugConfigurationService).forEach(debugConfigProvider => { |
| 200 | + context.subscriptions.push(debug.registerDebugConfigurationProvider(DebuggerTypeName, debugConfigProvider)); |
| 201 | + }); |
| 202 | + |
| 203 | + serviceContainer.get<IDebuggerBanner>(IDebuggerBanner).initialize(); |
| 204 | + |
| 205 | + return activationPromise; |
| 206 | +} |
0 commit comments