From 5c210a690e9cf1f034b2130842a9361b18afd129 Mon Sep 17 00:00:00 2001 From: Karl Cayme Date: Wed, 21 Oct 2026 11:30:43 +0800 Subject: [PATCH] feat(explorer): scroll diff panes from the explorer and history panels --- README.md | 4 ++++ doc/codediff.txt | 4 ++++ lua/codediff/config.lua | 4 ++++ lua/codediff/ui/keymap_help.lua | 4 ++++ lua/codediff/ui/lib/tree_utils.lua | 16 ++++++++++++++- tests/fixtures/keymap_matrix.txt | 4 ++++ tests/integration/keymap/actions_spec.lua | 4 ++++ .../ui/explorer/scroll_independence_spec.lua | 20 +++++++++++++++++++ 8 files changed, 59 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d78cb72..3f4fe7b9 100644 --- a/README.md +++ b/README.md @@ -273,6 +273,8 @@ Both methods automatically place the library in the plugin root directory. fold_toggle_recursive = "zA", -- Toggle fold recursively fold_open_all = "zR", -- Open all folds in tree fold_close_all = "zM", -- Close all folds in tree + scroll_down = "", -- Scroll diff panes down half a page + scroll_up = "", -- Scroll diff panes up half a page }, history = { select = "", -- Select commit/file or toggle expand @@ -287,6 +289,8 @@ Both methods automatically place the library in the plugin root directory. fold_toggle_recursive = "zA", -- Toggle fold recursively fold_open_all = "zR", -- Open all folds in tree fold_close_all = "zM", -- Close all folds in tree + scroll_down = "", -- Scroll diff panes down half a page + scroll_up = "", -- Scroll diff panes up half a page }, conflict = { accept_incoming = "ct", -- Accept incoming (theirs/left) change diff --git a/doc/codediff.txt b/doc/codediff.txt index c877d3d1..e5ba323f 100644 --- a/doc/codediff.txt +++ b/doc/codediff.txt @@ -359,6 +359,8 @@ Setup entry point: fold_toggle_recursive = "zA", fold_open_all = "zR", fold_close_all = "zM", + scroll_down = "", + scroll_up = "", }, history = { select = "", @@ -371,6 +373,8 @@ Setup entry point: fold_toggle_recursive = "zA", fold_open_all = "zR", fold_close_all = "zM", + scroll_down = "", + scroll_up = "", }, conflict = { accept_incoming = "ct", diff --git a/lua/codediff/config.lua b/lua/codediff/config.lua index 59a4139f..2bc472e8 100644 --- a/lua/codediff/config.lua +++ b/lua/codediff/config.lua @@ -147,6 +147,8 @@ M.defaults = { fold_toggle_recursive = "zA", -- Toggle fold recursively fold_open_all = "zR", -- Open all folds in tree fold_close_all = "zM", -- Close all folds in tree + scroll_down = "", -- Scroll diff panes down half a page + scroll_up = "", -- Scroll diff panes up half a page }, history = { select = "", -- Select commit/file or toggle expand @@ -161,6 +163,8 @@ M.defaults = { fold_toggle_recursive = "zA", fold_open_all = "zR", fold_close_all = "zM", + scroll_down = "", + scroll_up = "", }, -- Conflict mode keymaps (only active in merge conflict views) conflict = { diff --git a/lua/codediff/ui/keymap_help.lua b/lua/codediff/ui/keymap_help.lua index c9cbf978..57920c38 100644 --- a/lua/codediff/ui/keymap_help.lua +++ b/lua/codediff/ui/keymap_help.lua @@ -110,6 +110,8 @@ local function build_sections(keymaps, is_bound, shape) { ekm.fold_toggle_recursive, "Toggle fold recursively" }, { ekm.fold_open_all, "Open all folds" }, { ekm.fold_close_all, "Close all folds" }, + { ekm.scroll_down, "Scroll diff down" }, + { ekm.scroll_up, "Scroll diff up" }, }, is_bound) ) end @@ -131,6 +133,8 @@ local function build_sections(keymaps, is_bound, shape) { hkm.fold_toggle_recursive, "Toggle fold recursively" }, { hkm.fold_open_all, "Open all folds" }, { hkm.fold_close_all, "Close all folds" }, + { hkm.scroll_down, "Scroll diff down" }, + { hkm.scroll_up, "Scroll diff up" }, }, is_bound) ) end diff --git a/lua/codediff/ui/lib/tree_utils.lua b/lua/codediff/ui/lib/tree_utils.lua index 0c5fc302..cd0e0e86 100644 --- a/lua/codediff/ui/lib/tree_utils.lua +++ b/lua/codediff/ui/lib/tree_utils.lua @@ -42,7 +42,7 @@ function M.get_root_node(tree) return find_root(node) end --- Setup all fold-related keymaps on a tree buffer. +-- Setup fold and diff-scroll keymaps on a tree buffer. -- @param opts table { tree, keymaps, bufnr, tabpage } function M.setup_fold_keymaps(opts) local tree = opts.tree @@ -138,6 +138,18 @@ function M.setup_fold_keymaps(opts) update_tree_view(root) end + local function scroll_diff(key) + return function() + local orig_win, mod_win = require("codediff.ui.lifecycle").get_windows(tabpage) + local win = mod_win or orig_win + if win and vim.api.nvim_win_is_valid(win) then + vim.api.nvim_win_call(win, function() + vim.cmd("normal! " .. vim.keycode(key)) + end) + end + end + end + local fold_bindings = { { key = "fold_open", fn = fold_open, desc = "Open fold" }, { key = "fold_open_recursive", fn = fold_open_recursive, desc = "Open fold recursively" }, @@ -147,6 +159,8 @@ function M.setup_fold_keymaps(opts) { key = "fold_toggle_recursive", fn = fold_toggle_recursive, desc = "Toggle fold recursively" }, { key = "fold_open_all", fn = fold_open_all, desc = "Open all folds" }, { key = "fold_close_all", fn = fold_close_all, desc = "Close all folds" }, + { key = "scroll_down", fn = scroll_diff(""), desc = "Scroll diff down" }, + { key = "scroll_up", fn = scroll_diff(""), desc = "Scroll diff up" }, } local map_options = { noremap = true, silent = true, nowait = true } local lifecycle = require("codediff.ui.lifecycle") diff --git a/tests/fixtures/keymap_matrix.txt b/tests/fixtures/keymap_matrix.txt index 5d7fcb8a..19f40cd7 100644 --- a/tests/fixtures/keymap_matrix.txt +++ b/tests/fixtures/keymap_matrix.txt @@ -111,6 +111,8 @@ modified o ih Hunk textobject modified v ih Hunk textobject panel n - Toggle stage/unstage panel n <2-LeftMouse> Select file +panel n Scroll diff down +panel n Scroll diff up panel n Select/toggle entry panel n K Show full path panel n R Refresh explorer @@ -188,6 +190,8 @@ modified o ih Hunk textobject modified v ih Hunk textobject panel n - Toggle stage/unstage panel n <2-LeftMouse> Select file +panel n Scroll diff down +panel n Scroll diff up panel n Select/toggle entry panel n K Show full path panel n R Refresh explorer diff --git a/tests/integration/keymap/actions_spec.lua b/tests/integration/keymap/actions_spec.lua index 3071c7c0..8d4e75a3 100644 --- a/tests/integration/keymap/actions_spec.lua +++ b/tests/integration/keymap/actions_spec.lua @@ -103,6 +103,8 @@ local EXPECT = { fold_toggle_recursive = { "explorer" }, fold_open_all = { "explorer" }, fold_close_all = { "explorer" }, + scroll_down = { "explorer" }, + scroll_up = { "explorer" }, }, history = { select = { "history" }, @@ -116,6 +118,8 @@ local EXPECT = { fold_toggle_recursive = { "history" }, fold_open_all = { "history" }, fold_close_all = { "history" }, + scroll_down = { "history" }, + scroll_up = { "history" }, }, conflict = { accept_incoming = { "conflict" }, diff --git a/tests/integration/ui/explorer/scroll_independence_spec.lua b/tests/integration/ui/explorer/scroll_independence_spec.lua index cabd5442..03b5de66 100644 --- a/tests/integration/ui/explorer/scroll_independence_spec.lua +++ b/tests/integration/ui/explorer/scroll_independence_spec.lua @@ -149,4 +149,24 @@ describe("explorer scroll independence", function() assert.are.equal(mod_top, topline(session.modified_win), "modified diff pane must stay put while the explorer scrolls") assert.are.equal(orig_top, topline(session.original_win), "original diff pane must stay put while the explorer scrolls") end) + + it("scrolls the diff panes from the explorer with scroll_down/scroll_up", function() + repo = busy_repo() + local _, session, explorer = open_codediff_and_wait(repo) + vim.api.nvim_win_set_cursor(session.modified_win, { 1, 0 }) + vim.api.nvim_set_current_win(explorer.winid) + local explorer_top = topline(explorer.winid) + + vim.api.nvim_feedkeys(vim.keycode(""), "x", false) + vim.cmd("redraw") + local mod_top = topline(session.modified_win) + assert.is_true(mod_top > 1, "modified diff pane should scroll down") + assert.is_true(topline(session.original_win) > 1, "original diff pane should follow via scrollbind") + assert.are.equal(explorer.winid, vim.api.nvim_get_current_win(), "focus must stay in the explorer") + assert.are.equal(explorer_top, topline(explorer.winid), "explorer must not scroll") + + vim.api.nvim_feedkeys(vim.keycode(""), "x", false) + vim.cmd("redraw") + assert.is_true(topline(session.modified_win) < mod_top, "modified diff pane should scroll back up") + end) end)