From 126dcf9568aef5ae32a57d1be2f8ffaea213138c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Fl=C3=BCgge?= <952313+seflue@users.noreply.github.com> Date: Mon, 2 Nov 2026 09:17:24 +0100 Subject: [PATCH] fix(view): open folds only when stepping revisions The cursor carry is meant for browsing the revisions of one file and nothing else. Opening the fold under the restored cursor also ran on every other restore, such as after staging or when switching back to a file. It now runs only on a step to another revision. Fixes #371 --- .../views/file_history/file_history_view.lua | 1 + .../scene/views/standard/standard_view.lua | 28 +++++++- .../tests/functional/cursor_carry_spec.lua | 68 +++++++++++++++++++ 3 files changed, 94 insertions(+), 3 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 c6deccda..788263ef 100644 --- a/lua/diffview/scene/views/file_history/file_history_view.lua +++ b/lua/diffview/scene/views/file_history/file_history_view.lua @@ -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({ diff --git a/lua/diffview/scene/views/standard/standard_view.lua b/lua/diffview/scene/views/standard/standard_view.lua index 2221a875..f0f9ae1c 100644 --- a/lua/diffview/scene/views/standard/standard_view.lua +++ b/lua/diffview/scene/views/standard/standard_view.lua @@ -38,6 +38,7 @@ end ---@field layouts table ---@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 # 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. @@ -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 +end + ---StandardView constructor function StandardView:init(opt) opt = opt or {} @@ -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) @@ -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? @@ -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 @@ -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 @@ -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 self.cursor_map[path] = nil end return ok diff --git a/lua/diffview/tests/functional/cursor_carry_spec.lua b/lua/diffview/tests/functional/cursor_carry_spec.lua index 3a28366f..25a06a53 100644 --- a/lua/diffview/tests/functional/cursor_carry_spec.lua +++ b/lua/diffview/tests/functional/cursor_carry_spec.lua @@ -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)