forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
100 lines (88 loc) · 3.26 KB
/
index.ts
File metadata and controls
100 lines (88 loc) · 3.26 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as vscode from 'vscode';
import { IServiceContainer, IServiceManager } from '../ioc/types';
import { PythonEnvInfo } from './base/info';
import { ILocator, IPythonEnvsIterator, PythonLocatorQuery } from './base/locator';
import { PythonEnvsChangedEvent } from './base/watcher';
import { ExtensionLocators, WorkspaceLocators } from './discovery/locators';
import { registerForIOC } from './legacyIOC';
/**
* Activate the Python environments component (during extension activation).'
*/
export function activate(serviceManager: IServiceManager, serviceContainer: IServiceContainer) {
const [api, activateAPI] = createAPI();
registerForIOC(serviceManager, serviceContainer, api);
activateAPI();
}
/**
* The public API for the Python environments component.
*
* Note that this is composed of sub-components.
*/
export class PythonEnvironments implements ILocator {
constructor(
// These are the sub-components the full component is composed of:
private readonly locators: ILocator
) {}
public get onChanged(): vscode.Event<PythonEnvsChangedEvent> {
return this.locators.onChanged;
}
public iterEnvs(query?: PythonLocatorQuery): IPythonEnvsIterator {
return this.locators.iterEnvs(query);
}
public async resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
return this.locators.resolveEnv(env);
}
}
/**
* Initialize everything needed for the API and provide the API object.
*
* An activation function is also returned, which should be called soon.
*/
export function createAPI(): [PythonEnvironments, () => void] {
const [locators, activateLocators] = initLocators();
return [
new PythonEnvironments(locators),
() => {
activateLocators();
// Any other activation needed for the API will go here later.
}
];
}
function initLocators(): [ExtensionLocators, () => void] {
// We will add locators in similar order
// to PythonInterpreterLocatorService.getLocators().
const nonWorkspaceLocators: ILocator[] = [
// Add an ILocator object here for each non-workspace locator.
];
const workspaceLocators = new WorkspaceLocators([
// Add an ILocator factory func here for each kind of workspace-rooted locator.
]);
return [
new ExtensionLocators(nonWorkspaceLocators, workspaceLocators),
// combined activation func:
() => {
// Any non-workspace locator activation goes here.
workspaceLocators.activate(getWorkspaceFolders());
}
];
}
function getWorkspaceFolders() {
const rootAdded = new vscode.EventEmitter<vscode.Uri>();
const rootRemoved = new vscode.EventEmitter<vscode.Uri>();
vscode.workspace.onDidChangeWorkspaceFolders((event) => {
for (const root of event.removed) {
rootRemoved.fire(root.uri);
}
for (const root of event.added) {
rootAdded.fire(root.uri);
}
});
const folders = vscode.workspace.workspaceFolders;
return {
roots: folders ? folders.map((f) => f.uri) : [],
onAdded: rootAdded.event,
onRemoved: rootRemoved.event
};
}