diff --git a/src/lib/openFile.js b/src/lib/openFile.js index 3b8511467..70a71181a 100644 --- a/src/lib/openFile.js +++ b/src/lib/openFile.js @@ -7,13 +7,15 @@ import loader from "dialogs/loader"; import { reopenWithNewEncoding } from "palettes/changeEncoding"; import { decode, detectEncoding } from "utils/encodings"; import helpers from "utils/helpers"; -import EditorFile from "./editorFile"; +import EditorFile, { AUTO_SAVE } from "./editorFile"; import { promoteSessionPersistence } from "./fileSessionPersistence"; import fileTypeHandler from "./fileTypeHandler"; import recents from "./recents"; import appSettings from "./settings"; let loadingFileCount = 0; +/** @type {WeakMap} */ +const backgroundAutosaveTimers = new WeakMap(); /** * @typedef {object} FileOptions @@ -63,6 +65,24 @@ export default async function openFile(file, options = {}) { const incomingDoc = text != null ? Text.of(String(text).split("\n")) : null; + // Background update (e.g. replace all in files): change the text of an + // open, loaded tab without switching to it. + if ( + render === false && + incomingDoc && + !paneId && + !cursorPos && + !encoding && + existingFile.loaded && + existingFile.session + ) { + if (onsave) existingFile.onsave = onsave; + if (!existingFile.session.doc?.eq?.(incomingDoc)) { + updateInBackground(existingFile, String(text)); + } + return; + } + // If file is already opened const targetPane = paneId ? editorManager.panes?.find((pane) => pane.id === paneId) @@ -513,6 +533,46 @@ export default async function openFile(file, options = {}) { } } +/** + * Replace the text of an open tab without switching to it. + * @param {EditorFile} file + * @param {string} text + */ +function updateInBackground(file, text) { + // Shown in a pane (maybe not the focused one): edit that pane's live view. + // Its update listener syncs file.session and schedules autosave. Writing + // only file.session here would be undone when the pane is focused again. + const pane = editorManager.panes?.find( + (item) => item.editor && item.activeFile?.id === file.id, + ); + if (pane) { + pane.editor.dispatch({ + changes: { from: 0, to: pane.editor.state.doc.length, insert: text }, + }); + return; + } + + // Hidden tab: no view listener runs, so schedule autosave here. + file.session.setValue(text); + scheduleBackgroundAutosave(file); +} + +/** @param {EditorFile} file */ +function scheduleBackgroundAutosave(file) { + const { autosave } = appSettings.value; + if (!autosave || !file.uri) return; + clearTimeout(backgroundAutosaveTimers.get(file)); + const timer = setTimeout(() => { + backgroundAutosaveTimers.delete(file); + // Skip tabs closed meanwhile, or already saved. + if (!file.isUnsaved || !editorManager.getFile(file.id, "id")) return; + file.save(AUTO_SAVE)?.catch?.((error) => { + console.warn(`Failed to autosave ${file.filename || file.uri}`, error); + }); + }, autosave); + backgroundAutosaveTimers.set(file, timer); +} + /** Keep the shared indicator visible while any file open still needs it. */ function acquireTitleLoader(signal) { if (loadingFileCount++ === 0) loader.showTitleLoader(); diff --git a/tests/unit/openFileCancellation.test.js b/tests/unit/openFileCancellation.test.js index 80f4d4b0d..f9aeae7b9 100644 --- a/tests/unit/openFileCancellation.test.js +++ b/tests/unit/openFileCancellation.test.js @@ -1,6 +1,7 @@ import fs from "node:fs"; +import { Text } from "@codemirror/state"; import ts from "typescript"; -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { isBinaryFile } from "../../src/utils/binaryExtensions"; // openFile contains app-specific JSX. Compile the actual module for this @@ -18,6 +19,8 @@ const { outputText } = ts.transpileModule(source, { }, }); +const AUTO_SAVE = Symbol("auto-save"); + function deferred() { let resolve; const promise = new Promise((done) => { @@ -39,6 +42,7 @@ describe("openFile cancellation", () => { let controller; let titleLoader; let loaderVisible; + let settings; beforeEach(() => { controller = new AbortController(); @@ -52,6 +56,7 @@ describe("openFile cancellation", () => { }), }; manager = { getFile: vi.fn(), activeFile: null }; + settings = { maxFileSize: 10, defaultFileEncoding: "auto" }; stat = vi.fn().mockResolvedValue({ name: "target.txt", length: 10 }); readFile = vi.fn().mockResolvedValue("bytes"); decode = vi.fn().mockResolvedValue("target text"); @@ -63,7 +68,7 @@ describe("openFile cancellation", () => { }); const dependencies = { fileSystem: { default: () => ({ stat, readFile }) }, - "@codemirror/state": {}, + "@codemirror/state": { Text }, "components/audioPlayer": {}, "dialogs/alert": {}, "dialogs/confirm": {}, @@ -75,13 +80,11 @@ describe("openFile cancellation", () => { "utils/helpers": { default: { getStatMtime: () => 0, isBinary: isBinaryFile }, }, - "./editorFile": { default: createEditor }, + "./editorFile": { default: createEditor, AUTO_SAVE }, "./fileSessionPersistence": { promoteSessionPersistence: vi.fn() }, "./fileTypeHandler": { default: handler }, "./recents": { default: recents }, - "./settings": { - default: { value: { maxFileSize: 10, defaultFileEncoding: "auto" } }, - }, + "./settings": { default: { value: settings } }, }; const exports = {}; new Function("require", "exports", "editorManager", outputText)( @@ -350,4 +353,108 @@ describe("openFile cancellation", () => { await opening; expect(loaderVisible).toBe(false); }); + + function openTab(text) { + const file = { + loaded: true, + makeActive: vi.fn(), + session: { + doc: Text.of(text.split("\n")), + setValue: vi.fn(), + }, + }; + manager.getFile.mockReturnValue(file); + manager.editor = { dispatch: vi.fn(), state: { doc: { length: 0 } } }; + return file; + } + + it("updates an open tab in the background without switching to it", async () => { + const file = openTab("foo"); + await openFile("target", { render: false, text: "bar" }); + expect(file.makeActive).not.toHaveBeenCalled(); + expect(file.session.setValue).toHaveBeenCalledWith("bar"); + expect(manager.editor.dispatch).not.toHaveBeenCalled(); + }); + + it("leaves an open tab alone in the background when the text is the same", async () => { + const file = openTab("foo"); + await openFile("target", { render: false, text: "foo" }); + expect(file.makeActive).not.toHaveBeenCalled(); + expect(file.session.setValue).not.toHaveBeenCalled(); + }); + + it("still switches to an open tab when it should render", async () => { + const file = openTab("foo"); + await openFile("target", { text: "bar" }); + expect(file.makeActive).toHaveBeenCalledOnce(); + expect(manager.editor.dispatch).toHaveBeenCalledOnce(); + }); + it("edits the live view of a split pane that shows the tab", async () => { + const file = openTab("foo"); + file.id = "tab"; + const paneEditor = { + dispatch: vi.fn(), + state: { doc: { length: 3 } }, + }; + manager.panes = [ + { editor: manager.editor, activeFile: { id: "other" } }, + { editor: paneEditor, activeFile: file }, + ]; + await openFile("target", { render: false, text: "bar" }); + expect(file.makeActive).not.toHaveBeenCalled(); + expect(paneEditor.dispatch).toHaveBeenCalledWith({ + changes: { from: 0, to: 3, insert: "bar" }, + }); + expect(file.session.setValue).not.toHaveBeenCalled(); + expect(manager.editor.dispatch).not.toHaveBeenCalled(); + }); + + describe("autosave of background tabs", () => { + function hiddenTab() { + const file = openTab("foo"); + file.id = "tab"; + file.uri = "file:///tab.txt"; + file.isUnsaved = true; + file.save = vi.fn().mockResolvedValue(true); + manager.panes = [{ editor: manager.editor, activeFile: { id: "x" } }]; + return file; + } + + beforeEach(() => { + vi.useFakeTimers(); + settings.autosave = 1000; + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it("saves a hidden tab after the autosave delay", async () => { + const file = hiddenTab(); + manager.getFile.mockImplementation((key, type) => + type === "id" ? (key === file.id ? file : undefined) : file, + ); + await openFile("target", { render: false, text: "bar" }); + expect(file.session.setValue).toHaveBeenCalledWith("bar"); + expect(file.save).not.toHaveBeenCalled(); + vi.advanceTimersByTime(1000); + expect(file.save).toHaveBeenCalledWith(AUTO_SAVE); + }); + + it("does not save a hidden tab that was closed meanwhile", async () => { + const file = hiddenTab(); + await openFile("target", { render: false, text: "bar" }); + manager.getFile.mockReturnValue(undefined); + vi.advanceTimersByTime(1000); + expect(file.save).not.toHaveBeenCalled(); + }); + + it("does not save when autosave is off", async () => { + settings.autosave = 0; + const file = hiddenTab(); + await openFile("target", { render: false, text: "bar" }); + vi.advanceTimersByTime(10000); + expect(file.save).not.toHaveBeenCalled(); + }); + }); });