Skip to content

Commit 522ffac

Browse files
Add notification when re-selecting the same workspace environment (#441)
fixes #58 ## Problem When a user selects "Set As Workspace Environment" for an environment that's already selected as the workspace environment, there's no feedback provided to the user. This can be confusing, as it appears that nothing happens when clicking the button. ## Solution This PR adds feedback to users when they attempt to select an environment that's already set as the workspace environment: 1. When a user has one folder/project open and tries to set the same environment that's already selected 2. The user will now see an information message: "This environment is already selected for the workspace." but its more verbose Fixes #58 and refined by @eleanorjboyd --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: eleanorjboyd <[email protected]>
1 parent e9e1e38 commit 522ffac

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/features/envCommands.ts

Lines changed: 39 additions & 4 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';
@@ -21,7 +22,7 @@ import {} from '../common/errors/utils';
2122
import { pickEnvironment } from '../common/pickers/environments';
2223
import { pickCreator, pickEnvironmentManager, pickPackageManager } from '../common/pickers/managers';
2324
import { pickProject, pickProjectMany } from '../common/pickers/projects';
24-
import { activeTextEditor, showErrorMessage } from '../common/window.apis';
25+
import { activeTextEditor, showErrorMessage, showInformationMessage } from '../common/window.apis';
2526
import { quoteArgs } from './execution/execUtils';
2627
import { runAsTask } from './execution/runAsTask';
2728
import { runInTerminal } from './terminal/runInTerminal';
@@ -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,13 +272,47 @@ export async function setEnvironmentCommand(
271272
});
272273

273274
if (selected) {
274-
await em.setEnvironments(uris, selected);
275+
// Use the same logic for checking already set environments
276+
await setEnvironmentForProjects(projects, selected, em);
275277
}
276278
} else {
277279
traceError(`Invalid context for setting environment command: ${context}`);
278280
showErrorMessage('Invalid context for setting environment');
279281
}
280282
}
283+
/**
284+
* Sets the environment for the given projects, showing a warning for those already set.
285+
* @param selectedProjects Array of PythonProject selected by user
286+
* @param environment The environment to set for the projects
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+
}
281316

282317
export async function resetEnvironmentCommand(
283318
context: unknown,

0 commit comments

Comments
 (0)