33 CreateEnvironmentOptions ,
44 PythonEnvironment ,
55 PythonEnvironmentApi ,
6+ PythonProject ,
67 PythonProjectCreator ,
78 PythonProjectCreatorOptions ,
89} from '../api' ;
@@ -21,7 +22,7 @@ import {} from '../common/errors/utils';
2122import { pickEnvironment } from '../common/pickers/environments' ;
2223import { pickCreator , pickEnvironmentManager , pickPackageManager } from '../common/pickers/managers' ;
2324import { pickProject , pickProjectMany } from '../common/pickers/projects' ;
24- import { activeTextEditor , showErrorMessage } from '../common/window.apis' ;
25+ import { activeTextEditor , showErrorMessage , showInformationMessage } from '../common/window.apis' ;
2526import { quoteArgs } from './execution/execUtils' ;
2627import { runAsTask } from './execution/runAsTask' ;
2728import { runInTerminal } from './terminal/runInTerminal' ;
@@ -30,12 +31,10 @@ import {
3031 EnvManagerTreeItem ,
3132 EnvTreeItemKind ,
3233 GlobalProjectItem ,
33- PackageRootTreeItem ,
3434 PackageTreeItem ,
3535 ProjectEnvironment ,
3636 ProjectItem ,
3737 ProjectPackage ,
38- ProjectPackageRootTreeItem ,
3938 PythonEnvTreeItem ,
4039} from './views/treeViewItems' ;
4140
@@ -48,15 +47,25 @@ export async function refreshManagerCommand(context: unknown): Promise<void> {
4847 }
4948}
5049
51- export async function refreshPackagesCommand ( context : unknown ) {
52- if ( context instanceof ProjectPackageRootTreeItem ) {
53- const view = context as ProjectPackageRootTreeItem ;
54- const manager = view . manager ;
55- await manager . refresh ( view . environment ) ;
56- } else if ( context instanceof PackageRootTreeItem ) {
57- const view = context as PackageRootTreeItem ;
58- const manager = view . manager ;
59- await manager . refresh ( view . environment ) ;
50+ export async function refreshPackagesCommand ( context : unknown , managers ?: EnvironmentManagers ) : Promise < void > {
51+ if ( context instanceof ProjectEnvironment ) {
52+ const view = context as ProjectEnvironment ;
53+ if ( managers ) {
54+ const pkgManager = managers . getPackageManager ( view . parent . project . uri ) ;
55+ if ( pkgManager ) {
56+ await pkgManager . refresh ( view . environment ) ;
57+ }
58+ }
59+ } else if ( context instanceof PythonEnvTreeItem ) {
60+ const view = context as PythonEnvTreeItem ;
61+ const envManager =
62+ view . parent . kind === EnvTreeItemKind . environmentGroup
63+ ? view . parent . parent . manager
64+ : view . parent . manager ;
65+ const pkgManager = managers ?. getPackageManager ( envManager . preferredPackageManagerId ) ;
66+ if ( pkgManager ) {
67+ await pkgManager . refresh ( view . environment ) ;
68+ }
6069 } else {
6170 traceVerbose ( `Invalid context for refresh command: ${ context } ` ) ;
6271 }
@@ -192,7 +201,8 @@ export async function removeEnvironmentCommand(context: unknown, managers: Envir
192201export async function handlePackageUninstall ( context : unknown , em : EnvironmentManagers ) {
193202 if ( context instanceof PackageTreeItem || context instanceof ProjectPackage ) {
194203 const moduleName = context . pkg . name ;
195- const environment = context . parent . environment ;
204+ const environment =
205+ context instanceof ProjectPackage ? context . parent . environment : context . parent . environment ;
196206 const packageManager = em . getPackageManager ( environment ) ;
197207 await packageManager ?. manage ( environment , { uninstall : [ moduleName ] , install : [ ] } ) ;
198208 return ;
@@ -212,8 +222,8 @@ export async function setEnvironmentCommand(
212222 if ( projects . length > 0 ) {
213223 const selected = await pickProjectMany ( projects ) ;
214224 if ( selected && selected . length > 0 ) {
215- const uris = selected . map ( ( p ) => p . uri ) ;
216- await em . setEnvironments ( uris , view . environment ) ;
225+ // Check if the selected environment is already the current one for each project
226+ await setEnvironmentForProjects ( selected , context . environment , em ) ;
217227 }
218228 } else {
219229 await em . setEnvironments ( 'global' , view . environment ) ;
@@ -271,13 +281,47 @@ export async function setEnvironmentCommand(
271281 } ) ;
272282
273283 if ( selected ) {
274- await em . setEnvironments ( uris , selected ) ;
284+ // Use the same logic for checking already set environments
285+ await setEnvironmentForProjects ( projects , selected , em ) ;
275286 }
276287 } else {
277288 traceError ( `Invalid context for setting environment command: ${ context } ` ) ;
278289 showErrorMessage ( 'Invalid context for setting environment' ) ;
279290 }
280291}
292+ /**
293+ * Sets the environment for the given projects, showing a warning for those already set.
294+ * @param selectedProjects Array of PythonProject selected by user
295+ * @param environment The environment to set for the projects
296+ * @param em The EnvironmentManagers instance
297+ */
298+ async function setEnvironmentForProjects (
299+ selectedProjects : PythonProject [ ] ,
300+ environment : PythonEnvironment ,
301+ em : EnvironmentManagers ,
302+ ) {
303+ let alreadySet : PythonProject [ ] = [ ] ;
304+ for ( const p of selectedProjects ) {
305+ const currentEnv = await em . getEnvironment ( p . uri ) ;
306+ if ( currentEnv ?. envId . id === environment . envId . id ) {
307+ alreadySet . push ( p ) ;
308+ }
309+ }
310+ if ( alreadySet . length > 0 ) {
311+ const env = alreadySet . length > 1 ? 'environments' : 'environment' ;
312+ showInformationMessage (
313+ `"${ environment . name } " is already selected as the ${ env } for: ${ alreadySet
314+ . map ( ( p ) => `"${ p . name } "` )
315+ . join ( ', ' ) } `,
316+ ) ;
317+ }
318+ const toSet : PythonProject [ ] = selectedProjects . filter ( ( p ) => ! alreadySet . includes ( p ) ) ;
319+ const uris = toSet . map ( ( p ) => p . uri ) ;
320+ if ( uris . length === 0 ) {
321+ return ;
322+ }
323+ await em . setEnvironments ( uris , environment ) ;
324+ }
281325
282326export async function resetEnvironmentCommand (
283327 context : unknown ,
0 commit comments