Skip to content

Commit d276abd

Browse files
committed
fix(search): update open tabs in the background on replace all
Replace all calls openFile with render: false for every file but the last. For a file that was already open, openFile ignored render and always made the tab active before writing the new text, so a large replace switched through every open tab. Now a loaded, already open tab gets its text through its session without being activated when render is false.
1 parent 2745418 commit d276abd

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎src/lib/openFile.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ export default async function openFile(file, options = {}) {
6363
const incomingDoc =
6464
text != null ? Text.of(String(text).split("\n")) : null;
6565

66+
// Background update (e.g. replace all in files): change the text of an
67+
// open, loaded tab without switching to it.
68+
if (
69+
render === false &&
70+
incomingDoc &&
71+
!paneId &&
72+
!cursorPos &&
73+
!encoding &&
74+
existingFile.loaded &&
75+
existingFile.session
76+
) {
77+
if (onsave) existingFile.onsave = onsave;
78+
if (!existingFile.session.doc?.eq?.(incomingDoc)) {
79+
existingFile.session.setValue(String(text));
80+
}
81+
return;
82+
}
83+
6684
// If file is already opened
6785
const targetPane = paneId
6886
? editorManager.panes?.find((pane) => pane.id === paneId)

‎tests/unit/openFileCancellation.test.js‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "node:fs";
2+
import { Text } from "@codemirror/state";
23
import ts from "typescript";
34
import { beforeEach, describe, expect, it, vi } from "vitest";
45
import { isBinaryFile } from "../../src/utils/binaryExtensions";
@@ -63,7 +64,7 @@ describe("openFile cancellation", () => {
6364
});
6465
const dependencies = {
6566
fileSystem: { default: () => ({ stat, readFile }) },
66-
"@codemirror/state": {},
67+
"@codemirror/state": { Text },
6768
"components/audioPlayer": {},
6869
"dialogs/alert": {},
6970
"dialogs/confirm": {},
@@ -103,6 +104,42 @@ describe("openFile cancellation", () => {
103104
expect(recents.addFile).toHaveBeenCalledWith("target");
104105
});
105106

107+
function openTab(text) {
108+
const file = {
109+
loaded: true,
110+
makeActive: vi.fn(),
111+
session: {
112+
doc: Text.of(text.split("\n")),
113+
setValue: vi.fn(),
114+
},
115+
};
116+
manager.getFile.mockReturnValue(file);
117+
manager.editor = { dispatch: vi.fn(), state: { doc: { length: 0 } } };
118+
return file;
119+
}
120+
121+
it("updates an open tab in the background without switching to it", async () => {
122+
const file = openTab("foo");
123+
await openFile("target", { render: false, text: "bar" });
124+
expect(file.makeActive).not.toHaveBeenCalled();
125+
expect(file.session.setValue).toHaveBeenCalledWith("bar");
126+
expect(manager.editor.dispatch).not.toHaveBeenCalled();
127+
});
128+
129+
it("leaves an open tab alone in the background when the text is the same", async () => {
130+
const file = openTab("foo");
131+
await openFile("target", { render: false, text: "foo" });
132+
expect(file.makeActive).not.toHaveBeenCalled();
133+
expect(file.session.setValue).not.toHaveBeenCalled();
134+
});
135+
136+
it("still switches to an open tab when it should render", async () => {
137+
const file = openTab("foo");
138+
await openFile("target", { text: "bar" });
139+
expect(file.makeActive).toHaveBeenCalledOnce();
140+
expect(manager.editor.dispatch).toHaveBeenCalledOnce();
141+
});
142+
106143
it.each([
107144
"pdf",
108145
"docx",

0 commit comments

Comments
 (0)