|
3 | 3 | CreateEnvironmentOptions, |
4 | 4 | PythonEnvironment, |
5 | 5 | PythonEnvironmentApi, |
| 6 | + PythonProject, |
6 | 7 | PythonProjectCreator, |
7 | 8 | PythonProjectCreatorOptions, |
8 | 9 | } from '../api'; |
@@ -212,8 +213,8 @@ export async function setEnvironmentCommand( |
212 | 213 | if (projects.length > 0) { |
213 | 214 | const selected = await pickProjectMany(projects); |
214 | 215 | 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); |
217 | 218 | } |
218 | 219 | } else { |
219 | 220 | await em.setEnvironments('global', view.environment); |
@@ -271,23 +272,47 @@ export async function setEnvironmentCommand( |
271 | 272 | }); |
272 | 273 |
|
273 | 274 | 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); |
285 | 277 | } |
286 | 278 | } else { |
287 | 279 | traceError(`Invalid context for setting environment command: ${context}`); |
288 | 280 | showErrorMessage('Invalid context for setting environment'); |
289 | 281 | } |
290 | 282 | } |
| 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 | +} |
291 | 316 |
|
292 | 317 | export async function resetEnvironmentCommand( |
293 | 318 | context: unknown, |
|
0 commit comments