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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,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
hover = "K", -- Preview the conflict under cursor against the merge base
},
},
},
Expand Down
1 change: 1 addition & 0 deletions doc/codediff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ Setup entry point:
prev_conflict = "[x",
diffget_incoming = "2do",
diffget_current = "3do",
hover = "K",
},
},
})
Expand Down
1 change: 1 addition & 0 deletions lua/codediff/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,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
hover = "K", -- Preview the conflict under cursor against the merge base (input panes)
},
},
}
Expand Down
116 changes: 116 additions & 0 deletions lua/codediff/ui/conflict/hover.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
-- Hover preview of a conflict hunk against the merge base
-- The side's lines are real text; the base lines it replaced are the inline renderer's virtual lines.
local M = {}

local lifecycle = require("codediff.ui.lifecycle")
local config = require("codediff.config")
local tracking = require("codediff.ui.conflict.tracking")
local inline = require("codediff.ui.inline")
local virtual_file = require("codediff.core.virtual_file")

--- Toggle a float showing the conflict under the cursor against the merge base
--- @param tabpage number
--- @return boolean shown
function M.toggle(tabpage)
local session = lifecycle.get_session(tabpage)
if not session then
return false
end

if session._hover_win and vim.api.nvim_win_is_valid(session._hover_win) then
vim.api.nvim_win_close(session._hover_win, true)
session._hover_win = nil
return false
end

local source = vim.api.nvim_get_current_buf()
local side = (source == session.original_bufnr and "left") or
(source == session.modified_bufnr and "right") or nil
local block = side and session.conflict_blocks and
tracking.find_conflict_at_cursor(session, vim.api.nvim_win_get_cursor(0)[1], side, true)
if not block then
vim.notify("[codediff] No conflict at cursor position", vim.log.levels.INFO)
return false
end

local range = side == "left" and block.output1_range or block.output2_range
local lines = tracking.get_lines_for_range(source, range.start_line, range.end_line)
local base_lines = session.merge_base_lines or {}
local base = {}
for i = block.base_range.start_line, block.base_range.end_line - 1 do
table.insert(base, base_lines[i] or "")
end

