Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -286,17 +285,25 @@ 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 {
if (!newUrl) {
// A SAF storage root is a bare tree URI; its documents are `<tree>::<docId>`.
if (
Url.isSameOrDescendant(fileUrl, base) ||
fileUrl.startsWith(`${base}::`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Unrelated tabs lose their location

The new :: match applies to ordinary folders, not just SAF tree roots. Deleting sftp://profile/project/src also detaches a tab at sftp://profile/project/src::backup/index.js, even though that separate folder still exists. SFTP listings preserve these valid server filenames. The tab becomes marked deleted and loses its save location.

Limit this extra match to bare SAF tree URIs, and add a test that leaves a non-SAF sibling containing :: attached.

) {
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;
}

editorManager.onupdate("file-delete");
Expand Down
99 changes: 99 additions & 0 deletions tests/unit/updateUriOfAllActiveFiles.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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",
]);
});

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`]);
});