Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Both methods automatically place the library in the plugin root directory.
conflict_result_position = "bottom", -- "bottom" (default): result below diff panes or "center": result between diff panes (three columns)
conflict_result_height = 30, -- Height of result pane in bottom layout (% of total height)
conflict_result_width_ratio = { 1, 1, 1 }, -- Width ratio for center layout panes {left, center, right} (e.g., {1, 2, 1} for wider result)
conflict_show_base = false, -- Show the merge base (:1) as a 4th pane in the middle of the top row
cycle_next_hunk = true, -- Wrap around when navigating hunks (]c/[c): false to stop at first/last
cycle_next_file = true, -- Wrap around when navigating files (]f/[f): false to stop at first/last
cycle_hunks_across_files = false, -- ]c/[c at file boundary hops to first/last hunk of next/prev file (explorer/history)
Expand Down Expand Up @@ -302,6 +303,7 @@ Both methods automatically place the library in the plugin root directory.
prev_conflict = "[x", -- Jump to previous conflict
diffget_incoming = "2do", -- Get hunk from incoming (left/theirs) buffer
diffget_current = "3do", -- Get hunk from current (right/ours) buffer
toggle_base = "<leader>cm", -- Show/hide the merge base (:1) pane
},
},
},
Expand Down
10 changes: 10 additions & 0 deletions doc/codediff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ Git merge tool:
git config --global merge.tool codediff
git config --global mergetool.codediff.cmd 'nvim "$MERGED" -c "CodeDiff merge \"$MERGED\""'
<
*codediff-merge-base*
The conflict view shows Incoming, Current and Result. Press `<leader>cm`
(`keymaps.conflict.toggle_base`) to show or hide a read-only Base pane with
the common ancestor (:1), or set `diff.conflict_show_base = true` to open it
by default. Base appears in the middle of the top row: between Incoming and
Current when Result is at the bottom, and above Result when
`conflict_result_position = "center"`. It scrolls with the inputs and is
aligned line-for-line with them.

==============================================================================
CONFIGURATION *codediff-configuration*
Expand Down Expand Up @@ -274,6 +282,7 @@ Setup entry point:
conflict_result_position = "bottom",
conflict_result_height = 30,
conflict_result_width_ratio = { 1, 1, 1 },
conflict_show_base = false,
jump_to_first_change = true,
highlight_added_deleted_files = false,
highlight_priority = 100,
Expand Down Expand Up @@ -381,6 +390,7 @@ Setup entry point:
prev_conflict = "[x",
diffget_incoming = "2do",
diffget_current = "3do",
toggle_base = "<leader>cm",
},
},
})
Expand Down
2 changes: 2 additions & 0 deletions lua/codediff/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ M.defaults = {
conflict_result_position = "bottom", -- Position of result buffer in conflict view: "bottom" or "center"
conflict_result_height = 30, -- Height of result buffer in bottom layout (percentage of total height)
conflict_result_width_ratio = { 1, 1, 1 }, -- Width ratio for center layout panes {left, center, right} (e.g., {1, 2, 1} for wider result)
conflict_show_base = false, -- Show the merge base (:1) as a 4th conflict pane in the middle of the top row (toggle with keymaps.conflict.toggle_base)
cycle_next_hunk = true, -- Wrap around when navigating hunks (]c/[c): true = cycle, false = stop at first/last
cycle_next_file = true, -- Wrap around when navigating files (]f/[f): true = cycle, false = stop at first/last
cycle_hunks_across_files = false, -- ]c/[c at file boundary jumps to first/last hunk of next/prev file (explorer/history mode)
Expand Down Expand Up @@ -178,6 +179,7 @@ M.defaults = {
-- Vimdiff-style numbered diffget (from result buffer)
diffget_incoming = "2do", -- Get hunk from incoming (left/theirs) buffer
diffget_current = "3do", -- Get hunk from current (right/ours) buffer
toggle_base = "<leader>cm", -- Show/hide the merge base (:1) pane
},
},
}
Expand Down
29 changes: 29 additions & 0 deletions lua/codediff/ui/conflict/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@ local REPEATABLE_ACTIONS = {
{ key = "discard", fn = resolution.discard, desc = "Discard changes (keep base)" },
}

-- Required lazily: the Base view requires codediff.ui.conflict, which loads
-- this module, so a top-level require would be circular. `fn` must be a
-- function taking the tabpage, like every other action.
local TOGGLE_BASE = {
key = "toggle_base",
fn = function(tabpage)
require("codediff.ui.conflict.view.base").toggle(tabpage)
end,
desc = "Toggle merge base pane",
}

