From e8c3c515291d366df287f4b0925b65d1e40f10fb Mon Sep 17 00:00:00 2001 From: delphinus Date: Mon, 5 Oct 2026 12:46:51 +0900 Subject: [PATCH] fix(scroll): sync panes scrolled by the mouse wheel without focus Native scrollbind only syncs from the current window, while the mouse wheel scrolls the window under the pointer without making it current. Hovering an unfocused diff pane therefore scrolled it alone, and the next scroll in the focused pane snapped it back. On WinScrolled, replay Neovim's own scrollbind sync from a CodeDiff pane that scrolled without focus. Focused scrolling is untouched. Refs #286 Co-Authored-By: Claude Opus 5.5 (1M context) --- lua/codediff/ui/lifecycle/cleanup.lua | 25 +++++++++ tests/e2e/view/mouse_scroll_spec.lua | 78 +++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 tests/e2e/view/mouse_scroll_spec.lua diff --git a/lua/codediff/ui/lifecycle/cleanup.lua b/lua/codediff/ui/lifecycle/cleanup.lua index 1cdb874d..1f7fb8bc 100644 --- a/lua/codediff/ui/lifecycle/cleanup.lua +++ b/lua/codediff/ui/lifecycle/cleanup.lua @@ -139,6 +139,31 @@ function M.setup_autocmds() end, }) + -- The mouse wheel scrolls the window under the pointer, but Neovim syncs + -- 'scrollbind' only from the current window. Replay the sync from a pane + -- scrolled without focus, so hovering a fresh view scrolls its peers too. + vim.api.nvim_create_autocmd("WinScrolled", { + group = augroup, + callback = function() + local diff = session.get_active_diffs()[vim.api.nvim_get_current_tabpage()] + if not diff then + return + end + + local current_win = vim.api.nvim_get_current_win() + for id, delta in pairs(vim.v.event) do + local win = tonumber(id) + if win and win ~= current_win and delta.topline ~= 0 and vim.api.nvim_win_is_valid(win) and owns_window(diff, win) and vim.wo[win].scrollbind then + -- A no-op normal command runs the native scrollbind check with this + -- pane as curwin, which keeps the panes' relative offset intact. + vim.api.nvim_win_call(win, function() + vim.cmd([[execute "normal! \"]]) + end) + end + end + end, + }) + -- When a window is closed, check if we should cleanup the diff vim.api.nvim_create_autocmd("WinClosed", { group = augroup, diff --git a/tests/e2e/view/mouse_scroll_spec.lua b/tests/e2e/view/mouse_scroll_spec.lua new file mode 100644 index 00000000..eae570b9 --- /dev/null +++ b/tests/e2e/view/mouse_scroll_spec.lua @@ -0,0 +1,78 @@ +local h = require("tests.support.e2e") + +-- Neovim syncs 'scrollbind' only from the current window, while the mouse +-- wheel scrolls the window under the pointer. CodeDiff replays the sync so a +-- pane scrolled without focus still moves its peers. +describe("mouse wheel scrolling E2E", function() + local repo, screen + before_each(function() + repo = h.repo() + local lines = {} + for i = 1, 120 do + lines[i] = string.format("line-%03d", i) + end + repo.write_file("long.txt", lines) + repo.commit("add long file") + repo.replace("long.txt", 3, "working-003") + screen = h.screen("polling", "side-by-side") + h.open(screen, repo, "CodeDiff -- long.txt", "long.txt", "long.txt") + end) + after_each(function() + h.close(screen, repo) + screen, repo = nil, nil + end) + + local function focus(target) + screen:exec( + [[ + local s, target = refresh_session(), ... + local win = target == "panel" and s.panel.view.winid or s[target .. "_win"] + vim.api.nvim_set_current_win(win) + ]], + { target } + ) + end + + local function wheel(side, count) + local p = screen:exec( + [[ + local pos = vim.api.nvim_win_get_position(refresh_session()[(...) .. "_win"]) + return { pos[1] + 2, pos[2] + 2 } + ]], + { side } + ) + for _ = 1, count do + screen:request("nvim_input_mouse", { "wheel", "down", "", 0, p[1], p[2] }) + end + h.idle(screen) + end + + local function toplines() + local panes = h.panes(screen) + return { panes.original.view[1], panes.modified.view[1] } + end + + local function expect_toplines(expected) + screen:await(function() + return vim.deep_equal(toplines(), expected) + end, "expected toplines " .. vim.inspect(expected) .. ", got " .. vim.inspect(toplines())) + end + + it("[M01] scrolls both panes when the wheel is used over a pane while the explorer has focus", function() + focus("panel") + expect_toplines({ 1, 1 }) + wheel("modified", 2) + expect_toplines({ 7, 7 }) + wheel("original", 1) + expect_toplines({ 10, 10 }) + end) + + it("[M02] keeps panes aligned when the focused pane scrolls after an unfocused one", function() + focus("original") + wheel("modified", 2) + expect_toplines({ 7, 7 }) + -- Neovim tracks each pane's bound position; a stale one would snap back. + wheel("original", 1) + expect_toplines({ 10, 10 }) + end) +end)