Skip to content

Commit 3a3449f

Browse files
committed
feat: use the autoActivationType setting in terminal manager
1 parent 8246a0f commit 3a3449f

4 files changed

Lines changed: 37 additions & 22 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@
9494
"enum": [
9595
"command",
9696
"startup",
97-
"none"
97+
"off"
9898
],
9999
"enumDescriptions": [
100100
"%python-envs.terminal.autoActivationType.command%",
101101
"%python-envs.terminal.autoActivationType.startup%",
102-
"%python-envs.terminal.autoActivationType.none%"
102+
"%python-envs.terminal.autoActivationType.off%"
103103
],
104104
"scope": "machine"
105105
}

package.nls.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"python-envs.terminal.autoActivationType.description": "The type of activation to use when activating an environment in the terminal",
1010
"python-envs.terminal.autoActivationType.command": "Activation by executing a command in the terminal.",
1111
"python-envs.terminal.autoActivationType.startup": "Activation by modifying the terminal shell startup script.",
12-
"python-envs.terminal.autoActivationType.none": "No automatic activation of environments.",
12+
"python-envs.terminal.autoActivationType.off": "No automatic activation of environments.",
1313
"python-envs.setEnvManager.title": "Set Environment Manager",
1414
"python-envs.setPkgManager.title": "Set Package Manager",
1515
"python-envs.addPythonProject.title": "Add Python Project",

src/features/terminal/terminalManager.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import {
1111
import { PythonEnvironment, PythonEnvironmentApi, PythonProject, PythonTerminalCreateOptions } from '../../api';
1212
import { isActivatableEnvironment } from '../common/activation';
1313
import { getConfiguration } from '../../common/workspace.apis';
14-
import { getEnvironmentForTerminal, waitForShellIntegration } from './utils';
14+
import { getAutoActivationType, getEnvironmentForTerminal, waitForShellIntegration } from './utils';
1515
import {
1616
DidChangeTerminalActivationStateEvent,
1717
TerminalActivation,
1818
TerminalActivationInternal,
1919
TerminalEnvironment,
2020
} from './terminalActivationState';
2121
import { getPythonApi } from '../pythonApi';
22+
import { traceInfo, traceVerbose } from '../../common/logging';
2223

2324
export interface TerminalCreation {
2425
create(environment: PythonEnvironment, options: PythonTerminalCreateOptions): Promise<Terminal>;
@@ -97,26 +98,33 @@ export class TerminalManagerImpl implements TerminalManager {
9798
}
9899

99100
private async autoActivateOnTerminalOpen(terminal: Terminal, environment: PythonEnvironment): Promise<void> {
100-
const config = getConfiguration('python');
101-
if (!config.get<boolean>('terminal.activateEnvironment', false)) {
102-
return;
103-
}
104-
105-
if (isActivatableEnvironment(environment)) {
106-
await withProgress(
107-
{
108-
location: ProgressLocation.Window,
109-
title: `Activating environment: ${environment.environmentPath.fsPath}`,
110-
},
111-
async () => {
112-
await waitForShellIntegration(terminal);
113-
await this.activate(terminal, environment);
114-
},
101+
const actType = getAutoActivationType();
102+
if (actType === 'command') {
103+
if (isActivatableEnvironment(environment)) {
104+
await withProgress(
105+
{
106+
location: ProgressLocation.Window,
107+
title: `Activating environment: ${environment.environmentPath.fsPath}`,
108+
},
109+
async () => {
110+
await waitForShellIntegration(terminal);
111+
await this.activate(terminal, environment);
112+
},
113+
);
114+
} else {
115+
traceVerbose(`Environment ${environment.environmentPath.fsPath} is not activatable`);
116+
}
117+
} else if (actType === 'off') {
118+
traceInfo(`"python-envs.terminal.autoActivationType" is set to "${actType}", skipping auto activation`);
119+
} else if (actType === 'startup') {
120+
traceInfo(
121+
`"python-envs.terminal.autoActivationType" is set to "${actType}", terminal should be activated by shell startup script`,
115122
);
116123
}
117124
}
118125

119126
public async create(environment: PythonEnvironment, options: PythonTerminalCreateOptions): Promise<Terminal> {
127+
// https://github.com/microsoft/vscode-python-environments/issues/172
120128
// const name = options.name ?? `Python: ${environment.displayName}`;
121129
const newTerminal = createTerminal({
122130
name: options.name,
@@ -214,8 +222,7 @@ export class TerminalManagerImpl implements TerminalManager {
214222
}
215223

216224
public async initialize(api: PythonEnvironmentApi): Promise<void> {
217-
const config = getConfiguration('python');
218-
if (config.get<boolean>('terminal.activateEnvInCurrentTerminal', false)) {
225+
if (getAutoActivationType() === 'command') {
219226
await Promise.all(
220227
terminals().map(async (t) => {
221228
this.skipActivationOnOpen.add(t);

src/features/terminal/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from 'path';
22
import { Terminal, TerminalOptions, Uri } from 'vscode';
33
import { sleep } from '../../common/utils/asyncUtils';
44
import { PythonEnvironment, PythonProject, PythonProjectEnvironmentApi, PythonProjectGetterApi } from '../../api';
5-
import { getWorkspaceFolders } from '../../common/workspace.apis';
5+
import { getConfiguration, getWorkspaceFolders } from '../../common/workspace.apis';
66

77
const SHELL_INTEGRATION_TIMEOUT = 500; // 0.5 seconds
88
const SHELL_INTEGRATION_POLL_INTERVAL = 20; // 0.02 seconds
@@ -89,3 +89,11 @@ export async function getEnvironmentForTerminal(
8989

9090
return env;
9191
}
92+
93+
export function getAutoActivationType(): 'off' | 'command' | 'startup' {
94+
// 'startup' auto-activation means terminal is activated via shell startup scripts.
95+
// 'command' auto-activation means terminal is activated via a command.
96+
// 'off' means no auto-activation.
97+
const config = getConfiguration('python-envs');
98+
return config.get<'off' | 'command' | 'startup'>('terminal.autoActivationType', 'command');
99+
}

0 commit comments

Comments
 (0)