local diff
if #base == 0 or #lines == 0 then
-- The diff engine reports no changes when either side is empty
diff = { changes = { { original = { start_line = 1, end_line = #base + 1 }, modified = { start_line = 1, end_line = #lines + 1 } } } }
else
diff = require("codediff.core.diff").compute_diff(base, lines,
require("codediff.ui.view.render").diff_options())
end

-- A deletion past the last line is drawn above that line; a spare row keeps it below
local text = vim.list_extend({}, lines)
local last = diff.changes[#diff.changes]
if last and #lines > 0 and last.modified.start_line > #lines then
table.insert(text, "")
end

local buf = vim.api.nvim_create_buf(false, true)
vim.bo[buf].bufhidden = "wipe"
virtual_file.set_content(buf, text, vim.api.nvim_buf_get_name(session.result_bufnr))
inline.render_inline_diff(buf, diff, base, lines,
{ filetype = vim.bo[session.result_bufnr].filetype })

-- Virtual lines above row 0 only show when topfill matches their count exactly
local fill, topfill = 0, 0
for _, mark in ipairs(vim.api.nvim_buf_get_extmarks(buf, inline.ns_inline, 0, -1, { details = true })) do
local count = #(mark[4].virt_lines or {})
fill = fill + count
if mark[2] == 0 then
topfill = topfill + count
end
end

local ours = (side == "left") == (config.options.diff.conflict_ours_position == "left")
local title = ours and " Ours (Current) vs Base " or " Theirs (Incoming) vs Base "
local width = vim.fn.strdisplaywidth(title)
for _, line in ipairs(vim.list_extend(vim.list_extend({}, base), lines)) do
width = math.max(width, vim.fn.strdisplaywidth(line))
end

local win = vim.api.nvim_open_win(buf, false, {
relative = "cursor",
row = 1,
col = 0,
width = math.max(1, math.min(width, vim.o.columns - 4)),
height = math.max(1, math.min(vim.api.nvim_buf_line_count(buf) + fill, vim.o.lines - 4)),
style = "minimal",
border = "rounded",
title = title,
title_pos = "center",
focusable = false,
})
-- Floats inherit the pane's window options; scrollbind would drag the float along with the panes
vim.wo[win].scrollbind = false
vim.wo[win].foldenable = false
vim.wo[win].wrap = false
vim.api.nvim_win_call(win, function()
vim.fn.winrestview({ topfill = topfill })
end)
session._hover_win = win

vim.api.nvim_create_autocmd({ "CursorMoved", "BufLeave" }, {
buffer = source,
once = true,
callback = function()
if vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, true)
end
end,
})
return true
end

return M
4 changes: 4 additions & 0 deletions lua/codediff/ui/conflict/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local gutter = require("codediff.ui.conflict.gutter")
local resolution = require("codediff.ui.conflict.resolution")
local navigation = require("codediff.ui.conflict.navigation")
local keymaps = require("codediff.ui.conflict.keymaps")
local hover = require("codediff.ui.conflict.hover")

-- Delegate to tracking module
M.run_repeatable_action = tracking.run_repeatable_action
Expand Down Expand Up @@ -36,6 +37,9 @@ M.diffget_current = resolution.diffget_current
M.navigate_next_conflict = navigation.navigate_next_conflict
M.navigate_prev_conflict = navigation.navigate_prev_conflict

-- Delegate to hover module
M.toggle_hover = hover.toggle

-- Delegate to keymaps module
M.setup_keymaps = keymaps.setup_keymaps

Expand Down
9 changes: 9 additions & 0 deletions lua/codediff/ui/conflict/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local resolve = require("codediff.keymap.resolve")
local tracking = require("codediff.ui.conflict.tracking")
local resolution = require("codediff.ui.conflict.resolution")
local navigation = require("codediff.ui.conflict.navigation")
local hover = require("codediff.ui.conflict.hover")

-- Dot-repeatable actions are expr mappings (see conflict.tracking).
local REPEATABLE_ACTIONS = {
Expand Down Expand Up @@ -88,6 +89,14 @@ function M.setup_keymaps(tabpage)
extra = { desc = action.desc, expr = true },
})
end
else
-- Input panes only, so the Result (the real file) keeps the user's own key
bind(bufnr, { key = "hover" }, {
rhs = function()
hover.toggle(tabpage)
end,
extra = { desc = "Preview hunk against merge base" },
})
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lua/codediff/ui/keymap_help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ local function build_sections(keymaps, is_bound, shape)
{ ckm.prev_conflict, "Previous conflict" },
{ ckm.diffget_incoming, "Get hunk from incoming" },
{ ckm.diffget_current, "Get hunk from current" },
{ ckm.hover, "Preview hunk against base" },
}, is_bound)
)
end
Expand Down
2 changes: 1 addition & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ hand-authored projections; real merge E2Es keep the full controller active.
## Behavioral coverage

[e2e/COVERAGE.md](e2e/COVERAGE.md) maps the refresh changes against `main` to
scenario IDs and parameterized executions. Its 133 named scenarios / 548
scenario IDs and parameterized executions. Its 134 named scenarios / 556
executions are a specific cross-feature matrix, **not the total E2E suite** and
not a count of unit or integration tests. Additional command and working-file
E2Es live alongside it. The runner reports the selected suite's actual counts.
Expand Down
5 changes: 3 additions & 2 deletions tests/e2e/COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ are difficult to express through a Git merge. They are not counted below.
## Scenario matrix

