@@ -667,20 +667,117 @@ 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+ const extractPathFromUri = ( uri : string ) : string => {
717+ try {
718+ const queryStart = uri . indexOf ( "?" ) ;
719+ if ( queryStart === - 1 ) {
720+ return "" ;
721+ }
722+
723+ const queryString = uri . substring ( queryStart + 1 ) ;
724+
725+ const decodedQuery = decodeURIComponent ( queryString ) ;
726+ const idMatch = decodedQuery . match ( / i d = ( .+ ) / ) ;
727+ if ( ! idMatch || ! idMatch [ 1 ] ) {
728+ return "" ;
729+ }
730+ const uriWithoutPrefix = idMatch [ 1 ] . replace (
731+ / \/ c o m p u t e \/ s e s s i o n s \/ [ a - z A - Z 0 - 9 - ] * \/ f i l e s \/ / ,
732+ "" ,
733+ ) ;
734+ try {
735+ return decodeURIComponent ( uriWithoutPrefix ) ;
736+ } catch {
737+ return uriWithoutPrefix ;
738+ }
739+ } catch {
740+ return "" ;
741+ }
742+ } ;
743+
744+ const oldBasePath = extractPathFromUri ( movedItem . vscUri . toString ( ) ) ;
745+ const closedFilePath = extractPathFromUri ( closedFileUri . toString ( ) ) ;
746+
747+ // Check if the closed file was inside the moved folder
748+ if (
749+ oldBasePath &&
750+ closedFilePath &&
751+ closedFilePath . startsWith ( oldBasePath + "~fs~" )
752+ ) {
753+ try {
754+ const relativePath = closedFilePath . substring ( oldBasePath . length ) ;
755+ const filename = relativePath . replace ( / ^ ~ f s ~ / , "" ) ;
756+ const newUriStr = newItemUri . toString ( ) ;
757+ // Extract and modify the query to append the filename path
758+ const queryMatch = newUriStr . match ( / \? ( .+ ) $ / ) ;
759+ if ( ! queryMatch ) {
760+ return null ;
761+ }
762+
763+ const decodedQuery = decodeURIComponent ( queryMatch [ 1 ] ) ;
764+ const newQuery = decodedQuery . replace (
765+ / ( \/ f i l e s \/ [ ^ & ] * ) / ,
766+ `$1~fs~${ filename } ` ,
767+ ) ;
768+
769+ return Uri . parse (
770+ `sasServer:/${ filename } ?${ encodeURIComponent ( newQuery ) } ` ,
771+ ) ;
772+ } catch {
773+ return null ;
774+ }
775+ }
776+ }
777+
778+ return null ;
779+ }
780+
684781 private async handleContentItemDrop (
685782 target : ContentItem ,
686783 item : ContentItem ,
0 commit comments