diff --git a/README.md b/README.md index 996ee27..f0a04a8 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,18 @@ syntax highlighting. ## Features -- Treesitter syntax highlighting in fugitive diffs and commit views -- Character-level intra-line diff highlighting -- `:Gdiff` unified diff against any revision -- Background-only diff colors for `&diff` buffers -- Inline merge conflict detection, highlighting, and resolution -- Vim syntax fallback, context padding, configurable blend/debounce +- Treesitter syntax highlighting in `:Git` diffs and commit views +- Diff header highlighting (`diff --git`, `index`, `---`, `+++`) +- `:Gdiffsplit` / `:Gvdiffsplit` syntax through diff backgrounds +- `:Gdiff` unified diff against any git revision with syntax highlighting +- Fugitive status buffer keymaps (`du`/`dU`) for unified diffs +- Background-only diff colors for any `&diff` buffer (`:diffthis`, `vimdiff`) +- Vim syntax fallback for languages without a treesitter parser +- Hunk header context highlighting (`@@ ... @@ function foo()`) +- Character-level (intra-line) diff highlighting for changed characters +- Inline merge conflict detection, highlighting, and resolution keymaps +- Configurable debouncing, max lines, diff prefix concealment, blend alpha, and + highlight overrides ## Requirements diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index f199d31..7663150 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -85,7 +85,6 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads: enabled = true, disable_diagnostics = true, show_virtual_text = true, - show_actions = false, keymaps = { ours = 'doo', theirs = 'dot', @@ -316,32 +315,6 @@ Example configuration: >lua vim.keymap.set('n', '[x', '(diffs-conflict-prev)') < - *(diffs-merge-ours)* -(diffs-merge-ours) - Accept ours in a merge diff view. Resolves the - conflict in the working file with ours content. - - *(diffs-merge-theirs)* -(diffs-merge-theirs) - Accept theirs in a merge diff view. - - *(diffs-merge-both)* -(diffs-merge-both) - Accept both (ours then theirs) in a merge diff view. - - *(diffs-merge-none)* -(diffs-merge-none) - Reject both in a merge diff view. - - *(diffs-merge-next)* -(diffs-merge-next) - Jump to next unresolved conflict hunk in merge diff. - - *(diffs-merge-prev)* -(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://` @@ -372,7 +345,6 @@ 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 @@ -417,7 +389,6 @@ Configuration: ~ enabled = true, disable_diagnostics = true, show_virtual_text = true, - show_actions = false, keymaps = { ours = 'doo', theirs = 'dot', @@ -444,32 +415,9 @@ Configuration: ~ diagnostics alone. {show_virtual_text} (boolean, default: true) - Show `(current)` and `(incoming)` labels at - the end of `<<<<<<<` and `>>>>>>>` marker - lines. Also controls hunk hints in merge - diff views. - - {format_virtual_text} (function|nil, default: nil) - Custom formatter for virtual text labels. - Receives `(side, keymap)` where `side` is - `"ours"` or `"theirs"` and `keymap` is the - configured keymap string or `false`. Return - a string (label text without parens) or - `nil` to hide the label. Example: >lua - format_virtual_text = function(side, keymap) - if keymap then - return side .. ' [' .. keymap .. ']' - end - return side - end -< - - {show_actions} (boolean, default: false) - Show a codelens-style action line above each - `<<<<<<<` marker listing available resolution - keymaps. Renders as virtual lines using the - `DiffsConflictActions` highlight group. - Only keymaps that are not `false` appear. + Show virtual text labels (" current" and + " incoming") at the end of `<<<<<<<` and + `>>>>>>>` marker lines. {keymaps} (table, default: see above) Buffer-local keymaps for conflict resolution @@ -510,31 +458,6 @@ 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* @@ -712,10 +635,6 @@ Conflict highlights: ~ *DiffsConflictBaseNr* DiffsConflictBaseNr Line number for base content lines (diff3). - *DiffsConflictActions* - DiffsConflictActions Dimmed foreground (no bold) for the codelens-style - action line shown when `show_actions` is true. - Diff mode window highlights: ~ These are used for |winhighlight| remapping in `&diff` windows. diff --git a/lua/diffs/commands.lua b/lua/diffs/commands.lua index 01f1d3a..dc6cfe2 100644 --- a/lua/diffs/commands.lua +++ b/lua/diffs/commands.lua @@ -36,24 +36,6 @@ 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 @@ -87,33 +69,6 @@ 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) @@ -183,7 +138,6 @@ 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 } @@ -203,17 +157,7 @@ function M.gdiff_file(filepath, opts) local old_lines, new_lines, err local diff_label - 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 + if opts.untracked then old_lines = {} new_lines, err = git.get_working_content(filepath) if not new_lines then @@ -292,14 +236,6 @@ 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() @@ -327,8 +263,6 @@ 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 @@ -391,8 +325,6 @@ 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 @@ -402,10 +334,7 @@ function M.read_buffer(bufnr) local old_lines, new_lines - 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 + if label == 'untracked' then old_lines = {} new_lines = git.get_working_content(abs_path) or {} elseif label == 'staged' then diff --git a/lua/diffs/conflict.lua b/lua/diffs/conflict.lua index ce3618d..9e62a15 100644 --- a/lua/diffs/conflict.lua +++ b/lua/diffs/conflict.lua @@ -92,17 +92,6 @@ local function parse_buffer(bufnr) return M.parse(lines) end ----@param side string ----@param config diffs.ConflictConfig ----@return string? -local function get_virtual_text_label(side, config) - if config.format_virtual_text then - local keymap = side == 'ours' and config.keymaps.ours or config.keymaps.theirs - return config.format_virtual_text(side, keymap) - end - return side == 'ours' and 'current' or 'incoming' -end - ---@param bufnr integer ---@param regions diffs.ConflictRegion[] ---@param config diffs.ConflictConfig @@ -118,37 +107,10 @@ local function apply_highlights(bufnr, regions, config) }) if config.show_virtual_text then - local ours_label = get_virtual_text_label('ours', config) - if ours_label then - pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_ours, 0, { - virt_text = { { ' (' .. ours_label .. ')', 'DiffsConflictMarker' } }, - virt_text_pos = 'eol', - }) - end - end - - if config.show_actions then - local parts = {} - local actions = { - { 'Current', config.keymaps.ours }, - { 'Incoming', config.keymaps.theirs }, - { 'Both', config.keymaps.both }, - { 'None', config.keymaps.none }, - } - for _, action in ipairs(actions) do - if action[2] then - if #parts > 0 then - table.insert(parts, { ' \226\148\130 ', 'DiffsConflictActions' }) - end - table.insert(parts, { ('%s (%s)'):format(action[1], action[2]), 'DiffsConflictActions' }) - end - end - if #parts > 0 then - pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_ours, 0, { - virt_lines = { parts }, - virt_lines_above = true, - }) - end + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_ours, 0, { + virt_text = { { ' (current)', 'DiffsConflictMarker' } }, + virt_text_pos = 'eol', + }) end for line = region.ours_start, region.ours_end - 1 do @@ -214,13 +176,10 @@ local function apply_highlights(bufnr, regions, config) }) if config.show_virtual_text then - local theirs_label = get_virtual_text_label('theirs', config) - if theirs_label then - pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_theirs, 0, { - virt_text = { { ' (' .. theirs_label .. ')', 'DiffsConflictMarker' } }, - virt_text_pos = 'eol', - }) - end + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_theirs, 0, { + virt_text = { { ' (incoming)', 'DiffsConflictMarker' } }, + virt_text_pos = 'eol', + }) end end end @@ -240,7 +199,7 @@ end ---@param bufnr integer ---@param region diffs.ConflictRegion ---@param replacement string[] -function M.replace_region(bufnr, region, replacement) +local function replace_region(bufnr, region, replacement) vim.api.nvim_buf_set_lines( bufnr, region.marker_ours, @@ -252,7 +211,7 @@ end ---@param bufnr integer ---@param config diffs.ConflictConfig -function M.refresh(bufnr, config) +local function refresh(bufnr, config) local regions = parse_buffer(bufnr) if #regions == 0 then vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1) @@ -285,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) - M.replace_region(bufnr, region, lines) - M.refresh(bufnr, config) + replace_region(bufnr, region, lines) + refresh(bufnr, config) end ---@param bufnr integer @@ -303,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) - M.replace_region(bufnr, region, lines) - M.refresh(bufnr, config) + replace_region(bufnr, region, lines) + refresh(bufnr, config) end ---@param bufnr integer @@ -329,8 +288,8 @@ function M.resolve_both(bufnr, config) for _, l in ipairs(theirs) do table.insert(combined, l) end - M.replace_region(bufnr, region, combined) - M.refresh(bufnr, config) + replace_region(bufnr, region, combined) + refresh(bufnr, config) end ---@param bufnr integer @@ -346,8 +305,8 @@ function M.resolve_none(bufnr, config) if not region then return end - M.replace_region(bufnr, region, {}) - M.refresh(bufnr, config) + replace_region(bufnr, region, {}) + refresh(bufnr, config) end ---@param bufnr integer @@ -458,7 +417,7 @@ function M.attach(bufnr, config) if not attached_buffers[bufnr] then return true end - M.refresh(bufnr, config) + refresh(bufnr, config) end, }) diff --git a/lua/diffs/fugitive.lua b/lua/diffs/fugitive.lua index b11985c..a588a22 100644 --- a/lua/diffs/fugitive.lua +++ b/lua/diffs/fugitive.lua @@ -27,19 +27,19 @@ function M.get_section_at_line(bufnr, lnum) end ---@param line string ----@return string?, string?, string? +---@return 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), 'R' + return vim.trim(new), vim.trim(old) end - local status, filename = line:match('^([MADRCU?])[MADRCU%s]*%s+(.+)$') - if status and filename then - return vim.trim(filename), nil, status + local filename = line:match('^[MADRCU?][MADRCU%s]*%s+(.+)$') + if filename then + return vim.trim(filename), nil end - return nil, nil, nil + return nil, nil end ---@param line string @@ -57,34 +57,34 @@ end ---@param bufnr integer ---@param lnum integer ----@return string?, diffs.FugitiveSection, boolean, string?, string? +---@return string?, diffs.FugitiveSection, boolean, 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, nil + return nil, nil, false, nil end local section_header = parse_section_header(current_line) if section_header then - return nil, section_header, true, nil, nil + return nil, section_header, true, nil end - local filename, old_filename, status = parse_file_line(current_line) + local filename, old_filename = parse_file_line(current_line) if filename then local section = M.get_section_at_line(bufnr, lnum) - return filename, section, false, old_filename, status + return filename, section, false, old_filename 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, status = parse_file_line(prev_line) + filename, old_filename = parse_file_line(prev_line) if filename then local section = M.get_section_at_line(bufnr, i) - return filename, section, false, old_filename, status + return filename, section, false, old_filename 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, nil + return nil, nil, false, 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, status = M.get_file_at_line(bufnr, lnum) + local filename, section, is_header, old_filename = M.get_file_at_line(bufnr, lnum) local repo_root = get_repo_root_from_fugitive(bufnr) if not repo_root then @@ -192,7 +192,6 @@ 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, }) diff --git a/lua/diffs/git.lua b/lua/diffs/git.lua index 1aa6328..7695283 100644 --- a/lua/diffs/git.lua +++ b/lua/diffs/git.lua @@ -1,19 +1,13 @@ local M = {} -local repo_root_cache = {} - ---@param filepath string ---@return string? function M.get_repo_root(filepath) local dir = vim.fn.fnamemodify(filepath, ':h') - if repo_root_cache[dir] ~= nil then - return repo_root_cache[dir] - end local result = vim.fn.systemlist({ 'git', '-C', dir, 'rev-parse', '--show-toplevel' }) if vim.v.shell_error ~= 0 then return nil end - repo_root_cache[dir] = result[1] return result[1] end diff --git a/lua/diffs/highlight.lua b/lua/diffs/highlight.lua index 6cdfe92..306b0e5 100644 --- a/lua/diffs/highlight.lua +++ b/lua/diffs/highlight.lua @@ -238,7 +238,7 @@ local function highlight_vim_syntax(bufnr, ns, hunk, code_lines, covered_lines, local spans = {} - pcall(vim.api.nvim_buf_call, scratch, function() + vim.api.nvim_buf_call(scratch, function() vim.cmd('setlocal syntax=' .. ft) vim.cmd('redraw') @@ -256,7 +256,7 @@ local function highlight_vim_syntax(bufnr, ns, hunk, code_lines, covered_lines, spans = M.coalesce_syntax_spans(query_fn, code_lines) end) - pcall(vim.api.nvim_buf_delete, scratch, { force = true }) + vim.api.nvim_buf_delete(scratch, { force = true }) local hunk_line_count = #hunk.lines local extmark_count = 0 diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index 85dc879..cdc29d1 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -41,8 +41,6 @@ ---@field enabled boolean ---@field disable_diagnostics boolean ---@field show_virtual_text boolean ----@field format_virtual_text? fun(side: string, keymap: string|false): string? ----@field show_actions boolean ---@field keymaps diffs.ConflictKeymaps ---@class diffs.Config @@ -130,7 +128,6 @@ local default_config = { enabled = true, disable_diagnostics = true, show_virtual_text = true, - show_actions = false, keymaps = { ours = 'doo', theirs = 'dot', @@ -203,9 +200,7 @@ local function create_debounced_highlight(bufnr) timer = nil t:close() end - if vim.api.nvim_buf_is_valid(bufnr) then - highlight_buffer(bufnr) - end + highlight_buffer(bufnr) end) ) end @@ -279,7 +274,6 @@ local function compute_highlight_groups() vim.api.nvim_set_hl(0, 'DiffsConflictTheirs', { default = true, bg = blended_theirs }) vim.api.nvim_set_hl(0, 'DiffsConflictBase', { default = true, bg = blended_base }) vim.api.nvim_set_hl(0, 'DiffsConflictMarker', { default = true, fg = 0x808080, bold = true }) - vim.api.nvim_set_hl(0, 'DiffsConflictActions', { default = true, fg = 0x808080 }) vim.api.nvim_set_hl( 0, 'DiffsConflictOursNr', @@ -394,8 +388,6 @@ local function init() ['conflict.enabled'] = { opts.conflict.enabled, 'boolean', true }, ['conflict.disable_diagnostics'] = { opts.conflict.disable_diagnostics, 'boolean', true }, ['conflict.show_virtual_text'] = { opts.conflict.show_virtual_text, 'boolean', true }, - ['conflict.format_virtual_text'] = { opts.conflict.format_virtual_text, 'function', true }, - ['conflict.show_actions'] = { opts.conflict.show_actions, 'boolean', true }, ['conflict.keymaps'] = { opts.conflict.keymaps, 'table', true }, }) diff --git a/lua/diffs/lib.lua b/lua/diffs/lib.lua index 8376925..5b3254b 100644 --- a/lua/diffs/lib.lua +++ b/lua/diffs/lib.lua @@ -8,9 +8,6 @@ local cached_handle = nil ---@type boolean local download_in_progress = false ----@type fun(handle: table?)[] -local pending_callbacks = {} - ---@return string local function get_os() local os_name = jit.os:lower() @@ -167,10 +164,9 @@ function M.ensure(callback) return end - table.insert(pending_callbacks, callback) - if download_in_progress then - dbg('download already in progress, queued callback') + dbg('download already in progress') + callback(nil) return end @@ -196,25 +192,21 @@ function M.ensure(callback) vim.system(cmd, {}, function(result) download_in_progress = false vim.schedule(function() - local handle = nil if result.code ~= 0 then vim.notify('[diffs] failed to download libvscode_diff', vim.log.levels.WARN) dbg('curl failed: %s', result.stderr or '') - else - local f = io.open(version_path(), 'w') - if f then - f:write(EXPECTED_VERSION) - f:close() - end - vim.notify('[diffs] libvscode_diff downloaded', vim.log.levels.INFO) - handle = M.load() + callback(nil) + return end - local cbs = pending_callbacks - pending_callbacks = {} - for _, cb in ipairs(cbs) do - cb(handle) + local f = io.open(version_path(), 'w') + if f then + f:write(EXPECTED_VERSION) + f:close() end + + vim.notify('[diffs] libvscode_diff downloaded', vim.log.levels.INFO) + callback(M.load()) end) end) end diff --git a/lua/diffs/merge.lua b/lua/diffs/merge.lua deleted file mode 100644 index 4332220..0000000 --- a/lua/diffs/merge.lua +++ /dev/null @@ -1,413 +0,0 @@ -local M = {} - -local conflict = require('diffs.conflict') - -local ns = vim.api.nvim_create_namespace('diffs-merge') - ----@type table> -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 -local function apply_hunk_hints(bufnr, config) - if not config.show_virtual_text then - return - end - - local hunks = M.parse_hunks(bufnr) - for _, hunk in ipairs(hunks) do - if M.is_resolved(bufnr, hunk.index) then - add_resolved_virtual_text(bufnr, hunk) - else - local parts = {} - local actions = { - { 'current', config.keymaps.ours }, - { 'incoming', config.keymaps.theirs }, - { 'both', config.keymaps.both }, - { 'none', config.keymaps.none }, - } - for _, action in ipairs(actions) do - if action[2] then - if #parts > 0 then - table.insert(parts, { ' | ', 'Comment' }) - end - table.insert(parts, { ('%s: %s'):format(action[2], action[1]), 'Comment' }) - end - end - if #parts > 0 then - pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, hunk.start_line, 0, { - virt_text = parts, - virt_text_pos = 'eol', - }) - end - end - end -end - ----@param bufnr integer ----@param config diffs.ConflictConfig -function M.setup_keymaps(bufnr, config) - local km = config.keymaps - - local maps = { - { km.ours, '(diffs-merge-ours)' }, - { km.theirs, '(diffs-merge-theirs)' }, - { km.both, '(diffs-merge-both)' }, - { km.none, '(diffs-merge-none)' }, - { km.next, '(diffs-merge-next)' }, - { km.prev, '(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 - - apply_hunk_hints(bufnr, config) - - 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 diff --git a/plugin/diffs.lua b/plugin/diffs.lua index face556..5d3c8b2 100644 --- a/plugin/diffs.lua +++ b/plugin/diffs.lua @@ -82,28 +82,3 @@ end, { desc = 'Jump to next conflict' }) vim.keymap.set('n', '(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', '(diffs-merge-ours)', function() - merge_action(require('diffs.merge').resolve_ours) -end, { desc = 'Accept ours in merge diff' }) -vim.keymap.set('n', '(diffs-merge-theirs)', function() - merge_action(require('diffs.merge').resolve_theirs) -end, { desc = 'Accept theirs in merge diff' }) -vim.keymap.set('n', '(diffs-merge-both)', function() - merge_action(require('diffs.merge').resolve_both) -end, { desc = 'Accept both in merge diff' }) -vim.keymap.set('n', '(diffs-merge-none)', function() - merge_action(require('diffs.merge').resolve_none) -end, { desc = 'Reject both in merge diff' }) -vim.keymap.set('n', '(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', '(diffs-merge-prev)', function() - require('diffs.merge').goto_prev(vim.api.nvim_get_current_buf()) -end, { desc = 'Jump to previous conflict hunk' }) diff --git a/spec/commands_spec.lua b/spec/commands_spec.lua index 1bf9e61..daba5d5 100644 --- a/spec/commands_spec.lua +++ b/spec/commands_spec.lua @@ -40,78 +40,6 @@ 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 = { diff --git a/spec/conflict_spec.lua b/spec/conflict_spec.lua index b163960..75eac23 100644 --- a/spec/conflict_spec.lua +++ b/spec/conflict_spec.lua @@ -6,7 +6,6 @@ local function default_config(overrides) enabled = true, disable_diagnostics = false, show_virtual_text = true, - show_actions = false, keymaps = { ours = 'doo', theirs = 'dot', @@ -686,158 +685,4 @@ describe('conflict', function() helpers.delete_buffer(bufnr) end) end) - - describe('virtual text formatting', function() - after_each(function() - conflict.detach(vim.api.nvim_get_current_buf()) - end) - - it('default labels show current and incoming without keymaps', function() - local bufnr = create_file_buffer({ - '<<<<<<< HEAD', - 'local x = 1', - '=======', - 'local x = 2', - '>>>>>>> feature', - }) - - conflict.attach(bufnr, default_config()) - - local extmarks = get_extmarks(bufnr) - local labels = {} - for _, mark in ipairs(extmarks) do - if mark[4] and mark[4].virt_text then - table.insert(labels, mark[4].virt_text[1][1]) - end - end - assert.are.equal(2, #labels) - assert.are.equal(' (current)', labels[1]) - assert.are.equal(' (incoming)', labels[2]) - - helpers.delete_buffer(bufnr) - end) - - it('uses custom format_virtual_text function', function() - local bufnr = create_file_buffer({ - '<<<<<<< HEAD', - 'local x = 1', - '=======', - 'local x = 2', - '>>>>>>> feature', - }) - - conflict.attach( - bufnr, - default_config({ - format_virtual_text = function(side) - return side == 'ours' and 'OURS' or 'THEIRS' - end, - }) - ) - - local extmarks = get_extmarks(bufnr) - local labels = {} - for _, mark in ipairs(extmarks) do - if mark[4] and mark[4].virt_text then - table.insert(labels, mark[4].virt_text[1][1]) - end - end - assert.are.equal(2, #labels) - assert.are.equal(' (OURS)', labels[1]) - assert.are.equal(' (THEIRS)', labels[2]) - - helpers.delete_buffer(bufnr) - end) - - it('hides label when format_virtual_text returns nil', function() - local bufnr = create_file_buffer({ - '<<<<<<< HEAD', - 'local x = 1', - '=======', - 'local x = 2', - '>>>>>>> feature', - }) - - conflict.attach( - bufnr, - default_config({ - format_virtual_text = function() - return nil - end, - }) - ) - - local extmarks = get_extmarks(bufnr) - local virt_text_count = 0 - for _, mark in ipairs(extmarks) do - if mark[4] and mark[4].virt_text then - virt_text_count = virt_text_count + 1 - end - end - assert.are.equal(0, virt_text_count) - - helpers.delete_buffer(bufnr) - end) - end) - - describe('action lines', function() - after_each(function() - conflict.detach(vim.api.nvim_get_current_buf()) - end) - - it('adds virt_lines when show_actions is true', function() - local bufnr = create_file_buffer({ - '<<<<<<< HEAD', - 'local x = 1', - '=======', - 'local x = 2', - '>>>>>>> feature', - }) - - conflict.attach(bufnr, default_config({ show_actions = true })) - - local extmarks = get_extmarks(bufnr) - local virt_lines_count = 0 - for _, mark in ipairs(extmarks) do - if mark[4] and mark[4].virt_lines then - virt_lines_count = virt_lines_count + 1 - end - end - assert.are.equal(1, virt_lines_count) - - helpers.delete_buffer(bufnr) - end) - - it('omits disabled keymaps from action line', function() - local bufnr = create_file_buffer({ - '<<<<<<< HEAD', - 'local x = 1', - '=======', - 'local x = 2', - '>>>>>>> feature', - }) - - conflict.attach( - bufnr, - default_config({ show_actions = true, keymaps = { both = false, none = false } }) - ) - - local extmarks = get_extmarks(bufnr) - for _, mark in ipairs(extmarks) do - if mark[4] and mark[4].virt_lines then - local line = mark[4].virt_lines[1] - local text = '' - for _, chunk in ipairs(line) do - text = text .. chunk[1] - end - assert.is_truthy(text:find('Current')) - assert.is_truthy(text:find('Incoming')) - assert.is_falsy(text:find('Both')) - assert.is_falsy(text:find('None')) - end - end - - helpers.delete_buffer(bufnr) - end) - end) end) diff --git a/spec/merge_spec.lua b/spec/merge_spec.lua deleted file mode 100644 index 7d5019b..0000000 --- a/spec/merge_spec.lua +++ /dev/null @@ -1,724 +0,0 @@ -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, - show_actions = false, - 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('hunk hints', function() - it('adds keymap hints on hunk header lines', function() - 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', - }) - - merge.setup_keymaps(d_bufnr, default_config()) - - local extmarks = - vim.api.nvim_buf_get_extmarks(d_bufnr, merge.get_namespace(), 0, -1, { details = true }) - local hint_marks = {} - for _, mark in ipairs(extmarks) do - if mark[4] and mark[4].virt_text then - local text = '' - for _, chunk in ipairs(mark[4].virt_text) do - text = text .. chunk[1] - end - table.insert(hint_marks, { line = mark[2], text = text }) - end - end - assert.are.equal(1, #hint_marks) - assert.are.equal(3, hint_marks[1].line) - assert.is_truthy(hint_marks[1].text:find('doo')) - assert.is_truthy(hint_marks[1].text:find('dot')) - - helpers.delete_buffer(d_bufnr) - end) - - it('does not add hints when show_virtual_text is false', function() - 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', - }) - - merge.setup_keymaps(d_bufnr, default_config({ show_virtual_text = false })) - - local extmarks = - vim.api.nvim_buf_get_extmarks(d_bufnr, merge.get_namespace(), 0, -1, { details = true }) - local virt_text_count = 0 - for _, mark in ipairs(extmarks) do - if mark[4] and mark[4].virt_text then - virt_text_count = virt_text_count + 1 - end - end - assert.are.equal(0, virt_text_count) - - helpers.delete_buffer(d_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)