Skip to content
Merged
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 @@ -41,6 +41,7 @@ function FileHistoryView:init(opt)
self.pinned_path = opt.pinned_path
self.no_panel = opt.no_panel
self._pinned_b_files = {}
self.steps_through_revisions = true

self:super({
panel = FileHistoryPanel({
Expand Down
28 changes: 25 additions & 3 deletions lua/diffview/scene/views/standard/standard_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ end
---@field layouts table<Layout, Layout>
---@field no_panel? boolean # Per-view `--no-panel` override. When set, takes precedence over the panel's `show` config (`nil` means defer to config).
---@field cursor_map table<string, StandardView.CarryState> # Repo-relative path → the cursor and viewport last seen for that path. Consumed the next time the path opens.
---@field steps_through_revisions? boolean # Whether the view lists one file across revisions, so that opening an entry can be a step to another revision of the file just left. See `_is_revision_step`.
---@field package _carry_lnum StandardView.CarryLnum? # A line resolved by a caller that knows it better than a diff between the two revisions can. Consumed once, by the next `restore_main_view`.
---@field package _set_file_in_flight Future? # Active `_set_file` worker; queued callers await this so `await(set_file)` returns only after the latest pending file is opened.
---@field package _set_file_pending FileEntry? # Newest file queued while `_set_file_in_flight` is set; the worker picks it up before terminating.
Expand Down Expand Up @@ -72,6 +73,20 @@ function StandardView._rename_alias(from, to)
return nil
end

---Whether opening `to` after `from` steps to another revision of the same
---file. Only such a step reveals the carried cursor: switching to another
---file, or coming back to one, restores the cursor and leaves folds alone.
---@param from FileEntry # The entry being left.
---@param to FileEntry? # The entry being opened.
---@param alias string? # From `_rename_alias`, or the path a caller followed the file to.
---@return boolean
function StandardView._is_revision_step(from, to, alias)
if not to or to == from then
return false
end
return to.path == from.path or alias ~= nil
Comment on lines +84 to +87
end

---StandardView constructor
function StandardView:init(opt)
opt = opt or {}
Expand Down Expand Up @@ -125,7 +140,9 @@ function StandardView:init(opt)
if not alias and target and target.path ~= cur_entry.path and self:has_carry_lnum(target) then
alias = target.path
end
self:snapshot_main_view(cur_entry.path, alias)
local reveal = self.steps_through_revisions
and StandardView._is_revision_step(cur_entry, target, alias)
self:snapshot_main_view(cur_entry.path, alias, reveal)
end
end)

Expand All @@ -149,6 +166,7 @@ end
---@field winview table # A `winsaveview()` dict.
---@field bufnr? integer # The buffer `winview` was captured in. Missing on a state restored from a session sidecar.
---@field bufname? string # The name `bufnr` carried at capture time.
---@field reveal? boolean # Open the folds hiding the cursor on restore. Set only for a step to another revision of the same file. See `_is_revision_step`.

---@param winid integer
---@return StandardView.CarryState?
Expand Down Expand Up @@ -264,7 +282,8 @@ end
---@param path string repo-relative file path; the map key.
---@param alias? string A second key holding the same state, for a file the
---next entry lists under another name. See `_rename_alias`.
function StandardView:snapshot_main_view(path, alias)
---@param reveal? boolean See `StandardView.CarryState.reveal`.
function StandardView:snapshot_main_view(path, alias, reveal)
local layout = self.cur_layout
local main = layout and layout:get_main_win()
if not (main and main.id and api.nvim_win_is_valid(main.id)) then
Expand All @@ -273,6 +292,7 @@ function StandardView:snapshot_main_view(path, alias)

local entry = capture_winview(main.id)
if entry then
entry.reveal = reveal or nil
self.cursor_map[path] = entry
if alias and alias ~= path then
self.cursor_map[alias] = entry
Expand Down Expand Up @@ -338,7 +358,9 @@ function StandardView:restore_main_view(path)
local ok = apply_winview(win.id, target, lnum)

if ok then
reveal_cursor_line(win.id)
if target.reveal then
reveal_cursor_line(win.id)
end
Comment on lines +361 to +363
self.cursor_map[path] = nil
end
return ok
Expand Down
68 changes: 68 additions & 0 deletions lua/diffview/tests/functional/cursor_carry_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,72 @@ describe("diffview.standard_view carry snapshot/restore", function()
assert.is_true(view:restore_main_view("file.txt"))
eq(12, api.nvim_win_get_cursor(b_win)[1])
end)

---Fold lines 5..15 of `win` and put the cursor inside the closed fold.
local function cursor_on_closed_fold(win)
api.nvim_win_call(win, function()
vim.wo.foldmethod = "manual"
vim.cmd("5,15fold")
end)
api.nvim_win_set_cursor(win, { 10, 0 })
end

local function fold_at_cursor(win)
return api.nvim_win_call(win, function()
return vim.fn.foldclosed(".")
end)
end

-- #371: staging moves to another file, and coming back to a file restores
-- its cursor. Neither is a step through one file's history.
it("leaves folds closed when restoring a file switched back to", 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)

cursor_on_closed_fold(b_win)
view:snapshot_main_view("file.txt")

assert.is_true(view:restore_main_view("file.txt"))
eq(5, fold_at_cursor(b_win))
end)

it("opens the fold hiding a cursor carried to another revision", 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)

cursor_on_closed_fold(b_win)
view:snapshot_main_view("file.txt", nil, true)

assert.is_true(view:restore_main_view("file.txt"))
eq(-1, fold_at_cursor(b_win))
end)
end)

describe("diffview.standard_view _is_revision_step", function()
local function entry(path)
return { path = path }
end

it("is a step when the arriving entry shows the same path", function()
assert.is_true(StandardView._is_revision_step(entry("a.txt"), entry("a.txt"), nil))
end)

it("is a step when a rename links the two paths", function()
assert.is_true(StandardView._is_revision_step(entry("old.txt"), entry("new.txt"), "new.txt"))
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)

it("is not a step when re-opening the entry being left", function()
local e = entry("a.txt")
assert.is_false(StandardView._is_revision_step(e, e, 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)
end)
Loading