@@ -272,7 +272,7 @@ export class PythonEnvironmentManagers implements EnvironmentManagers {
272272 }
273273 }
274274
275- public async setEnvironments ( scope : Uri [ ] , environment ?: PythonEnvironment ) : Promise < void > {
275+ public async setEnvironments ( scope : Uri [ ] | string , environment ?: PythonEnvironment ) : Promise < void > {
276276 if ( environment ) {
277277 const manager = this . managers . find ( ( m ) => m . id === environment . envId . managerId ) ;
278278 if ( ! manager ) {
@@ -287,50 +287,87 @@ export class PythonEnvironmentManagers implements EnvironmentManagers {
287287 const promises : Promise < void > [ ] = [ ] ;
288288 const settings : EditAllManagerSettings [ ] = [ ] ;
289289 const events : DidChangeEnvironmentEventArgs [ ] = [ ] ;
290- scope . forEach ( ( uri ) => {
291- const m = this . getEnvironmentManager ( uri ) ;
292- promises . push ( manager . set ( uri , environment ) ) ;
290+ if ( Array . isArray ( scope ) && scope . every ( ( s ) => s instanceof Uri ) ) {
291+ scope . forEach ( ( uri ) => {
292+ const m = this . getEnvironmentManager ( uri ) ;
293+ promises . push ( manager . set ( uri , environment ) ) ;
294+ if ( manager . id !== m ?. id ) {
295+ settings . push ( {
296+ project : this . pm . get ( uri ) ,
297+ envManager : manager . id ,
298+ packageManager : manager . preferredPackageManagerId ,
299+ } ) ;
300+ }
301+
302+ const project = this . pm . get ( uri ) ;
303+ const oldEnv = this . _previousEnvironments . get ( project ?. uri . toString ( ) ?? 'global' ) ;
304+ if ( oldEnv ?. envId . id !== environment ?. envId . id ) {
305+ this . _previousEnvironments . set ( project ?. uri . toString ( ) ?? 'global' , environment ) ;
306+ events . push ( { uri : project ?. uri , new : environment , old : oldEnv } ) ;
307+ }
308+ } ) ;
309+ } else if ( typeof scope === 'string' && scope === 'global' ) {
310+ const m = this . getEnvironmentManager ( undefined ) ;
311+ promises . push ( manager . set ( undefined , environment ) ) ;
293312 if ( manager . id !== m ?. id ) {
294313 settings . push ( {
295- project : this . pm . get ( uri ) ,
314+ project : undefined ,
296315 envManager : manager . id ,
297316 packageManager : manager . preferredPackageManagerId ,
298317 } ) ;
299318 }
300319
301- const project = this . pm . get ( uri ) ;
302- const oldEnv = this . _previousEnvironments . get ( project ?. uri . toString ( ) ?? 'global' ) ;
320+ const oldEnv = this . _previousEnvironments . get ( 'global' ) ;
303321 if ( oldEnv ?. envId . id !== environment ?. envId . id ) {
304- this . _previousEnvironments . set ( project ?. uri . toString ( ) ?? 'global' , environment ) ;
305- events . push ( { uri : project ?. uri , new : environment , old : oldEnv } ) ;
322+ this . _previousEnvironments . set ( 'global' , environment ) ;
323+ events . push ( { uri : undefined , new : environment , old : oldEnv } ) ;
306324 }
307- } ) ;
325+ }
308326 await Promise . all ( promises ) ;
309327 await setAllManagerSettings ( settings ) ;
310328 setImmediate ( ( ) => events . forEach ( ( e ) => this . _onDidChangeEnvironmentFiltered . fire ( e ) ) ) ;
311329 } else {
312330 const promises : Promise < void > [ ] = [ ] ;
313331 const events : DidChangeEnvironmentEventArgs [ ] = [ ] ;
314- scope . forEach ( ( uri ) => {
315- const manager = this . getEnvironmentManager ( uri ) ;
332+ if ( Array . isArray ( scope ) && scope . every ( ( s ) => s instanceof Uri ) ) {
333+ scope . forEach ( ( uri ) => {
334+ const manager = this . getEnvironmentManager ( uri ) ;
335+ if ( manager ) {
336+ const setAndAddEvent = async ( ) => {
337+ await manager . set ( uri ) ;
338+
339+ const project = this . pm . get ( uri ) ;
340+
341+ // Always get the new first, then compare with the old. This has minor impact on the ordering of
342+ // events. But it ensures that we always get the latest environment at the time of this call.
343+ const newEnv = await manager . get ( uri ) ;
344+ const oldEnv = this . _previousEnvironments . get ( project ?. uri . toString ( ) ?? 'global' ) ;
345+ if ( oldEnv ?. envId . id !== newEnv ?. envId . id ) {
346+ this . _previousEnvironments . set ( project ?. uri . toString ( ) ?? 'global' , newEnv ) ;
347+ events . push ( { uri : project ?. uri , new : newEnv , old : oldEnv } ) ;
348+ }
349+ } ;
350+ promises . push ( setAndAddEvent ( ) ) ;
351+ }
352+ } ) ;
353+ } else if ( typeof scope === 'string' && scope === 'global' ) {
354+ const manager = this . getEnvironmentManager ( undefined ) ;
316355 if ( manager ) {
317356 const setAndAddEvent = async ( ) => {
318- await manager . set ( uri ) ;
319-
320- const project = this . pm . get ( uri ) ;
357+ await manager . set ( undefined ) ;
321358
322359 // Always get the new first, then compare with the old. This has minor impact on the ordering of
323360 // events. But it ensures that we always get the latest environment at the time of this call.
324- const newEnv = await manager . get ( uri ) ;
325- const oldEnv = this . _previousEnvironments . get ( project ?. uri . toString ( ) ?? 'global' ) ;
361+ const newEnv = await manager . get ( undefined ) ;
362+ const oldEnv = this . _previousEnvironments . get ( 'global' ) ;
326363 if ( oldEnv ?. envId . id !== newEnv ?. envId . id ) {
327- this . _previousEnvironments . set ( project ?. uri . toString ( ) ?? 'global' , newEnv ) ;
328- events . push ( { uri : project ?. uri , new : newEnv , old : oldEnv } ) ;
364+ this . _previousEnvironments . set ( 'global' , newEnv ) ;
365+ events . push ( { uri : undefined , new : newEnv , old : oldEnv } ) ;
329366 }
330367 } ;
331368 promises . push ( setAndAddEvent ( ) ) ;
332369 }
333- } ) ;
370+ }
334371 await Promise . all ( promises ) ;
335372 setImmediate ( ( ) => events . forEach ( ( e ) => this . _onDidChangeEnvironmentFiltered . fire ( e ) ) ) ;
336373 }
0 commit comments