local PLAIN_ACTIONS = {
{ key = "accept_all_incoming", fn = resolution.accept_all_incoming, desc = "Accept ALL incoming changes" },
{ key = "accept_all_current", fn = resolution.accept_all_current, desc = "Accept ALL current changes" },
{ key = "accept_all_both", fn = resolution.accept_all_both, desc = "Accept ALL both changes" },
{ key = "discard_all", fn = resolution.discard_all, desc = "Discard ALL, reset to base" },
{ key = "next_conflict", fn = navigation.navigate_next_conflict, desc = "Next conflict" },
{ key = "prev_conflict", fn = navigation.navigate_prev_conflict, desc = "Previous conflict" },
TOGGLE_BASE,
}

-- Vimdiff-style numbered diffget, only meaningful on the result buffer.
Expand Down Expand Up @@ -92,6 +104,23 @@ function M.setup_keymaps(tabpage)
end
end

-- The read-only Base pane only needs a way to hide itself and to quit.
local base_bufnr = session.base_bufnr
if base_bufnr and vim.api.nvim_buf_is_valid(base_bufnr) then
bind(base_bufnr, TOGGLE_BASE, {
rhs = function()
TOGGLE_BASE.fn(tabpage)
end,
extra = { desc = TOGGLE_BASE.desc },
})
local quit = resolve.keymaps_for("view").quit
if quit then
lifecycle.set_buf_keymap(tabpage, base_bufnr, "n", quit, function()
lifecycle.close(tabpage)
end, vim.tbl_extend("force", base_opts, { desc = "Close codediff tab" }))
end
end

lifecycle.end_keymap_scope(tabpage, "conflict")
end

Expand Down
20 changes: 18 additions & 2 deletions lua/codediff/ui/conflict/merge/fillers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ end
-- Compute fillers AND identify which changes are in conflict regions
-- A conflict region is where BOTH input1 and input2 have changes to the same base region
-- This matches VSCode's behavior of only highlighting conflicting changes
function M.compute_merge_fillers_and_conflicts(base_to_input1_diff, base_to_input2_diff, base_lines, input1_lines, input2_lines)
-- @param include_base boolean?: also align a visible Base pane, adding `base_fillers`
-- to the result and padding the inputs where Base is taller.
function M.compute_merge_fillers_and_conflicts(base_to_input1_diff, base_to_input2_diff, base_lines, input1_lines, input2_lines, include_base)
-- Use VSCode's MappingAlignment.compute approach
local mapping_alignments = compute_mapping_alignments(base_to_input1_diff.changes, base_to_input2_diff.changes)

local all_left_fillers = {}
local all_right_fillers = {}
local all_base_fillers = {}
local left_total = 0
local right_total = 0
local base_total = 0

-- Track which changes are in conflict regions
local conflict_left_changes = {}
Expand Down Expand Up @@ -109,8 +113,13 @@ function M.compute_merge_fillers_and_conflicts(base_to_input1_diff, base_to_inpu
if a.input1_line and a.input2_line then
local left_adj = a.input1_line + left_total
local right_adj = a.input2_line + right_total
local base_adj = include_base and a.base_line and (a.base_line + base_total)
local mx = math.max(left_adj, right_adj)

if base_adj then
mx = math.max(mx, base_adj)
end

if mx - left_adj > 0 then
table.insert(all_left_fillers, { after_line = a.input1_line - 1, count = mx - left_adj })
left_total = left_total + (mx - left_adj)
Expand All @@ -119,15 +128,22 @@ function M.compute_merge_fillers_and_conflicts(base_to_input1_diff, base_to_inpu
table.insert(all_right_fillers, { after_line = a.input2_line - 1, count = mx - right_adj })
right_total = right_total + (mx - right_adj)
end
if base_adj and mx - base_adj > 0 then
table.insert(all_base_fillers, { after_line = a.base_line - 1, count = mx - base_adj })
base_total = base_total + (mx - base_adj)
end
end
end
end

return {
left_fillers = all_left_fillers,
right_fillers = all_right_fillers,
base_fillers = include_base and all_base_fillers or nil,
conflict_blocks = conflict_blocks,
}, conflict_left_changes, conflict_right_changes
},
conflict_left_changes,
conflict_right_changes
end

return M
Loading