diff --git a/doc/diffview.txt b/doc/diffview.txt index a40dc2b9..bcf82840 100644 --- a/doc/diffview.txt +++ b/doc/diffview.txt @@ -2403,7 +2403,10 @@ toggle_select_entry *diffview-actions-toggle_select_ directory (tree view), selects all files under it if any are unselected, or deselects all if every file is already selected. Also works in visual mode to toggle all files in the selected - line range. When files are selected, `toggle_stage_entry` and + line range. After selecting, the cursor moves to the nearest + unselected file in normal mode (the file below wins ties); after deselecting, it + stays on the deselected file. When files are selected, + `toggle_stage_entry` and `restore_entry` will operate on all selected files as a batch. Selection marks are shown using `signs.selected_file`, `signs.unselected_file`, `signs.selected_dir`, diff --git a/lua/diffview/scene/views/diff/listeners.lua b/lua/diffview/scene/views/diff/listeners.lua index 05cd0dbf..6817cee4 100644 --- a/lua/diffview/scene/views/diff/listeners.lua +++ b/lua/diffview/scene/views/diff/listeners.lua @@ -35,6 +35,43 @@ local function find_visible_neighbor(files, anchor, excluded) end end +---Find the unmarked file closest to the contiguous block of `anchors` in the +---panel's file list, alternating down then up by increasing distance (ties +---prefer down). +---@param panel FilePanel +---@param anchors FileEntry[] +---@return FileEntry? +local function find_nearest_unmarked(panel, anchors) + local files = panel:ordered_file_list() + local anchor_set = {} ---@type table + for _, f in ipairs(anchors) do + anchor_set[f] = true + end + local lo, hi + for i, f in ipairs(files) do + if anchor_set[f] then + lo = lo or i + hi = i + end + end + if not lo then + return + end + + for d = 1, #files do + local down, up = files[hi + d], files[lo - d] + if not down and not up then + return + end + if down and not panel:is_selected(down) then + return down + end + if up and not panel:is_selected(up) then + return up + end + end +end + ---Move the cursor to the panel's repository path when filtering leaves no ---selectable file rows. ---@param panel FilePanel @@ -395,13 +432,11 @@ return function(view) view.panel:render() view.panel:redraw() if will_hide then - -- Viewing: advance to the next entry (established behaviour). - view.panel:highlight_next_file() - -- Open the entry the cursor landed on so the panel, cur_file, and - -- displayed diff stay in sync. - local next_item = view.panel:get_item_at_cursor() - if next_item and type(next_item.collapsed) ~= "boolean" then - view:set_file(next_item --[[@as FileEntry]], false, true) + -- Viewing: jump to the nearest still-unviewed file. set_file syncs + -- cur_file + highlight and keeps focus in the panel. + local nearest = find_nearest_unmarked(view.panel, leaves) + if nearest then + view:set_file(nearest, false, true) end else -- Unviewing: open the just-unmarked file in the diff and keep the diff --git a/lua/diffview/tests/functional/hide_selected_navigation_spec.lua b/lua/diffview/tests/functional/hide_selected_navigation_spec.lua index bb71fa17..361e0872 100644 --- a/lua/diffview/tests/functional/hide_selected_navigation_spec.lua +++ b/lua/diffview/tests/functional/hide_selected_navigation_spec.lua @@ -137,11 +137,8 @@ describe("reviewed-file navigation", function() eq(true, reconstrained) end) - it("opens the file selected by the panel after marking an entry", function() - local current = { path = "b.lua" } - local next_file = { path = "c.lua" } - local cursor_item = current - local selected = {} + ---Build a non-hide panel stub over `files` with the cursor on `cursor_item`. + local function make_view(files, cursor_item, selected) local panel = { hide_selected = false, is_open = function() @@ -150,6 +147,9 @@ describe("reviewed-file navigation", function() get_item_at_cursor = function() return cursor_item end, + ordered_file_list = function() + return files + end, is_selected = function(_, file) return selected[file] == true end, @@ -159,24 +159,49 @@ describe("reviewed-file navigation", function() select_file = function(_, file) selected[file] = true end, - highlight_next_file = function() - cursor_item = next_file + deselect_file = function(_, file) + selected[file] = nil end, render = function() end, redraw = function() end, } - local opened - local view = { - panel = panel, - set_file = function(_, file, focus, highlight) - opened = { file, focus, highlight } - end, - } + local view = { panel = panel } + view.set_file = function(_, file, focus, highlight) + view.opened = { file, focus, highlight } + end + return view + end + + it("jumps to the nearest unmarked file after marking an entry", function() + local a, b, c, d = { path = "a" }, { path = "b" }, { path = "c" }, { path = "d" } + local selected = { [c] = true } + local view = make_view({ a, b, c, d }, b, selected) listeners_factory(view).toggle_select_entry() - eq(true, selected[current]) - eq({ next_file, false, true }, opened) + eq(true, selected[b]) + eq({ a, false, true }, view.opened) + end) + + it("prefers the file below when marking with unmarked files on both sides", function() + local a, b, c = { path = "a" }, { path = "b" }, { path = "c" } + local selected = {} + local view = make_view({ a, b, c }, b, selected) + + listeners_factory(view).toggle_select_entry() + + eq({ c, false, true }, view.opened) + end) + + it("stays on the file after unmarking it", function() + local a, b, c = { path = "a" }, { path = "b" }, { path = "c" } + local selected = { [a] = true, [b] = true, [c] = true } + local view = make_view({ a, b, c }, b, selected) + + listeners_factory(view).toggle_select_entry() + + eq(nil, selected[b]) + eq({ b, false, true }, view.opened) end) it("opens the next visible file when H hides the active file", function()