Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, vcs.File> # 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)
Expand Down
27 changes: 26 additions & 1 deletion lua/diffview/scene/views/standard/standard_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lua/diffview/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 52 additions & 2 deletions lua/diffview/tests/functional/cursor_carry_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lua/diffview/tests/functional/session_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading