From bca53c4b381d4d0e5e5c80663784872cb57f64ed Mon Sep 17 00:00:00 2001 From: Melker Ulander Date: Thu, 15 Oct 2026 13:42:10 +0200 Subject: [PATCH] feat: conflict hover action --- README.md | 1 + doc/codediff.txt | 1 + lua/codediff/config.lua | 1 + lua/codediff/ui/conflict/hover.lua | 116 ++++++++++++++++ lua/codediff/ui/conflict/init.lua | 4 + lua/codediff/ui/conflict/keymaps.lua | 9 ++ lua/codediff/ui/keymap_help.lua | 1 + tests/README.md | 2 +- tests/e2e/COVERAGE.md | 5 +- tests/e2e/conflict/actions_spec.lua | 15 +++ tests/fixtures/keymap_matrix.txt | 2 + tests/integration/keymap/actions_spec.lua | 1 + tests/integration/ui/conflict/hover_spec.lua | 135 +++++++++++++++++++ 13 files changed, 290 insertions(+), 3 deletions(-) create mode 100644 lua/codediff/ui/conflict/hover.lua create mode 100644 tests/integration/ui/conflict/hover_spec.lua diff --git a/README.md b/README.md index 6d78cb72..3e43d418 100644 --- a/README.md +++ b/README.md @@ -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 }, }, }, diff --git a/doc/codediff.txt b/doc/codediff.txt index c877d3d1..fda55db9 100644 --- a/doc/codediff.txt +++ b/doc/codediff.txt @@ -381,6 +381,7 @@ Setup entry point: prev_conflict = "[x", diffget_incoming = "2do", diffget_current = "3do", + hover = "K", }, }, }) diff --git a/lua/codediff/config.lua b/lua/codediff/config.lua index 59a4139f..8f594304 100644 --- a/lua/codediff/config.lua +++ b/lua/codediff/config.lua @@ -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) }, }, } diff --git a/lua/codediff/ui/conflict/hover.lua b/lua/codediff/ui/conflict/hover.lua new file mode 100644 index 00000000..e00e88ce --- /dev/null +++ b/lua/codediff/ui/conflict/hover.lua @@ -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 diff --git a/lua/codediff/ui/conflict/init.lua b/lua/codediff/ui/conflict/init.lua index 50a4773a..929bbccc 100644 --- a/lua/codediff/ui/conflict/init.lua +++ b/lua/codediff/ui/conflict/init.lua @@ -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 @@ -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 diff --git a/lua/codediff/ui/conflict/keymaps.lua b/lua/codediff/ui/conflict/keymaps.lua index c328d4c5..9e244f41 100644 --- a/lua/codediff/ui/conflict/keymaps.lua +++ b/lua/codediff/ui/conflict/keymaps.lua @@ -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 = { @@ -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 diff --git a/lua/codediff/ui/keymap_help.lua b/lua/codediff/ui/keymap_help.lua index c9cbf978..0ccf0d31 100644 --- a/lua/codediff/ui/keymap_help.lua +++ b/lua/codediff/ui/keymap_help.lua @@ -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 diff --git a/tests/README.md b/tests/README.md index 44ba93e0..4c82301f 100644 --- a/tests/README.md +++ b/tests/README.md @@ -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. diff --git a/tests/e2e/COVERAGE.md b/tests/e2e/COVERAGE.md index 4852d15b..ac67b5fb 100644 --- a/tests/e2e/COVERAGE.md +++ b/tests/e2e/COVERAGE.md @@ -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. @@ -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 | @@ -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 | diff --git a/tests/e2e/conflict/actions_spec.lua b/tests/e2e/conflict/actions_spec.lua index 11404ce1..c438e4d7 100644 --- a/tests/e2e/conflict/actions_spec.lua +++ b/tests/e2e/conflict/actions_spec.lua @@ -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 diff --git a/tests/fixtures/keymap_matrix.txt b/tests/fixtures/keymap_matrix.txt index 5d7fcb8a..297840e5 100644 --- a/tests/fixtures/keymap_matrix.txt +++ b/tests/fixtures/keymap_matrix.txt @@ -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 @@ -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 diff --git a/tests/integration/keymap/actions_spec.lua b/tests/integration/keymap/actions_spec.lua index 3071c7c0..8e8cf76e 100644 --- a/tests/integration/keymap/actions_spec.lua +++ b/tests/integration/keymap/actions_spec.lua @@ -130,6 +130,7 @@ local EXPECT = { prev_conflict = { "conflict" }, diffget_incoming = { "conflict" }, diffget_current = { "conflict" }, + hover = { "conflict" }, }, } diff --git a/tests/integration/ui/conflict/hover_spec.lua b/tests/integration/ui/conflict/hover_spec.lua new file mode 100644 index 00000000..74d52191 --- /dev/null +++ b/tests/integration/ui/conflict/hover_spec.lua @@ -0,0 +1,135 @@ +local conflict = require("codediff.ui.conflict") +local lifecycle = require("codediff.ui.lifecycle") +local inline = require("codediff.ui.inline") + +describe("conflict hover preview", function() + local original_get_session + local tabpage + local session + local buffers + + local function new_buffer(lines) + local bufnr = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) + buffers[#buffers + 1] = bufnr + return bufnr + end + + -- One conflict block whose current (right) side is `current_lines`, against `base_lines`. + local function configure(base_lines, current_lines, base_range, current_range) + local block = { + base_range = base_range, + result_range = base_range, + output1_range = { start_line = 1, end_line = 2 }, + output2_range = current_range, + } + session = { + result_bufnr = new_buffer(vim.deepcopy(base_lines)), + original_bufnr = new_buffer({ "incoming" }), + modified_bufnr = new_buffer(current_lines), + result_base_lines = vim.deepcopy(base_lines), + merge_base_lines = vim.deepcopy(base_lines), + conflict_blocks = { block }, + } + lifecycle.get_session = function(candidate) + return candidate == tabpage and session or nil + end + conflict.initialize_tracking(session.result_bufnr, session.conflict_blocks) + vim.api.nvim_set_current_buf(session.modified_bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + end + + -- Virtual lines of the preview as { row = 0-based anchor, text = trimmed line }. + local function virtual_lines(win) + local found = {} + local marks = vim.api.nvim_buf_get_extmarks(vim.api.nvim_win_get_buf(win), inline.ns_inline, 0, -1, { details = true }) + for _, mark in ipairs(marks) do + for _, chunks in ipairs(mark[4].virt_lines or {}) do + local text = table.concat(vim.tbl_map(function(chunk) + return chunk[1] + end, chunks)) + table.insert(found, { row = mark[2], text = (text:gsub("%s+$", "")) }) + end + end + return found + end + + local function topfill(win) + return vim.api.nvim_win_call(win, vim.fn.winsaveview).topfill + end + + before_each(function() + original_get_session = lifecycle.get_session + tabpage = vim.api.nvim_get_current_tabpage() + buffers = {} + end) + + after_each(function() + lifecycle.get_session = original_get_session + for _, win in ipairs(vim.api.nvim_list_wins()) do + if vim.api.nvim_win_get_config(win).relative ~= "" then + vim.api.nvim_win_close(win, true) + end + end + vim.wo.scrollbind = false + vim.api.nvim_set_current_buf(vim.api.nvim_create_buf(false, true)) + for _, bufnr in ipairs(buffers) do + if vim.api.nvim_buf_is_valid(bufnr) then + vim.api.nvim_buf_delete(bufnr, { force = true }) + end + end + end) + + it("shows the focused side with the replaced base line above it", function() + configure({ "base" }, { "current" }, { start_line = 1, end_line = 2 }, { start_line = 1, end_line = 2 }) + vim.wo.scrollbind = true + + assert.is_true(conflict.toggle_hover(tabpage)) + local win = session._hover_win + assert.is_true(vim.api.nvim_win_is_valid(win)) + assert.same({ "current" }, vim.api.nvim_buf_get_lines(vim.api.nvim_win_get_buf(win), 0, -1, false)) + assert.same({ { row = 0, text = "base" } }, virtual_lines(win)) + assert.equals(1, topfill(win)) + assert.equals(2, vim.api.nvim_win_get_height(win)) + assert.is_false(vim.wo[win].scrollbind) + assert.equals(" Ours (Current) vs Base ", vim.api.nvim_win_get_config(win).title[1][1]) + + assert.is_false(conflict.toggle_hover(tabpage)) + assert.is_false(vim.api.nvim_win_is_valid(win)) + end) + + it("shows a side added on an empty base as inserted lines only", function() + configure({ "context" }, { "added" }, { start_line = 1, end_line = 1 }, { start_line = 1, end_line = 2 }) + + assert.is_true(conflict.toggle_hover(tabpage)) + local win = session._hover_win + assert.same({}, virtual_lines(win)) + local marks = vim.api.nvim_buf_get_extmarks(vim.api.nvim_win_get_buf(win), inline.ns_inline, 0, -1, { details = true }) + assert.is_true(#marks > 0) + assert.equals("CodeDiffLineInsert", marks[1][4].hl_group) + assert.equals(0, topfill(win)) + assert.equals(1, vim.api.nvim_win_get_height(win)) + end) + + it("keeps a deletion at the end of the hunk below the side's last line", function() + configure({ "a", "b", "c" }, { "A", "b" }, { start_line = 1, end_line = 4 }, { start_line = 1, end_line = 3 }) + + assert.is_true(conflict.toggle_hover(tabpage)) + local win = session._hover_win + assert.same({ "A", "b", "" }, vim.api.nvim_buf_get_lines(vim.api.nvim_win_get_buf(win), 0, -1, false)) + assert.same({ { row = 0, text = "a" }, { row = 2, text = "c" } }, virtual_lines(win)) + assert.equals(1, topfill(win)) + assert.equals(5, vim.api.nvim_win_get_height(win)) + end) + + it("does nothing outside a conflict or in the result pane", function() + configure({ "base", "tail" }, { "current", "tail" }, { start_line = 1, end_line = 2 }, { start_line = 1, end_line = 2 }) + + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + assert.is_false(conflict.toggle_hover(tabpage)) + vim.api.nvim_set_current_buf(session.result_bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + assert.is_false(conflict.toggle_hover(tabpage)) + assert.is_nil(session._hover_win) + end) +end)