forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
extensionActivation.ts
194 lines (156 loc) · 10.2 KB
/
extensionActivation.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { CodeActionKind, debug, DebugConfigurationProvider, languages, OutputChannel, window } from 'vscode';
import { registerTypes as activationRegisterTypes } from './activation/serviceRegistry';
import { IExtensionActivationManager, ILanguageServerExtension } from './activation/types';
import { registerTypes as appRegisterTypes } from './application/serviceRegistry';
import { IApplicationDiagnostics } from './application/types';
import { DebugService } from './common/application/debugService';
import { IApplicationEnvironment, ICommandManager } from './common/application/types';
import { Commands, PYTHON, PYTHON_LANGUAGE, STANDARD_OUTPUT_CHANNEL, UseProposedApi } from './common/constants';
import { registerTypes as installerRegisterTypes } from './common/installer/serviceRegistry';
import { IFileSystem } from './common/platform/types';
import { IConfigurationService, IDisposableRegistry, IExtensions, IOutputChannel } from './common/types';
import { noop } from './common/utils/misc';
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 { IInterpreterService } from './interpreter/contracts';
import { getLanguageConfiguration } from './language/languageConfiguration';
import { LinterCommands } from './linters/linterCommands';
import { registerTypes as lintersRegisterTypes } from './linters/serviceRegistry';
import { setLoggingLevel } from './logging';
import { PythonCodeActionProvider } from './providers/codeActionProvider/pythonCodeActionProvider';
import { PythonFormattingEditProvider } from './providers/formatProvider';
import { ReplProvider } from './providers/replProvider';
import { registerTypes as providersRegisterTypes } from './providers/serviceRegistry';
import { TerminalProvider } from './providers/terminalProvider';
import { ISortImportsEditingProvider } from './providers/types';
import { setExtensionInstallTelemetryProperties } from './telemetry/extensionInstallTelemetry';
import { registerTypes as tensorBoardRegisterTypes } from './tensorBoard/serviceRegistry';
import { registerTypes as commonRegisterTerminalTypes } from './terminals/serviceRegistry';
import { ICodeExecutionManager, ITerminalAutoActivation } from './terminals/types';
import { registerTypes as unitTestsRegisterTypes } from './testing/serviceRegistry';
import { registerTypes as interpretersRegisterTypes } from './interpreter/serviceRegistry';
// components
import * as pythonEnvironments from './pythonEnvironments';
import { ActivationResult, ExtensionState } from './components';
import { Components } from './extensionInit';
import { setDefaultLanguageServer } from './activation/common/defaultlanguageServer';
import { getLoggingLevel } from './logging/settings';
export async function activateComponents(
// `ext` is passed to any extra activation funcs.
ext: ExtensionState,
components: Components,
): Promise<ActivationResult[]> {
// Note that each activation returns a promise that resolves
// when that activation completes. However, it might have started
// some non-critical background operations that do not block
// extension activation but do block use of the extension "API".
// Each component activation can't just resolve an "inner" promise
// for those non-critical operations because `await` (and
// `Promise.all()`, etc.) will flatten nested promises. Thus
// activation resolves `ActivationResult`, which can safely wrap
// the "inner" promise.
// TODO: As of now activateLegacy() registers various classes which might
// be required while activating components. Once registration from
// activateLegacy() are moved before we activate other components, we can
// activate them in parallel with the other components.
// https://github.com/microsoft/vscode-python/issues/15380
// These will go away eventually once everything is refactored into components.
const legacyActivationResult = await activateLegacy(ext);
const promises: Promise<ActivationResult>[] = [
// More component activations will go here
pythonEnvironments.activate(components.pythonEnvs, ext),
];
return Promise.all([legacyActivationResult, ...promises]);
}
/// //////////////////////////
// old activation code
// TODO: Gradually move simple initialization
// and DI registration currently in this function over
// to initializeComponents(). Likewise with complex
// init and activation: move them to activateComponents().
// See https://github.com/microsoft/vscode-python/issues/10454.
async function activateLegacy(ext: ExtensionState): Promise<ActivationResult> {
const { context, legacyIOC } = ext;
const { serviceManager, serviceContainer } = legacyIOC;
// register "services"
// We need to setup this property before any telemetry is sent
const fs = serviceManager.get<IFileSystem>(IFileSystem);
await setExtensionInstallTelemetryProperties(fs);
const applicationEnv = serviceManager.get<IApplicationEnvironment>(IApplicationEnvironment);
const { enableProposedApi } = applicationEnv.packageJson;
serviceManager.addSingletonInstance<boolean>(UseProposedApi, enableProposedApi);
// Feature specific registrations.
unitTestsRegisterTypes(serviceManager);
lintersRegisterTypes(serviceManager);
interpretersRegisterTypes(serviceManager);
formattersRegisterTypes(serviceManager);
installerRegisterTypes(serviceManager);
commonRegisterTerminalTypes(serviceManager);
debugConfigurationRegisterTypes(serviceManager);
tensorBoardRegisterTypes(serviceManager);
const extensions = serviceContainer.get<IExtensions>(IExtensions);
await setDefaultLanguageServer(extensions, serviceManager);
// Note we should not trigger any extension related code which logs, until we have set logging level. So we cannot
// use configurations service to get level setting. Instead, we use Workspace service to query for setting as it
// directly queries VSCode API.
setLoggingLevel(getLoggingLevel());
// `IConfigurationService` may depend any of the registered types, so doing it after all registrations are finished.
const configuration = serviceManager.get<IConfigurationService>(IConfigurationService);
const languageServerType = configuration.getSettings().languageServer;
// Language feature registrations.
appRegisterTypes(serviceManager, languageServerType);
providersRegisterTypes(serviceManager);
activationRegisterTypes(serviceManager, languageServerType);
// "initialize" "services"
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()));
cmdManager.executeCommand('setContext', 'python.vscode.channel', applicationEnv.channel).then(noop, noop);
serviceContainer.get<IApplicationDiagnostics>(IApplicationDiagnostics).register();
serviceContainer.get<ILanguageServerExtension>(ILanguageServerExtension).register();
// "activate" everything else
const manager = serviceContainer.get<IExtensionActivationManager>(IExtensionActivationManager);
context.subscriptions.push(manager);
// Settings are dependent on Experiment service, so we need to initialize it after experiments are activated.
serviceContainer.get<IConfigurationService>(IConfigurationService).getSettings().initialize();
const activationPromise = manager.activate();
serviceManager.get<ITerminalAutoActivation>(ITerminalAutoActivation).register();
const pythonSettings = configuration.getSettings();
const sortImports = serviceContainer.get<ISortImportsEditingProvider>(ISortImportsEditingProvider);
sortImports.registerCommands();
serviceManager.get<ICodeExecutionManager>(ICodeExecutionManager).registerCommands();
context.subscriptions.push(new LinterCommands(serviceManager));
languages.setLanguageConfiguration(PYTHON_LANGUAGE, getLanguageConfiguration());
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));
}
context.subscriptions.push(new ReplProvider(serviceContainer));
const terminalProvider = new TerminalProvider(serviceContainer);
terminalProvider.initialize(window.activeTerminal).ignoreErrors();
context.subscriptions.push(terminalProvider);
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();
return { fullyReady: activationPromise };
}