A named scenario is distinct from its parameterized executions. The matrix has
133 named scenarios and 548 E2E executions on a platform with a native watcher.
134 named scenarios and 556 E2E executions on a platform with a native watcher.
This is the refresh change-audit matrix, not the entire E2E suite. Other command,
working-file and diagnostic E2Es are additional coverage. Fixture self-tests,
unit tests and component integrations are not included in these counts.
Expand All @@ -53,7 +53,7 @@ unit tests and component integrations are not included in these counts.
| [view/hunk_actions_spec.lua](view/hunk_actions_spec.lua) | 14 | 54 | both transports/layouts; manual-only mode runs per layout |
| [explorer/actions_spec.lua](explorer/actions_spec.lua) | 20 | 80 | both transports/layouts |
| [history/actions_spec.lua](history/actions_spec.lua) | 14 | 56 | both transports/layouts |
| [conflict/actions_spec.lua](conflict/actions_spec.lua) | 15 | 120 | both transports × ours left/right × Result bottom/center |
| [conflict/actions_spec.lua](conflict/actions_spec.lua) | 16 | 128 | both transports × ours left/right × Result bottom/center |
| [refresh/sources_spec.lua](refresh/sources_spec.lua) | 19 | 74 | both transports/layouts; plain-file case runs per layout |
| [virtual_file/buffers_spec.lua](virtual_file/buffers_spec.lua) | 4 | 4 | direct public `codediff://` buffer commands |

Expand Down Expand Up @@ -164,6 +164,7 @@ Each ID is part of the test name, making failures and source searches traceable.
| C01–C05 | Independent fixture seed, marker cells/colors, per-block resolution, undo/redo/discard |
| C06–C11 | Whole-file resolution/reset and numbered diffget |
| C12–C15 | Conflict navigation, save, unrelated staging, quit dialogs, finish-and-stage workflow |
| C16 | Input-pane hover preview of a conflict against the merge base |
| S01–S05 | Unicode/BOM/CRLF, virtual syntax/readonly, no final newline, empty files |
| S06–S10 | Renames, fixed ancestry, movable tags, regular Git directories, SHA-256 |
| S11–S14 | Plain paths with spaces/Unicode, literal arrows, renamed arrows, unborn branches |
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/conflict/actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ for _, backend in ipairs({ "native", "polling" }) do
assert.equals("", repo.command({ "ls-files", "--unmerged", "merge.txt" }))
assert.same(expected("current", true), repo.blob_lines(":0", "merge.txt"))
end)

it("[C16] hover previews the focused input's conflict against the merge base", function()
act("hover")
local hover = screen:exec("return refresh_session()._hover_win")
assert.is_not_nil(hover)
screen:await(function()
return h.window_text(screen, hover):find("base%-one%s+current%-one") ~= nil
end, "base line was not rendered above the current side")
assert.equals(" Ours (Current) vs Base ", screen:exec("return vim.api.nvim_win_get_config(...).title[1][1]", { hover }))
h.feed(screen, "j")
screen:await(function()
return not screen:exec("return vim.api.nvim_win_is_valid(...)", { hover })
end, "hover did not close on cursor movement")
h.expect_lines(screen, "result", expected())
end)
end)
end
end
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/keymap_matrix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ panel n zc Close fold
panel n zo Open fold

## conflict
original n K Preview hunk against merge base
original n [c Previous hunk
original n [x Previous conflict
original n \cB Accept ALL both changes
Expand All @@ -244,6 +245,7 @@ original n t Toggle diff layout
original x ih Hunk textobject
original o ih Hunk textobject
original v ih Hunk textobject
modified n K Preview hunk against merge base
modified n [c Previous hunk
modified n [x Previous conflict
modified n \cB Accept ALL both changes
Expand Down
1 change: 1 addition & 0 deletions tests/integration/keymap/actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ local EXPECT = {
prev_conflict = { "conflict" },
diffget_incoming = { "conflict" },
diffget_current = { "conflict" },
hover = { "conflict" },
},
}

Expand Down
Loading