Skip to content

Commit ab40b99

Browse files
authored
chore: refactoring and cleanup (#337)
1 parent e68bf72 commit ab40b99

File tree

6 files changed

+52
-39
lines changed

6 files changed

+52
-39
lines changed

src/api.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
// Licensed under the MIT License.
33

44
import {
5-
Uri,
65
Disposable,
7-
MarkdownString,
86
Event,
7+
FileChangeType,
98
LogOutputChannel,
10-
ThemeIcon,
11-
Terminal,
9+
MarkdownString,
1210
TaskExecution,
11+
Terminal,
1312
TerminalOptions,
14-
FileChangeType,
13+
ThemeIcon,
14+
Uri,
1515
} from 'vscode';
1616

1717
/**

src/common/localize.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ export namespace EnvViewStrings {
169169
export const selectedWorkspaceTooltip = l10n.t('This environment is selected for workspace files');
170170
}
171171

172-
export namespace ShellStartupActivationStrings {
172+
export namespace ActivationStrings {
173173
export const envCollectionDescription = l10n.t('Environment variables for shell activation');
174174
export const revertedShellStartupScripts = l10n.t(
175175
'Removed shell startup profile code for Python environment activation. See [logs](command:{0})',
176176
Commands.viewLogs,
177177
);
178+
export const activatingEnvironment = l10n.t('Activating environment');
178179
}

src/features/terminal/shellStartupActivationVariablesManager.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { ConfigurationChangeEvent, Disposable, GlobalEnvironmentVariableCollection } from 'vscode';
2-
import { DidChangeEnvironmentEventArgs } from '../../api';
3-
import { ShellStartupActivationStrings } from '../../common/localize';
2+
import { DidChangeEnvironmentEventArgs, PythonProjectEnvironmentApi } from '../../api';
3+
import { ActivationStrings } from '../../common/localize';
44
import { getWorkspaceFolder, getWorkspaceFolders, onDidChangeConfiguration } from '../../common/workspace.apis';
5-
import { EnvironmentManagers } from '../../internal.api';
65
import { ShellEnvsProvider } from './shells/startupProvider';
7-
import { getAutoActivationType } from './utils';
6+
import { ACT_TYPE_SHELL, getAutoActivationType } from './utils';
87

98
export interface ShellStartupActivationVariablesManager extends Disposable {
109
initialize(): Promise<void>;
@@ -15,14 +14,14 @@ export class ShellStartupActivationVariablesManagerImpl implements ShellStartupA
1514
constructor(
1615
private readonly envCollection: GlobalEnvironmentVariableCollection,
1716
private readonly shellEnvsProviders: ShellEnvsProvider[],
18-
private readonly em: EnvironmentManagers,
17+
private readonly api: PythonProjectEnvironmentApi,
1918
) {
20-
this.envCollection.description = ShellStartupActivationStrings.envCollectionDescription;
19+
this.envCollection.description = ActivationStrings.envCollectionDescription;
2120
this.disposables.push(
2221
onDidChangeConfiguration(async (e: ConfigurationChangeEvent) => {
2322
await this.handleConfigurationChange(e);
2423
}),
25-
this.em.onDidChangeEnvironmentFiltered(async (e: DidChangeEnvironmentEventArgs) => {
24+
this.api.onDidChangeEnvironment(async (e: DidChangeEnvironmentEventArgs) => {
2625
await this.handleEnvironmentChange(e);
2726
}),
2827
);
@@ -31,7 +30,7 @@ export class ShellStartupActivationVariablesManagerImpl implements ShellStartupA
3130
private async handleConfigurationChange(e: ConfigurationChangeEvent) {
3231
if (e.affectsConfiguration('python-envs.terminal.autoActivationType')) {
3332
const autoActType = getAutoActivationType();
34-
if (autoActType === 'shellStartup') {
33+
if (autoActType === ACT_TYPE_SHELL) {
3534
await this.initializeInternal();
3635
} else {
3736
const workspaces = getWorkspaceFolders() ?? [];
@@ -49,7 +48,7 @@ export class ShellStartupActivationVariablesManagerImpl implements ShellStartupA
4948

5049
private async handleEnvironmentChange(e: DidChangeEnvironmentEventArgs) {
5150
const autoActType = getAutoActivationType();
52-
if (autoActType === 'shellStartup' && e.uri) {
51+
if (autoActType === ACT_TYPE_SHELL && e.uri) {
5352
const wf = getWorkspaceFolder(e.uri);
5453
if (wf) {
5554
const envVars = this.envCollection.getScoped({ workspaceFolder: wf });
@@ -75,7 +74,7 @@ export class ShellStartupActivationVariablesManagerImpl implements ShellStartupA
7574
const collection = this.envCollection.getScoped({ workspaceFolder: workspace });
7675
promises.push(
7776
...this.shellEnvsProviders.map(async (provider) => {
78-
const env = await this.em.getEnvironment(workspace.uri);
77+
const env = await this.api.getEnvironment(workspace.uri);
7978
if (env) {
8079
provider.updateEnvVariables(collection, env);
8180
} else {
@@ -86,7 +85,7 @@ export class ShellStartupActivationVariablesManagerImpl implements ShellStartupA
8685
});
8786
await Promise.all(promises);
8887
} else {
89-
const env = await this.em.getEnvironment(undefined);
88+
const env = await this.api.getEnvironment(undefined);
9089
await Promise.all(
9190
this.shellEnvsProviders.map(async (provider) => {
9291
if (env) {
@@ -101,7 +100,7 @@ export class ShellStartupActivationVariablesManagerImpl implements ShellStartupA
101100

102101
public async initialize(): Promise<void> {
103102
const autoActType = getAutoActivationType();
104-
if (autoActType === 'shellStartup') {
103+
if (autoActType === ACT_TYPE_SHELL) {
105104
await this.initializeInternal();
106105
}
107106
}

src/features/terminal/shellStartupSetupHandlers.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { l10n, ProgressLocation } from 'vscode';
22
import { executeCommand } from '../../common/command.api';
3-
import { Common, ShellStartupActivationStrings } from '../../common/localize';
3+
import { ActivationStrings, Common } from '../../common/localize';
44
import { traceInfo, traceVerbose } from '../../common/logging';
55
import { showErrorMessage, showInformationMessage, withProgress } from '../../common/window.apis';
66
import { ShellScriptEditState, ShellStartupScriptProvider } from './shells/startupProvider';
7-
import { getAutoActivationType, setAutoActivationType } from './utils';
7+
import { ACT_TYPE_COMMAND, ACT_TYPE_SHELL, getAutoActivationType, setAutoActivationType } from './utils';
88

99
export async function handleSettingUpShellProfile(
1010
providers: ShellStartupScriptProvider[],
@@ -14,7 +14,7 @@ export async function handleSettingUpShellProfile(
1414
const response = await showInformationMessage(
1515
l10n.t(
1616
'To use "{0}" activation, the shell profiles need to be set up. Do you want to set it up now?',
17-
'shellStartup',
17+
ACT_TYPE_SHELL,
1818
),
1919
{ modal: true, detail: l10n.t('Shells: {0}', shells) },
2020
Common.yes,
@@ -59,11 +59,11 @@ export async function handleSettingUpShellProfile(
5959

6060
export async function cleanupStartupScripts(allProviders: ShellStartupScriptProvider[]): Promise<void> {
6161
await Promise.all(allProviders.map((provider) => provider.teardownScripts()));
62-
if (getAutoActivationType() === 'shellStartup') {
63-
setAutoActivationType('command');
62+
if (getAutoActivationType() === ACT_TYPE_SHELL) {
63+
setAutoActivationType(ACT_TYPE_COMMAND);
6464
traceInfo(
6565
'Setting `python-envs.terminal.autoActivationType` to `command`, after removing shell startup scripts.',
6666
);
6767
}
68-
setImmediate(async () => await showInformationMessage(ShellStartupActivationStrings.revertedShellStartupScripts));
68+
setImmediate(async () => await showInformationMessage(ActivationStrings.revertedShellStartupScripts));
6969
}

src/features/terminal/terminalManager.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fsapi from 'fs-extra';
22
import * as path from 'path';
33
import { Disposable, EventEmitter, ProgressLocation, Terminal, TerminalOptions, Uri } from 'vscode';
44
import { PythonEnvironment, PythonEnvironmentApi, PythonProject, PythonTerminalCreateOptions } from '../../api';
5+
import { ActivationStrings } from '../../common/localize';
56
import { traceInfo, traceVerbose } from '../../common/logging';
67
import {
78
createTerminal,
@@ -23,7 +24,15 @@ import {
2324
TerminalActivationInternal,
2425
TerminalEnvironment,
2526
} from './terminalActivationState';
26-
import { AutoActivationType, getAutoActivationType, getEnvironmentForTerminal, waitForShellIntegration } from './utils';
27+
import {
28+
ACT_TYPE_COMMAND,
29+
ACT_TYPE_OFF,
30+
ACT_TYPE_SHELL,
31+
AutoActivationType,
32+
getAutoActivationType,
33+
getEnvironmentForTerminal,
34+
waitForShellIntegration,
35+
} from './utils';
2736

2837
export interface TerminalCreation {
2938
create(environment: PythonEnvironment, options: PythonTerminalCreateOptions): Promise<Terminal>;
@@ -108,7 +117,7 @@ export class TerminalManagerImpl implements TerminalManager {
108117
onDidChangeConfiguration(async (e) => {
109118
if (e.affectsConfiguration('python-envs.terminal.autoActivationType')) {
110119
const actType = getAutoActivationType();
111-
if (actType === 'shellStartup') {
120+
if (actType === ACT_TYPE_SHELL) {
112121
traceInfo(`Auto activation type changed to ${actType}`);
113122
const shells = new Set(
114123
terminals()
@@ -178,10 +187,10 @@ export class TerminalManagerImpl implements TerminalManager {
178187
let isSetup = this.shellSetup.get(shellType);
179188
if (isSetup === true) {
180189
traceVerbose(`Shell profile for ${shellType} is already setup.`);
181-
return 'shellStartup';
190+
return ACT_TYPE_SHELL;
182191
} else if (isSetup === false) {
183192
traceVerbose(`Shell profile for ${shellType} is not set up, using command fallback.`);
184-
return 'command';
193+
return ACT_TYPE_COMMAND;
185194
}
186195
}
187196

@@ -197,25 +206,25 @@ export class TerminalManagerImpl implements TerminalManager {
197206
await this.handleSetupCheck(shellType);
198207

199208
// Check again after the setup check.
200-
return this.getShellActivationType(shellType) ?? 'command';
209+
return this.getShellActivationType(shellType) ?? ACT_TYPE_COMMAND;
201210
}
202211
traceInfo(`Shell startup not supported for ${shellType}, using command activation as fallback`);
203-
return 'command';
212+
return ACT_TYPE_COMMAND;
204213
}
205214

206215
private async autoActivateOnTerminalOpen(terminal: Terminal, environment: PythonEnvironment): Promise<void> {
207216
let actType = getAutoActivationType();
208217
const shellType = identifyTerminalShell(terminal);
209-
if (actType === 'shellStartup') {
218+
if (actType === ACT_TYPE_SHELL) {
210219
actType = await this.getEffectiveActivationType(shellType);
211220
}
212221

213-
if (actType === 'command') {
222+
if (actType === ACT_TYPE_COMMAND) {
214223
if (isActivatableEnvironment(environment)) {
215224
await withProgress(
216225
{
217226
location: ProgressLocation.Window,
218-
title: `Activating environment: ${environment.environmentPath.fsPath}`,
227+
title: `${ActivationStrings.activatingEnvironment}: ${environment.environmentPath.fsPath}`,
219228
},
220229
async () => {
221230
await waitForShellIntegration(terminal);
@@ -225,9 +234,9 @@ export class TerminalManagerImpl implements TerminalManager {
225234
} else {
226235
traceVerbose(`Environment ${environment.environmentPath.fsPath} is not activatable`);
227236
}
228-
} else if (actType === 'off') {
237+
} else if (actType === ACT_TYPE_OFF) {
229238
traceInfo(`"python-envs.terminal.autoActivationType" is set to "${actType}", skipping auto activation`);
230-
} else if (actType === 'shellStartup') {
239+
} else if (actType === ACT_TYPE_SHELL) {
231240
traceInfo(
232241
`"python-envs.terminal.autoActivationType" is set to "${actType}", terminal should be activated by shell startup script`,
233242
);
@@ -237,7 +246,7 @@ export class TerminalManagerImpl implements TerminalManager {
237246
public async create(environment: PythonEnvironment, options: PythonTerminalCreateOptions): Promise<Terminal> {
238247
const autoActType = getAutoActivationType();
239248
let envVars = options.env;
240-
if (autoActType === 'shellStartup') {
249+
if (autoActType === ACT_TYPE_SHELL) {
241250
const vars = await Promise.all(this.startupEnvProviders.map((p) => p.getEnvVariables(environment)));
242251

243252
vars.forEach((varMap) => {
@@ -249,6 +258,7 @@ export class TerminalManagerImpl implements TerminalManager {
249258
});
250259
}
251260

261+
// Uncomment the code line below after the issue is resolved:
252262
// https://github.com/microsoft/vscode-python-environments/issues/172
253263
// const name = options.name ?? `Python: ${environment.displayName}`;
254264
const newTerminal = createTerminal({
@@ -266,7 +276,7 @@ export class TerminalManagerImpl implements TerminalManager {
266276
isTransient: options.isTransient,
267277
});
268278

269-
if (autoActType === 'command') {
279+
if (autoActType === ACT_TYPE_COMMAND) {
270280
if (options.disableActivation) {
271281
this.skipActivationOnOpen.add(newTerminal);
272282
return newTerminal;
@@ -360,9 +370,9 @@ export class TerminalManagerImpl implements TerminalManager {
360370

361371
public async initialize(api: PythonEnvironmentApi): Promise<void> {
362372
const actType = getAutoActivationType();
363-
if (actType === 'command') {
373+
if (actType === ACT_TYPE_COMMAND) {
364374
await Promise.all(terminals().map(async (t) => this.activateUsingCommand(api, t)));
365-
} else if (actType === 'shellStartup') {
375+
} else if (actType === ACT_TYPE_SHELL) {
366376
const shells = new Set(
367377
terminals()
368378
.map((t) => identifyTerminalShell(t))

src/features/terminal/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ export async function getEnvironmentForTerminal(
9090
return env;
9191
}
9292

93+
export const ACT_TYPE_SHELL = 'shellStartup';
94+
export const ACT_TYPE_COMMAND = 'command';
95+
export const ACT_TYPE_OFF = 'off';
9396
export type AutoActivationType = 'off' | 'command' | 'shellStartup';
9497
export function getAutoActivationType(): AutoActivationType {
9598
// 'startup' auto-activation means terminal is activated via shell startup scripts.

0 commit comments

Comments
 (0)