Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
513 changes: 0 additions & 513 deletions lua/codediff/scrollsync.lua

This file was deleted.

79 changes: 50 additions & 29 deletions lua/codediff/ui/auto_refresh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 3 additions & 8 deletions lua/codediff/ui/conflict/view/inputs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions lua/codediff/ui/conflict/view/result.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions lua/codediff/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 29 additions & 5 deletions lua/codediff/ui/lifecycle/cleanup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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,
Expand Down
50 changes: 32 additions & 18 deletions lua/codediff/ui/lifecycle/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down
Loading
Loading