From 0aaf5763a125cc1b92c60034a704ebf2ba5a66ed Mon Sep 17 00:00:00 2001 From: bardrop Date: Thu, 12 Nov 2026 21:19:56 +0530 Subject: [PATCH 1/2] fix: allow replace all with an empty replacement Search in files returned early from replaceAll() when the replace field was empty, so there was no way to delete every match. Both the worker and the native replace already handle an empty string, so only the search query is required now. Fixes #3044 --- src/sidebarApps/searchInFiles/index.js | 3 +- tests/unit/searchReplaceAll.test.js | 70 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/unit/searchReplaceAll.test.js diff --git a/src/sidebarApps/searchInFiles/index.js b/src/sidebarApps/searchInFiles/index.js index 5729f1e45..336fc3ef1 100644 --- a/src/sidebarApps/searchInFiles/index.js +++ b/src/sidebarApps/searchInFiles/index.js @@ -930,7 +930,8 @@ async function replaceAll() { const search = $search.value; const replace = $replace.value; const options = getOptions(); - if (!search || !replace) return; + // An empty replacement is valid: it deletes every match. + if (!search) return; const regex = toRegex(search, options); if (!regex) return; diff --git a/tests/unit/searchReplaceAll.test.js b/tests/unit/searchReplaceAll.test.js new file mode 100644 index 000000000..fdc77e158 --- /dev/null +++ b/tests/unit/searchReplaceAll.test.js @@ -0,0 +1,70 @@ +import { readFileSync } from "node:fs"; +import vm from "node:vm"; +import { describe, expect, it, vi } from "vitest"; + +const source = readFileSync( + `${process.cwd()}/src/sidebarApps/searchInFiles/index.js`, + "utf8", +); + +function createReplaceAll({ search, replace, native = false }) { + const context = vm.createContext({ + $search: { value: search }, + $replace: { value: replace }, + filesSearched: [{ url: "file:///a.js" }, { url: "file:///b.js" }], + filesReplaced: [], + replacing: false, + activeReplaceTasks: 0, + terminateWorker: vi.fn(), + getOptions: () => ({ regExp: false }), + toRegex: (value) => new RegExp(value, "g"), + supportsNativeSearch: () => native, + sendNativeSearch: vi.fn(), + sendMessage: vi.fn(), + }); + vm.runInContext( + source.slice( + source.indexOf("async function replaceAll()"), + source.indexOf("function sendMessage("), + ), + context, + ); + return context; +} + +describe("search in files replace all", () => { + it("replaces matches with an empty string", async () => { + const context = createReplaceAll({ search: "foo", replace: "" }); + await vm.runInContext("replaceAll()", context); + + expect(context.sendMessage).toHaveBeenCalledTimes(1); + const [action, files, , , replace] = context.sendMessage.mock.calls[0]; + expect(action).toBe("replace-files"); + expect(files).toHaveLength(2); + expect(replace).toBe(""); + expect(context.replacing).toBe(true); + }); + + it("sends an empty replacement to the native search", async () => { + const context = createReplaceAll({ + search: "foo", + replace: "", + native: true, + }); + await vm.runInContext("replaceAll()", context); + + expect(context.sendNativeSearch).toHaveBeenCalledTimes(1); + expect(context.sendNativeSearch.mock.calls[0][0]).toBe("replace"); + expect(context.sendNativeSearch.mock.calls[0][4]).toBe(""); + expect(context.sendMessage).not.toHaveBeenCalled(); + }); + + it("does nothing without a search query", async () => { + const context = createReplaceAll({ search: "", replace: "bar" }); + await vm.runInContext("replaceAll()", context); + + expect(context.sendMessage).not.toHaveBeenCalled(); + expect(context.sendNativeSearch).not.toHaveBeenCalled(); + expect(context.replacing).toBe(false); + }); +}); From a71b4613a592edd294c23f51720591e0692299dd Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Thu, 12 Nov 2026 21:31:31 +0530 Subject: [PATCH 2/2] fix: keep empty replacement text for files that are not open openFile ignored supplied text when it was an empty string, so a closed file whose whole content was replaced with "" was reloaded from disk. --- src/lib/openFile.js | 4 ++-- tests/unit/openFileCancellation.test.js | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib/openFile.js b/src/lib/openFile.js index 3b8511467..c90b4f852 100644 --- a/src/lib/openFile.js +++ b/src/lib/openFile.js @@ -189,8 +189,8 @@ export default async function openFile(file, options = {}) { } } - if (text) { - // If file is not opened and has unsaved text + if (text != null) { + // If file is not opened and has unsaved text (may be "" after a replace) createEditor(true, text); return; } diff --git a/tests/unit/openFileCancellation.test.js b/tests/unit/openFileCancellation.test.js index 80f4d4b0d..5e97d5b94 100644 --- a/tests/unit/openFileCancellation.test.js +++ b/tests/unit/openFileCancellation.test.js @@ -103,6 +103,14 @@ describe("openFile cancellation", () => { expect(recents.addFile).toHaveBeenCalledWith("target"); }); + it("opens supplied empty text as unsaved instead of reading the file", async () => { + await openFile("target", { render: true, text: "" }); + expect(createEditor).toHaveBeenCalledOnce(); + expect(manager.activeFile.text).toBe(""); + expect(manager.activeFile.isUnsaved).toBe(true); + expect(readFile).not.toHaveBeenCalled(); + }); + it.each([ "pdf", "docx",