forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.ts
117 lines (112 loc) · 5.75 KB
/
index.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
import { inject, injectable } from 'inversify';
import { Disposable, OutputChannel, StatusBarAlignment, StatusBarItem, Uri } from 'vscode';
import { IApplicationShell, IWorkspaceService } from '../../common/application/types';
import { STANDARD_OUTPUT_CHANNEL } from '../../common/constants';
import '../../common/extensions';
import { IConfigurationService, IDisposableRegistry, IOutputChannel, IPathUtils, Resource } from '../../common/types';
import { Interpreters } from '../../common/utils/localize';
import { IServiceContainer } from '../../ioc/types';
import { PythonEnvironment } from '../../pythonEnvironments/info';
import { IInterpreterAutoSelectionService } from '../autoSelection/types';
import {
IInterpreterDisplay,
IInterpreterHelper,
IInterpreterService,
IInterpreterStatusbarVisibilityFilter,
} from '../contracts';
@injectable()
export class InterpreterDisplay implements IInterpreterDisplay {
private readonly statusBar: StatusBarItem;
private readonly helper: IInterpreterHelper;
private readonly workspaceService: IWorkspaceService;
private readonly pathUtils: IPathUtils;
private readonly interpreterService: IInterpreterService;
private readonly configService: IConfigurationService;
private currentlySelectedInterpreterPath?: string;
private currentlySelectedWorkspaceFolder: Resource;
private readonly autoSelection: IInterpreterAutoSelectionService;
private interpreterPath: string | undefined;
private statusBarCanBeDisplayed?: boolean;
private visibilityFilters: IInterpreterStatusbarVisibilityFilter[] = [];
constructor(@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer) {
this.helper = serviceContainer.get<IInterpreterHelper>(IInterpreterHelper);
this.workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
this.pathUtils = serviceContainer.get<IPathUtils>(IPathUtils);
this.interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
this.autoSelection = serviceContainer.get<IInterpreterAutoSelectionService>(IInterpreterAutoSelectionService);
const application = serviceContainer.get<IApplicationShell>(IApplicationShell);
const disposableRegistry = serviceContainer.get<Disposable[]>(IDisposableRegistry);
this.configService = serviceContainer.get<IConfigurationService>(IConfigurationService);
this.statusBar = application.createStatusBarItem(StatusBarAlignment.Left, 100);
this.statusBar.command = 'python.setInterpreter';
disposableRegistry.push(this.statusBar);
this.interpreterService.onDidChangeInterpreterInformation(
this.onDidChangeInterpreterInformation,
this,
disposableRegistry,
);
}
public async refresh(resource?: Uri) {
// Use the workspace Uri if available
if (resource && this.workspaceService.getWorkspaceFolder(resource)) {
resource = this.workspaceService.getWorkspaceFolder(resource)!.uri;
}
if (!resource) {
const wkspc = this.helper.getActiveWorkspaceUri(resource);
resource = wkspc ? wkspc.folderUri : undefined;
}
await this.updateDisplay(resource);
}
public registerVisibilityFilter(filter: IInterpreterStatusbarVisibilityFilter) {
const disposableRegistry = this.serviceContainer.get<Disposable[]>(IDisposableRegistry);
this.visibilityFilters.push(filter);
if (filter.changed) {
filter.changed(this.updateVisibility, this, disposableRegistry);
}
}
private onDidChangeInterpreterInformation(info: PythonEnvironment) {
if (!this.currentlySelectedInterpreterPath || this.currentlySelectedInterpreterPath === info.path) {
this.updateDisplay(this.currentlySelectedWorkspaceFolder).ignoreErrors();
}
}
private async updateDisplay(workspaceFolder?: Uri) {
const interpreterPath = this.configService.getSettings(workspaceFolder)?.pythonPath;
if (!interpreterPath || interpreterPath === 'python') {
await this.autoSelection.autoSelectInterpreter(workspaceFolder); // Block on this only if no interpreter selected.
}
const interpreter = await this.interpreterService.getActiveInterpreter(workspaceFolder);
this.currentlySelectedWorkspaceFolder = workspaceFolder;
if (interpreter) {
this.statusBar.color = '';
this.statusBar.tooltip = this.pathUtils.getDisplayName(interpreter.path, workspaceFolder?.fsPath);
if (this.interpreterPath !== interpreter.path) {
const output = this.serviceContainer.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
output.appendLine(
Interpreters.pythonInterpreterPath().format(
this.pathUtils.getDisplayName(interpreter.path, workspaceFolder?.fsPath),
),
);
this.interpreterPath = interpreter.path;
}
this.statusBar.text = interpreter.displayName!;
this.currentlySelectedInterpreterPath = interpreter.path;
} else {
this.statusBar.tooltip = '';
this.statusBar.color = '';
this.statusBar.text = '$(alert) Select Python Interpreter';
this.currentlySelectedInterpreterPath = undefined;
}
this.statusBarCanBeDisplayed = true;
this.updateVisibility();
}
private updateVisibility() {
if (!this.statusBarCanBeDisplayed) {
return;
}
if (this.visibilityFilters.length === 0 || this.visibilityFilters.every((filter) => !filter.hidden)) {
this.statusBar.show();
} else {
this.statusBar.hide();
}
}
}