From 8ea820e01334c39062d0b0acc2d9bf33fc57ddba Mon Sep 17 00:00:00 2001 From: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com> Date: Tue, 1 Dec 2026 02:18:18 -0800 Subject: [PATCH 1/2] fix: keep sub-paths of open tabs when a folder is renamed or moved updateUriOfAllActiveFiles rebuilt each matching tab's URI as newUrl + basename, so a nested file such as src/components/Button.js became lib/Button.js after renaming src to lib. It also matched on a bare string prefix, so renaming or deleting src also rewrote or detached tabs under src-old/ or src2/. Match with Url.isSameOrDescendant and keep the file's path relative to the renamed folder. Renaming a single open file (openFolder.renameItem) now gives the new file URL instead of new.js/old.js. --- src/utils/helpers.js | 15 ++-- tests/unit/updateUriOfAllActiveFiles.test.js | 88 ++++++++++++++++++++ 2 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 tests/unit/updateUriOfAllActiveFiles.test.js diff --git a/src/utils/helpers.js b/src/utils/helpers.js index cbc8f912d..f0c6eeb9c 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -1,6 +1,5 @@ import fsOperation from "fileSystem"; import alert from "dialogs/alert"; -import escapeStringRegexp from "escape-string-regexp"; import adRewards from "lib/adRewards"; import config from "lib/config"; import fileIcons from "lib/fileIcons"; @@ -286,17 +285,19 @@ export default { updateUriOfAllActiveFiles(oldUrl, newUrl) { const files = editorManager.files; const { url } = Url.parse(oldUrl); + const base = url.endsWith("/") ? url.slice(0, -1) : url; for (let file of files) { if (!file.uri) continue; const fileUrl = Url.parse(file.uri).url; - if (new RegExp("^" + escapeStringRegexp(url)).test(fileUrl)) { - if (newUrl) { - file.uri = Url.join(newUrl, file.filename); - } else { - file.uri = null; - } + if (!Url.isSameOrDescendant(fileUrl, base)) continue; + if (!newUrl) { + file.uri = null; + continue; } + // Keep the file's path relative to the renamed/moved folder. + const relative = fileUrl.slice(base.length + 1); + file.uri = relative ? Url.join(newUrl, relative) : newUrl; } editorManager.onupdate("file-delete"); diff --git a/tests/unit/updateUriOfAllActiveFiles.test.js b/tests/unit/updateUriOfAllActiveFiles.test.js new file mode 100644 index 000000000..ad12fa4ca --- /dev/null +++ b/tests/unit/updateUriOfAllActiveFiles.test.js @@ -0,0 +1,88 @@ +import { afterEach, expect, test, vi } from "vitest"; + +vi.mock("fileSystem", () => ({ default: vi.fn() })); +vi.mock("dialogs/alert", () => ({ default: vi.fn() })); +vi.mock("lib/adRewards", () => ({ default: {} })); +vi.mock("lib/config", () => ({ default: {} })); +vi.mock("lib/fileIcons", () => ({ default: {} })); +vi.mock("lib/startAd", () => ({ + interstitialAd: {}, + requestBannerForPage: vi.fn(), +})); + +import helpers from "utils/helpers"; + +afterEach(() => vi.unstubAllGlobals()); + +function openTabs(...uris) { + const files = uris.map((uri) => ({ + uri, + filename: uri.slice(uri.lastIndexOf("/") + 1), + })); + vi.stubGlobal("editorManager", { + files, + onupdate: vi.fn(), + emit: vi.fn(), + }); + return files; +} + +const uris = (files) => files.map((file) => file.uri); + +test("renaming a folder keeps the sub-path of nested open files", () => { + const files = openTabs( + "file:///sdcard/proj/src/index.js", + "file:///sdcard/proj/src/components/Button.js", + "file:///sdcard/proj/src/a/b/c.js", + ); + helpers.updateUriOfAllActiveFiles( + "file:///sdcard/proj/src/", + "file:///sdcard/proj/lib/", + ); + expect(uris(files)).toEqual([ + "file:///sdcard/proj/lib/index.js", + "file:///sdcard/proj/lib/components/Button.js", + "file:///sdcard/proj/lib/a/b/c.js", + ]); +}); + +test("renaming a folder leaves sibling folders that share its name prefix alone", () => { + const files = openTabs( + "file:///sdcard/proj/src/index.js", + "file:///sdcard/proj/src-old/legacy.js", + "file:///sdcard/proj/src2/main.js", + ); + helpers.updateUriOfAllActiveFiles( + "file:///sdcard/proj/src", + "file:///sdcard/proj/lib", + ); + expect(uris(files)).toEqual([ + "file:///sdcard/proj/lib/index.js", + "file:///sdcard/proj/src-old/legacy.js", + "file:///sdcard/proj/src2/main.js", + ]); +}); + +test("deleting a folder only detaches files inside it", () => { + const files = openTabs( + "file:///sdcard/proj/src/components/Button.js", + "file:///sdcard/proj/src-old/legacy.js", + ); + helpers.updateUriOfAllActiveFiles("file:///sdcard/proj/src", null); + expect(uris(files)).toEqual([null, "file:///sdcard/proj/src-old/legacy.js"]); +}); + +test("renaming an open file points its tab at the new name", () => { + const files = openTabs( + "file:///sdcard/proj/old.js", + "file:///sdcard/proj/old.json", + ); + helpers.updateUriOfAllActiveFiles( + "file:///sdcard/proj/old.js", + "file:///sdcard/proj/new.js", + ); + expect(uris(files)).toEqual([ + "file:///sdcard/proj/new.js", + "file:///sdcard/proj/old.json", + ]); +}); From 3f291ac3ecdec1c13bb0f66cb7616cd0ed5b2494 Mon Sep 17 00:00:00 2001 From: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com> Date: Tue, 1 Dec 2026 02:37:11 -0800 Subject: [PATCH 2/2] fix: detach tabs from a removed SAF storage removeStorage passes a bare SAF tree URI, whose documents are ::. Url.isSameOrDescendant only accepts a "/" boundary, so those tabs were no longer detached. Treat "::" as inside the removed root when detaching. --- src/utils/helpers.js | 10 ++++++++-- tests/unit/updateUriOfAllActiveFiles.test.js | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/utils/helpers.js b/src/utils/helpers.js index f0c6eeb9c..359b0d58f 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -290,11 +290,17 @@ export default { for (let file of files) { if (!file.uri) continue; const fileUrl = Url.parse(file.uri).url; - if (!Url.isSameOrDescendant(fileUrl, base)) continue; if (!newUrl) { - file.uri = null; + // A SAF storage root is a bare tree URI; its documents are `::`. + if ( + Url.isSameOrDescendant(fileUrl, base) || + fileUrl.startsWith(`${base}::`) + ) { + file.uri = null; + } continue; } + if (!Url.isSameOrDescendant(fileUrl, base)) continue; // Keep the file's path relative to the renamed/moved folder. const relative = fileUrl.slice(base.length + 1); file.uri = relative ? Url.join(newUrl, relative) : newUrl; diff --git a/tests/unit/updateUriOfAllActiveFiles.test.js b/tests/unit/updateUriOfAllActiveFiles.test.js index ad12fa4ca..5b24f46c8 100644 --- a/tests/unit/updateUriOfAllActiveFiles.test.js +++ b/tests/unit/updateUriOfAllActiveFiles.test.js @@ -86,3 +86,14 @@ test("renaming an open file points its tab at the new name", () => { "file:///sdcard/proj/old.json", ]); }); + +test("removing a SAF storage detaches the tabs opened from it", () => { + const tree = + "content://com.android.externalstorage.documents/tree/primary%3ATesthtml"; + const files = openTabs( + `${tree}::primary:Testhtml/Styles/index.html`, + `${tree}2::primary:Testhtml2/index.html`, + ); + helpers.updateUriOfAllActiveFiles(tree, null); + expect(uris(files)).toEqual([null, `${tree}2::primary:Testhtml2/index.html`]); +});