Skip to content

Commit 802bd3b

Browse files
committed
fix: reopens files that were opened when dragging and dropping files or folders
Signed-off-by: tariku <[email protected]>
1 parent e2d53a3 commit 802bd3b

File tree

1 file changed

+101
-4
lines changed

1 file changed

+101
-4
lines changed

client/src/components/ContentNavigator/ContentDataProvider.ts

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/id=(.+)/);
727+
if (!idMatch || !idMatch[1]) {
728+
return "";
729+
}
730+
const uriWithoutPrefix = idMatch[1].replace(
731+
/\/compute\/sessions\/[a-zA-Z0-9-]*\/files\//,
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(/^~fs~/, "");
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+
/(\/files\/[^&]*)/,
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

Comments
 (0)