feat: unified diff conflict resolution for unmerged files (#99)
## Problem Pressing `du` on a `UU` (unmerged) file in the fugitive status buffer had no effect. There was no way to see a proper ours-vs-theirs diff with syntax highlighting and intra-line changes, or to resolve conflicts from within a unified diff view. Additionally, pressing `du` on a section header containing only unmerged files showed "no changes in section" because `git diff` produces combined (`diff --cc`) output for unmerged files, which was stripped entirely. ## Solution Fetch `:2:` (ours) and `:3:` (theirs) from the git index and generate a standard unified diff. The existing highlight pipeline (treesitter + intra-line) applies automatically. Resolution keymaps (`doo`/`dot`/`dob`/`don`) on hunks in the diff view write changes back to the working file's conflict markers. Navigation (`]x`/`[x`) jumps between unresolved conflict hunks. For section diffs, combined diff entries are now replaced with generated ours-vs-theirs unified diffs instead of being stripped. Works for merge, cherry-pick, and rebase conflicts — git populates `:2:`/`:3:` the same way for all three. Closes #61
This commit is contained in:
parent
49fc446aae
commit
a2053a132b
8 changed files with 1287 additions and 28 deletions
|
|
@ -315,6 +315,32 @@ Example configuration: >lua
|
|||
vim.keymap.set('n', '[x', '<Plug>(diffs-conflict-prev)')
|
||||
<
|
||||
|
||||
*<Plug>(diffs-merge-ours)*
|
||||
<Plug>(diffs-merge-ours)
|
||||
Accept ours in a merge diff view. Resolves the
|
||||
conflict in the working file with ours content.
|
||||
|
||||
*<Plug>(diffs-merge-theirs)*
|
||||
<Plug>(diffs-merge-theirs)
|
||||
Accept theirs in a merge diff view.
|
||||
|
||||
*<Plug>(diffs-merge-both)*
|
||||
<Plug>(diffs-merge-both)
|
||||
Accept both (ours then theirs) in a merge diff view.
|
||||
|
||||
*<Plug>(diffs-merge-none)*
|
||||
<Plug>(diffs-merge-none)
|
||||
Reject both in a merge diff view.
|
||||
|
||||
*<Plug>(diffs-merge-next)*
|
||||
<Plug>(diffs-merge-next)
|
||||
Jump to next unresolved conflict hunk in merge diff.
|
||||
|
||||
*<Plug>(diffs-merge-prev)*
|
||||
<Plug>(diffs-merge-prev)
|
||||
Jump to previous unresolved conflict hunk in merge
|
||||
diff.
|
||||
|
||||
Diff buffer mappings: ~
|
||||
*diffs-q*
|
||||
q Close the diff window. Available in all `diffs://`
|
||||
|
|
@ -345,6 +371,7 @@ Behavior by file status: ~
|
|||
A Staged (empty) index file as all-added
|
||||
D Staged HEAD (empty) file as all-removed
|
||||
R Staged HEAD:oldname index:newname content diff
|
||||
U Unstaged :2: (ours) :3: (theirs) merge diff
|
||||
? Untracked (empty) working tree file as all-added
|
||||
|
||||
On section headers, the keymap runs `git diff` (or `git diff --cached` for
|
||||
|
|
@ -458,6 +485,31 @@ User events: ~
|
|||
})
|
||||
<
|
||||
|
||||
==============================================================================
|
||||
MERGE DIFF RESOLUTION *diffs-merge*
|
||||
|
||||
When pressing `du`/`dU` on an unmerged (`U`) file in the fugitive status
|
||||
buffer, diffs.nvim opens a unified diff of ours (`git show :2:path`) vs
|
||||
theirs (`git show :3:path`) with full treesitter and intra-line highlighting.
|
||||
|
||||
The same conflict resolution keymaps (`doo`/`dot`/`dob`/`don`/`]x`/`[x`)
|
||||
are available on the diff buffer. They resolve conflicts in the working
|
||||
file by matching diff hunks to conflict markers:
|
||||
|
||||
- `doo` replaces the conflict region with ours content
|
||||
- `dot` replaces the conflict region with theirs content
|
||||
- `dob` replaces with both (ours then theirs)
|
||||
- `don` removes the conflict region entirely
|
||||
- `]x`/`[x` navigate between unresolved conflict hunks
|
||||
|
||||
Resolved hunks are marked with `(resolved)` virtual text. Hunks that
|
||||
correspond to auto-merged content (no conflict markers) show an
|
||||
informational notification and are left unchanged.
|
||||
|
||||
The working file buffer is modified in place; save it when ready.
|
||||
Phase 1 inline conflict highlights (see |diffs-conflict|) are refreshed
|
||||
automatically after each resolution.
|
||||
|
||||
==============================================================================
|
||||
API *diffs-api*
|
||||
|
||||
|
|
|
|||
|
|
@ -36,6 +36,24 @@ function M.find_hunk_line(diff_lines, hunk_position)
|
|||
return nil
|
||||
end
|
||||
|
||||
---@param lines string[]
|
||||
---@return string[]
|
||||
function M.filter_combined_diffs(lines)
|
||||
local result = {}
|
||||
local skip = false
|
||||
for _, line in ipairs(lines) do
|
||||
if line:match('^diff %-%-cc ') then
|
||||
skip = true
|
||||
elseif line:match('^diff %-%-git ') then
|
||||
skip = false
|
||||
end
|
||||
if not skip then
|
||||
table.insert(result, line)
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
---@param old_lines string[]
|
||||
---@param new_lines string[]
|
||||
---@param old_name string
|
||||
|
|
@ -69,6 +87,33 @@ local function generate_unified_diff(old_lines, new_lines, old_name, new_name)
|
|||
return result
|
||||
end
|
||||
|
||||
---@param raw_lines string[]
|
||||
---@param repo_root string
|
||||
---@return string[]
|
||||
local function replace_combined_diffs(raw_lines, repo_root)
|
||||
local unmerged_files = {}
|
||||
for _, line in ipairs(raw_lines) do
|
||||
local cc_file = line:match('^diff %-%-cc (.+)$')
|
||||
if cc_file then
|
||||
table.insert(unmerged_files, cc_file)
|
||||
end
|
||||
end
|
||||
|
||||
local result = M.filter_combined_diffs(raw_lines)
|
||||
|
||||
for _, filename in ipairs(unmerged_files) do
|
||||
local filepath = repo_root .. '/' .. filename
|
||||
local old_lines = git.get_file_content(':2', filepath) or {}
|
||||
local new_lines = git.get_file_content(':3', filepath) or {}
|
||||
local diff_lines = generate_unified_diff(old_lines, new_lines, filename, filename)
|
||||
for _, dl in ipairs(diff_lines) do
|
||||
table.insert(result, dl)
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
---@param revision? string
|
||||
---@param vertical? boolean
|
||||
function M.gdiff(revision, vertical)
|
||||
|
|
@ -138,6 +183,7 @@ end
|
|||
---@field vertical? boolean
|
||||
---@field staged? boolean
|
||||
---@field untracked? boolean
|
||||
---@field unmerged? boolean
|
||||
---@field old_filepath? string
|
||||
---@field hunk_position? { hunk_header: string, offset: integer }
|
||||
|
||||
|
|
@ -157,7 +203,17 @@ function M.gdiff_file(filepath, opts)
|
|||
local old_lines, new_lines, err
|
||||
local diff_label
|
||||
|
||||
if opts.untracked then
|
||||
if opts.unmerged then
|
||||
old_lines = git.get_file_content(':2', filepath)
|
||||
if not old_lines then
|
||||
old_lines = {}
|
||||
end
|
||||
new_lines = git.get_file_content(':3', filepath)
|
||||
if not new_lines then
|
||||
new_lines = {}
|
||||
end
|
||||
diff_label = 'unmerged'
|
||||
elseif opts.untracked then
|
||||
old_lines = {}
|
||||
new_lines, err = git.get_working_content(filepath)
|
||||
if not new_lines then
|
||||
|
|
@ -236,6 +292,14 @@ function M.gdiff_file(filepath, opts)
|
|||
end
|
||||
|
||||
M.setup_diff_buf(diff_buf)
|
||||
|
||||
if diff_label == 'unmerged' then
|
||||
vim.api.nvim_buf_set_var(diff_buf, 'diffs_unmerged', true)
|
||||
vim.api.nvim_buf_set_var(diff_buf, 'diffs_working_path', filepath)
|
||||
local conflict_config = require('diffs').get_conflict_config()
|
||||
require('diffs.merge').setup_keymaps(diff_buf, conflict_config)
|
||||
end
|
||||
|
||||
dbg('opened diff buffer %d for %s (%s)', diff_buf, rel_path, diff_label)
|
||||
|
||||
vim.schedule(function()
|
||||
|
|
@ -263,6 +327,8 @@ function M.gdiff_section(repo_root, opts)
|
|||
return
|
||||
end
|
||||
|
||||
result = replace_combined_diffs(result, repo_root)
|
||||
|
||||
if #result == 0 then
|
||||
vim.notify('[diffs.nvim]: no changes in section', vim.log.levels.INFO)
|
||||
return
|
||||
|
|
@ -325,6 +391,8 @@ function M.read_buffer(bufnr)
|
|||
if vim.v.shell_error ~= 0 then
|
||||
diff_lines = {}
|
||||
end
|
||||
|
||||
diff_lines = replace_combined_diffs(diff_lines, repo_root)
|
||||
else
|
||||
local abs_path = repo_root .. '/' .. path
|
||||
|
||||
|
|
@ -334,7 +402,10 @@ function M.read_buffer(bufnr)
|
|||
|
||||
local old_lines, new_lines
|
||||
|
||||
if label == 'untracked' then
|
||||
if label == 'unmerged' then
|
||||
old_lines = git.get_file_content(':2', abs_path) or {}
|
||||
new_lines = git.get_file_content(':3', abs_path) or {}
|
||||
elseif label == 'untracked' then
|
||||
old_lines = {}
|
||||
new_lines = git.get_working_content(abs_path) or {}
|
||||
elseif label == 'staged' then
|
||||
|
|
|
|||
|
|
@ -199,7 +199,7 @@ end
|
|||
---@param bufnr integer
|
||||
---@param region diffs.ConflictRegion
|
||||
---@param replacement string[]
|
||||
local function replace_region(bufnr, region, replacement)
|
||||
function M.replace_region(bufnr, region, replacement)
|
||||
vim.api.nvim_buf_set_lines(
|
||||
bufnr,
|
||||
region.marker_ours,
|
||||
|
|
@ -211,7 +211,7 @@ end
|
|||
|
||||
---@param bufnr integer
|
||||
---@param config diffs.ConflictConfig
|
||||
local function refresh(bufnr, config)
|
||||
function M.refresh(bufnr, config)
|
||||
local regions = parse_buffer(bufnr)
|
||||
if #regions == 0 then
|
||||
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
|
||||
|
|
@ -244,8 +244,8 @@ function M.resolve_ours(bufnr, config)
|
|||
return
|
||||
end
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, region.ours_start, region.ours_end, false)
|
||||
replace_region(bufnr, region, lines)
|
||||
refresh(bufnr, config)
|
||||
M.replace_region(bufnr, region, lines)
|
||||
M.refresh(bufnr, config)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
|
|
@ -262,8 +262,8 @@ function M.resolve_theirs(bufnr, config)
|
|||
return
|
||||
end
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, region.theirs_start, region.theirs_end, false)
|
||||
replace_region(bufnr, region, lines)
|
||||
refresh(bufnr, config)
|
||||
M.replace_region(bufnr, region, lines)
|
||||
M.refresh(bufnr, config)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
|
|
@ -288,8 +288,8 @@ function M.resolve_both(bufnr, config)
|
|||
for _, l in ipairs(theirs) do
|
||||
table.insert(combined, l)
|
||||
end
|
||||
replace_region(bufnr, region, combined)
|
||||
refresh(bufnr, config)
|
||||
M.replace_region(bufnr, region, combined)
|
||||
M.refresh(bufnr, config)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
|
|
@ -305,8 +305,8 @@ function M.resolve_none(bufnr, config)
|
|||
if not region then
|
||||
return
|
||||
end
|
||||
replace_region(bufnr, region, {})
|
||||
refresh(bufnr, config)
|
||||
M.replace_region(bufnr, region, {})
|
||||
M.refresh(bufnr, config)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
|
|
@ -417,7 +417,7 @@ function M.attach(bufnr, config)
|
|||
if not attached_buffers[bufnr] then
|
||||
return true
|
||||
end
|
||||
refresh(bufnr, config)
|
||||
M.refresh(bufnr, config)
|
||||
end,
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -27,19 +27,19 @@ function M.get_section_at_line(bufnr, lnum)
|
|||
end
|
||||
|
||||
---@param line string
|
||||
---@return string?, string?
|
||||
---@return string?, string?, string?
|
||||
local function parse_file_line(line)
|
||||
local old, new = line:match('^R%d*%s+(.-)%s+->%s+(.+)$')
|
||||
if old and new then
|
||||
return vim.trim(new), vim.trim(old)
|
||||
return vim.trim(new), vim.trim(old), 'R'
|
||||
end
|
||||
|
||||
local filename = line:match('^[MADRCU?][MADRCU%s]*%s+(.+)$')
|
||||
if filename then
|
||||
return vim.trim(filename), nil
|
||||
local status, filename = line:match('^([MADRCU?])[MADRCU%s]*%s+(.+)$')
|
||||
if status and filename then
|
||||
return vim.trim(filename), nil, status
|
||||
end
|
||||
|
||||
return nil, nil
|
||||
return nil, nil, nil
|
||||
end
|
||||
|
||||
---@param line string
|
||||
|
|
@ -57,34 +57,34 @@ end
|
|||
|
||||
---@param bufnr integer
|
||||
---@param lnum integer
|
||||
---@return string?, diffs.FugitiveSection, boolean, string?
|
||||
---@return string?, diffs.FugitiveSection, boolean, string?, string?
|
||||
function M.get_file_at_line(bufnr, lnum)
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
local current_line = lines[lnum]
|
||||
|
||||
if not current_line then
|
||||
return nil, nil, false, nil
|
||||
return nil, nil, false, nil, nil
|
||||
end
|
||||
|
||||
local section_header = parse_section_header(current_line)
|
||||
if section_header then
|
||||
return nil, section_header, true, nil
|
||||
return nil, section_header, true, nil, nil
|
||||
end
|
||||
|
||||
local filename, old_filename = parse_file_line(current_line)
|
||||
local filename, old_filename, status = parse_file_line(current_line)
|
||||
if filename then
|
||||
local section = M.get_section_at_line(bufnr, lnum)
|
||||
return filename, section, false, old_filename
|
||||
return filename, section, false, old_filename, status
|
||||
end
|
||||
|
||||
local prefix = current_line:sub(1, 1)
|
||||
if prefix == '+' or prefix == '-' or prefix == ' ' then
|
||||
for i = lnum - 1, 1, -1 do
|
||||
local prev_line = lines[i]
|
||||
filename, old_filename = parse_file_line(prev_line)
|
||||
filename, old_filename, status = parse_file_line(prev_line)
|
||||
if filename then
|
||||
local section = M.get_section_at_line(bufnr, i)
|
||||
return filename, section, false, old_filename
|
||||
return filename, section, false, old_filename, status
|
||||
end
|
||||
if prev_line:match('^%w+ %(') or prev_line == '' then
|
||||
break
|
||||
|
|
@ -92,7 +92,7 @@ function M.get_file_at_line(bufnr, lnum)
|
|||
end
|
||||
end
|
||||
|
||||
return nil, nil, false, nil
|
||||
return nil, nil, false, nil, nil
|
||||
end
|
||||
|
||||
---@class diffs.HunkPosition
|
||||
|
|
@ -150,7 +150,7 @@ function M.diff_file_under_cursor(vertical)
|
|||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local lnum = vim.api.nvim_win_get_cursor(0)[1]
|
||||
|
||||
local filename, section, is_header, old_filename = M.get_file_at_line(bufnr, lnum)
|
||||
local filename, section, is_header, old_filename, status = M.get_file_at_line(bufnr, lnum)
|
||||
|
||||
local repo_root = get_repo_root_from_fugitive(bufnr)
|
||||
if not repo_root then
|
||||
|
|
@ -192,6 +192,7 @@ function M.diff_file_under_cursor(vertical)
|
|||
vertical = vertical,
|
||||
staged = section == 'staged',
|
||||
untracked = section == 'untracked',
|
||||
unmerged = status == 'U',
|
||||
old_filepath = old_filepath,
|
||||
hunk_position = hunk_position,
|
||||
})
|
||||
|
|
|
|||
374
lua/diffs/merge.lua
Normal file
374
lua/diffs/merge.lua
Normal file
|
|
@ -0,0 +1,374 @@
|
|||
local M = {}
|
||||
|
||||
local conflict = require('diffs.conflict')
|
||||
|
||||
local ns = vim.api.nvim_create_namespace('diffs-merge')
|
||||
|
||||
---@type table<integer, table<integer, true>>
|
||||
local resolved_hunks = {}
|
||||
|
||||
---@class diffs.MergeHunkInfo
|
||||
---@field index integer
|
||||
---@field start_line integer
|
||||
---@field end_line integer
|
||||
---@field del_lines string[]
|
||||
---@field add_lines string[]
|
||||
|
||||
---@param bufnr integer
|
||||
---@return diffs.MergeHunkInfo[]
|
||||
function M.parse_hunks(bufnr)
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
local hunks = {}
|
||||
local current = nil
|
||||
|
||||
for i, line in ipairs(lines) do
|
||||
local idx = i - 1
|
||||
if line:match('^@@') then
|
||||
if current then
|
||||
current.end_line = idx - 1
|
||||
table.insert(hunks, current)
|
||||
end
|
||||
current = {
|
||||
index = #hunks + 1,
|
||||
start_line = idx,
|
||||
end_line = idx,
|
||||
del_lines = {},
|
||||
add_lines = {},
|
||||
}
|
||||
elseif current then
|
||||
local prefix = line:sub(1, 1)
|
||||
if prefix == '-' then
|
||||
table.insert(current.del_lines, line:sub(2))
|
||||
elseif prefix == '+' then
|
||||
table.insert(current.add_lines, line:sub(2))
|
||||
elseif prefix ~= ' ' and prefix ~= '\\' then
|
||||
current.end_line = idx - 1
|
||||
table.insert(hunks, current)
|
||||
current = nil
|
||||
end
|
||||
if current then
|
||||
current.end_line = idx
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if current then
|
||||
table.insert(hunks, current)
|
||||
end
|
||||
|
||||
return hunks
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@return diffs.MergeHunkInfo?
|
||||
function M.find_hunk_at_cursor(bufnr)
|
||||
local hunks = M.parse_hunks(bufnr)
|
||||
local cursor_line = vim.api.nvim_win_get_cursor(0)[1] - 1
|
||||
|
||||
for _, hunk in ipairs(hunks) do
|
||||
if cursor_line >= hunk.start_line and cursor_line <= hunk.end_line then
|
||||
return hunk
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param hunk diffs.MergeHunkInfo
|
||||
---@param working_bufnr integer
|
||||
---@return diffs.ConflictRegion?
|
||||
function M.match_hunk_to_conflict(hunk, working_bufnr)
|
||||
local working_lines = vim.api.nvim_buf_get_lines(working_bufnr, 0, -1, false)
|
||||
local regions = conflict.parse(working_lines)
|
||||
|
||||
for _, region in ipairs(regions) do
|
||||
local ours_lines = {}
|
||||
for line = region.ours_start + 1, region.ours_end do
|
||||
table.insert(ours_lines, working_lines[line])
|
||||
end
|
||||
|
||||
if #ours_lines == #hunk.del_lines then
|
||||
local match = true
|
||||
for j = 1, #ours_lines do
|
||||
if ours_lines[j] ~= hunk.del_lines[j] then
|
||||
match = false
|
||||
break
|
||||
end
|
||||
end
|
||||
if match then
|
||||
return region
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param diff_bufnr integer
|
||||
---@return integer?
|
||||
function M.get_or_load_working_buf(diff_bufnr)
|
||||
local ok, working_path = pcall(vim.api.nvim_buf_get_var, diff_bufnr, 'diffs_working_path')
|
||||
if not ok or not working_path then
|
||||
return nil
|
||||
end
|
||||
|
||||
local existing = vim.fn.bufnr(working_path)
|
||||
if existing ~= -1 then
|
||||
return existing
|
||||
end
|
||||
|
||||
local bufnr = vim.fn.bufadd(working_path)
|
||||
vim.fn.bufload(bufnr)
|
||||
return bufnr
|
||||
end
|
||||
|
||||
---@param diff_bufnr integer
|
||||
---@param hunk_index integer
|
||||
local function mark_resolved(diff_bufnr, hunk_index)
|
||||
if not resolved_hunks[diff_bufnr] then
|
||||
resolved_hunks[diff_bufnr] = {}
|
||||
end
|
||||
resolved_hunks[diff_bufnr][hunk_index] = true
|
||||
end
|
||||
|
||||
---@param diff_bufnr integer
|
||||
---@param hunk_index integer
|
||||
---@return boolean
|
||||
function M.is_resolved(diff_bufnr, hunk_index)
|
||||
return resolved_hunks[diff_bufnr] and resolved_hunks[diff_bufnr][hunk_index] or false
|
||||
end
|
||||
|
||||
---@param diff_bufnr integer
|
||||
---@param hunk diffs.MergeHunkInfo
|
||||
local function add_resolved_virtual_text(diff_bufnr, hunk)
|
||||
pcall(vim.api.nvim_buf_set_extmark, diff_bufnr, ns, hunk.start_line, 0, {
|
||||
virt_text = { { ' (resolved)', 'Comment' } },
|
||||
virt_text_pos = 'eol',
|
||||
})
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param config diffs.ConflictConfig
|
||||
function M.resolve_ours(bufnr, config)
|
||||
local hunk = M.find_hunk_at_cursor(bufnr)
|
||||
if not hunk then
|
||||
return
|
||||
end
|
||||
if M.is_resolved(bufnr, hunk.index) then
|
||||
vim.notify('[diffs.nvim]: hunk already resolved', vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
local working_bufnr = M.get_or_load_working_buf(bufnr)
|
||||
if not working_bufnr then
|
||||
return
|
||||
end
|
||||
local region = M.match_hunk_to_conflict(hunk, working_bufnr)
|
||||
if not region then
|
||||
vim.notify('[diffs.nvim]: hunk does not correspond to a conflict region', vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
local lines = vim.api.nvim_buf_get_lines(working_bufnr, region.ours_start, region.ours_end, false)
|
||||
conflict.replace_region(working_bufnr, region, lines)
|
||||
conflict.refresh(working_bufnr, config)
|
||||
mark_resolved(bufnr, hunk.index)
|
||||
add_resolved_virtual_text(bufnr, hunk)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param config diffs.ConflictConfig
|
||||
function M.resolve_theirs(bufnr, config)
|
||||
local hunk = M.find_hunk_at_cursor(bufnr)
|
||||
if not hunk then
|
||||
return
|
||||
end
|
||||
if M.is_resolved(bufnr, hunk.index) then
|
||||
vim.notify('[diffs.nvim]: hunk already resolved', vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
local working_bufnr = M.get_or_load_working_buf(bufnr)
|
||||
if not working_bufnr then
|
||||
return
|
||||
end
|
||||
local region = M.match_hunk_to_conflict(hunk, working_bufnr)
|
||||
if not region then
|
||||
vim.notify('[diffs.nvim]: hunk does not correspond to a conflict region', vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
local lines =
|
||||
vim.api.nvim_buf_get_lines(working_bufnr, region.theirs_start, region.theirs_end, false)
|
||||
conflict.replace_region(working_bufnr, region, lines)
|
||||
conflict.refresh(working_bufnr, config)
|
||||
mark_resolved(bufnr, hunk.index)
|
||||
add_resolved_virtual_text(bufnr, hunk)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param config diffs.ConflictConfig
|
||||
function M.resolve_both(bufnr, config)
|
||||
local hunk = M.find_hunk_at_cursor(bufnr)
|
||||
if not hunk then
|
||||
return
|
||||
end
|
||||
if M.is_resolved(bufnr, hunk.index) then
|
||||
vim.notify('[diffs.nvim]: hunk already resolved', vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
local working_bufnr = M.get_or_load_working_buf(bufnr)
|
||||
if not working_bufnr then
|
||||
return
|
||||
end
|
||||
local region = M.match_hunk_to_conflict(hunk, working_bufnr)
|
||||
if not region then
|
||||
vim.notify('[diffs.nvim]: hunk does not correspond to a conflict region', vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
local ours = vim.api.nvim_buf_get_lines(working_bufnr, region.ours_start, region.ours_end, false)
|
||||
local theirs =
|
||||
vim.api.nvim_buf_get_lines(working_bufnr, region.theirs_start, region.theirs_end, false)
|
||||
local combined = {}
|
||||
for _, l in ipairs(ours) do
|
||||
table.insert(combined, l)
|
||||
end
|
||||
for _, l in ipairs(theirs) do
|
||||
table.insert(combined, l)
|
||||
end
|
||||
conflict.replace_region(working_bufnr, region, combined)
|
||||
conflict.refresh(working_bufnr, config)
|
||||
mark_resolved(bufnr, hunk.index)
|
||||
add_resolved_virtual_text(bufnr, hunk)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param config diffs.ConflictConfig
|
||||
function M.resolve_none(bufnr, config)
|
||||
local hunk = M.find_hunk_at_cursor(bufnr)
|
||||
if not hunk then
|
||||
return
|
||||
end
|
||||
if M.is_resolved(bufnr, hunk.index) then
|
||||
vim.notify('[diffs.nvim]: hunk already resolved', vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
local working_bufnr = M.get_or_load_working_buf(bufnr)
|
||||
if not working_bufnr then
|
||||
return
|
||||
end
|
||||
local region = M.match_hunk_to_conflict(hunk, working_bufnr)
|
||||
if not region then
|
||||
vim.notify('[diffs.nvim]: hunk does not correspond to a conflict region', vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
conflict.replace_region(working_bufnr, region, {})
|
||||
conflict.refresh(working_bufnr, config)
|
||||
mark_resolved(bufnr, hunk.index)
|
||||
add_resolved_virtual_text(bufnr, hunk)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
function M.goto_next(bufnr)
|
||||
local hunks = M.parse_hunks(bufnr)
|
||||
if #hunks == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local working_bufnr = M.get_or_load_working_buf(bufnr)
|
||||
if not working_bufnr then
|
||||
return
|
||||
end
|
||||
|
||||
local cursor_line = vim.api.nvim_win_get_cursor(0)[1] - 1
|
||||
|
||||
local candidates = {}
|
||||
for _, hunk in ipairs(hunks) do
|
||||
if not M.is_resolved(bufnr, hunk.index) then
|
||||
if M.match_hunk_to_conflict(hunk, working_bufnr) then
|
||||
table.insert(candidates, hunk)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if #candidates == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
for _, hunk in ipairs(candidates) do
|
||||
if hunk.start_line > cursor_line then
|
||||
vim.api.nvim_win_set_cursor(0, { hunk.start_line + 1, 0 })
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
vim.api.nvim_win_set_cursor(0, { candidates[1].start_line + 1, 0 })
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
function M.goto_prev(bufnr)
|
||||
local hunks = M.parse_hunks(bufnr)
|
||||
if #hunks == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local working_bufnr = M.get_or_load_working_buf(bufnr)
|
||||
if not working_bufnr then
|
||||
return
|
||||
end
|
||||
|
||||
local cursor_line = vim.api.nvim_win_get_cursor(0)[1] - 1
|
||||
|
||||
local candidates = {}
|
||||
for _, hunk in ipairs(hunks) do
|
||||
if not M.is_resolved(bufnr, hunk.index) then
|
||||
if M.match_hunk_to_conflict(hunk, working_bufnr) then
|
||||
table.insert(candidates, hunk)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if #candidates == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
for i = #candidates, 1, -1 do
|
||||
if candidates[i].start_line < cursor_line then
|
||||
vim.api.nvim_win_set_cursor(0, { candidates[i].start_line + 1, 0 })
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
vim.api.nvim_win_set_cursor(0, { candidates[#candidates].start_line + 1, 0 })
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param config diffs.ConflictConfig
|
||||
function M.setup_keymaps(bufnr, config)
|
||||
local km = config.keymaps
|
||||
|
||||
local maps = {
|
||||
{ km.ours, '<Plug>(diffs-merge-ours)' },
|
||||
{ km.theirs, '<Plug>(diffs-merge-theirs)' },
|
||||
{ km.both, '<Plug>(diffs-merge-both)' },
|
||||
{ km.none, '<Plug>(diffs-merge-none)' },
|
||||
{ km.next, '<Plug>(diffs-merge-next)' },
|
||||
{ km.prev, '<Plug>(diffs-merge-prev)' },
|
||||
}
|
||||
|
||||
for _, map in ipairs(maps) do
|
||||
if map[1] then
|
||||
vim.keymap.set('n', map[1], map[2], { buffer = bufnr })
|
||||
end
|
||||
end
|
||||
|
||||
vim.api.nvim_create_autocmd('BufWipeout', {
|
||||
buffer = bufnr,
|
||||
callback = function()
|
||||
resolved_hunks[bufnr] = nil
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
---@return integer
|
||||
function M.get_namespace()
|
||||
return ns
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
@ -82,3 +82,28 @@ end, { desc = 'Jump to next conflict' })
|
|||
vim.keymap.set('n', '<Plug>(diffs-conflict-prev)', function()
|
||||
require('diffs.conflict').goto_prev(vim.api.nvim_get_current_buf())
|
||||
end, { desc = 'Jump to previous conflict' })
|
||||
|
||||
local function merge_action(fn)
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local config = require('diffs').get_conflict_config()
|
||||
fn(bufnr, config)
|
||||
end
|
||||
|
||||
vim.keymap.set('n', '<Plug>(diffs-merge-ours)', function()
|
||||
merge_action(require('diffs.merge').resolve_ours)
|
||||
end, { desc = 'Accept ours in merge diff' })
|
||||
vim.keymap.set('n', '<Plug>(diffs-merge-theirs)', function()
|
||||
merge_action(require('diffs.merge').resolve_theirs)
|
||||
end, { desc = 'Accept theirs in merge diff' })
|
||||
vim.keymap.set('n', '<Plug>(diffs-merge-both)', function()
|
||||
merge_action(require('diffs.merge').resolve_both)
|
||||
end, { desc = 'Accept both in merge diff' })
|
||||
vim.keymap.set('n', '<Plug>(diffs-merge-none)', function()
|
||||
merge_action(require('diffs.merge').resolve_none)
|
||||
end, { desc = 'Reject both in merge diff' })
|
||||
vim.keymap.set('n', '<Plug>(diffs-merge-next)', function()
|
||||
require('diffs.merge').goto_next(vim.api.nvim_get_current_buf())
|
||||
end, { desc = 'Jump to next conflict hunk' })
|
||||
vim.keymap.set('n', '<Plug>(diffs-merge-prev)', function()
|
||||
require('diffs.merge').goto_prev(vim.api.nvim_get_current_buf())
|
||||
end, { desc = 'Jump to previous conflict hunk' })
|
||||
|
|
|
|||
|
|
@ -40,6 +40,78 @@ describe('commands', function()
|
|||
end)
|
||||
end)
|
||||
|
||||
describe('filter_combined_diffs', function()
|
||||
it('strips diff --cc entries entirely', function()
|
||||
local lines = {
|
||||
'diff --cc main.lua',
|
||||
'index d13ab94,b113aee..0000000',
|
||||
'--- a/main.lua',
|
||||
'+++ b/main.lua',
|
||||
'@@@ -1,7 -1,7 +1,11 @@@',
|
||||
' local M = {}',
|
||||
'++<<<<<<< HEAD',
|
||||
' + return 1',
|
||||
'++=======',
|
||||
'+ return 2',
|
||||
'++>>>>>>> theirs',
|
||||
' end',
|
||||
}
|
||||
local result = commands.filter_combined_diffs(lines)
|
||||
assert.are.equal(0, #result)
|
||||
end)
|
||||
|
||||
it('preserves diff --git entries', function()
|
||||
local lines = {
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,3 +1,3 @@',
|
||||
' local M = {}',
|
||||
'-local x = 1',
|
||||
'+local x = 2',
|
||||
' return M',
|
||||
}
|
||||
local result = commands.filter_combined_diffs(lines)
|
||||
assert.are.equal(8, #result)
|
||||
assert.are.same(lines, result)
|
||||
end)
|
||||
|
||||
it('strips combined but keeps unified in mixed output', function()
|
||||
local lines = {
|
||||
'diff --cc conflict.lua',
|
||||
'index aaa,bbb..000',
|
||||
'@@@ -1,1 -1,1 +1,5 @@@',
|
||||
'++<<<<<<< HEAD',
|
||||
'diff --git a/clean.lua b/clean.lua',
|
||||
'--- a/clean.lua',
|
||||
'+++ b/clean.lua',
|
||||
'@@ -1,1 +1,1 @@',
|
||||
'-old',
|
||||
'+new',
|
||||
}
|
||||
local result = commands.filter_combined_diffs(lines)
|
||||
assert.are.equal(6, #result)
|
||||
assert.are.equal('diff --git a/clean.lua b/clean.lua', result[1])
|
||||
assert.are.equal('+new', result[6])
|
||||
end)
|
||||
|
||||
it('returns empty for empty input', function()
|
||||
local result = commands.filter_combined_diffs({})
|
||||
assert.are.equal(0, #result)
|
||||
end)
|
||||
|
||||
it('returns empty when all entries are combined', function()
|
||||
local lines = {
|
||||
'diff --cc a.lua',
|
||||
'some content',
|
||||
'diff --cc b.lua',
|
||||
'more content',
|
||||
}
|
||||
local result = commands.filter_combined_diffs(lines)
|
||||
assert.are.equal(0, #result)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('find_hunk_line', function()
|
||||
it('finds matching @@ header and returns target line', function()
|
||||
local diff_lines = {
|
||||
|
|
|
|||
664
spec/merge_spec.lua
Normal file
664
spec/merge_spec.lua
Normal file
|
|
@ -0,0 +1,664 @@
|
|||
local helpers = require('spec.helpers')
|
||||
local merge = require('diffs.merge')
|
||||
|
||||
local function default_config(overrides)
|
||||
local cfg = {
|
||||
enabled = true,
|
||||
disable_diagnostics = false,
|
||||
show_virtual_text = true,
|
||||
keymaps = {
|
||||
ours = 'doo',
|
||||
theirs = 'dot',
|
||||
both = 'dob',
|
||||
none = 'don',
|
||||
next = ']x',
|
||||
prev = '[x',
|
||||
},
|
||||
}
|
||||
if overrides then
|
||||
cfg = vim.tbl_deep_extend('force', cfg, overrides)
|
||||
end
|
||||
return cfg
|
||||
end
|
||||
|
||||
local function create_diff_buffer(lines, working_path)
|
||||
local bufnr = helpers.create_buffer(lines)
|
||||
if working_path then
|
||||
vim.api.nvim_buf_set_var(bufnr, 'diffs_working_path', working_path)
|
||||
end
|
||||
return bufnr
|
||||
end
|
||||
|
||||
local function create_working_buffer(lines, name)
|
||||
local bufnr = vim.api.nvim_create_buf(true, false)
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||
if name then
|
||||
vim.api.nvim_buf_set_name(bufnr, name)
|
||||
end
|
||||
return bufnr
|
||||
end
|
||||
|
||||
describe('merge', function()
|
||||
describe('parse_hunks', function()
|
||||
it('parses a single hunk', function()
|
||||
local bufnr = helpers.create_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,3 +1,3 @@',
|
||||
' local M = {}',
|
||||
'-local x = 1',
|
||||
'+local x = 2',
|
||||
' return M',
|
||||
})
|
||||
|
||||
local hunks = merge.parse_hunks(bufnr)
|
||||
assert.are.equal(1, #hunks)
|
||||
assert.are.equal(3, hunks[1].start_line)
|
||||
assert.are.equal(7, hunks[1].end_line)
|
||||
assert.are.same({ 'local x = 1' }, hunks[1].del_lines)
|
||||
assert.are.same({ 'local x = 2' }, hunks[1].add_lines)
|
||||
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('parses multiple hunks', function()
|
||||
local bufnr = helpers.create_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,3 +1,3 @@',
|
||||
' local M = {}',
|
||||
'-local x = 1',
|
||||
'+local x = 2',
|
||||
' return M',
|
||||
'@@ -10,3 +10,3 @@',
|
||||
' function M.foo()',
|
||||
'- return 1',
|
||||
'+ return 2',
|
||||
' end',
|
||||
})
|
||||
|
||||
local hunks = merge.parse_hunks(bufnr)
|
||||
assert.are.equal(2, #hunks)
|
||||
assert.are.equal(3, hunks[1].start_line)
|
||||
assert.are.equal(8, hunks[2].start_line)
|
||||
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('parses add-only hunk', function()
|
||||
local bufnr = helpers.create_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,2 +1,3 @@',
|
||||
' local M = {}',
|
||||
'+local new = true',
|
||||
' return M',
|
||||
})
|
||||
|
||||
local hunks = merge.parse_hunks(bufnr)
|
||||
assert.are.equal(1, #hunks)
|
||||
assert.are.same({}, hunks[1].del_lines)
|
||||
assert.are.same({ 'local new = true' }, hunks[1].add_lines)
|
||||
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('parses delete-only hunk', function()
|
||||
local bufnr = helpers.create_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,3 +1,2 @@',
|
||||
' local M = {}',
|
||||
'-local old = false',
|
||||
' return M',
|
||||
})
|
||||
|
||||
local hunks = merge.parse_hunks(bufnr)
|
||||
assert.are.equal(1, #hunks)
|
||||
assert.are.same({ 'local old = false' }, hunks[1].del_lines)
|
||||
assert.are.same({}, hunks[1].add_lines)
|
||||
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('returns empty for buffer with no hunks', function()
|
||||
local bufnr = helpers.create_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
})
|
||||
|
||||
local hunks = merge.parse_hunks(bufnr)
|
||||
assert.are.equal(0, #hunks)
|
||||
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('match_hunk_to_conflict', function()
|
||||
it('matches hunk to conflict region', function()
|
||||
local working_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'local x = 1',
|
||||
'=======',
|
||||
'local x = 2',
|
||||
'>>>>>>> feature',
|
||||
}, '/tmp/diffs_test_match.lua')
|
||||
|
||||
local hunk = {
|
||||
index = 1,
|
||||
start_line = 3,
|
||||
end_line = 7,
|
||||
del_lines = { 'local x = 1' },
|
||||
add_lines = { 'local x = 2' },
|
||||
}
|
||||
|
||||
local region = merge.match_hunk_to_conflict(hunk, working_bufnr)
|
||||
assert.is_not_nil(region)
|
||||
assert.are.equal(0, region.marker_ours)
|
||||
|
||||
helpers.delete_buffer(working_bufnr)
|
||||
end)
|
||||
|
||||
it('returns nil for auto-merged content', function()
|
||||
local working_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'local x = 1',
|
||||
'=======',
|
||||
'local x = 2',
|
||||
'>>>>>>> feature',
|
||||
}, '/tmp/diffs_test_auto.lua')
|
||||
|
||||
local hunk = {
|
||||
index = 1,
|
||||
start_line = 3,
|
||||
end_line = 7,
|
||||
del_lines = { 'local y = 3' },
|
||||
add_lines = { 'local y = 4' },
|
||||
}
|
||||
|
||||
local region = merge.match_hunk_to_conflict(hunk, working_bufnr)
|
||||
assert.is_nil(region)
|
||||
|
||||
helpers.delete_buffer(working_bufnr)
|
||||
end)
|
||||
|
||||
it('matches with empty ours section', function()
|
||||
local working_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'=======',
|
||||
'local x = 2',
|
||||
'>>>>>>> feature',
|
||||
}, '/tmp/diffs_test_empty_ours.lua')
|
||||
|
||||
local hunk = {
|
||||
index = 1,
|
||||
start_line = 3,
|
||||
end_line = 5,
|
||||
del_lines = {},
|
||||
add_lines = { 'local x = 2' },
|
||||
}
|
||||
|
||||
local region = merge.match_hunk_to_conflict(hunk, working_bufnr)
|
||||
assert.is_not_nil(region)
|
||||
|
||||
helpers.delete_buffer(working_bufnr)
|
||||
end)
|
||||
|
||||
it('matches correct region among multiple conflicts', function()
|
||||
local working_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'local a = 1',
|
||||
'=======',
|
||||
'local a = 2',
|
||||
'>>>>>>> feature',
|
||||
'middle',
|
||||
'<<<<<<< HEAD',
|
||||
'local b = 3',
|
||||
'=======',
|
||||
'local b = 4',
|
||||
'>>>>>>> feature',
|
||||
}, '/tmp/diffs_test_multi.lua')
|
||||
|
||||
local hunk = {
|
||||
index = 2,
|
||||
start_line = 8,
|
||||
end_line = 12,
|
||||
del_lines = { 'local b = 3' },
|
||||
add_lines = { 'local b = 4' },
|
||||
}
|
||||
|
||||
local region = merge.match_hunk_to_conflict(hunk, working_bufnr)
|
||||
assert.is_not_nil(region)
|
||||
assert.are.equal(6, region.marker_ours)
|
||||
|
||||
helpers.delete_buffer(working_bufnr)
|
||||
end)
|
||||
|
||||
it('matches with diff3 format', function()
|
||||
local working_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'local x = 1',
|
||||
'||||||| base',
|
||||
'local x = 0',
|
||||
'=======',
|
||||
'local x = 2',
|
||||
'>>>>>>> feature',
|
||||
}, '/tmp/diffs_test_diff3.lua')
|
||||
|
||||
local hunk = {
|
||||
index = 1,
|
||||
start_line = 3,
|
||||
end_line = 7,
|
||||
del_lines = { 'local x = 1' },
|
||||
add_lines = { 'local x = 2' },
|
||||
}
|
||||
|
||||
local region = merge.match_hunk_to_conflict(hunk, working_bufnr)
|
||||
assert.is_not_nil(region)
|
||||
assert.are.equal(2, region.marker_base)
|
||||
|
||||
helpers.delete_buffer(working_bufnr)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('resolution', function()
|
||||
local diff_bufnr, working_bufnr
|
||||
|
||||
local function setup_buffers()
|
||||
local working_path = '/tmp/diffs_test_resolve.lua'
|
||||
working_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'local x = 1',
|
||||
'=======',
|
||||
'local x = 2',
|
||||
'>>>>>>> feature',
|
||||
}, working_path)
|
||||
|
||||
diff_bufnr = create_diff_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,1 +1,1 @@',
|
||||
'-local x = 1',
|
||||
'+local x = 2',
|
||||
}, working_path)
|
||||
vim.api.nvim_set_current_buf(diff_bufnr)
|
||||
end
|
||||
|
||||
local function cleanup()
|
||||
helpers.delete_buffer(diff_bufnr)
|
||||
helpers.delete_buffer(working_bufnr)
|
||||
end
|
||||
|
||||
it('resolve_ours keeps ours content in working file', function()
|
||||
setup_buffers()
|
||||
vim.api.nvim_win_set_cursor(0, { 5, 0 })
|
||||
|
||||
merge.resolve_ours(diff_bufnr, default_config())
|
||||
|
||||
local lines = vim.api.nvim_buf_get_lines(working_bufnr, 0, -1, false)
|
||||
assert.are.equal(1, #lines)
|
||||
assert.are.equal('local x = 1', lines[1])
|
||||
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
it('resolve_theirs keeps theirs content in working file', function()
|
||||
setup_buffers()
|
||||
vim.api.nvim_win_set_cursor(0, { 5, 0 })
|
||||
|
||||
merge.resolve_theirs(diff_bufnr, default_config())
|
||||
|
||||
local lines = vim.api.nvim_buf_get_lines(working_bufnr, 0, -1, false)
|
||||
assert.are.equal(1, #lines)
|
||||
assert.are.equal('local x = 2', lines[1])
|
||||
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
it('resolve_both keeps ours then theirs in working file', function()
|
||||
setup_buffers()
|
||||
vim.api.nvim_win_set_cursor(0, { 5, 0 })
|
||||
|
||||
merge.resolve_both(diff_bufnr, default_config())
|
||||
|
||||
local lines = vim.api.nvim_buf_get_lines(working_bufnr, 0, -1, false)
|
||||
assert.are.equal(2, #lines)
|
||||
assert.are.equal('local x = 1', lines[1])
|
||||
assert.are.equal('local x = 2', lines[2])
|
||||
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
it('resolve_none removes entire block from working file', function()
|
||||
setup_buffers()
|
||||
vim.api.nvim_win_set_cursor(0, { 5, 0 })
|
||||
|
||||
merge.resolve_none(diff_bufnr, default_config())
|
||||
|
||||
local lines = vim.api.nvim_buf_get_lines(working_bufnr, 0, -1, false)
|
||||
assert.are.equal(1, #lines)
|
||||
assert.are.equal('', lines[1])
|
||||
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
it('tracks resolved hunks', function()
|
||||
setup_buffers()
|
||||
vim.api.nvim_win_set_cursor(0, { 5, 0 })
|
||||
|
||||
assert.is_false(merge.is_resolved(diff_bufnr, 1))
|
||||
merge.resolve_ours(diff_bufnr, default_config())
|
||||
assert.is_true(merge.is_resolved(diff_bufnr, 1))
|
||||
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
it('adds virtual text for resolved hunks', function()
|
||||
setup_buffers()
|
||||
vim.api.nvim_win_set_cursor(0, { 5, 0 })
|
||||
|
||||
merge.resolve_ours(diff_bufnr, default_config())
|
||||
|
||||
local extmarks =
|
||||
vim.api.nvim_buf_get_extmarks(diff_bufnr, merge.get_namespace(), 0, -1, { details = true })
|
||||
local has_resolved_text = false
|
||||
for _, mark in ipairs(extmarks) do
|
||||
if mark[4] and mark[4].virt_text then
|
||||
for _, chunk in ipairs(mark[4].virt_text) do
|
||||
if chunk[1]:match('resolved') then
|
||||
has_resolved_text = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
assert.is_true(has_resolved_text)
|
||||
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
it('notifies when hunk is already resolved', function()
|
||||
setup_buffers()
|
||||
vim.api.nvim_win_set_cursor(0, { 5, 0 })
|
||||
|
||||
merge.resolve_ours(diff_bufnr, default_config())
|
||||
|
||||
local notified = false
|
||||
local orig_notify = vim.notify
|
||||
vim.notify = function(msg)
|
||||
if msg:match('already resolved') then
|
||||
notified = true
|
||||
end
|
||||
end
|
||||
|
||||
merge.resolve_ours(diff_bufnr, default_config())
|
||||
vim.notify = orig_notify
|
||||
|
||||
assert.is_true(notified)
|
||||
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
it('notifies when hunk does not match a conflict', function()
|
||||
local working_path = '/tmp/diffs_test_no_conflict.lua'
|
||||
local w_bufnr = create_working_buffer({
|
||||
'local y = 1',
|
||||
}, working_path)
|
||||
|
||||
local d_bufnr = create_diff_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,1 +1,1 @@',
|
||||
'-local x = 1',
|
||||
'+local x = 2',
|
||||
}, working_path)
|
||||
vim.api.nvim_set_current_buf(d_bufnr)
|
||||
vim.api.nvim_win_set_cursor(0, { 5, 0 })
|
||||
|
||||
local notified = false
|
||||
local orig_notify = vim.notify
|
||||
vim.notify = function(msg)
|
||||
if msg:match('does not correspond') then
|
||||
notified = true
|
||||
end
|
||||
end
|
||||
|
||||
merge.resolve_ours(d_bufnr, default_config())
|
||||
vim.notify = orig_notify
|
||||
|
||||
assert.is_true(notified)
|
||||
|
||||
helpers.delete_buffer(d_bufnr)
|
||||
helpers.delete_buffer(w_bufnr)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('navigation', function()
|
||||
it('goto_next jumps to next conflict hunk', function()
|
||||
local working_path = '/tmp/diffs_test_nav.lua'
|
||||
local w_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'local a = 1',
|
||||
'=======',
|
||||
'local a = 2',
|
||||
'>>>>>>> feature',
|
||||
'middle',
|
||||
'<<<<<<< HEAD',
|
||||
'local b = 3',
|
||||
'=======',
|
||||
'local b = 4',
|
||||
'>>>>>>> feature',
|
||||
}, working_path)
|
||||
|
||||
local d_bufnr = create_diff_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,1 +1,1 @@',
|
||||
'-local a = 1',
|
||||
'+local a = 2',
|
||||
'@@ -5,1 +5,1 @@',
|
||||
'-local b = 3',
|
||||
'+local b = 4',
|
||||
}, working_path)
|
||||
vim.api.nvim_set_current_buf(d_bufnr)
|
||||
vim.api.nvim_win_set_cursor(0, { 1, 0 })
|
||||
|
||||
merge.goto_next(d_bufnr)
|
||||
assert.are.equal(4, vim.api.nvim_win_get_cursor(0)[1])
|
||||
|
||||
merge.goto_next(d_bufnr)
|
||||
assert.are.equal(7, vim.api.nvim_win_get_cursor(0)[1])
|
||||
|
||||
helpers.delete_buffer(d_bufnr)
|
||||
helpers.delete_buffer(w_bufnr)
|
||||
end)
|
||||
|
||||
it('goto_next wraps around', function()
|
||||
local working_path = '/tmp/diffs_test_wrap.lua'
|
||||
local w_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'local x = 1',
|
||||
'=======',
|
||||
'local x = 2',
|
||||
'>>>>>>> feature',
|
||||
}, working_path)
|
||||
|
||||
local d_bufnr = create_diff_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,1 +1,1 @@',
|
||||
'-local x = 1',
|
||||
'+local x = 2',
|
||||
}, working_path)
|
||||
vim.api.nvim_set_current_buf(d_bufnr)
|
||||
vim.api.nvim_win_set_cursor(0, { 6, 0 })
|
||||
|
||||
merge.goto_next(d_bufnr)
|
||||
assert.are.equal(4, vim.api.nvim_win_get_cursor(0)[1])
|
||||
|
||||
helpers.delete_buffer(d_bufnr)
|
||||
helpers.delete_buffer(w_bufnr)
|
||||
end)
|
||||
|
||||
it('goto_prev jumps to previous conflict hunk', function()
|
||||
local working_path = '/tmp/diffs_test_prev.lua'
|
||||
local w_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'local a = 1',
|
||||
'=======',
|
||||
'local a = 2',
|
||||
'>>>>>>> feature',
|
||||
'middle',
|
||||
'<<<<<<< HEAD',
|
||||
'local b = 3',
|
||||
'=======',
|
||||
'local b = 4',
|
||||
'>>>>>>> feature',
|
||||
}, working_path)
|
||||
|
||||
local d_bufnr = create_diff_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,1 +1,1 @@',
|
||||
'-local a = 1',
|
||||
'+local a = 2',
|
||||
'@@ -5,1 +5,1 @@',
|
||||
'-local b = 3',
|
||||
'+local b = 4',
|
||||
}, working_path)
|
||||
vim.api.nvim_set_current_buf(d_bufnr)
|
||||
vim.api.nvim_win_set_cursor(0, { 9, 0 })
|
||||
|
||||
merge.goto_prev(d_bufnr)
|
||||
assert.are.equal(7, vim.api.nvim_win_get_cursor(0)[1])
|
||||
|
||||
merge.goto_prev(d_bufnr)
|
||||
assert.are.equal(4, vim.api.nvim_win_get_cursor(0)[1])
|
||||
|
||||
helpers.delete_buffer(d_bufnr)
|
||||
helpers.delete_buffer(w_bufnr)
|
||||
end)
|
||||
|
||||
it('goto_prev wraps around', function()
|
||||
local working_path = '/tmp/diffs_test_prev_wrap.lua'
|
||||
local w_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'local x = 1',
|
||||
'=======',
|
||||
'local x = 2',
|
||||
'>>>>>>> feature',
|
||||
}, working_path)
|
||||
|
||||
local d_bufnr = create_diff_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,1 +1,1 @@',
|
||||
'-local x = 1',
|
||||
'+local x = 2',
|
||||
}, working_path)
|
||||
vim.api.nvim_set_current_buf(d_bufnr)
|
||||
vim.api.nvim_win_set_cursor(0, { 1, 0 })
|
||||
|
||||
merge.goto_prev(d_bufnr)
|
||||
assert.are.equal(4, vim.api.nvim_win_get_cursor(0)[1])
|
||||
|
||||
helpers.delete_buffer(d_bufnr)
|
||||
helpers.delete_buffer(w_bufnr)
|
||||
end)
|
||||
|
||||
it('skips resolved hunks', function()
|
||||
local working_path = '/tmp/diffs_test_skip_resolved.lua'
|
||||
local w_bufnr = create_working_buffer({
|
||||
'<<<<<<< HEAD',
|
||||
'local a = 1',
|
||||
'=======',
|
||||
'local a = 2',
|
||||
'>>>>>>> feature',
|
||||
'middle',
|
||||
'<<<<<<< HEAD',
|
||||
'local b = 3',
|
||||
'=======',
|
||||
'local b = 4',
|
||||
'>>>>>>> feature',
|
||||
}, working_path)
|
||||
|
||||
local d_bufnr = create_diff_buffer({
|
||||
'diff --git a/file.lua b/file.lua',
|
||||
'--- a/file.lua',
|
||||
'+++ b/file.lua',
|
||||
'@@ -1,1 +1,1 @@',
|
||||
'-local a = 1',
|
||||
'+local a = 2',
|
||||
'@@ -5,1 +5,1 @@',
|
||||
'-local b = 3',
|
||||
'+local b = 4',
|
||||
}, working_path)
|
||||
vim.api.nvim_set_current_buf(d_bufnr)
|
||||
vim.api.nvim_win_set_cursor(0, { 5, 0 })
|
||||
|
||||
merge.resolve_ours(d_bufnr, default_config())
|
||||
|
||||
vim.api.nvim_win_set_cursor(0, { 1, 0 })
|
||||
merge.goto_next(d_bufnr)
|
||||
assert.are.equal(7, vim.api.nvim_win_get_cursor(0)[1])
|
||||
|
||||
helpers.delete_buffer(d_bufnr)
|
||||
helpers.delete_buffer(w_bufnr)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('fugitive integration', function()
|
||||
it('parse_file_line returns status for unmerged files', function()
|
||||
local fugitive = require('diffs.fugitive')
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {
|
||||
'Unstaged (1)',
|
||||
'U conflict.lua',
|
||||
})
|
||||
local filename, section, is_header, old_filename, status = fugitive.get_file_at_line(buf, 2)
|
||||
assert.are.equal('conflict.lua', filename)
|
||||
assert.are.equal('unstaged', section)
|
||||
assert.is_false(is_header)
|
||||
assert.is_nil(old_filename)
|
||||
assert.are.equal('U', status)
|
||||
vim.api.nvim_buf_delete(buf, { force = true })
|
||||
end)
|
||||
|
||||
it('parse_file_line returns status for modified files', function()
|
||||
local fugitive = require('diffs.fugitive')
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {
|
||||
'Unstaged (1)',
|
||||
'M file.lua',
|
||||
})
|
||||
local _, _, _, _, status = fugitive.get_file_at_line(buf, 2)
|
||||
assert.are.equal('M', status)
|
||||
vim.api.nvim_buf_delete(buf, { force = true })
|
||||
end)
|
||||
|
||||
it('walkback from hunk line propagates status', function()
|
||||
local fugitive = require('diffs.fugitive')
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {
|
||||
'Unstaged (1)',
|
||||
'U conflict.lua',
|
||||
'@@ -1,3 +1,4 @@',
|
||||
' local M = {}',
|
||||
'+local new = true',
|
||||
})
|
||||
local _, _, _, _, status = fugitive.get_file_at_line(buf, 5)
|
||||
assert.are.equal('U', status)
|
||||
vim.api.nvim_buf_delete(buf, { force = true })
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue