@@ -667,20 +667,129 @@ class ContentDataProvider
667667 if ( ! targetUri ) {
668668 return false ;
669669 }
670-
671670 const closing = closeFileIfOpen ( item ) ;
672- if ( ! ( await closing ) ) {
671+ const closedFiles = await closing ;
672+ if ( ! closedFiles ) {
673673 return false ;
674674 }
675675
676676 const newUri = await this . model . moveTo ( item , targetUri ) ;
677- if ( closing !== true ) {
678- commands . executeCommand ( "vscode.open" , newUri ) ;
677+ if ( Array . isArray ( closedFiles ) && closedFiles . length > 0 ) {
678+ // Reopen only the files that were closed
679+ for ( const closedFileUri of closedFiles ) {
680+ // Calculate the new URI for each closed file
681+ const newFileUri = this . calculateNewFileUri (
682+ closedFileUri ,
683+ item ,
684+ newUri ,
685+ ) ;
686+ if ( newFileUri ) {
687+ commands . executeCommand ( "vscode.open" , newFileUri ) ;
688+ }
689+ }
679690 }
680691
681692 return ! ! newUri ;
682693 }
683694
695+ private calculateNewFileUri (
696+ closedFileUri : Uri ,
697+ movedItem : ContentItem ,
698+ newItemUri : boolean | Uri ,
699+ ) : Uri | null {
700+ if ( typeof newItemUri === "boolean" || ! newItemUri ) {
701+ return null ;
702+ }
703+
704+ const isFolder = getIsContainer ( movedItem ) ;
705+
706+ // If the moved item is a file and matches the closed file, return the new URI
707+ if (
708+ ! isFolder &&
709+ closedFileUri . toString ( ) === movedItem . vscUri ?. toString ( )
710+ ) {
711+ return newItemUri ;
712+ }
713+
714+ // If the moved item is a folder, calculate the new path for files within it
715+ if ( isFolder && movedItem . vscUri ) {
716+ // Extract the actual file system path from the URI query parameters
717+ const extractPathFromUri = ( uri : string ) : string => {
718+ try {
719+ // The URI has double-encoded query parameters, need to decode first
720+ const queryStart = uri . indexOf ( "?" ) ;
721+ if ( queryStart === - 1 ) {
722+ return "" ;
723+ }
724+
725+ const queryString = uri . substring ( queryStart + 1 ) ;
726+
727+ // Decode the query string first (it's URL encoded)
728+ const decodedQuery = decodeURIComponent ( queryString ) ;
729+
730+ // Now parse the decoded query for id=
731+ const idMatch = decodedQuery . match ( / i d = ( .+ ) / ) ;
732+
733+ if ( idMatch && idMatch [ 1 ] ) {
734+ const idValue = idMatch [ 1 ] ;
735+
736+ const filesIndex = idValue . indexOf ( "/files/" ) ;
737+
738+ if ( filesIndex !== - 1 ) {
739+ const path = idValue . substring ( filesIndex + 7 ) ; // +7 to skip '/files/'
740+ return path ;
741+ }
742+ }
743+
744+ return "" ;
745+ } catch {
746+ return "" ;
747+ }
748+ } ;
749+
750+ const oldBasePath = extractPathFromUri ( movedItem . vscUri . toString ( ) ) ;
751+ const closedFilePath = extractPathFromUri ( closedFileUri . toString ( ) ) ;
752+
753+ // Check if the closed file was inside the moved folder
754+ if (
755+ oldBasePath &&
756+ closedFilePath &&
757+ closedFilePath . startsWith ( oldBasePath + "~fs~" )
758+ ) {
759+ const relativePath = closedFilePath . substring ( oldBasePath . length ) ;
760+ const newUriStr = newItemUri . toString ( ) ;
761+
762+ try {
763+ // Use the working folder URI and just modify the path to include the file
764+ const newFolderUri = newUriStr ;
765+ const filename = relativePath . replace ( / ^ ~ f s ~ / , "" ) ; // Remove leading ~fs~
766+
767+ // Create a URI that matches the original working format exactly
768+ const folderQueryMatch = newFolderUri . match ( / \? ( .+ ) $ / ) ;
769+ const folderQuery = folderQueryMatch
770+ ? decodeURIComponent ( folderQueryMatch [ 1 ] )
771+ : "" ;
772+
773+ // Append filename to the files path in the query
774+ const newQuery = folderQuery . replace (
775+ / ( \/ f i l e s \/ [ ^ & ] * ) / ,
776+ `$1~fs~${ filename } ` ,
777+ ) ;
778+
779+ // Use the original working format with filename in path
780+ const newFileUri = `sasServer:/${ filename } ?${ encodeURIComponent ( newQuery ) } ` ;
781+
782+ const reconstructedUri = Uri . parse ( newFileUri ) ;
783+ return reconstructedUri ;
784+ } catch {
785+ return null ;
786+ }
787+ }
788+ }
789+
790+ return null ;
791+ }
792+
684793 private async handleContentItemDrop (
685794 target : ContentItem ,
686795 item : ContentItem ,
0 commit comments