From 61a702a322ae914f292bc48a0dc9a6e4308dd962 Mon Sep 17 00:00:00 2001 From: David Yonge-Mallo Date: Tue, 3 Nov 2026 16:08:05 +0100 Subject: [PATCH] fix(view): tighten cursor-carry around rename aliases and refreshes --- .../views/file_history/file_history_view.lua | 1 + .../scene/views/standard/standard_view.lua | 27 +++++++++- lua/diffview/session.lua | 7 +++ .../tests/functional/cursor_carry_spec.lua | 54 ++++++++++++++++++- .../tests/functional/session_spec.lua | 4 +- 5 files changed, 89 insertions(+), 4 deletions(-) diff --git a/lua/diffview/scene/views/file_history/file_history_view.lua b/lua/diffview/scene/views/file_history/file_history_view.lua index 788263ef..6558b99b 100644 --- a/lua/diffview/scene/views/file_history/file_history_view.lua +++ b/lua/diffview/scene/views/file_history/file_history_view.lua @@ -32,6 +32,7 @@ local M = {} ---@field pinned_path? string # Working-tree path the b-window is pinned to. Seeded from `path_args[1]` for single-file pinning; the cursor follower updates it when the user highlights a file row in multi-file mode. ---@field _pinned_cursor_follow? CancellableFn # Debounced CursorMoved handler installed in pin_local mode; closed in `close()` to release the underlying uv timer. ---@field _pinned_b_files table # View-owned cache of working-tree `vcs.File` instances keyed by path. Each pinned-mode b-window across the entire history reuses the entry for its path, so identity is stable across panel refreshes; entry destruction skips these files (see `Diff2*Pinned.shared_symbols`) and the view destroys them in `close()`. +---@field steps_through_revisions true # Opts into fold reveal when stepping onto another revision of the file just left. See `StandardView._is_revision_step`. local FileHistoryView = oop.create_class("FileHistoryView", StandardView.__get()) function FileHistoryView:init(opt) diff --git a/lua/diffview/scene/views/standard/standard_view.lua b/lua/diffview/scene/views/standard/standard_view.lua index f0f9ae1c..2281fea9 100644 --- a/lua/diffview/scene/views/standard/standard_view.lua +++ b/lua/diffview/scene/views/standard/standard_view.lua @@ -84,6 +84,13 @@ function StandardView._is_revision_step(from, to, alias) if not to or to == from then return false end + -- `FileHistoryPanel:update_entries` (triggered by `R` or `FugitiveChanged`) + -- rebuilds entries from scratch, so a reopen of the same commit lands on a + -- fresh `FileEntry` for the same path. That's not a step through revisions; + -- the commit hash distinguishes it from a real step onto the next one. + if from.commit and to.commit and from.commit.hash == to.commit.hash then + return false + end return to.path == from.path or alias ~= nil end @@ -293,6 +300,17 @@ function StandardView:snapshot_main_view(path, alias, reveal) local entry = capture_winview(main.id) if entry then entry.reveal = reveal or nil + -- A prior snapshot at `path` may have installed its state under an + -- alias this one doesn't share. Drop every key pointing to it before + -- the overwrite, so the orphaned alias can't replay a stale `reveal`. + local prev = self.cursor_map[path] + if prev then + for key, state in pairs(self.cursor_map) do + if state == prev then + self.cursor_map[key] = nil + end + end + end self.cursor_map[path] = entry if alias and alias ~= path then self.cursor_map[alias] = entry @@ -361,7 +379,14 @@ function StandardView:restore_main_view(path) if target.reveal then reveal_cursor_line(win.id) end - self.cursor_map[path] = nil + -- `snapshot_main_view` can install the same state under both a path and + -- a rename alias; dropping only the consumed key would leave the other + -- to replay a stale `reveal` on a later open. + for key, state in pairs(self.cursor_map) do + if state == target then + self.cursor_map[key] = nil + end + end end return ok end diff --git a/lua/diffview/session.lua b/lua/diffview/session.lua index 0d0c695d..eedd3f89 100644 --- a/lua/diffview/session.lua +++ b/lua/diffview/session.lua @@ -485,6 +485,13 @@ local function restore_file_history_entry(lib, entry) end local cursor_map = sanitize_cursor_map(entry.cursor_map) if cursor_map then + -- A restored session resumes a file-history browse the user was in the + -- middle of; the next open is a resume-step, so open folds around the + -- carried cursor just like live stepping does. The sidecar schema stays + -- bare (`sanitize_cursor_map`), so the flag is reapplied here. + for _, state in pairs(cursor_map) do + state.reveal = true + end view.cursor_map = cursor_map end local open_ok, open_err = pcall(view.open, view) diff --git a/lua/diffview/tests/functional/cursor_carry_spec.lua b/lua/diffview/tests/functional/cursor_carry_spec.lua index 25a06a53..f7d2c18e 100644 --- a/lua/diffview/tests/functional/cursor_carry_spec.lua +++ b/lua/diffview/tests/functional/cursor_carry_spec.lua @@ -398,11 +398,47 @@ describe("diffview.standard_view carry snapshot/restore", function() assert.is_true(view:restore_main_view("file.txt")) eq(-1, fold_at_cursor(b_win)) end) + + -- `snapshot_main_view` installs one state under both `path` and `alias`. + -- Consuming either key must drop the other, so a later plain file switch + -- can't inherit the rename step's `reveal`. + it("drops the alias when the primary key is consumed", function() + local a_win = pool.open(body("body", 20)) + local b_win = pool.open(body("body", 20)) + local view = make_view(a_win, b_win) + + view:snapshot_main_view("new.txt", "old.txt", true) + assert.is_true(view:restore_main_view("new.txt")) + eq(nil, view.cursor_map["old.txt"]) + end) + + it("drops the primary key when the alias is consumed", function() + local a_win = pool.open(body("body", 20)) + local b_win = pool.open(body("body", 20)) + local view = make_view(a_win, b_win) + + view:snapshot_main_view("new.txt", "old.txt", true) + assert.is_true(view:restore_main_view("old.txt")) + eq(nil, view.cursor_map["new.txt"]) + end) + + -- A fresh snapshot at `path` must not inherit the previous snapshot's + -- unrelated alias; otherwise a cancelled rename step could leave its + -- `reveal` lurking on a key the next step never names. + it("drops a stale alias when the same path is re-snapshotted", function() + local a_win = pool.open(body("body", 20)) + local b_win = pool.open(body("body", 20)) + local view = make_view(a_win, b_win) + + view:snapshot_main_view("new.txt", "old.txt", true) + view:snapshot_main_view("new.txt", nil, false) + eq(nil, view.cursor_map["old.txt"]) + end) end) describe("diffview.standard_view _is_revision_step", function() - local function entry(path) - return { path = path } + local function entry(path, hash) + return { path = path, commit = hash and { hash = hash } or nil } end it("is a step when the arriving entry shows the same path", function() @@ -413,6 +449,12 @@ describe("diffview.standard_view _is_revision_step", function() assert.is_true(StandardView._is_revision_step(entry("old.txt"), entry("new.txt"), "new.txt")) end) + it("is a step when the arriving entry targets a different commit", function() + assert.is_true( + StandardView._is_revision_step(entry("a.txt", "abc123"), entry("a.txt", "def456"), nil) + ) + end) + it("is not a step between two different files", function() assert.is_false(StandardView._is_revision_step(entry("a.txt"), entry("b.txt"), nil)) end) @@ -422,6 +464,14 @@ describe("diffview.standard_view _is_revision_step", function() assert.is_false(StandardView._is_revision_step(e, e, nil)) end) + -- A panel refresh (`R`, `FugitiveChanged`) rebuilds entries, so the current + -- commit's entry becomes a fresh object with the same path. Not a step. + it("is not a step when a rebuilt entry targets the same commit", function() + assert.is_false( + StandardView._is_revision_step(entry("a.txt", "abc123"), entry("a.txt", "abc123"), nil) + ) + end) + it("is not a step without an arriving entry", function() assert.is_false(StandardView._is_revision_step(entry("a.txt"), nil, nil)) end) diff --git a/lua/diffview/tests/functional/session_spec.lua b/lua/diffview/tests/functional/session_spec.lua index 6f0e5771..9a9b7d64 100644 --- a/lua/diffview/tests/functional/session_spec.lua +++ b/lua/diffview/tests/functional/session_spec.lua @@ -744,8 +744,10 @@ describe("session save/restore", function() session.restore() assert.is_not_nil(captured) + -- A restored file-history view resumes a browse: the first open is + -- a step onto the carried cursor, so each entry carries `reveal`. assert.are.same({ - ["src/main.lua"] = { winview = { lnum = 42, col = 3, topline = 10 } }, + ["src/main.lua"] = { winview = { lnum = 42, col = 3, topline = 10 }, reveal = true }, }, captured.cursor_map) end)