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
21 changes: 21 additions & 0 deletions lua/codediff/ui/view/cursor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local api = vim.api

local M = {}

function M.clamp_line(bufnr, line)
local line_count = api.nvim_buf_line_count(bufnr)
return math.max(1, math.min(line, line_count))
end

function M.clamp_window_line(win, line)
return M.clamp_line(api.nvim_win_get_buf(win), line)
end

function M.clamp_cursor(win, position)
return {
M.clamp_window_line(win, position[1]),
position[2] or 0,
}
end

return M
2 changes: 2 additions & 0 deletions lua/codediff/ui/view/inline_view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local config = require("codediff.config")
local diff_module = require("codediff.core.diff")
local inline = require("codediff.ui.inline")
local semantic = require("codediff.ui.semantic_tokens")
local cursor_util = require("codediff.ui.view.cursor")

function M.compute_and_render_inline(modified_buf, original_buf, original_lines, modified_lines, original_is_virtual, modified_is_virtual, modified_win, auto_scroll_to_first_hunk)
local diff_options = {
Expand Down Expand Up @@ -45,6 +46,7 @@ function M.compute_and_render_inline(modified_buf, original_buf, original_lines,
end

local target_line = landing == "last" and lines_diff.changes[#lines_diff.changes].modified.start_line or lines_diff.changes[1].modified.start_line
target_line = cursor_util.clamp_window_line(modified_win, target_line)
pcall(vim.api.nvim_win_set_cursor, modified_win, { target_line, 0 })
vim.api.nvim_set_current_win(modified_win)
vim.cmd("normal! zz")
Expand Down
9 changes: 5 additions & 4 deletions lua/codediff/ui/view/navigation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local M = {}

local lifecycle = require("codediff.ui.lifecycle")
local config = require("codediff.config")
local cursor_util = require("codediff.ui.view.cursor")

-- Hop to the next/previous file in the explorer's list.
--
Expand Down Expand Up @@ -80,7 +81,7 @@ function M.next_hunk()

-- Find next hunk after current line
for i, mapping in ipairs(diff_result.changes) do
local target_line = is_original and mapping.original.start_line or mapping.modified.start_line
local target_line = cursor_util.clamp_line(vim.api.nvim_get_current_buf(), (is_original and mapping.original or mapping.modified).start_line)
if target_line > current_line then
pcall(vim.api.nvim_win_set_cursor, 0, { target_line, 0 })
vim.cmd("normal! zz")
Expand All @@ -98,7 +99,7 @@ function M.next_hunk()
-- Wrap around to first hunk (if cycling enabled)
if config.options.diff.cycle_next_hunk then
local first_hunk = diff_result.changes[1]
local target_line = is_original and first_hunk.original.start_line or first_hunk.modified.start_line
local target_line = cursor_util.clamp_line(vim.api.nvim_get_current_buf(), (is_original and first_hunk.original or first_hunk.modified).start_line)
pcall(vim.api.nvim_win_set_cursor, 0, { target_line, 0 })
vim.cmd("normal! zz")
vim.api.nvim_echo({ { string.format("Hunk 1 of %d", #diff_result.changes), "None" } }, false, {})
Expand Down Expand Up @@ -155,7 +156,7 @@ function M.prev_hunk()
-- Find previous hunk before current line (search backwards)
for i = #diff_result.changes, 1, -1 do
local mapping = diff_result.changes[i]
local target_line = is_original and mapping.original.start_line or mapping.modified.start_line
local target_line = cursor_util.clamp_line(vim.api.nvim_get_current_buf(), (is_original and mapping.original or mapping.modified).start_line)
if target_line < current_line then
pcall(vim.api.nvim_win_set_cursor, 0, { target_line, 0 })
vim.cmd("normal! zz")
Expand All @@ -172,7 +173,7 @@ function M.prev_hunk()
-- Wrap around to last hunk (if cycling enabled)
if config.options.diff.cycle_next_hunk then
local last_hunk = diff_result.changes[#diff_result.changes]
local target_line = is_original and last_hunk.original.start_line or last_hunk.modified.start_line
local target_line = cursor_util.clamp_line(vim.api.nvim_get_current_buf(), (is_original and last_hunk.original or last_hunk.modified).start_line)
pcall(vim.api.nvim_win_set_cursor, 0, { target_line, 0 })
vim.cmd("normal! zz")
vim.api.nvim_echo({ { string.format("Hunk %d of %d", #diff_result.changes, #diff_result.changes), "None" } }, false, {})
Expand Down
5 changes: 3 additions & 2 deletions lua/codediff/ui/view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local core = require("codediff.ui.core")
local semantic = require("codediff.ui.semantic_tokens")
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
Expand All @@ -24,10 +25,10 @@ function M.establish_scrollbind(orig_win, mod_win, orig_buf, mod_buf, lines_diff

-- Place cursors on their respective hunk lines (per-pane coordinates).
if orig_cursor then
pcall(vim.api.nvim_win_set_cursor, orig_win, orig_cursor)
pcall(vim.api.nvim_win_set_cursor, orig_win, cursor_util.clamp_cursor(orig_win, orig_cursor))
end
if mod_cursor then
pcall(vim.api.nvim_win_set_cursor, mod_win, mod_cursor)
pcall(vim.api.nvim_win_set_cursor, mod_win, cursor_util.clamp_cursor(mod_win, mod_cursor))
end

-- Bind the two panes and align the original pane to the modified pane.
Expand Down
41 changes: 36 additions & 5 deletions tests/ui/view/cycle_hunks_across_files_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ describe("cycle_hunks_across_files (#161)", function()

local function has_expected_hunk_count(session, expected_count)
local changes = session and session.stored_diff_result and session.stored_diff_result.changes
if not changes then return false end
if expected_count ~= nil then return #changes == expected_count end
if not changes then
return false
end
if expected_count ~= nil then
return #changes == expected_count
end
return #changes > 0
end

Expand Down Expand Up @@ -90,9 +94,7 @@ describe("cycle_hunks_across_files (#161)", function()
local s = lifecycle.get_session(tabpage)
local e = lifecycle.get_panel_view(tabpage)
local session_path = s and s.modified.absolute ~= "" and s.modified.absolute or s and s.original.absolute
return session_path and e and e.current_file_path
and session_path:find(e.current_file_path, 1, true) ~= nil
and has_expected_hunk_count(s, expected_hunk_count)
return session_path and e and e.current_file_path and session_path:find(e.current_file_path, 1, true) ~= nil and has_expected_hunk_count(s, expected_hunk_count)
end, 20)
end

Expand Down Expand Up @@ -314,4 +316,33 @@ describe("cycle_hunks_across_files (#161)", function()
local cursor_after = vim.api.nvim_win_get_cursor(new_session.modified_win)[1]
assert.equal(last_modified, cursor_after, "cursor must land on the MODIFIED-side last-hunk line (" .. last_modified .. "), not the original-side (" .. last_original .. ")")
end)

it("issue #479: EOF deletions use the last valid line and cross-file cycling advances", function()
config.options.diff.cycle_hunks_across_files = true

-- The modified side ends immediately before the deletion hunk's raw start
-- line. The hunk is reported as modified [4, 4), while line 3 is the last
-- cursor position that exists in the buffer.
repo.write_file("a.txt", { "a1", "a2", "a3" })

local tabpage, session, explorer = open_explorer("a.txt", 1)
assert.is_not_nil(session)
assert.is_not_nil(explorer)
assert.equal(3, vim.api.nvim_win_get_cursor(session.modified_win)[1], "EOF deletion should land on the last valid modified line")

local first_file = explorer.current_file_path
nav.next_hunk()
assert.is_true(wait_for_file_switch(tabpage, first_file), "next hunk should advance past the EOF deletion")
assert.equal("b.txt", explorer.current_file_path)

local next_session = lifecycle.get_session(tabpage)
local first_hunk_line = next_session.stored_diff_result.changes[1].modified.start_line
assert.equal(first_hunk_line, vim.api.nvim_win_get_cursor(next_session.modified_win)[1])

nav.prev_hunk()
assert.is_true(wait_for_file_switch(tabpage, "b.txt"), "previous hunk should return to the EOF deletion")
local previous_session = lifecycle.get_session(tabpage)
assert.equal("a.txt", explorer.current_file_path)
assert.equal(3, vim.api.nvim_win_get_cursor(previous_session.modified_win)[1], "backward landing should clamp the EOF deletion")
end)
end)
Loading