-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathutils.ts
More file actions
173 lines (154 loc) · 6.51 KB
/
utils.ts
File metadata and controls
173 lines (154 loc) · 6.51 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
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
import * as path from 'path';
import { Terminal, TerminalOptions, Uri } from 'vscode';
import { PythonEnvironment, PythonProject, PythonProjectEnvironmentApi, PythonProjectGetterApi } from '../../api';
import { sleep } from '../../common/utils/asyncUtils';
import { getConfiguration, getWorkspaceFolders } from '../../common/workspace.apis';
export const SHELL_INTEGRATION_TIMEOUT = 500; // 0.5 seconds
export const SHELL_INTEGRATION_POLL_INTERVAL = 20; // 0.02 seconds
export async function waitForShellIntegration(terminal: Terminal): Promise<boolean> {
let timeout = 0;
while (!terminal.shellIntegration && timeout < SHELL_INTEGRATION_TIMEOUT) {
await sleep(SHELL_INTEGRATION_POLL_INTERVAL);
timeout += SHELL_INTEGRATION_POLL_INTERVAL;
}
return terminal.shellIntegration !== undefined;
}
export function isTaskTerminal(terminal: Terminal): boolean {
// TODO: Need API for core for this https://github.com/microsoft/vscode/issues/234440
return terminal.name.toLowerCase().includes('task');
}
export function getTerminalCwd(terminal: Terminal): string | undefined {
if (terminal.shellIntegration?.cwd) {
return terminal.shellIntegration.cwd.fsPath;
}
const cwd = (terminal.creationOptions as TerminalOptions)?.cwd;
if (cwd) {
return typeof cwd === 'string' ? cwd : cwd.fsPath;
}
return undefined;
}
async function getDistinctProjectEnvs(
api: PythonProjectEnvironmentApi,
projects: readonly PythonProject[],
): Promise<PythonEnvironment[]> {
const envs: PythonEnvironment[] = [];
await Promise.all(
projects.map(async (p) => {
const e = await api.getEnvironment(p.uri);
if (e && !envs.find((x) => x.envId.id === e.envId.id)) {
envs.push(e);
}
}),
);
return envs;
}
export async function getEnvironmentForTerminal(
api: PythonProjectGetterApi & PythonProjectEnvironmentApi,
terminal?: Terminal,
): Promise<PythonEnvironment | undefined> {
let env: PythonEnvironment | undefined;
const projects = api.getPythonProjects();
if (projects.length === 0) {
env = await api.getEnvironment(undefined);
} else if (projects.length === 1) {
env = await api.getEnvironment(projects[0].uri);
} else {
const envs = await getDistinctProjectEnvs(api, projects);
if (envs.length === 1) {
// If we have only one distinct environment, then use that.
env = envs[0];
} else {
// If we have multiple distinct environments, then we can't pick one
// So skip selecting so we can try heuristic approach
env = undefined;
}
}
if (env) {
return env;
}
// This is a heuristic approach to attempt to find the environment for this terminal.
// This is not guaranteed to work, but is better than nothing.
const terminalCwd = terminal ? getTerminalCwd(terminal) : undefined;
if (terminalCwd) {
env = await api.getEnvironment(Uri.file(path.resolve(terminalCwd)));
} else {
const workspaces = getWorkspaceFolders() ?? [];
if (workspaces.length === 1) {
env = await api.getEnvironment(workspaces[0].uri);
}
}
return env;
}
export const ACT_TYPE_SHELL = 'shellStartup';
export const ACT_TYPE_COMMAND = 'command';
export const ACT_TYPE_OFF = 'off';
export type AutoActivationType = 'off' | 'command' | 'shellStartup';
/**
* Determines the auto-activation type for Python environments in terminals.
*
* The following types are supported:
* - 'shellStartup': Environment is activated via shell startup scripts
* - 'command': Environment is activated via explicit command
* - 'off': Auto-activation is disabled
*
* Priority order:
* 1. python-envs.terminal.autoActivationType
* a. globalRemoteValue
* b. globalLocalValue
* c. globalValue
* 2. python.terminal.activateEnvironment setting (if false, returns 'off' & sets autoActivationType to 'off')
* 3. Default to 'command' if no setting is found
*
* @returns {AutoActivationType} The determined auto-activation type
*/
export function getAutoActivationType(): AutoActivationType {
const pyEnvsConfig = getConfiguration('python-envs');
const pyEnvsActivationType = pyEnvsConfig.inspect<AutoActivationType>('terminal.autoActivationType');
if (pyEnvsActivationType) {
// Priority order: globalRemoteValue > globalLocalValue > globalValue
const activationType = pyEnvsActivationType as Record<string, unknown>;
if ('globalRemoteValue' in pyEnvsActivationType && activationType.globalRemoteValue !== undefined) {
return activationType.globalRemoteValue as AutoActivationType;
}
if ('globalLocalValue' in pyEnvsActivationType && activationType.globalLocalValue !== undefined) {
return activationType.globalLocalValue as AutoActivationType;
}
if (pyEnvsActivationType.globalValue !== undefined) {
return pyEnvsActivationType.globalValue;
}
}
// If none of the python-envs settings are defined, check the legacy python setting
const pythonConfig = getConfiguration('python');
const pythonActivateSetting = pythonConfig.get<boolean | undefined>('terminal.activateEnvironment', undefined);
if (pythonActivateSetting === false) {
// Set autoActivationType to 'off' if python.terminal.activateEnvironment is false
pyEnvsConfig.update('terminal.autoActivationType', ACT_TYPE_OFF);
return ACT_TYPE_OFF;
}
// Default to 'command' if no settings are found or if pythonActivateSetting is true/undefined
return ACT_TYPE_COMMAND;
}
export async function setAutoActivationType(value: AutoActivationType): Promise<void> {
const config = getConfiguration('python-envs');
return await config.update('terminal.autoActivationType', value, true);
}
export async function getAllDistinctProjectEnvironments(
api: PythonProjectGetterApi & PythonProjectEnvironmentApi,
): Promise<PythonEnvironment[] | undefined> {
const envs: PythonEnvironment[] | undefined = [];
const projects = api.getPythonProjects();
if (projects.length === 0) {
const env = await api.getEnvironment(undefined);
if (env) {
envs.push(env);
}
} else if (projects.length === 1) {
const env = await api.getEnvironment(projects[0].uri);
if (env) {
envs.push(env);
}
} else {
envs.push(...(await getDistinctProjectEnvs(api, projects)));
}
return envs.length > 0 ? envs : undefined;
}