@@ -234,9 +234,8 @@ export class ConnectionConfig implements IConnectionConfig {
234234 */
235235 public async removeConnection ( profile : IConnectionProfile ) : Promise < boolean > {
236236 // Determine if this is a workspace connection
237- const workspaceGroupId = this . getWorkspaceConnectionsGroupId ( ) ;
238237 let target = ConfigurationTarget . Global ;
239- if ( profile . groupId === workspaceGroupId ) {
238+ if ( profile . scope === "workspace" ) {
240239 target = ConfigurationTarget . Workspace ;
241240 }
242241 let profiles = this . getConnectionsFromSettings ( target ) ;
@@ -349,8 +348,11 @@ export class ConnectionConfig implements IConnectionConfig {
349348 id : string ,
350349 contentAction : "delete" | "move" = "delete" ,
351350 ) : Promise < boolean > {
352- const connections = this . getConnectionsFromSettings ( ) ;
353- const groups = this . getGroupsFromSettings ( ) ;
351+ // Get all connections and groups from both user and workspace
352+ const userConnections = this . getConnectionsFromSettings ( ConfigurationTarget . Global ) ;
353+ const workspaceConnections = this . getConnectionsFromSettings ( ConfigurationTarget . Workspace ) ;
354+ const allConnections = [ ...userConnections , ...workspaceConnections ] ;
355+ const groups = this . getAllConnectionGroups ( ) ;
354356 const rootGroup = this . getRootGroup ( ) ;
355357
356358 if ( ! rootGroup ) {
@@ -371,18 +373,34 @@ export class ConnectionConfig implements IConnectionConfig {
371373 } ;
372374
373375 let connectionModified = false ;
374- let remainingConnections : IConnectionProfile [ ] ;
375- let remainingGroups : IConnectionGroup [ ] ;
376+ let remainingUserConnections : IConnectionProfile [ ] = userConnections . slice ( ) ;
377+ let remainingWorkspaceConnections : IConnectionProfile [ ] = workspaceConnections . slice ( ) ;
378+ let remainingUserGroups : IConnectionGroup [ ] = this . getGroupsFromSettings (
379+ ConfigurationTarget . Global ,
380+ ) . slice ( ) ;
381+ let remainingWorkspaceGroups : IConnectionGroup [ ] = this . getGroupsFromSettings (
382+ ConfigurationTarget . Workspace ,
383+ ) . slice ( ) ;
376384
377385 if ( contentAction === "delete" ) {
378386 // Get all nested subgroups to remove
379387 const groupsToRemove = getAllSubgroupIds ( id ) ;
380388
381389 // Remove all connections in the groups being removed
382- remainingConnections = connections . filter ( ( conn ) => {
390+ remainingUserConnections = remainingUserConnections . filter ( ( conn ) => {
383391 if ( groupsToRemove . has ( conn . groupId ) ) {
384392 this . _logger . verbose (
385- `Removing connection '${ conn . id } ' because its group '${ conn . groupId } ' was removed` ,
393+ `Removing user connection '${ conn . id } ' because its group '${ conn . groupId } ' was removed` ,
394+ ) ;
395+ connectionModified = true ;
396+ return false ;
397+ }
398+ return true ;
399+ } ) ;
400+ remainingWorkspaceConnections = remainingWorkspaceConnections . filter ( ( conn ) => {
401+ if ( groupsToRemove . has ( conn . groupId ) ) {
402+ this . _logger . verbose (
403+ `Removing workspace connection '${ conn . id } ' because its group '${ conn . groupId } ' was removed` ,
386404 ) ;
387405 connectionModified = true ;
388406 return false ;
@@ -391,14 +409,27 @@ export class ConnectionConfig implements IConnectionConfig {
391409 } ) ;
392410
393411 // Remove all groups that were marked for removal
394- remainingGroups = groups . filter ( ( g ) => ! groupsToRemove . has ( g . id ) ) ;
412+ remainingUserGroups = remainingUserGroups . filter ( ( g ) => ! groupsToRemove . has ( g . id ) ) ;
413+ remainingWorkspaceGroups = remainingWorkspaceGroups . filter (
414+ ( g ) => ! groupsToRemove . has ( g . id ) ,
415+ ) ;
395416 } else {
396417 // Move immediate child connections and groups to User Connections group
397418 const userGroupId = this . getUserConnectionsGroupId ( ) ;
398- remainingConnections = connections . map ( ( conn ) => {
419+ remainingUserConnections = remainingUserConnections . map ( ( conn ) => {
420+ if ( conn . groupId === id ) {
421+ this . _logger . verbose (
422+ `Moving user connection '${ conn . id } ' to User Connections group because its immediate parent group '${ id } ' was removed` ,
423+ ) ;
424+ connectionModified = true ;
425+ return { ...conn , groupId : userGroupId } ;
426+ }
427+ return conn ;
428+ } ) ;
429+ remainingWorkspaceConnections = remainingWorkspaceConnections . map ( ( conn ) => {
399430 if ( conn . groupId === id ) {
400431 this . _logger . verbose (
401- `Moving connection '${ conn . id } ' to User Connections group because its immediate parent group '${ id } ' was removed` ,
432+ `Moving workspace connection '${ conn . id } ' to User Connections group because its immediate parent group '${ id } ' was removed` ,
402433 ) ;
403434 connectionModified = true ;
404435 return { ...conn , groupId : userGroupId } ;
@@ -407,30 +438,58 @@ export class ConnectionConfig implements IConnectionConfig {
407438 } ) ;
408439
409440 // First remove the target group
410- remainingGroups = groups . filter ( ( g ) => g . id !== id ) ;
441+ remainingUserGroups = remainingUserGroups . filter ( ( g ) => g . id !== id ) ;
442+ remainingWorkspaceGroups = remainingWorkspaceGroups . filter ( ( g ) => g . id !== id ) ;
411443
412444 // Then reparent immediate children to User Connections group
413- remainingGroups = remainingGroups . map ( ( g ) => {
445+ remainingUserGroups = remainingUserGroups . map ( ( g ) => {
414446 if ( g . parentId === id ) {
415447 this . _logger . verbose (
416- `Moving group '${ g . id } ' to User Connections group because its immediate parent group '${ id } ' was removed` ,
448+ `Moving user group '${ g . id } ' to User Connections group because its immediate parent group '${ id } ' was removed` ,
449+ ) ;
450+ return { ...g , parentId : userGroupId } ;
451+ }
452+ return g ;
453+ } ) ;
454+ remainingWorkspaceGroups = remainingWorkspaceGroups . map ( ( g ) => {
455+ if ( g . parentId === id ) {
456+ this . _logger . verbose (
457+ `Moving workspace group '${ g . id } ' to User Connections group because its immediate parent group '${ id } ' was removed` ,
417458 ) ;
418459 return { ...g , parentId : userGroupId } ;
419460 }
420461 return g ;
421462 } ) ;
422463 }
423464
424- if ( remainingGroups . length === groups . length ) {
465+ // If no group was removed, return false
466+ const originalUserGroups = this . getGroupsFromSettings ( ConfigurationTarget . Global ) ;
467+ const originalWorkspaceGroups = this . getGroupsFromSettings ( ConfigurationTarget . Workspace ) ;
468+ if (
469+ remainingUserGroups . length === originalUserGroups . length &&
470+ remainingWorkspaceGroups . length === originalWorkspaceGroups . length
471+ ) {
425472 this . _logger . error ( `Connection group with ID '${ id } ' not found when removing.` ) ;
426473 return false ;
427474 }
428475
476+ // Write updated connections and groups to correct settings
429477 if ( connectionModified ) {
430- await this . writeConnectionsToSettings ( remainingConnections ) ;
478+ await this . writeConnectionsToSettings (
479+ remainingUserConnections ,
480+ ConfigurationTarget . Global ,
481+ ) ;
482+ await this . writeConnectionsToSettings (
483+ remainingWorkspaceConnections ,
484+ ConfigurationTarget . Workspace ,
485+ ) ;
431486 }
432487
433- await this . writeConnectionGroupsToSettings ( remainingGroups ) ;
488+ await this . writeConnectionGroupsToSettings ( remainingUserGroups ) ;
489+ await this . writeConnectionGroupsToSettingsWithTarget (
490+ remainingWorkspaceGroups ,
491+ ConfigurationTarget . Workspace ,
492+ ) ;
434493 return true ;
435494 }
436495
0 commit comments