diff --git a/lua/codediff/scrollsync.lua b/lua/codediff/scrollsync.lua deleted file mode 100644 index 9fda6b6a..00000000 --- a/lua/codediff/scrollsync.lua +++ /dev/null @@ -1,513 +0,0 @@ --- Scroll synchronization for aligned diff windows. --- --- Replaces Neovim's built-in `scrollbind` for codediff's side-by-side and --- 3-way (conflict/merge) views. Native `scrollbind` (the non-diff, generic --- path) tracks position with a display-line count (`get_vtopline`) that becomes --- discontinuous when a single virt_lines/diff filler block is taller than the --- window (`w_topfill` is clamped). That discontinuity makes the bound window --- oscillate between the correct position and a wrong one, redrawing the whole --- pane each step -> visible flicker. Built-in `:diffthis` avoids this because it --- syncs with `diff_set_topline` (a structural, diff-block mapping) instead. --- --- This module ports that structural principle: it maps a window's scroll --- position to a shared "virtual row" coordinate (each real line plus its filler --- rows) and, on every scroll, sets the other windows to the same virtual row. --- Because the mapping is structural, not display-line based, it has no clamp --- discontinuity and produces zero flicker while scrolling through a full-screen --- filler block, in either direction. --- --- The alignment is derived from the fillers actually present in each window's --- buffer (any `virt_lines` extmark), so the module stays decoupled from how the --- diff is computed and automatically reflects fillers added by the renderer. --- --- Assumes diff windows use `nowrap` (codediff does), so each real line occupies --- exactly one screen row. - -local api = vim.api - -local M = {} - --- --------------------------------------------------------------------------- --- Virtual-row mapping --- --------------------------------------------------------------------------- - ---- Build a sparse cumulative filler table for a buffer. ---- Reads every `virt_lines` extmark (all namespaces) so the mapping matches ---- Neovim's own fill accounting and covers alignment fillers plus any other ---- full-line virtual text. ---- @param bufnr integer ---- @return table ft { line_count, anchors (sorted 1-indexed), cum, total_fill } -local function build_fill_table(bufnr) - local line_count = api.nvim_buf_line_count(bufnr) - local marks = api.nvim_buf_get_extmarks(bufnr, -1, 0, -1, { details = true }) - - local by_anchor = {} - for _, m in ipairs(marks) do - local details = m[4] - local vlines = details and details.virt_lines - if vlines and #vlines > 0 then - local row = m[2] -- 0-indexed row the extmark is attached to - -- `count` filler rows sit immediately ABOVE real line `anchor` (1-indexed). - -- virt_lines_above => above line (row+1); otherwise below it => above (row+2). - local anchor = details.virt_lines_above and (row + 1) or (row + 2) - if anchor < 1 then - anchor = 1 - elseif anchor > line_count + 1 then - anchor = line_count + 1 - end - by_anchor[anchor] = (by_anchor[anchor] or 0) + #vlines - end - end - - local anchors = {} - for a in pairs(by_anchor) do - anchors[#anchors + 1] = a - end - table.sort(anchors) - - local cum = {} - local running = 0 - for i, a in ipairs(anchors) do - running = running + by_anchor[a] - cum[i] = running - end - - return { line_count = line_count, anchors = anchors, cum = cum, total_fill = running } -end - ---- Total filler rows whose anchor line is <= L. O(log fillers). -local function cumfill_le(ft, l) - local anchors = ft.anchors - local n = #anchors - if n == 0 or l < anchors[1] then - return 0 - end - local lo, hi = 1, n - while lo < hi do - local mid = math.floor((lo + hi + 1) / 2) - if anchors[mid] <= l then - lo = mid - else - hi = mid - 1 - end - end - return ft.cum[lo] -end - ---- 0-indexed virtual row of the text of real line L. -local function vrow_of_line(ft, l) - return (l - 1) + cumfill_le(ft, l) -end - ---- Map a window view {topline, topfill} to its top virtual row. -local function view_to_vrow(ft, topline, topfill) - if topline < 1 then - topline = 1 - elseif topline > ft.line_count then - topline = ft.line_count - end - return vrow_of_line(ft, topline) - (topfill or 0) -end - ---- Inverse: for a target virtual row, the (topline, topfill) that puts that ---- virtual row at the top of the window. topfill is NOT display-clamped here; ---- callers clamp against the actual window height. -local function vrow_to_view(ft, vrow) - local lc = ft.line_count - if vrow <= 0 then - return 1, 0 - end - if vrow_of_line(ft, lc) < vrow then - -- Past the last line's text (into trailing fillers or EOF): pin to last line. - return lc, 0 - end - -- Smallest real line whose text vrow >= target. - local lo, hi = 1, lc - while lo < hi do - local mid = math.floor((lo + hi) / 2) - if vrow_of_line(ft, mid) >= vrow then - hi = mid - else - lo = mid + 1 - end - end - local topline = lo - local topfill = vrow_of_line(ft, topline) - vrow - if topfill < 0 then - topfill = 0 - end - return topline, topfill -end - --- --------------------------------------------------------------------------- --- Group --- --------------------------------------------------------------------------- - -local Group = {} -Group.__index = Group - -local function view_fp(v) - return string.format("%d,%d,%d", v.topline or 0, v.topfill or 0, v.leftcol or 0) -end - -local function win_view(w) - return api.nvim_win_call(w, vim.fn.winsaveview) -end - -local function changed_windows(event, first) - local changed = {} - for key in pairs(event or {}) do - local win = tonumber(key) - if win then - changed[win] = true - end - end - local first_win = tonumber(first) - if first_win then - changed[first_win] = true - end - return next(changed) and changed or nil -end - -function Group:_valid_wins() - local out = {} - for _, w in ipairs(self.wins) do - if api.nvim_win_is_valid(w) then - out[#out + 1] = w - end - end - return out -end - -function Group:_is_member(w) - for _, m in ipairs(self.wins) do - if m == w then - return true - end - end - return false -end - -function Group:_restore_scrolloff(w) - local saved = self.saved_scrolloff[w] - if saved == nil then - return - end - self.saved_scrolloff[w] = nil - if api.nvim_win_is_valid(w) then - api.nvim_set_option_value("scrolloff", saved, { win = w, scope = "local" }) - end -end - -function Group:_invalidate_pending_echo(w) - self.pending_echo_generation[w] = (self.pending_echo_generation[w] or 0) + 1 - self.pending_echo[w] = nil -end - -function Group:_mark_pending_echo(w) - local generation = (self.pending_echo_generation[w] or 0) + 1 - self.pending_echo_generation[w] = generation - self.pending_echo[w] = true -end - --- A filler correction can emit another WinScrolled in the same redraw turn. --- Keep ownership of the follower until that turn has fully drained. -function Group:_settle_pending_echo(w) - local generation = self.pending_echo_generation[w] - if not generation or not self.pending_echo[w] then - return - end - vim.schedule(function() - if self.active and self.pending_echo_generation[w] == generation then - self.pending_echo[w] = nil - end - end) -end - ---- Rebuild the per-window fill tables from current buffer fillers. Call after ---- the diff/fillers are re-rendered. -function Group:refresh() - for w in pairs(self.saved_scrolloff) do - self:_restore_scrolloff(w) - end - self.ft = {} - for _, w in ipairs(self:_valid_wins()) do - self.ft[w] = build_fill_table(api.nvim_win_get_buf(w)) - end - -- Fill tables changed; stored fingerprints are stale. - self.expected = {} - self.pending_echo = {} - -- Invalidate scheduled echo-settling callbacks without resetting their - -- generation numbers (which could let an old callback match a new echo). - for w, generation in pairs(self.pending_echo_generation) do - self.pending_echo_generation[w] = generation + 1 - end -end - ---- Align every window other than `leader` to `leader`'s virtual-row position. ---- Mirrors horizontal scroll (leftcol). Uses a RELATIVE scroll by the computed ---- display-row delta so Neovim keeps its scroll optimization (hardware ---- `grid_scroll` + one drawn row), matching native scrollbind's rendering cost; ---- falls back to an absolute `winrestview` only if the relative scroll cannot ---- land exactly (buffer boundaries). `jump` forces the absolute path (used for ---- one-shot re-alignment where a large jump is expected). -function Group:_apply(leader, jump) - local ftl = self.ft[leader] - if not ftl then - return - end - self:_invalidate_pending_echo(leader) - self:_restore_scrolloff(leader) - local lv = win_view(leader) - local vrow = view_to_vrow(ftl, lv.topline, lv.topfill) - self.vrow = vrow - local leftcol = lv.leftcol or 0 - - for _, w in ipairs(self:_valid_wins()) do - if w ~= leader then - local ft = self.ft[w] - if ft then - local target_tl, target_tf = vrow_to_view(ft, vrow) - -- Clamp topfill to what the window can actually display (Neovim - -- mishandles a stored topfill larger than the window height). - local maxfill = api.nvim_win_get_height(w) - 1 - if maxfill < 0 then - maxfill = 0 - end - if target_tf > maxfill then - target_tf = maxfill - end - - local cur = win_view(w) - local target_top_vrow = vrow_of_line(ft, target_tl) - target_tf - local cur_top_vrow = vrow_of_line(ft, cur.topline) - (cur.topfill or 0) - local delta = target_top_vrow - cur_top_vrow - - if delta ~= 0 then - self:_mark_pending_echo(w) - if not jump then - -- Relative scroll (/) preserves the grid_scroll - -- optimization so the follower is not fully repainted. - local n = math.abs(delta) - local key = delta > 0 and "\5" or "\25" -- / - api.nvim_win_call(w, function() - vim.cmd("normal! " .. n .. key) - end) - end - -- Correct to the exact target if a jump was requested or the relative - -- scroll fell short (e.g. near the top/bottom of the buffer). - local after = win_view(w) - if jump or after.topline ~= target_tl or (after.topfill or 0) ~= target_tf then - local scrolloff = api.nvim_get_option_value("scrolloff", { win = w }) - if scrolloff > 0 and self.saved_scrolloff[w] == nil then - self.saved_scrolloff[w] = api.nvim_get_option_value("scrolloff", { win = w, scope = "local" }) - api.nvim_set_option_value("scrolloff", 0, { win = w, scope = "local" }) - end - api.nvim_win_call(w, function() - vim.fn.winrestview({ topline = target_tl, topfill = target_tf, lnum = target_tl }) - end) - end - end - - -- Mirror horizontal scroll (leftcol) without disturbing the vertical view. - local fv = win_view(w) - if (fv.leftcol or 0) ~= leftcol then - self:_mark_pending_echo(w) - api.nvim_win_call(w, function() - local v = vim.fn.winsaveview() - v.leftcol = leftcol - vim.fn.winrestview(v) - end) - end - - self.expected[w] = view_fp(win_view(w)) - end - end - end - self.expected[leader] = view_fp(lv) -end - ---- Force a sync now, using `leader` (defaults to the current window if it is a ---- member, else the first window) as the source of truth. Uses the absolute ---- (jump) path since the windows may be far out of sync. -function Group:resync(leader) - if leader == nil or not (leader and api.nvim_win_is_valid(leader) and self:_is_member(leader)) then - local cur = api.nvim_get_current_win() - leader = self:_is_member(cur) and cur or self:_valid_wins()[1] - end - if not leader then - return - end - self.syncing = true - local ok, err = pcall(function() - self:_apply(leader, true) - end) - self.syncing = false - if not ok then - vim.notify_once("[codediff] scrollsync resync error: " .. tostring(err), vim.log.levels.DEBUG) - end -end - ---- Detect which member window the user actually scrolled by comparing each ---- changed window's current view against the fingerprint we last recorded. This ---- catches topfill-only scrolls and mouse scrolls of a non-focused window while ---- avoiding unrelated window events and pending follower redraw echoes. -function Group:_detect_leader(changed) - local cur = api.nvim_get_current_win() - local candidate - for _, w in ipairs(self:_valid_wins()) do - if not changed or changed[w] then - local fp = view_fp(win_view(w)) - if fp ~= self.expected[w] then - if w == cur then - return w -- prefer the focused window when it moved - end - candidate = candidate or w - end - end - end - return candidate -end - -function Group:_consume_pending_echoes(changed) - if not changed then - return - end - local current = api.nvim_get_current_win() - for w in pairs(changed) do - if w ~= current and self.pending_echo[w] then - self:_restore_scrolloff(w) - self.expected[w] = view_fp(win_view(w)) - self:_settle_pending_echo(w) - end - end -end - -function Group:_on_scroll(changed) - if self.syncing then - return - end - self:_consume_pending_echoes(changed) - if self.paused then - return - end - local leader = self:_detect_leader(changed) - if not leader then - return - end - self.syncing = true - local ok, err = pcall(function() - self:_apply(leader) - end) - self.syncing = false - if not ok then - vim.notify_once("[codediff] scrollsync error: " .. tostring(err), vim.log.levels.DEBUG) - end -end - ---- Temporarily stop syncing (e.g. while a feature imposes its own alignment). -function Group:pause() - self.paused = true -end - ---- Resume syncing and immediately re-align from the current window. -function Group:resume() - self.paused = false - self:resync() - for w in pairs(self.saved_scrolloff) do - self:_restore_scrolloff(w) - end -end - ---- Tear down: remove autocmds and mark inactive. -function Group:unbind() - if self.augroup then - pcall(api.nvim_del_augroup_by_id, self.augroup) - self.augroup = nil - end - for w in pairs(self.saved_scrolloff) do - self:_restore_scrolloff(w) - end - self.active = false -end - --- --------------------------------------------------------------------------- --- Public API --- --------------------------------------------------------------------------- - ---- Bind a set of windows so they scroll together, aligned by the fillers in ---- their buffers. Returns a group handle. ---- @param opts { wins: integer[] } ---- @return table group -function M.bind(opts) - assert(opts and opts.wins and #opts.wins >= 2, "scrollsync.bind requires at least two windows") - - local self = setmetatable({ - wins = vim.deepcopy(opts.wins), - ft = {}, - expected = {}, - pending_echo = {}, - pending_echo_generation = {}, - saved_scrolloff = {}, - syncing = false, - paused = false, - active = true, - vrow = 0, - }, Group) - - self:refresh() - -- Seed fingerprints from the current views so the first real scroll is - -- detected as a leader move (and our own echoes are not). - for _, w in ipairs(self:_valid_wins()) do - self.expected[w] = view_fp(win_view(w)) - end - - self.augroup = api.nvim_create_augroup("codediff_scrollsync_" .. tostring(self):gsub("%W", ""), { clear = true }) - api.nvim_create_autocmd("WinScrolled", { - group = self.augroup, - callback = function(args) - if not self.active then - return - end - -- Only react when a member window is involved. - self:_on_scroll(changed_windows(vim.v.event, args.match)) - end, - }) - -- A height change alters the topfill clamp; re-align to stay consistent. - api.nvim_create_autocmd({ "WinResized", "VimResized" }, { - group = self.augroup, - callback = function() - if not self.active or self.paused then - return - end - self.syncing = true - pcall(function() - self:_apply(self:_detect_leader() or api.nvim_get_current_win(), true) - end) - self.syncing = false - end, - }) - -- Auto-teardown if any member window closes. - api.nvim_create_autocmd("WinClosed", { - group = self.augroup, - callback = function(args) - local closed = tonumber(args.match) - if closed and self:_is_member(closed) then - self:unbind() - end - end, - }) - - return self -end - --- Exposed for tests. -M._internal = { - build_fill_table = build_fill_table, - changed_windows = changed_windows, - vrow_of_line = vrow_of_line, - view_to_vrow = view_to_vrow, - vrow_to_view = vrow_to_view, -} - -return M diff --git a/lua/codediff/ui/auto_refresh.lua b/lua/codediff/ui/auto_refresh.lua index b650ffc5..d1d9598c 100644 --- a/lua/codediff/ui/auto_refresh.lua +++ b/lua/codediff/ui/auto_refresh.lua @@ -103,9 +103,12 @@ local function do_diff_update(bufnr, skip_watcher_check) -- Refresh compact mode folds if active require("codediff.ui.view.compact").refresh(tabpage) - -- Check if this is an inline mode session + -- Check if the session still owns the tab after the asynchronous diff. local session = lifecycle.get_session(tabpage) - if session and session.layout == "inline" then + if not session then + return + end + if session.layout == "inline" then local inline_mod = require("codediff.ui.inline") inline_mod.render_inline_diff(modified_bufnr, lines_diff, original_lines, modified_lines) return @@ -114,36 +117,54 @@ local function do_diff_update(bufnr, skip_watcher_check) -- Side-by-side mode: Update decorations on both buffers core.render_diff(original_bufnr, modified_bufnr, original_lines, modified_lines, lines_diff) - -- Re-align scroll-sync after filler changes so all panes stay aligned even - -- if fillers were added/removed. The structural sync preserves each pane's - -- scroll position (no reset-to-top / view save-restore dance needed). - local original_win, modified_win, result_win = nil, nil, nil - local _, stored_result_win = lifecycle.get_result(tabpage) - - for _, win in ipairs(vim.api.nvim_list_wins()) do - local buf = vim.api.nvim_win_get_buf(win) - if buf == original_bufnr then - original_win = win - elseif buf == modified_bufnr then - modified_win = win - end - end + -- Re-sync scrollbind after filler changes. Use the session's windows rather + -- than searching by buffer, because a duplicated pane may show the same + -- buffer without belonging to the diff. + local original_win = session.original_win + local modified_win = session.modified_win + local _, result_win = lifecycle.get_result(tabpage) - -- Check if result window is valid - if stored_result_win and vim.api.nvim_win_is_valid(stored_result_win) then - result_win = stored_result_win - end - - if original_win and modified_win then + if original_win and modified_win and original_win ~= modified_win and vim.api.nvim_win_is_valid(original_win) and vim.api.nvim_win_is_valid(modified_win) then local current_win = vim.api.nvim_get_current_win() - local scroll = require("codediff.ui.scroll") - local wins = { original_win, modified_win } - if result_win then - wins[#wins + 1] = result_win + if current_win == original_win or current_win == modified_win or current_win == result_win then + local other_win = current_win == original_win and modified_win or original_win + + local saved_view = vim.fn.winsaveview() + vim.api.nvim_set_current_win(other_win) + local other_saved_view = vim.fn.winsaveview() + local result_saved_view + if result_win then + vim.api.nvim_set_current_win(result_win) + result_saved_view = vim.fn.winsaveview() + end + vim.api.nvim_set_current_win(current_win) + + vim.api.nvim_win_set_cursor(original_win, { 1, 0 }) + vim.api.nvim_win_set_cursor(modified_win, { 1, 0 }) + if result_win then + vim.api.nvim_win_set_cursor(result_win, { 1, 0 }) + end + + vim.wo[original_win].scrollbind = false + vim.wo[modified_win].scrollbind = false + if result_win then + vim.wo[result_win].scrollbind = false + end + vim.wo[original_win].scrollbind = true + vim.wo[modified_win].scrollbind = true + if result_win then + vim.wo[result_win].scrollbind = true + end + + vim.api.nvim_set_current_win(other_win) + vim.fn.winrestview(other_saved_view) + if result_win and result_saved_view then + vim.api.nvim_set_current_win(result_win) + vim.fn.winrestview(result_saved_view) + end + vim.api.nvim_set_current_win(current_win) + vim.fn.winrestview(saved_view) end - scroll.bind(tabpage, wins) - local leader = (current_win == original_win or current_win == modified_win or current_win == result_win) and current_win or modified_win - scroll.refresh(tabpage, leader) end end) end diff --git a/lua/codediff/ui/conflict/view/inputs.lua b/lua/codediff/ui/conflict/view/inputs.lua index 04a142b0..53acc212 100644 --- a/lua/codediff/ui/conflict/view/inputs.lua +++ b/lua/codediff/ui/conflict/view/inputs.lua @@ -42,19 +42,14 @@ function M.compute_and_render_conflict(original_buf, modified_buf, base_lines, o -- Render merge view with alignment and filler lines local render_result = core.render_merge_view(original_buf, modified_buf, base_to_original_diff, base_to_modified_diff, base_lines, original_lines, modified_lines) - -- Setup window options with structural scroll-sync (filler lines enable proper alignment) + -- Enable native scrollbind for the two conflict input panes. if original_win and modified_win and vim.api.nvim_win_is_valid(original_win) and vim.api.nvim_win_is_valid(modified_win) then vim.wo[original_win].wrap = false vim.wo[modified_win].wrap = false - - -- Reset scroll position and bind the two panes (the result pane, if any, - -- is added to the group later once it exists). vim.api.nvim_win_set_cursor(original_win, { 1, 0 }) vim.api.nvim_win_set_cursor(modified_win, { 1, 0 }) - local scroll = require("codediff.ui.scroll") - local tabpage = vim.api.nvim_win_get_tabpage(modified_win) - scroll.bind(tabpage, { original_win, modified_win }) - scroll.resync(tabpage, modified_win) + vim.wo[original_win].scrollbind = true + vim.wo[modified_win].scrollbind = true -- Scroll to first change in either buffer if auto_scroll_to_first_hunk then diff --git a/lua/codediff/ui/conflict/view/result.lua b/lua/codediff/ui/conflict/view/result.lua index b5e4839c..797a2c45 100644 --- a/lua/codediff/ui/conflict/view/result.lua +++ b/lua/codediff/ui/conflict/view/result.lua @@ -122,11 +122,9 @@ function M.setup_conflict_result_window(tabpage, session_config, original_win, m vim.wo[result_win].wrap = false vim.wo[result_win].cursorline = true - -- Add the result window to the structural scroll-sync group (all 3 panes). + -- Enable native scrollbind for the result pane as the third bound window. vim.api.nvim_win_set_cursor(result_win, { 1, 0 }) - local scroll = require("codediff.ui.scroll") - scroll.bind(tabpage, { original_win, modified_win, result_win }) - scroll.resync(tabpage, modified_win) + vim.wo[result_win].scrollbind = true -- Update lifecycle with result buffer/window FIRST -- (This must happen before setting winbar so ensure_no_winbar knows we're in conflict mode) diff --git a/lua/codediff/ui/init.lua b/lua/codediff/ui/init.lua index 26dff662..6708a54b 100644 --- a/lua/codediff/ui/init.lua +++ b/lua/codediff/ui/init.lua @@ -5,18 +5,12 @@ local highlights = require("codediff.ui.highlights") local view = require("codediff.ui.view") local core = require("codediff.ui.core") local lifecycle = require("codediff.ui.lifecycle") --- Eagerly load the scroll-sync manager so it is cached at setup time (like the --- other UI submodules). It is otherwise required lazily from render/lifecycle --- code paths that can run after the cwd has changed, where a not-yet-cached --- module may fail to resolve on a relative runtimepath. -local scroll = require("codediff.ui.scroll") -- Public functions M.setup_highlights = highlights.setup M.create_diff_view = view.create M.update_diff_view = view.update M.render_diff = core.render_diff -M.scroll = scroll -- lifecycle.setup() is called on first view.create() via a once-guard, -- so autocmds are only registered when actually needed. diff --git a/lua/codediff/ui/lifecycle/cleanup.lua b/lua/codediff/ui/lifecycle/cleanup.lua index c32f5736..9984efe8 100644 --- a/lua/codediff/ui/lifecycle/cleanup.lua +++ b/lua/codediff/ui/lifecycle/cleanup.lua @@ -78,15 +78,18 @@ local function cleanup_diff(tabpage) -- Clear window variables if windows still exist if diff.original_win and vim.api.nvim_win_is_valid(diff.original_win) then welcome_window.apply_normal(diff.original_win) + vim.wo[diff.original_win].scrollbind = false vim.w[diff.original_win].codediff_restore = nil end if diff.modified_win and vim.api.nvim_win_is_valid(diff.modified_win) then welcome_window.apply_normal(diff.modified_win) + vim.wo[diff.modified_win].scrollbind = false vim.w[diff.modified_win].codediff_restore = nil end -- Clear result window variable if exists (conflict mode) if diff.result_win and vim.api.nvim_win_is_valid(diff.result_win) then + vim.wo[diff.result_win].scrollbind = false vim.w[diff.result_win].codediff_restore = nil end @@ -97,15 +100,18 @@ local function cleanup_diff(tabpage) pcall(vim.api.nvim_del_augroup_by_name, "codediff_lifecycle_tab_" .. tabpage) pcall(vim.api.nvim_del_augroup_by_name, "codediff_working_sync_" .. tabpage) - -- Tear down the scroll-sync group for this tab - pcall(function() - require("codediff.ui.scroll").teardown(tabpage) - end) - -- Remove from tracking active_diffs[tabpage] = nil end +local function owns_window(diff, winid) + if diff.original_win == winid or diff.modified_win == winid or diff.result_win == winid then + return true + end + local panel_view = diff.panel and diff.panel.view + return panel_view and panel_view.winid == winid or false +end + -- Count windows in current tabpage that have diff markers local function count_diff_windows() local count = 0 @@ -125,6 +131,24 @@ end -- Setup autocmds for automatic cleanup function M.setup_autocmds() + -- `scrollbind` is copied by :split. Clear it from windows that are not + -- owned by CodeDiff before a duplicated pane can mirror the diff. + vim.api.nvim_create_autocmd("WinNew", { + group = augroup, + callback = function() + local tabpage = vim.api.nvim_get_current_tabpage() + local diff = session.get_active_diffs()[tabpage] + if not diff then + return + end + + local new_win = vim.api.nvim_get_current_win() + if vim.api.nvim_win_is_valid(new_win) and not owns_window(diff, new_win) and vim.wo[new_win].scrollbind then + vim.wo[new_win].scrollbind = false + 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/lua/codediff/ui/lifecycle/state.lua b/lua/codediff/ui/lifecycle/state.lua index 9c938325..ec929e79 100644 --- a/lua/codediff/ui/lifecycle/state.lua +++ b/lua/codediff/ui/lifecycle/state.lua @@ -223,9 +223,7 @@ local function resume_diff(tabpage) core.render_diff(diff.original_bufnr, diff.modified_bufnr, original_lines, modified_lines, lines_diff) end - -- Re-align scroll-sync ONLY if diff was recomputed and not inline mode. - -- Fillers may have changed, so rebuild the alignment and re-sync in place - -- (the structural sync preserves scroll position, no reset-to-top needed). + -- Re-sync scrollbind ONLY if diff was recomputed and not inline mode. if diff_was_recomputed and diff.layout ~= "inline" @@ -237,22 +235,38 @@ local function resume_diff(tabpage) local current_win = vim.api.nvim_get_current_win() local result_win = diff.result_win and vim.api.nvim_win_is_valid(diff.result_win) and diff.result_win or nil - -- Re-apply critical window options that might have been reset - vim.wo[diff.original_win].wrap = false - vim.wo[diff.modified_win].wrap = false - if result_win then - vim.wo[result_win].wrap = false + if current_win == diff.original_win or current_win == diff.modified_win or current_win == result_win then + local saved_cursor = vim.api.nvim_win_get_cursor(current_win) + + vim.api.nvim_win_set_cursor(diff.original_win, { 1, 0 }) + vim.api.nvim_win_set_cursor(diff.modified_win, { 1, 0 }) + if result_win then + vim.api.nvim_win_set_cursor(result_win, { 1, 0 }) + end + + vim.wo[diff.original_win].scrollbind = false + vim.wo[diff.modified_win].scrollbind = false + if result_win then + vim.wo[result_win].scrollbind = false + end + vim.wo[diff.original_win].scrollbind = true + vim.wo[diff.modified_win].scrollbind = true + if result_win then + vim.wo[result_win].scrollbind = true + end + + vim.wo[diff.original_win].wrap = false + vim.wo[diff.modified_win].wrap = false + if result_win then + vim.wo[result_win].wrap = false + end + + pcall(vim.api.nvim_win_set_cursor, diff.original_win, saved_cursor) + pcall(vim.api.nvim_win_set_cursor, diff.modified_win, saved_cursor) + if result_win then + pcall(vim.api.nvim_win_set_cursor, result_win, saved_cursor) + end end - - local scroll = require("codediff.ui.scroll") - local wins = { diff.original_win, diff.modified_win } - if result_win then - wins[#wins + 1] = result_win - end - scroll.bind(tabpage, wins) - -- Prefer the focused diff window as the alignment leader. - local leader = (current_win == diff.original_win or current_win == diff.modified_win or current_win == result_win) and current_win or diff.modified_win - scroll.refresh(tabpage, leader) end end diff --git a/lua/codediff/ui/scroll.lua b/lua/codediff/ui/scroll.lua deleted file mode 100644 index 51adf81f..00000000 --- a/lua/codediff/ui/scroll.lua +++ /dev/null @@ -1,121 +0,0 @@ --- Session-level scroll-sync manager. --- --- Owns one `codediff.scrollsync` group per diff tabpage and exposes the small --- surface the view/lifecycle code needs. This replaces every use of Neovim's --- native `scrollbind` in codediff (see codediff.scrollsync for why native --- scrollbind flickers with tall virt_lines fillers). - -local scrollsync = require("codediff.scrollsync") - -local M = {} - --- tabpage -> { group, wins } -local groups = {} - -local function same_wins(a, b) - if #a ~= #b then - return false - end - local set = {} - for _, w in ipairs(a) do - set[w] = true - end - for _, w in ipairs(b) do - if not set[w] then - return false - end - end - return true -end - ---- (Re)bind the diff windows for `tabpage` so they scroll together via the ---- structural virtual-row sync. Turns off native scrollbind defensively. ---- Reuses the existing group when the window set is unchanged. ---- @param tabpage integer ---- @param wins integer[] ---- @return table|nil group -function M.bind(tabpage, wins) - local valid, seen = {}, {} - for _, w in ipairs(wins or {}) do - if w and vim.api.nvim_win_is_valid(w) and not seen[w] then - seen[w] = true - valid[#valid + 1] = w - -- Ensure native scrollbind is off so it can't re-introduce the bug. - pcall(function() - vim.wo[w].scrollbind = false - end) - end - end - - if #valid < 2 then - M.teardown(tabpage) - return nil - end - - local existing = groups[tabpage] - if existing and existing.group.active and same_wins(existing.wins, valid) then - existing.group:refresh() - return existing.group - end - - if existing then - existing.group:unbind() - end - - local group = scrollsync.bind({ wins = valid }) - groups[tabpage] = { group = group, wins = valid } - return group -end - ---- @param tabpage integer ---- @return table|nil group -function M.get(tabpage) - local entry = groups[tabpage] - return entry and entry.group or nil -end - ---- Force an immediate re-alignment (optionally from an explicit leader window). -function M.resync(tabpage, leader) - local entry = groups[tabpage] - if entry then - entry.group:resync(leader) - end -end - ---- Rebuild the alignment from current buffer fillers, then re-align. Call after ---- the diff/fillers are re-rendered. -function M.refresh(tabpage, leader) - local entry = groups[tabpage] - if entry then - entry.group:refresh() - entry.group:resync(leader) - end -end - ---- Temporarily suspend syncing (e.g. while the move-alignment feature imposes ---- its own alignment). -function M.pause(tabpage) - local entry = groups[tabpage] - if entry then - entry.group:pause() - end -end - ---- Resume syncing and re-align from the current window. -function M.resume(tabpage) - local entry = groups[tabpage] - if entry then - entry.group:resume() - end -end - ---- Remove the group for a tabpage. -function M.teardown(tabpage) - local entry = groups[tabpage] - if entry then - entry.group:unbind() - groups[tabpage] = nil - end -end - -return M diff --git a/lua/codediff/ui/view/actions/move.lua b/lua/codediff/ui/view/actions/move.lua index 9306eb4e..0c99e798 100644 --- a/lua/codediff/ui/view/actions/move.lua +++ b/lua/codediff/ui/view/actions/move.lua @@ -4,6 +4,7 @@ local M = {} local lifecycle = require("codediff.ui.lifecycle") +local render = require("codediff.ui.view.render") function M.align_move(ctx) local session = lifecycle.get_session(ctx.tabpage) @@ -61,9 +62,9 @@ function M.align_move(ctx) end) local saved_scrolloff_other = vim.wo[other_win].scrolloff - -- Pause structural scroll-sync while we impose the move alignment. - local scroll = require("codediff.ui.scroll") - scroll.pause(ctx.tabpage) + -- Disable native scrollbind while we impose the move alignment. + vim.wo[current_win].scrollbind = false + vim.wo[other_win].scrollbind = false vim.wo[other_win].scrolloff = 0 -- Align using the annotation virt_line as anchor: @@ -111,14 +112,26 @@ function M.align_move(ctx) if not vim.api.nvim_win_is_valid(current_win) or not vim.api.nvim_win_is_valid(other_win) then return end - -- Restore views first, then resume structural scroll-sync. + -- Restore views first, then re-establish native scrollbind. vim.api.nvim_win_call(other_win, function() vim.fn.winrestview(other_view) end) vim.api.nvim_win_call(current_win, function() vim.fn.winrestview(current_view) end) - scroll.resume(ctx.tabpage) + + local session = lifecycle.get_session(ctx.tabpage) + local orig_win = session and session.original_win or current_win + local mod_win = session and session.modified_win or other_win + local orig_bufnr = session and session.original_bufnr or vim.api.nvim_win_get_buf(orig_win) + local mod_bufnr = session and session.modified_bufnr or vim.api.nvim_win_get_buf(mod_win) + local diff_result = session and session.stored_diff_result + local orig_cur = { current_view.lnum, current_view.col } + local mod_cur = { other_view.lnum, other_view.col } + if not is_on_original then + orig_cur, mod_cur = mod_cur, orig_cur + end + render.establish_scrollbind(orig_win, mod_win, orig_bufnr, mod_bufnr, diff_result, orig_cur, mod_cur) end -- Restore when cursor moves out of the moved block diff --git a/lua/codediff/ui/view/render.lua b/lua/codediff/ui/view/render.lua index 45a18136..0b2d0d7e 100644 --- a/lua/codediff/ui/view/render.lua +++ b/lua/codediff/ui/view/render.lua @@ -6,33 +6,53 @@ local config = require("codediff.config") local diff_module = require("codediff.core.diff") local cursor_util = require("codediff.ui.view.cursor") ---- Establish scroll synchronization between the two diff windows. ---- Positions each pane's cursor on its own hunk line, then binds them with the ---- structural virtual-row scroll-sync (see codediff.scrollsync) and aligns the ---- original pane to the modified (primary) pane. Replaces native scrollbind so ---- a full-screen filler block no longer causes scroll flicker. +--- Establish native scrollbind between two windows using the anchor technique. +--- Anchors at the first unchanged line for insertions/deletions at the start of +--- a file, so `syncbind` establishes the correct baseline before the view is +--- positioned on the requested hunk. --- @param orig_win number --- @param mod_win number ---- @param orig_buf number (unused; kept for call-site compatibility) ---- @param mod_buf number (unused; kept for call-site compatibility) ---- @param lines_diff table (unused; alignment is derived from placed fillers) ---- @param orig_cursor table|nil: {line, col} on original side ---- @param mod_cursor table|nil: {line, col} on modified side +--- @param orig_buf number +--- @param mod_buf number +--- @param lines_diff table: diff result with .changes +--- @param orig_cursor table|nil: {line, col} to restore on original side +--- @param mod_cursor table|nil: {line, col} to restore on modified side function M.establish_scrollbind(orig_win, mod_win, orig_buf, mod_buf, lines_diff, orig_cursor, mod_cursor) - local scroll = require("codediff.ui.scroll") - local tabpage = vim.api.nvim_win_get_tabpage(mod_win) - - -- Place cursors on their respective hunk lines (per-pane coordinates). - if orig_cursor then - pcall(vim.api.nvim_win_set_cursor, orig_win, cursor_util.clamp_cursor(orig_win, orig_cursor)) + local function set_cursor(win, cursor) + if cursor then + pcall(vim.api.nvim_win_set_cursor, win, cursor_util.clamp_cursor(win, cursor)) + end end - if mod_cursor then - pcall(vim.api.nvim_win_set_cursor, mod_win, cursor_util.clamp_cursor(mod_win, mod_cursor)) + + -- When the first change is a pure insertion/deletion at line 1, filler + -- virt_lines sit above line 1 on one side. Start at the first corresponding + -- unchanged line so syncbind establishes the correct baseline. + if lines_diff and lines_diff.changes and #lines_diff.changes > 0 then + local first = lines_diff.changes[1] + local orig_empty = first.original.start_line >= first.original.end_line + local mod_empty = first.modified.start_line >= first.modified.end_line + if (first.original.start_line == 1 or first.modified.start_line == 1) and (orig_empty or mod_empty) then + local anchor_orig = math.max(first.original.end_line, 1) + local anchor_mod = math.max(first.modified.end_line, 1) + anchor_orig = math.min(anchor_orig, vim.api.nvim_buf_line_count(orig_buf)) + anchor_mod = math.min(anchor_mod, vim.api.nvim_buf_line_count(mod_buf)) + set_cursor(orig_win, { anchor_orig, 0 }) + set_cursor(mod_win, { anchor_mod, 0 }) + vim.wo[orig_win].scrollbind = true + vim.wo[mod_win].scrollbind = true + vim.cmd("syncbind") + return + end end - -- Bind the two panes and align the original pane to the modified pane. - scroll.bind(tabpage, { orig_win, mod_win }) - scroll.resync(tabpage, mod_win) + -- Normal path: establish the native baseline at line 1, then restore the + -- requested per-pane cursor positions. + set_cursor(orig_win, { 1, 0 }) + set_cursor(mod_win, { 1, 0 }) + vim.wo[orig_win].scrollbind = true + vim.wo[mod_win].scrollbind = true + set_cursor(orig_win, orig_cursor) + set_cursor(mod_win, mod_cursor) end -- Common logic: Compute diff and render highlights @@ -73,7 +93,7 @@ function M.compute_and_render( saved_cursor = vim.api.nvim_win_get_cursor(modified_win) end - -- Step 1: Ensure native scrollbind is off (replaced by structural sync) and disable wrap + -- Step 1: Disable native scrollbind while repositioning cursors and disable wrap vim.wo[original_win].scrollbind = false vim.wo[modified_win].scrollbind = false vim.wo[original_win].wrap = false @@ -133,7 +153,7 @@ function M.compute_and_render( mod_cursor = { 1, 0 } end - -- Step 3: Position cursors and bind the structural scroll-sync + -- Step 3: Position cursors and establish native scrollbind M.establish_scrollbind(original_win, modified_win, original_buf, modified_buf, lines_diff, orig_cursor, mod_cursor) -- Step 4: Center view on first hunk for initial open diff --git a/lua/codediff/ui/view/side_by_side/create.lua b/lua/codediff/ui/view/side_by_side/create.lua index 3462b5e6..bb4552ec 100644 --- a/lua/codediff/ui/view/side_by_side/create.lua +++ b/lua/codediff/ui/view/side_by_side/create.lua @@ -95,9 +95,9 @@ local function open_diff_panes(session_config) return original_win, modified_win, original_info, modified_info end ---- Window options both diff panes get. 'wrap' is load-bearing: the scroll-sync ---- maps one buffer line to one screen row. 'number'/'relativenumber' are left ---- alone so the user's own settings survive. +--- Window options both diff panes get. 'wrap' is load-bearing for native +--- scrollbind alignment. 'number'/'relativenumber' are left alone so the +--- user's own settings survive. --- @param original_win number --- @param modified_win number local function apply_pane_options(original_win, modified_win) diff --git a/tests/scrollbind_spec.lua b/tests/scrollbind_spec.lua new file mode 100644 index 00000000..37a041c4 --- /dev/null +++ b/tests/scrollbind_spec.lua @@ -0,0 +1,95 @@ +-- Regression tests for Neovim's native scrollbind in CodeDiff-shaped panes. + +local function make_window(lines, filler_count) + vim.cmd("enew") + local bufnr = vim.api.nvim_get_current_buf() + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) + vim.wo.wrap = false + + if filler_count and filler_count > 0 then + local namespace = vim.api.nvim_create_namespace("scrollbind_test") + local virt_lines = {} + for i = 1, filler_count do + virt_lines[i] = { { string.format("filler %02d", i), "Comment" } } + end + vim.api.nvim_buf_set_extmark(bufnr, namespace, 19, 0, { virt_lines = virt_lines }) + end + + return vim.api.nvim_get_current_win() +end + +local function make_content(prefix, count) + local lines = {} + for i = 1, count do + lines[i] = string.format("%s%03d", prefix, i) + end + return lines +end + +local function topline(win) + return vim.api.nvim_win_call(win, function() + return vim.fn.line("w0") + end) +end + +describe("native scrollbind", function() + local original_lines + + before_each(function() + original_lines = vim.o.lines + vim.o.lines = 16 + vim.cmd("tabnew") + end) + + after_each(function() + vim.o.lines = original_lines + pcall(vim.cmd, "tabclose!") + end) + + it("enables native scrollbind on both side-by-side panes", function() + local left = make_window({ "left" }) + vim.cmd("rightbelow vsplit") + local right = make_window({ "right" }) + + vim.wo[left].scrollbind = true + vim.wo[right].scrollbind = true + + assert.is_true(vim.wo[left].scrollbind) + assert.is_true(vim.wo[right].scrollbind) + end) + + it("keeps scrolling monotonic through a full-screen filler block", function() + if vim.fn.has("nvim-0.13") ~= 1 then + pending("requires Neovim's upstream tall virt_lines scrollbind fix") + end + + local left = make_window(make_content("C", 60), 30) + vim.cmd("rightbelow vsplit") + local right_lines = make_content("C", 20) + for i = 1, 30 do + right_lines[#right_lines + 1] = string.format("I%03d", i) + end + for i = 21, 60 do + right_lines[#right_lines + 1] = string.format("C%03d", i) + end + local right = make_window(right_lines) + + vim.wo[left].scrollbind = true + vim.wo[right].scrollbind = true + vim.api.nvim_set_current_win(right) + vim.cmd("syncbind") + + local previous = 0 + local max_backjump = 0 + for _ = 1, 40 do + vim.cmd("normal! \5") + local current = topline(left) + if current < previous then + max_backjump = math.max(max_backjump, previous - current) + end + previous = current + end + + assert.is_true(max_backjump <= 1, "native scrollbind must not oscillate; max backward jump was " .. max_backjump) + end) +end) diff --git a/tests/scrollsync_spec.lua b/tests/scrollsync_spec.lua deleted file mode 100644 index 4d205fbd..00000000 --- a/tests/scrollsync_spec.lua +++ /dev/null @@ -1,452 +0,0 @@ --- Tests for codediff.scrollsync (structural scroll synchronization that --- replaces native scrollbind) and the codediff.ui.scroll manager. - -local scrollsync = require("codediff.scrollsync") -local internal = scrollsync._internal - -local function make_win(lines, fillers) - vim.cmd("enew") - local buf = vim.api.nvim_get_current_buf() - vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) - local ns = vim.api.nvim_create_namespace("scrollsync_test") - for _, f in ipairs(fillers or {}) do - local virt = {} - for i = 1, f.count do - virt[i] = { { "~fill~", "Comment" } } - end - -- f.after is 1-indexed "after this line"; extmark row is 0-indexed. - vim.api.nvim_buf_set_extmark(buf, ns, f.after - 1, 0, { virt_lines = virt }) - end - vim.wo.wrap = false - return vim.api.nvim_get_current_win(), buf -end - -local function view(win) - return vim.api.nvim_win_call(win, vim.fn.winsaveview) -end - -describe("scrollsync virtual-row mapping", function() - it("builds a fill table and maps lines to virtual rows", function() - local _, buf = make_win({ "a", "b", "c", "d", "e" }, { { after = 2, count = 3 } }) - local ft = internal.build_fill_table(buf) - -- 3 filler rows sit above line 3 (after line 2). - assert.equals(0, internal.vrow_of_line(ft, 1)) - assert.equals(1, internal.vrow_of_line(ft, 2)) - -- line 3 text is below its 3 fillers: 2 lines above + 3 fillers = vrow 5. - assert.equals(5, internal.vrow_of_line(ft, 3)) - assert.equals(6, internal.vrow_of_line(ft, 4)) - end) - - it("collects changed window IDs from WinScrolled data", function() - assert.same({ [12] = true, [13] = true, [14] = true }, internal.changed_windows({ all = {}, ["12"] = {}, [13] = {} }, "14")) - assert.is_nil(internal.changed_windows({}, "")) - end) - - it("vrow_to_view is the inverse of view_to_vrow", function() - local _, buf = make_win({ "a", "b", "c", "d", "e", "f" }, { { after = 3, count = 4 } }) - local ft = internal.build_fill_table(buf) - for _, tl in ipairs({ 1, 2, 3, 4, 5, 6 }) do - for _, tf in ipairs({ 0, 1, 2 }) do - -- topfill only meaningful up to the fill above tl; clamp to it - local fill_above = internal.vrow_of_line(ft, tl) - internal.vrow_of_line(ft, tl - 1 >= 1 and tl - 1 or 1) - if tl == 1 then - fill_above = 0 - end - local use_tf = math.min(tf, math.max(fill_above - (tl > 1 and 1 or 0), 0)) - local vrow = internal.view_to_vrow(ft, tl, use_tf) - local back_tl, back_tf = internal.vrow_to_view(ft, vrow) - assert.equals(vrow, internal.view_to_vrow(ft, back_tl, back_tf), string.format("roundtrip vrow mismatch tl=%d tf=%d", tl, use_tf)) - end - end - end) -end) - -describe("scrollsync manager alignment", function() - local scroll = require("codediff.ui.scroll") - local tab - - before_each(function() - vim.cmd("tabnew") - tab = vim.api.nvim_get_current_tabpage() - vim.o.lines = 16 -- small screen so a tall filler block clamps - end) - - after_each(function() - scroll.teardown(tab) - pcall(vim.cmd, "tabclose") - end) - - local function setup_pair(insert_count) - insert_count = insert_count or 30 - -- left = "delete" side (30 real lines + a filler block), right normal - local left = {} - for i = 1, 60 do - left[i] = string.format("C%03d", i) - end - local win_left = make_win(left, { { after = 20, count = insert_count } }) - vim.cmd("rightbelow vsplit") - local right = {} - for i = 1, 20 do - right[i] = string.format("C%03d", i) - end - for k = 0, insert_count - 1 do - right[20 + k + 1] = string.format("I%03d", k) - end - for i = 21, 60 do - right[#right + 1] = string.format("C%03d", i) - end - local win_right = make_win(right, {}) - return win_left, win_right - end - - local function set_view(win, topline, cursor_line) - vim.api.nvim_win_call(win, function() - vim.fn.winrestview({ topline = topline, lnum = cursor_line }) - end) - end - - local function fire_win_scrolled(group, win) - group:_on_scroll({ [win] = true }) - end - - local function setup_pending_follower_echo() - local follower, leader = setup_pair() - local group = scroll.bind(tab, { follower, leader }) - vim.api.nvim_set_current_win(leader) - set_view(leader, 35, 40) - group:resync(leader) - group:_mark_pending_echo(follower) - return { group = group, leader = leader, follower = follower } - end - - it("binds windows and keeps native scrollbind off", function() - local wl, wr = setup_pair() - scroll.bind(tab, { wl, wr }) - scroll.resync(tab, wr) - assert.is_not_nil(scroll.get(tab)) - assert.is_false(vim.wo[wl].scrollbind) - assert.is_false(vim.wo[wr].scrollbind) - end) - - it("aligns the follower to the same virtual row on normal-content scroll", function() - local wl, wr = setup_pair() - scroll.bind(tab, { wl, wr }) - vim.api.nvim_set_current_win(wr) - vim.cmd("normal! gg") - scroll.resync(tab, wr) - - -- scroll right pane down through the shared top region (before the block) - vim.api.nvim_win_call(wr, function() - vim.fn.winrestview({ topline = 10, lnum = 10 }) - end) - vim.api.nvim_exec_autocmds("WinScrolled", {}) - - local group = scroll.get(tab) - local ftl = group.ft[wl] - local ftr = group.ft[wr] - local vl = view(wl) - local vr = view(wr) - -- Neither is clamped here (top region has no fillers), so vrows are equal. - assert.equals( - internal.view_to_vrow(ftr, vr.topline, vr.topfill), - internal.view_to_vrow(ftl, vl.topline, vl.topfill), - "panes should share the same virtual row on normal-content scroll" - ) - end) - - it("does not drift: scrolling down then up returns to the start", function() - local wl, wr = setup_pair() - scroll.bind(tab, { wl, wr }) - vim.api.nvim_set_current_win(wr) - vim.cmd("normal! gg") - scroll.resync(tab, wr) - local start_l, start_r = view(wl), view(wr) - - for _ = 1, 25 do - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "nx", false) - vim.api.nvim_exec_autocmds("WinScrolled", {}) - end - for _ = 1, 25 do - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "nx", false) - vim.api.nvim_exec_autocmds("WinScrolled", {}) - end - - local end_l, end_r = view(wl), view(wr) - assert.equals(start_r.topline, end_r.topline, "leader topline restored") - assert.equals(start_l.topline, end_l.topline, "follower topline restored") - assert.equals(start_l.topfill, end_l.topfill, "follower topfill restored") - end) - - it("does not oscillate through a full-screen filler block (the bug fix)", function() - -- Filler block (30) is taller than the window, so it clamps. Native - -- scrollbind oscillates the follower topline here; the structural sync must - -- keep it monotonic (never jumping backwards to the top of the buffer). - local wl, wr = setup_pair() - scroll.bind(tab, { wl, wr }) - vim.api.nvim_set_current_win(wr) - vim.cmd("normal! gg") - scroll.resync(tab, wr) - - local toplines = {} - for _ = 1, 40 do - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "nx", false) - vim.api.nvim_exec_autocmds("WinScrolled", {}) - table.insert(toplines, view(wl).topline) - end - - local prev = 0 - local max_backjump = 0 - for _, tl in ipairs(toplines) do - if tl < prev then - max_backjump = math.max(max_backjump, prev - tl) - end - prev = tl - end - -- Follower topline must be (weakly) monotonic while scrolling down; any - -- large backwards jump would be the scrollbind oscillation. - assert.is_true(max_backjump <= 1, "follower topline should not oscillate; max backward jump was " .. max_backjump) - end) - - it("waits for the pending follower's own WinScrolled event", function() - local scenario = setup_pending_follower_echo() - - fire_win_scrolled(scenario.group, scenario.leader) - assert.is_true(scenario.group.pending_echo[scenario.follower], "another window must not consume the follower's echo") - - fire_win_scrolled(scenario.group, scenario.follower) - vim.wait(10) - assert.is_nil(scenario.group.pending_echo[scenario.follower], "the follower's own event must consume its echo") - end) - - it("issue #518: ignores delayed follower corrections without blocking later user scroll", function() - local scenario = setup_pending_follower_echo() - local leader_before = view(scenario.leader) - - vim.schedule(function() - set_view(scenario.follower, 10, 20) - fire_win_scrolled(scenario.group, scenario.follower) - end) - vim.wait(100, function() - return scenario.group.pending_echo[scenario.follower] == nil - end) - - local leader_after_correction = view(scenario.leader) - assert.equals(leader_before.topline, leader_after_correction.topline, "delayed redraw must not move the leader") - assert.equals(leader_before.topfill, leader_after_correction.topfill, "delayed redraw must not change leader filler") - - set_view(scenario.follower, 15, 25) - fire_win_scrolled(scenario.group, scenario.follower) - assert.not_equal(leader_before.topline, view(scenario.leader).topline, "later user scroll must still move the leader") - end) - - it("keeps the active leader fixed when a follower redraw corrects its view", function() - local scenario = setup_pending_follower_echo() - local leader_before = view(scenario.leader) - local leader_cursor_before = vim.api.nvim_win_get_cursor(scenario.leader) - set_view(scenario.follower, 10, 20) - - fire_win_scrolled(scenario.group, scenario.follower) - - local leader_after = view(scenario.leader) - assert.equals(leader_before.topline, leader_after.topline, "follower redraw must not move the leader") - assert.equals(leader_before.topfill, leader_after.topfill, "follower redraw must not change the leader's filler offset") - assert.same(leader_cursor_before, vim.api.nvim_win_get_cursor(scenario.leader), "follower redraw must not move the leader cursor") - end) - - it("keeps the follower on the aligned baseline after leaving a full-screen filler", function() - local follower, leader = setup_pair(214) - local group = scroll.bind(tab, { follower, leader }) - vim.api.nvim_set_current_win(leader) - vim.wo[follower].scrolloff = 8 - set_view(follower, 21, 21) - set_view(leader, 21, 25) - group:resync(leader) - fire_win_scrolled(group, follower) - - set_view(leader, 20, 24) - fire_win_scrolled(group, leader) - local follower_view = view(follower) - local follower_cursor = vim.api.nvim_win_get_cursor(follower)[1] - - assert.is_true(follower_cursor >= follower_view.topline, "the follower cursor must remain in the aligned view") - assert.equals(0, vim.wo[follower].scrolloff, "the follower view must remain stable until its redraw") - fire_win_scrolled(group, follower) - vim.cmd("redraw") - assert.equals(8, vim.wo[follower].scrolloff, "the follower's scrolloff must be restored after its redraw") - assert.equals(20, view(follower).topline, "the follower must not flash unrelated baseline text") - end) - - it("treats a later non-focused follower scroll as user input", function() - local scenario = setup_pending_follower_echo() - fire_win_scrolled(scenario.group, scenario.follower) - vim.wait(10) - assert.is_nil(scenario.group.pending_echo[scenario.follower], "the programmatic echo must be consumed first") - local leader_before = view(scenario.leader) - set_view(scenario.follower, 10, 20) - - fire_win_scrolled(scenario.group, scenario.follower) - - local leader_after = view(scenario.leader) - assert.not_equal(leader_before.topline, leader_after.topline, "later user scrolling in the follower must move the leader") - end) - - it("syncs three panes together (conflict/merge view)", function() - -- Three equal-height aligned panes, each with a small filler block in a - -- different place (blocks smaller than the window so nothing clamps). After - -- any scroll, all three panes must sit at the same shared virtual row. - local function forty() - local t = {} - for i = 1, 40 do - t[i] = string.format("C%03d", i) - end - return t - end - local wa = make_win(forty(), { { after = 10, count = 5 } }) - vim.cmd("rightbelow vsplit") - local wb = make_win(forty(), { { after = 20, count = 5 } }) - vim.cmd("rightbelow vsplit") - local wc = make_win(forty(), { { after = 30, count = 5 } }) - - scroll.bind(tab, { wa, wb, wc }) - assert.is_not_nil(scroll.get(tab)) - local group = scroll.get(tab) - - local function vrow(w) - local v = view(w) - return internal.view_to_vrow(group.ft[w], v.topline, v.topfill) - end - - -- Drive each pane as the leader in turn; the other two must match its vrow. - for _, leader in ipairs({ wc, wa, wb }) do - vim.api.nvim_set_current_win(leader) - vim.cmd("normal! gg") - scroll.resync(tab, leader) - for _ = 1, 20 do - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "nx", false) - vim.api.nvim_exec_autocmds("WinScrolled", {}) - local va, vb, vc = vrow(wa), vrow(wb), vrow(wc) - assert.equals(va, vb, "pane A and B must share the same virtual row") - assert.equals(vb, vc, "pane B and C must share the same virtual row") - end - end - end) -end) - --- Regression (#254): duplicating a diff window (`s`, optionally followed by --- `T` to move it to a new tab) must not carry the scroll mirroring with it. --- Native `scrollbind` is a window-local option that `:split` copies, so a clone --- of a bound pane stayed scroll-bound to its sibling. The structural sync only --- ever drives the explicit window IDs it was given and keeps every bindable --- window option off, so clones are always independent. -describe("scrollsync window duplication (#254)", function() - local scroll = require("codediff.ui.scroll") - local tab - - before_each(function() - vim.cmd("tabnew") - tab = vim.api.nvim_get_current_tabpage() - vim.o.lines = 24 - end) - - after_each(function() - scroll.teardown(tab) - while vim.fn.tabpagenr("$") > 1 do - pcall(vim.cmd, "tabclose!") - end - end) - - -- Two bound panes over the same long content, no fillers needed: the bug is - -- about option inheritance, not alignment. - local function bound_pair() - local lines = {} - for i = 1, 200 do - lines[i] = string.format("line %03d", i) - end - local left = make_win(lines, {}) - vim.cmd("rightbelow vsplit") - local right = make_win(lines, {}) - scroll.bind(tab, { left, right }) - scroll.resync(tab, right) - return left, right - end - - local function topline(win) - return vim.api.nvim_win_call(win, function() - return vim.fn.line("w0") - end) - end - - local function scroll_down(win, count) - vim.api.nvim_set_current_win(win) - for _ = 1, count do - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "nx", false) - end - -- Headless has no UI, so WinScrolled does not fire on its own. - vim.api.nvim_exec_autocmds("WinScrolled", {}) - end - - it("leaves no inheritable scroll-binding option on the diff windows", function() - local wl, wr = bound_pair() - for _, w in ipairs({ wl, wr }) do - -- Every one of these is copied by :split and would mirror scrolling. - assert.is_false(vim.wo[w].scrollbind, "scrollbind must stay off on diff windows") - assert.is_false(vim.wo[w].cursorbind, "cursorbind must stay off on diff windows") - assert.is_false(vim.wo[w].diff, "diff must stay off on diff windows") - end - end) - - it("does not scroll-mirror a window split off a diff pane", function() - local wl, wr = bound_pair() - - vim.api.nvim_set_current_win(wr) - vim.cmd("split") - local clone = vim.api.nvim_get_current_win() - assert.are_not.equal(wr, clone, "split should create a new window") - assert.is_false(vim.wo[clone].scrollbind, "the split window must not inherit scrollbind") - - local group = scroll.get(tab) - assert.is_not_nil(group, "the diff panes should still be bound") - for _, w in ipairs(group.wins) do - assert.are_not.equal(clone, w, "the split window must not join the scroll-sync group") - end - - local clone_top = topline(clone) - scroll_down(wr, 30) - - assert.is_true(topline(wr) > 1, "the diff pane should have scrolled") - assert.is_true(topline(wl) > 1, "the other diff pane should follow (sync is live)") - assert.are.equal(clone_top, topline(clone), "the split window must stay put") - end) - - it("does not scroll-mirror the panes of a new tab opened with sT", function() - local _, wr = bound_pair() - - -- The exact sequence from the issue: split the diff pane, then move that - -- split to its own tab. - vim.api.nvim_set_current_win(wr) - vim.cmd("wincmd s") - local moved = vim.api.nvim_get_current_win() - -- The intermediate split is where the mirroring used to leak in: `wincmd T` - -- itself resets scrollbind, so the clone must already be clean here. - assert.is_false(vim.wo[moved].scrollbind, "the intermediate split must not inherit scrollbind") - vim.cmd("wincmd T") - local new_tab = vim.api.nvim_get_current_tabpage() - assert.are_not.equal(tab, new_tab, "wincmd T should move the window to a new tab") - - -- Splitting inside the new tab must give two fully independent windows. - vim.cmd("split") - local wins = vim.api.nvim_tabpage_list_wins(new_tab) - assert.are.equal(2, #wins, "the new tab should have two windows") - for _, w in ipairs(wins) do - assert.is_false(vim.wo[w].scrollbind, "windows in the new tab must not have scrollbind") - end - - local other = wins[1] - local driver = wins[2] - local other_top = topline(other) - scroll_down(driver, 30) - - assert.is_true(topline(driver) > 1, "the driven window should have scrolled") - assert.are.equal(other_top, topline(other), "the sibling in the new tab must stay put") - end) -end) diff --git a/tests/ui/conflict/inline_tab_conflict_spec.lua b/tests/ui/conflict/inline_tab_conflict_spec.lua index d8119b3b..2b3a48f6 100644 --- a/tests/ui/conflict/inline_tab_conflict_spec.lua +++ b/tests/ui/conflict/inline_tab_conflict_spec.lua @@ -96,6 +96,9 @@ describe("conflicted file in an inline tab", function() assert.is_not_nil(session.original_win, "OURS needs a window") assert.is_not_nil(session.modified_win, "THEIRS needs a window") assert.not_equal(session.original_win, session.modified_win, "the two sides must not share a window") + for _, win in ipairs({ session.original_win, session.modified_win, session.result_win }) do + assert.is_true(vim.wo[win].scrollbind, "all conflict panes should use native scrollbind") + end local ours = vim.api.nvim_buf_get_lines(vim.api.nvim_win_get_buf(session.original_win), 0, -1, false) local theirs = vim.api.nvim_buf_get_lines(vim.api.nvim_win_get_buf(session.modified_win), 0, -1, false) diff --git a/tests/ui/explorer/explorer_scroll_independence_spec.lua b/tests/ui/explorer/explorer_scroll_independence_spec.lua index 8a335fef..ec33d90f 100644 --- a/tests/ui/explorer/explorer_scroll_independence_spec.lua +++ b/tests/ui/explorer/explorer_scroll_independence_spec.lua @@ -1,7 +1,5 @@ -- Regression: the explorer pane must NOT scroll together with the side-by-side --- diff panes. With native scrollbind this could happen if the explorer window --- ever inherited scrollbind=true. codediff's structural scroll-sync only ever --- binds the explicit diff windows, so the explorer is always independent. +-- diff panes. Native scrollbind is enabled only on the diff windows. local h = dofile("tests/helpers.lua") @@ -46,16 +44,15 @@ local function open_codediff_and_wait(repo, timeout_ms) vim.cmd("edit " .. repo.path("file01.txt")) vim.cmd("CodeDiff") local lifecycle = require("codediff.ui.lifecycle") - local scroll = require("codediff.ui.scroll") local tabpage - -- Wait until the diff content has loaded AND the scroll-sync group is bound + -- Wait until the diff content has loaded and native scrollbind is enabled -- (both happen asynchronously after :CodeDiff). local ready = vim.wait(timeout_ms, function() for _, tp in ipairs(vim.api.nvim_list_tabpages()) do local s = lifecycle.get_session(tp) - if s and (s.panel or {}).view and s.modified_win and vim.api.nvim_win_is_valid(s.modified_win) then + if s and (s.panel or {}).view and s.original_win and s.modified_win and vim.api.nvim_win_is_valid(s.original_win) and vim.api.nvim_win_is_valid(s.modified_win) then local mbuf = vim.api.nvim_win_get_buf(s.modified_win) - if vim.api.nvim_buf_is_valid(mbuf) and vim.api.nvim_buf_line_count(mbuf) > 100 and scroll.get(tp) then + if vim.api.nvim_buf_is_valid(mbuf) and vim.api.nvim_buf_line_count(mbuf) > 100 and vim.wo[s.original_win].scrollbind and vim.wo[s.modified_win].scrollbind then tabpage = tp return true end @@ -63,7 +60,7 @@ local function open_codediff_and_wait(repo, timeout_ms) end return false end, 100) - assert.is_true(ready, "CodeDiff explorer, loaded diff panes, and scroll-sync group should be ready") + assert.is_true(ready, "CodeDiff explorer, loaded diff panes, and native scrollbind should be ready") local session = lifecycle.get_session(tabpage) return tabpage, session, (session.panel or {}).view end @@ -98,17 +95,12 @@ describe("explorer scroll independence", function() end end) - it("keeps the explorer out of the diff scroll-sync group", function() + it("keeps the explorer out of native scrollbind", function() repo = busy_repo() - local tabpage, _, explorer = open_codediff_and_wait(repo) + local _, session, explorer = open_codediff_and_wait(repo) assert.is_not_nil(explorer and explorer.winid, "explorer window should exist") - - local scroll = require("codediff.ui.scroll") - local group = scroll.get(tabpage) - assert.is_not_nil(group, "a scroll-sync group should exist for the diff") - for _, w in ipairs(group.wins) do - assert.are_not.equal(explorer.winid, w, "explorer must not be in the scroll-sync group") - end + assert.is_true(vim.wo[session.original_win].scrollbind, "original diff pane should use native scrollbind") + assert.is_true(vim.wo[session.modified_win].scrollbind, "modified diff pane should use native scrollbind") assert.is_false(vim.wo[explorer.winid].scrollbind, "explorer must not have native scrollbind") end) @@ -123,9 +115,6 @@ describe("explorer scroll independence", function() for _ = 1, 80 do vim.cmd("normal! \5") -- end - -- Synchronous spec execution never returns to the main loop, so - -- WinScrolled is not dispatched. Fire it manually. - vim.api.nvim_exec_autocmds("WinScrolled", {}) vim.cmd("redraw") assert.is_true(topline(session.modified_win) > 20, "modified diff pane should have scrolled down") @@ -145,18 +134,16 @@ describe("explorer scroll independence", function() for _ = 1, 40 do vim.cmd("normal! \5") end - vim.api.nvim_exec_autocmds("WinScrolled", {}) vim.cmd("redraw") local mod_top = topline(session.modified_win) local orig_top = topline(session.original_win) - -- Scroll the explorer and fire WinScrolled; the diff panes must not move. + -- Scroll the explorer; the diff panes must not move. vim.api.nvim_set_current_win(explorer.winid) vim.api.nvim_win_set_cursor(explorer.winid, { 1, 0 }) for _ = 1, 20 do vim.cmd("normal! \5") end - vim.api.nvim_exec_autocmds("WinScrolled", {}) vim.cmd("redraw") assert.are.equal(mod_top, topline(session.modified_win), "modified diff pane must stay put while the explorer scrolls") diff --git a/tests/ui/view/move_keymap_spec.lua b/tests/ui/view/move_keymap_spec.lua index 5170174d..72946375 100644 --- a/tests/ui/view/move_keymap_spec.lua +++ b/tests/ui/view/move_keymap_spec.lua @@ -1,5 +1,5 @@ -- Tests for the `gm` align_move keymap --- Validates keymap binding, alignment behavior, scrollbind restore, and edge cases +-- Validates keymap binding, alignment behavior, scrollbind restoration, and edge cases local view = require("codediff.ui.view") local diff_module = require("codediff.core.diff") @@ -209,9 +209,9 @@ describe("gm align_move keymap", function() end) -- ────────────────────────────────────────────────────────────── - -- 4. Resume scroll-sync on cursor leave + -- 4. Resume native scrollbind on cursor leave -- ────────────────────────────────────────────────────────────── - it("pauses scroll-sync during move alignment and resumes on cursor leave", function() + it("pauses native scrollbind during move alignment and resumes on cursor leave", function() require("codediff").setup({ diff = { compute_moves = true, layout = "side-by-side" } }) highlights.setup() @@ -226,15 +226,10 @@ describe("gm align_move keymap", function() local moves = session.stored_diff_result.moves local move = moves[1] - local scroll = require("codediff.ui.scroll") - local group = scroll.get(tabpage) - assert.is_not_nil(group, "A scroll-sync group should be bound for the diff view") - - -- Native scrollbind must never be enabled (replaced by structural sync). + -- Native scrollbind is active before alignment. vim.wo[session.modified_win].scrolloff = 8 - assert.is_false(vim.wo[session.original_win].scrollbind, "native scrollbind stays off") - assert.is_false(vim.wo[session.modified_win].scrollbind, "native scrollbind stays off") - assert.is_falsy(group.paused, "scroll-sync active before alignment") + assert.is_true(vim.wo[session.original_win].scrollbind, "native scrollbind active before alignment") + assert.is_true(vim.wo[session.modified_win].scrollbind, "native scrollbind active before alignment") -- Focus original window, position on moved block, trigger gm vim.api.nvim_set_current_win(session.original_win) @@ -245,8 +240,9 @@ describe("gm align_move keymap", function() vim.api.nvim_feedkeys(gm_keys, "x", false) vim.cmd("redraw") - -- Scroll-sync should be paused while the move alignment is imposed. - assert.is_true(group.paused, "scroll-sync should be paused during alignment") + -- Native scrollbind should be paused while the move alignment is imposed. + assert.is_false(vim.wo[session.original_win].scrollbind, "native scrollbind should be paused during alignment") + assert.is_false(vim.wo[session.modified_win].scrollbind, "native scrollbind should be paused during alignment") -- Move cursor OUT of the moved block range using feedkeys so CursorMoved fires. -- Note: CursorMoved does not fire in headless mode, so we manually trigger it @@ -257,8 +253,9 @@ describe("gm align_move keymap", function() vim.cmd("doautocmd CursorMoved") vim.cmd("redraw") - -- Scroll-sync should be resumed (not paused) after leaving the moved block. - assert.is_falsy(group.paused, "scroll-sync should resume after leaving the moved block") + -- Native scrollbind should be resumed after leaving the moved block. + assert.is_true(vim.wo[session.original_win].scrollbind, "native scrollbind should resume after leaving the moved block") + assert.is_true(vim.wo[session.modified_win].scrollbind, "native scrollbind should resume after leaving the moved block") assert.are.equal(8, vim.wo[session.modified_win].scrolloff, "scrolloff should be restored on modified window") end) @@ -316,11 +313,8 @@ describe("gm align_move keymap", function() assert.are.equal(orig_view_before.topline, orig_view_after.topline, "original window topline should be restored after WinLeave") assert.are.equal(mod_view_before.topline, mod_view_after.topline, "modified window topline should be restored after WinLeave") - local group = require("codediff.ui.scroll").get(tabpage) - assert.is_not_nil(group, "scroll-sync group should exist") - assert.is_falsy(group.paused, "scroll-sync should be resumed after WinLeave restore") - assert.is_false(vim.wo[session.original_win].scrollbind, "native scrollbind stays off") - assert.is_false(vim.wo[session.modified_win].scrollbind, "native scrollbind stays off") + assert.is_true(vim.wo[session.original_win].scrollbind, "native scrollbind should be restored") + assert.is_true(vim.wo[session.modified_win].scrollbind, "native scrollbind should be restored") end) -- ────────────────────────────────────────────────────────────── @@ -411,9 +405,10 @@ describe("gm align_move keymap", function() vim.cmd("normal! zz") vim.cmd("redraw") - -- Get winline before align (pause structural sync so our manual - -- alignment measurement is not overridden by the sync group) - require("codediff.ui.scroll").pause(tp) + -- Get winline before align without native scrollbind overriding + -- the manual alignment measurement. + vim.wo[orig_win].scrollbind = false + vim.wo[mod_win].scrollbind = false local my_wl = vim.api.nvim_win_call(orig_win, function() vim.api.nvim_win_set_cursor(orig_win, { move.original.start_line, 0 }) @@ -443,8 +438,9 @@ describe("gm align_move keymap", function() assert.are.equal(my_wl, other_wl, name .. ": gm alignment failed, orig_wl=" .. my_wl .. " mod_wl=" .. other_wl) end - -- Resume structural scroll-sync - require("codediff.ui.scroll").resume(tp) + -- Restore native scrollbind for the next moved block. + vim.wo[orig_win].scrollbind = true + vim.wo[mod_win].scrollbind = true end end diff --git a/tests/ui/view/pane_setup_spec.lua b/tests/ui/view/pane_setup_spec.lua index 2e9e7324..9d92d23d 100644 --- a/tests/ui/view/pane_setup_spec.lua +++ b/tests/ui/view/pane_setup_spec.lua @@ -76,8 +76,7 @@ describe("diff pane setup", function() local session = open_diff() for _, win in ipairs({ session.original_win, session.modified_win }) do - -- nowrap is load-bearing: the scroll-sync maps one buffer line to one - -- screen row, which wrapping breaks. + -- nowrap is load-bearing for native scrollbind alignment. assert.is_false(vim.wo[win].wrap, "diff panes must not wrap") assert.is_true(vim.wo[win].cursorline, "diff panes should show cursorline") assert.is_false(vim.wo[win].list, "diff panes should not show listchars") diff --git a/tests/ui/view/view_spec.lua b/tests/ui/view/view_spec.lua index b48aa5d8..72e36196 100644 --- a/tests/ui/view/view_spec.lua +++ b/tests/ui/view/view_spec.lua @@ -155,15 +155,11 @@ describe("Render View", function() -- Should have at least 2 windows assert.is_true(#wins >= 2, "Should have at least 2 windows in diff view") - -- Diff windows are kept in sync by codediff's structural scroll-sync - -- (replaces native scrollbind, which flickers with tall virt_lines fillers). - -- Native scrollbind must therefore stay OFF, and a sync group must exist. + -- Side-by-side diff panes use Neovim's native scrollbind. if #wins >= 2 then for _, win in ipairs({ wins[1], wins[2] }) do - assert.is_false(vim.api.nvim_win_get_option(win, "scrollbind"), "Native scrollbind should be off (replaced by structural scroll-sync)") + assert.is_true(vim.api.nvim_win_get_option(win, "scrollbind"), "Native scrollbind should be enabled on diff panes") end - local scroll = require("codediff.ui.scroll") - assert.is_not_nil(scroll.get(tabpage), "A scroll-sync group should be bound for the diff view") end vim.fn.delete(left_path) @@ -171,9 +167,8 @@ describe("Render View", function() end) -- Test 4b: Regression (#254) - duplicating a diff window must not carry the - -- scroll mirroring into the copy. scrollbind/cursorbind/diff are window-local - -- options that :split copies, so leaving any of them on a diff pane made the - -- duplicate scroll in lockstep with its sibling. + -- scroll mirroring into the copy. CodeDiff clears inherited scrollbind on + -- windows that are not part of the active diff. it("Does not scroll-mirror a window split off a diff pane", function() local original = {} local modified = {} @@ -197,7 +192,7 @@ describe("Render View", function() local mod_win = session.modified_win for _, win in ipairs({ session.original_win, mod_win }) do - assert.is_false(vim.wo[win].scrollbind, "scrollbind must stay off on diff windows") + assert.is_true(vim.wo[win].scrollbind, "scrollbind must be enabled on diff windows") assert.is_false(vim.wo[win].cursorbind, "cursorbind must stay off on diff windows") assert.is_false(vim.wo[win].diff, "diff must stay off on diff windows") end @@ -219,10 +214,6 @@ describe("Render View", function() for _ = 1, 30 do vim.cmd("normal! \5") -- end - -- Synchronous spec execution never returns to the main loop, so - -- WinScrolled is not dispatched. Fire it manually. - vim.api.nvim_exec_autocmds("WinScrolled", {}) - assert.is_true(topline(mod_win) > 1, "the diff pane should have scrolled") assert.are.equal(clone_top, topline(clone), "the split window must stay put")