Skip to content

Commit ec8a02d

Browse files
committed
update to have more sophisticated method of warning msg
1 parent 2c9a0d7 commit ec8a02d

1 file changed

Lines changed: 38 additions & 13 deletions

File tree

src/features/envCommands.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CreateEnvironmentOptions,
44
PythonEnvironment,
55
PythonEnvironmentApi,
6+
PythonProject,
67
PythonProjectCreator,
78
PythonProjectCreatorOptions,
89
} from '../api';
@@ -212,8 +213,8 @@ export async function setEnvironmentCommand(
212213
if (projects.length > 0) {
213214
const selected = await pickProjectMany(projects);
214215
if (selected && selected.length > 0) {
215-
const uris = selected.map((p) => p.uri);
216-
await em.setEnvironments(uris, view.environment);
216+
// Check if the selected environment is already the current one for each project
217+
await setEnvironmentForProjects(selected, context.environment, em);
217218
}
218219
} else {
219220
await em.setEnvironments('global', view.environment);
@@ -271,23 +272,47 @@ export async function setEnvironmentCommand(
271272
});
272273

273274
if (selected) {
274-
// Check if the selected environment is already the current one
275-
// Only show notification when we have exactly one project and it already has this environment
276-
if (uris.length === 1) {
277-
const currentEnv = await em.getEnvironment(uris[0]);
278-
if (currentEnv?.envId.id === selected.envId.id) {
279-
// The environment is already selected for this project
280-
showInformationMessage('This environment is already selected for the workspace.');
281-
return;
282-
}
283-
}
284-
await em.setEnvironments(uris, selected);
275+
// Use the same logic for checking already set environments
276+
await setEnvironmentForProjects(projects, selected, em);
285277
}
286278
} else {
287279
traceError(`Invalid context for setting environment command: ${context}`);
288280
showErrorMessage('Invalid context for setting environment');
289281
}
290282
}
283+
/**
284+
* Sets the environment for the given projects, showing a warning for those already set.
285+
* @param selectedProjects Array of project items (with .uri and .name)
286+
* @param environment The environment to set
287+
* @param em The EnvironmentManagers instance
288+
*/
289+
async function setEnvironmentForProjects(
290+
selectedProjects: PythonProject[],
291+
environment: PythonEnvironment,
292+
em: EnvironmentManagers,
293+
) {
294+
let alreadySet: PythonProject[] = [];
295+
for (const p of selectedProjects) {
296+
const currentEnv = await em.getEnvironment(p.uri);
297+
if (currentEnv?.envId.id === environment.envId.id) {
298+
alreadySet.push(p);
299+
}
300+
}
301+
if (alreadySet.length > 0) {
302+
const env = alreadySet.length > 1 ? 'environments' : 'environment';
303+
showInformationMessage(
304+
`"${environment.name}" is already selected as the ${env} for: ${alreadySet
305+
.map((p) => `"${p.name}"`)
306+
.join(', ')}`,
307+
);
308+
}
309+
const toSet: PythonProject[] = selectedProjects.filter((p) => !alreadySet.includes(p));
310+
const uris = toSet.map((p) => p.uri);
311+
if (uris.length === 0) {
312+
return;
313+
}
314+
await em.setEnvironments(uris, environment);
315+
}
291316

292317
export async function resetEnvironmentCommand(
293318
context: unknown,

0 commit comments

Comments
 (0)