From 59313f3f62e0d80a3037f2809f8036e861c5bc42 Mon Sep 17 00:00:00 2001 From: danny Date: Sun, 8 Nov 2026 12:26:51 +0100 Subject: [PATCH] A2: Compare with Disk as a UI command (editor.compareWithDisk) Co-Authored-By: Claude Opus 5.5 --- docs/local-history.md | 3 ++- ui/src/commands/uiCommands.ts | 1 + ui/src/components/editor/EditorArea.vue | 3 ++- ui/src/i18n/en.json | 3 +++ ui/src/stores/diskConflicts.ts | 32 +++++++++++++++++++++++-- ui/tests/editor/diskConflicts.test.ts | 9 +++++++ 6 files changed, 47 insertions(+), 4 deletions(-) diff --git a/docs/local-history.md b/docs/local-history.md index c7afbd7..33db335 100644 --- a/docs/local-history.md +++ b/docs/local-history.md @@ -23,7 +23,7 @@ Lutra watches the folders of every open file (`docs/architecture.md` › VFS). E - **Reload (discard mine)** replaces your text with the file's (`file.reload`). Your text goes into the Local History first. - **Keep mine** hides the banner. The next save asks once more before it overwrites the newer file. -- **Compare** opens a diff tab: the file on disk on the left, your text on the right (`file.readDisk`). You can edit the right side; it is the same buffer as the tab. +- **Compare** opens a diff tab: the file on disk on the left, your text on the right (`file.readDisk`). You can edit the right side; it is the same buffer as the tab. The palette's Compare with Disk does the same for any file. - **Close** closes the tab without saving; the text is in the Local History. ### Saving never overwrites a newer file silently @@ -80,6 +80,7 @@ Files that are not open in Lutra are not watched for the history. git protects t | `history.read {path, id}` | One entry's text, answered like `git.show`. | | `history.revert {path, id}` | Puts an entry's text into the buffer (an undoable `buffer.edit`). | | `history.show {path?}` | UI command: the dialog. | +| `editor.compareWithDisk {path?}` | UI command: the banner's Compare for any file (palette, agents). | Events: `fileChangedOnDisk {path, id, removed}` sets the banner, `diskConflictResolved {id}` removes it. diff --git a/ui/src/commands/uiCommands.ts b/ui/src/commands/uiCommands.ts index 2a971fc..6c7fc41 100644 --- a/ui/src/commands/uiCommands.ts +++ b/ui/src/commands/uiCommands.ts @@ -33,6 +33,7 @@ export const UI_COMMANDS: readonly UiCommandSpec[] = [ { id: "deployment.upload", labelKey: "command.deployment.upload", menuPath: "Tools/Deployment/Upload Current File" }, { id: "deployment.uploadProject", labelKey: "command.deployment.uploadProject", menuPath: "Tools/Deployment/Upload Project" }, { id: "editor.reveal", labelKey: "command.editor.reveal" }, + { id: "editor.compareWithDisk", labelKey: "command.editor.compareWithDisk" }, { id: "editor.complete", labelKey: "command.editor.complete", menuPath: "Edit/Complete Code" }, { id: "editor.quickDocumentation", labelKey: "command.editor.quickDocumentation", menuPath: "Edit/Quick Documentation", defaultShortcut: "Mod+I" }, { id: "editor.parameterInfo", labelKey: "command.editor.parameterInfo", menuPath: "Edit/Parameter Info" }, diff --git a/ui/src/components/editor/EditorArea.vue b/ui/src/components/editor/EditorArea.vue index 9bde581..ca7a411 100644 --- a/ui/src/components/editor/EditorArea.vue +++ b/ui/src/components/editor/EditorArea.vue @@ -20,7 +20,7 @@ import { onBeforeUnmount, onMounted } from "vue"; import { useCommandsStore } from "../../commands/store"; import { t } from "../../i18n"; import { useEditorStore } from "../../stores/editor"; -import { useDiskConflictsStore } from "../../stores/diskConflicts"; +import { COMPARE_WITH_DISK_COMMAND, useDiskConflictsStore } from "../../stores/diskConflicts"; import ClosePrompt from "./ClosePrompt.vue"; import EditorPane from "./EditorPane.vue"; import { provideTabCommands } from "./tabs/commands"; @@ -47,6 +47,7 @@ onMounted(() => { commands.provide("file.saveAll", { run: () => editor.saveAll(), enabled: () => editor.tabs.length > 0 }), commands.provide("buffer.close", { run: () => editor.requestClose(), enabled: hasActive }), commands.provide(REVEAL_COMMAND, { run: (params) => reveal(editor, params) }), + commands.provide(COMPARE_WITH_DISK_COMMAND, { run: (params) => disk.compareCommand(params), enabled: hasActive }), commands.provide(assist.COMPLETE_COMMAND, { run: assist.completeAtCursor, enabled: assist.assistEnabled }), commands.provide(assist.QUICK_DOC_COMMAND, { run: assist.quickDocumentation, enabled: assist.assistEnabled }), commands.provide(assist.PARAMETER_INFO_COMMAND, { run: assist.parameterInfo, enabled: assist.assistEnabled }), diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json index 72b192a..c63520a 100644 --- a/ui/src/i18n/en.json +++ b/ui/src/i18n/en.json @@ -9,6 +9,7 @@ "command.deployment.testConnection": "Test Deployment Connection", "command.deployment.upload": "Upload Current File to Server", "command.deployment.uploadProject": "Upload Project to Server", + "command.editor.compareWithDisk": "Compare with Disk", "command.editor.complete": "Complete Code", "command.editor.parameterInfo": "Parameter Info", "command.editor.quickDocumentation": "Quick Documentation", @@ -231,6 +232,8 @@ "editor.disk.banner.changed": "{name} was changed by another program. You have unsaved changes.", "editor.disk.banner.removed": "{name} was deleted on disk. Your text is still open.", "editor.disk.compare": "Compare", + "editor.disk.compareInvalidParams": "Compare with Disk takes { path?: string }.", + "editor.disk.compareNoFile": "Open a file to compare it with its text on disk.", "editor.disk.keepMine": "Keep mine", "editor.disk.keepUnsaved": "Keep as unsaved", "editor.disk.overwrite.confirm": "Overwrite", diff --git a/ui/src/stores/diskConflicts.ts b/ui/src/stores/diskConflicts.ts index 0deaff9..4ef11d8 100644 --- a/ui/src/stores/diskConflicts.ts +++ b/ui/src/stores/diskConflicts.ts @@ -7,7 +7,9 @@ * engine keeps the discarded text in the Local History); "Keep mine" * hides the banner, and the next save asks once more because the engine * refuses it with `disk_changed`; "Compare" opens the diff viewer with - * the disk text on the left and the buffer on the right. + * the disk text on the left and the buffer on the right; the UI command + * `editor.compareWithDisk {path?}` does the same for any open or closed + * file (palette, agents). * - **Banner, removed:** "Keep as unsaved" hides the banner (the engine * already made the buffer dirty, so closing asks and saving writes the * file again); "Close" closes the tab without saving. @@ -26,7 +28,8 @@ */ import { defineStore } from "pinia"; import { computed, ref } from "vue"; -import { run } from "../ipc"; +import { t } from "../i18n"; +import { run, type JsonValue } from "../ipc"; import { onEditorEvent, type EditorEvent } from "../components/editor/engineEvents"; import { useDiffStore } from "../components/vcs/diff/diff"; import { baseName, useEditorStore } from "./editor"; @@ -46,6 +49,9 @@ export interface DiskConflict { kept: boolean; } +/** Command id of "Compare with Disk" (the banner's Compare). */ +export const COMPARE_WITH_DISK_COMMAND = "editor.compareWithDisk"; + /** A pending overwrite question. */ export interface OverwritePrompt { /** Buffer id. */ @@ -148,6 +154,27 @@ export const useDiskConflictsStore = defineStore("diskConflicts", () => { } } + /** + * Runs `editor.compareWithDisk {path?}`: the given file, else the active + * tab's, against its text on disk. + * + * @param params - untrusted params. + * @throws Error with a catalog message for malformed params or without a + * file. + */ + async function compareCommand(params: JsonValue): Promise { + const p = typeof params === "object" && params !== null && !Array.isArray(params) ? params : {}; + const raw = p.path; + if (raw !== undefined && raw !== null && (typeof raw !== "string" || raw === "")) { + throw new Error(t("editor.disk.compareInvalidParams")); + } + const path = typeof raw === "string" ? raw : (editor.activeTab?.path ?? null); + if (path === null) { + throw new Error(t("editor.disk.compareNoFile")); + } + await useDiffStore().openDisk(path); + } + /** * "Close": closes the tab without saving (the engine keeps the text in * the Local History). @@ -260,6 +287,7 @@ export const useDiskConflictsStore = defineStore("diskConflicts", () => { keep, reload, compare, + compareCommand, closeTab, askOverwrite, resolveOverwrite, diff --git a/ui/tests/editor/diskConflicts.test.ts b/ui/tests/editor/diskConflicts.test.ts index 77533c0..7703689 100644 --- a/ui/tests/editor/diskConflicts.test.ts +++ b/ui/tests/editor/diskConflicts.test.ts @@ -155,6 +155,15 @@ describe("files changed on disk", () => { expect(calls.find((c) => c.command === "file.readDisk")?.params).toEqual({ path: "src/a.php" }); }); + it("compares any file with the disk through editor.compareWithDisk", async () => { + await mountWithFile(); + await useCommandsStore(pinia).execute("editor.compareWithDisk", {}); + await flushPromises(); + expect(useDiffStore(pinia).activeTab?.key).toBe("disk:src/a.php"); + const disk = useDiskConflictsStore(pinia); + await expect(disk.compareCommand({ path: 1 })).rejects.toThrow("Compare with Disk takes"); + }); + it("asks once more when a kept buffer is saved over the newer file", async () => { const area = await mountWithFile(); const editor = useEditorStore(pinia);