forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathactivationManager.ts
149 lines (128 loc) · 6.52 KB
/
activationManager.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable, multiInject } from 'inversify';
import { TextDocument } from 'vscode';
import { IApplicationDiagnostics } from '../application/types';
import { IActiveResourceService, IDocumentManager, IWorkspaceService } from '../common/application/types';
import { PYTHON_LANGUAGE } from '../common/constants';
import { DeprecatePythonPath } from '../common/experiments/groups';
import { traceDecorators } from '../common/logger';
import { IFileSystem } from '../common/platform/types';
import { IDisposable, IExperimentService, IInterpreterPathService, Resource } from '../common/types';
import { Deferred } from '../common/utils/async';
import { IInterpreterAutoSelectionService } from '../interpreter/autoSelection/types';
import { IInterpreterService } from '../interpreter/contracts';
import { sendActivationTelemetry } from '../telemetry/envFileTelemetry';
import { IExtensionActivationManager, IExtensionActivationService, IExtensionSingleActivationService } from './types';
@injectable()
export class ExtensionActivationManager implements IExtensionActivationManager {
public readonly activatedWorkspaces = new Set<string>();
protected readonly isInterpreterSetForWorkspacePromises = new Map<string, Deferred<void>>();
private readonly disposables: IDisposable[] = [];
private docOpenedHandler?: IDisposable;
constructor(
@multiInject(IExtensionActivationService) private readonly activationServices: IExtensionActivationService[],
@multiInject(IExtensionSingleActivationService)
private readonly singleActivationServices: IExtensionSingleActivationService[],
@inject(IDocumentManager) private readonly documentManager: IDocumentManager,
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
@inject(IInterpreterAutoSelectionService) private readonly autoSelection: IInterpreterAutoSelectionService,
@inject(IApplicationDiagnostics) private readonly appDiagnostics: IApplicationDiagnostics,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
@inject(IFileSystem) private readonly fileSystem: IFileSystem,
@inject(IActiveResourceService) private readonly activeResourceService: IActiveResourceService,
@inject(IExperimentService) private readonly experiments: IExperimentService,
@inject(IInterpreterPathService) private readonly interpreterPathService: IInterpreterPathService,
) {}
public dispose(): void {
while (this.disposables.length > 0) {
const disposable = this.disposables.shift()!;
disposable.dispose();
}
if (this.docOpenedHandler) {
this.docOpenedHandler.dispose();
this.docOpenedHandler = undefined;
}
}
public async activate(): Promise<void> {
await this.initialize();
// Activate all activation services together.
await Promise.all([
Promise.all(this.singleActivationServices.map((item) => item.activate())),
this.activateWorkspace(this.activeResourceService.getActiveResource()),
]);
await this.autoSelection.autoSelectInterpreter(undefined);
}
@traceDecorators.error('Failed to activate a workspace')
public async activateWorkspace(resource: Resource): Promise<void> {
const key = this.getWorkspaceKey(resource);
if (this.activatedWorkspaces.has(key)) {
return;
}
this.activatedWorkspaces.add(key);
if (this.experiments.inExperimentSync(DeprecatePythonPath.experiment)) {
await this.interpreterPathService.copyOldInterpreterStorageValuesToNew(resource);
}
// Get latest interpreter list in the background.
this.interpreterService.getInterpreters(resource).ignoreErrors();
await sendActivationTelemetry(this.fileSystem, this.workspaceService, resource);
await this.autoSelection.autoSelectInterpreter(resource);
await Promise.all(this.activationServices.map((item) => item.activate(resource)));
await this.appDiagnostics.performPreStartupHealthCheck(resource);
}
public async initialize(): Promise<void> {
this.addHandlers();
this.addRemoveDocOpenedHandlers();
}
public onDocOpened(doc: TextDocument): void {
if (doc.languageId !== PYTHON_LANGUAGE) {
return;
}
const key = this.getWorkspaceKey(doc.uri);
// If we have opened a doc that does not belong to workspace, then do nothing.
if (key === '' && this.workspaceService.hasWorkspaceFolders) {
return;
}
if (this.activatedWorkspaces.has(key)) {
return;
}
const folder = this.workspaceService.getWorkspaceFolder(doc.uri);
this.activateWorkspace(folder ? folder.uri : undefined).ignoreErrors();
}
protected addHandlers(): void {
this.disposables.push(this.workspaceService.onDidChangeWorkspaceFolders(this.onWorkspaceFoldersChanged, this));
}
protected addRemoveDocOpenedHandlers(): void {
if (this.hasMultipleWorkspaces()) {
if (!this.docOpenedHandler) {
this.docOpenedHandler = this.documentManager.onDidOpenTextDocument(this.onDocOpened, this);
}
return;
}
if (this.docOpenedHandler) {
this.docOpenedHandler.dispose();
this.docOpenedHandler = undefined;
}
}
protected onWorkspaceFoldersChanged(): void {
// If an activated workspace folder was removed, delete its key
const workspaceKeys = this.workspaceService.workspaceFolders!.map((workspaceFolder) =>
this.getWorkspaceKey(workspaceFolder.uri),
);
const activatedWkspcKeys = Array.from(this.activatedWorkspaces.keys());
const activatedWkspcFoldersRemoved = activatedWkspcKeys.filter((item) => workspaceKeys.indexOf(item) < 0);
if (activatedWkspcFoldersRemoved.length > 0) {
for (const folder of activatedWkspcFoldersRemoved) {
this.activatedWorkspaces.delete(folder);
}
}
this.addRemoveDocOpenedHandlers();
}
protected hasMultipleWorkspaces(): boolean {
return this.workspaceService.hasWorkspaceFolders && this.workspaceService.workspaceFolders!.length > 1;
}
protected getWorkspaceKey(resource: Resource): string {
return this.workspaceService.getWorkspaceFolderIdentifier(resource, '');
}
}