From 011db2f8b312a4f4f6570fb212892aae3114fa38 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 14:40:55 -0500 Subject: [PATCH 01/21] fix(highlight): make hl_eol work on background extmarks Problem: hl_eol requires a multiline extmark to extend the background past end-of-line. The extmark used end_col = line_len on the same row, so it was single-line and hl_eol was effectively a no-op. Solution: replace end_col with end_row targeting the next line so the extmark is multiline and hl_eol takes effect. Split number_hl_group into a separate extmark to prevent it bleeding to adjacent lines. --- lua/diffs/highlight.lua | 9 ++++-- spec/highlight_spec.lua | 66 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/lua/diffs/highlight.lua b/lua/diffs/highlight.lua index 9582910..bf9063d 100644 --- a/lua/diffs/highlight.lua +++ b/lua/diffs/highlight.lua @@ -458,12 +458,17 @@ function M.highlight_hunk(bufnr, ns, hunk, opts) if opts.highlights.background and is_diff_line then pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, { - end_col = line_len, + end_row = buf_line + 1, hl_group = line_hl, hl_eol = true, - number_hl_group = opts.highlights.gutter and number_hl or nil, priority = PRIORITY_LINE_BG, }) + if opts.highlights.gutter then + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, { + number_hl_group = number_hl, + priority = PRIORITY_LINE_BG, + }) + end end if char_spans_by_line[i] then diff --git a/spec/highlight_spec.lua b/spec/highlight_spec.lua index 5fd1fa5..2f95d02 100644 --- a/spec/highlight_spec.lua +++ b/spec/highlight_spec.lua @@ -800,6 +800,72 @@ describe('highlight', function() delete_buffer(bufnr) end) + it('hl_eol background extmarks are multiline so hl_eol takes effect', function() + local bufnr = create_buffer({ + '@@ -1,2 +1,1 @@', + '-local x = 1', + '+local y = 2', + }) + + local hunk = { + filename = 'test.lua', + lang = 'lua', + start_line = 1, + lines = { '-local x = 1', '+local y = 2' }, + } + + highlight.highlight_hunk( + bufnr, + ns, + hunk, + default_opts({ highlights = { background = true } }) + ) + + local extmarks = get_extmarks(bufnr) + for _, mark in ipairs(extmarks) do + local d = mark[4] + if d and (d.hl_group == 'DiffsAdd' or d.hl_group == 'DiffsDelete') then + assert.is_true(d.end_row > mark[2]) + end + end + delete_buffer(bufnr) + end) + + it('number_hl_group does not bleed to adjacent lines', function() + local bufnr = create_buffer({ + '@@ -1,3 +1,3 @@', + ' local a = 0', + '-local x = 1', + '+local y = 2', + ' local b = 3', + }) + + local hunk = { + filename = 'test.lua', + lang = 'lua', + start_line = 1, + lines = { ' local a = 0', '-local x = 1', '+local y = 2', ' local b = 3' }, + } + + highlight.highlight_hunk( + bufnr, + ns, + hunk, + default_opts({ highlights = { background = true, gutter = true } }) + ) + + local extmarks = get_extmarks(bufnr) + for _, mark in ipairs(extmarks) do + local d = mark[4] + if d and d.number_hl_group then + local start_row = mark[2] + local end_row = d.end_row or start_row + assert.are.equal(start_row, end_row) + end + end + delete_buffer(bufnr) + end) + it('line bg priority > DiffsClear priority', function() local bufnr = create_buffer({ '@@ -1,2 +1,1 @@', From 38220ab368f11da12ae0dd4f5ca25fb3da4e03e5 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 15:10:07 -0500 Subject: [PATCH 02/21] fix(highlight): use hunk body as context for header treesitter parsing Problem: the header context string (e.g. "function M.setup()") was parsed in isolation by treesitter, which couldn't recognize "function" as @keyword.function because the snippet is an incomplete definition with no body or "end". Solution: append the already-built new_code lines as trailing context when parsing the header string, giving treesitter a complete function definition. Filter captures to row 0 only so body-line captures don't produce extmarks on the header line. --- lua/diffs/highlight.lua | 52 +++++++++++++++++++++++++++-------------- spec/highlight_spec.lua | 34 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 18 deletions(-) diff --git a/lua/diffs/highlight.lua b/lua/diffs/highlight.lua index bf9063d..fed979b 100644 --- a/lua/diffs/highlight.lua +++ b/lua/diffs/highlight.lua @@ -41,9 +41,15 @@ local PRIORITY_CHAR_BG = 201 ---@param col_offset integer ---@param text string ---@param lang string +---@param context_lines? string[] ---@return integer -local function highlight_text(bufnr, ns, hunk, col_offset, text, lang) - local ok, parser_obj = pcall(vim.treesitter.get_string_parser, text, lang) +local function highlight_text(bufnr, ns, hunk, col_offset, text, lang, context_lines) + local parse_text = text + if context_lines and #context_lines > 0 then + parse_text = text .. '\n' .. table.concat(context_lines, '\n') + end + + local ok, parser_obj = pcall(vim.treesitter.get_string_parser, parse_text, lang) if not ok or not parser_obj then return 0 end @@ -61,24 +67,27 @@ local function highlight_text(bufnr, ns, hunk, col_offset, text, lang) local extmark_count = 0 local header_line = hunk.start_line - 1 - for id, node, metadata in query:iter_captures(trees[1]:root(), text) do - local capture_name = '@' .. query.captures[id] .. '.' .. lang + for id, node, metadata in query:iter_captures(trees[1]:root(), parse_text) do local sr, sc, er, ec = node:range() + if sr == 0 then + er = 0 + local capture_name = '@' .. query.captures[id] .. '.' .. lang - local buf_sr = header_line + sr - local buf_er = header_line + er - local buf_sc = col_offset + sc - local buf_ec = col_offset + ec + local buf_sr = header_line + local buf_er = header_line + local buf_sc = col_offset + sc + local buf_ec = col_offset + ec - local priority = lang == 'diff' and (tonumber(metadata.priority) or 100) or PRIORITY_SYNTAX + local priority = lang == 'diff' and (tonumber(metadata.priority) or 100) or PRIORITY_SYNTAX - pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_sr, buf_sc, { - end_row = buf_er, - end_col = buf_ec, - hl_group = capture_name, - priority = priority, - }) - extmark_count = extmark_count + 1 + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_sr, buf_sc, { + end_row = buf_er, + end_col = buf_ec, + hl_group = capture_name, + priority = priority, + }) + extmark_count = extmark_count + 1 + end end return extmark_count @@ -360,8 +369,15 @@ function M.highlight_hunk(bufnr, ns, hunk, opts) hl_group = 'DiffsClear', priority = PRIORITY_CLEAR, }) - local header_extmarks = - highlight_text(bufnr, ns, hunk, hunk.header_context_col, hunk.header_context, hunk.lang) + local header_extmarks = highlight_text( + bufnr, + ns, + hunk, + hunk.header_context_col, + hunk.header_context, + hunk.lang, + new_code + ) if header_extmarks > 0 then dbg('header %s:%d applied %d extmarks', hunk.filename, hunk.start_line, header_extmarks) end diff --git a/spec/highlight_spec.lua b/spec/highlight_spec.lua index 2f95d02..0b9051e 100644 --- a/spec/highlight_spec.lua +++ b/spec/highlight_spec.lua @@ -220,6 +220,40 @@ describe('highlight', function() delete_buffer(bufnr) end) + it('highlights function keyword in header context', function() + local bufnr = create_buffer({ + '@@ -5,3 +5,4 @@ function M.setup()', + ' local x = 1', + '+local y = 2', + ' return x', + }) + + local hunk = { + filename = 'test.lua', + lang = 'lua', + start_line = 1, + header_context = 'function M.setup()', + header_context_col = 18, + lines = { ' local x = 1', '+local y = 2', ' return x' }, + } + + highlight.highlight_hunk(bufnr, ns, hunk, default_opts()) + + local extmarks = get_extmarks(bufnr) + local has_keyword_function = false + for _, mark in ipairs(extmarks) do + if mark[2] == 0 and mark[4] and mark[4].hl_group then + local hl = mark[4].hl_group + if hl == '@keyword.function.lua' or hl == '@keyword.lua' then + has_keyword_function = true + break + end + end + end + assert.is_true(has_keyword_function) + delete_buffer(bufnr) + end) + it('does not highlight header when no header_context', function() local bufnr = create_buffer({ '@@ -10,3 +10,4 @@', From 825012daeb6c3a95c114f161090e262a2a0794dd Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 15:12:44 -0500 Subject: [PATCH 03/21] fix(ci): remove unused variable --- lua/diffs/highlight.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lua/diffs/highlight.lua b/lua/diffs/highlight.lua index fed979b..306b0e5 100644 --- a/lua/diffs/highlight.lua +++ b/lua/diffs/highlight.lua @@ -68,9 +68,8 @@ local function highlight_text(bufnr, ns, hunk, col_offset, text, lang, context_l local header_line = hunk.start_line - 1 for id, node, metadata in query:iter_captures(trees[1]:root(), parse_text) do - local sr, sc, er, ec = node:range() + local sr, sc, _, ec = node:range() if sr == 0 then - er = 0 local capture_name = '@' .. query.captures[id] .. '.' .. lang local buf_sr = header_line From d10eaed6acdc1730ef412c96fc937d1d1cf0330e Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 15:23:14 -0500 Subject: [PATCH 04/21] fix(highlight): reduce word-level blend alpha and match line number bg Problem: word-level diff highlights were too intense at 70% alpha, and line number backgrounds used the line-level blend instead of matching the word-level highlights. Solution: reduce DiffsAddText/DiffsDeleteText blend alpha from 0.7 to 0.6 and use the same blended background for DiffsAddNr/DiffsDeleteNr. --- doc/diffs.nvim.txt | 12 ++++++++---- lua/diffs/init.lua | 8 ++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index 5807736..6443063 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -402,6 +402,10 @@ diffs.nvim defines custom highlight groups. Fugitive unified diff groups use `default = true`, so colorschemes can override them. Diff mode groups are always derived from the corresponding `Diff*` groups. +All derived groups are computed by alpha-blending a source color into the +`Normal` background. Line-level groups blend at 40% alpha for a subtle tint; +character-level and line-number groups blend at 60% for more contrast. + Fugitive unified diff highlights: ~ *DiffsAdd* DiffsAdd Background for `+` lines. Derived by blending @@ -413,23 +417,23 @@ Fugitive unified diff highlights: ~ *DiffsAddNr* DiffsAddNr Line number for `+` lines. Foreground from - `diffAdded`, background from `DiffsAdd`. + `diffAdded`, background from `DiffsAddText`. *DiffsDeleteNr* DiffsDeleteNr Line number for `-` lines. Foreground from - `diffRemoved`, background from `DiffsDelete`. + `diffRemoved`, background from `DiffsDeleteText`. *DiffsAddText* DiffsAddText Character-level background for changed characters within `+` lines. Derived by blending `diffAdded` - foreground with `Normal` background at 70% alpha. + foreground with `Normal` background at 60% alpha. Only sets `bg`, so treesitter foreground colors show through. *DiffsDeleteText* DiffsDeleteText Character-level background for changed characters within `-` lines. Derived by blending `diffRemoved` - foreground with `Normal` background at 70% alpha. + foreground with `Normal` background at 60% alpha. Diff mode window highlights: ~ These are used for |winhighlight| remapping in `&diff` windows. diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index 4338175..76c17d6 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -192,14 +192,14 @@ local function compute_highlight_groups() local blended_add = blend_color(add_bg, bg, 0.4) local blended_del = blend_color(del_bg, bg, 0.4) - local blended_add_text = blend_color(add_fg, bg, 0.7) - local blended_del_text = blend_color(del_fg, bg, 0.7) + local blended_add_text = blend_color(add_fg, bg, 0.6) + local blended_del_text = blend_color(del_fg, bg, 0.6) vim.api.nvim_set_hl(0, 'DiffsClear', { default = true, fg = normal.fg or 0xc0c0c0 }) vim.api.nvim_set_hl(0, 'DiffsAdd', { default = true, bg = blended_add }) vim.api.nvim_set_hl(0, 'DiffsDelete', { default = true, bg = blended_del }) - vim.api.nvim_set_hl(0, 'DiffsAddNr', { default = true, fg = add_fg, bg = blended_add }) - vim.api.nvim_set_hl(0, 'DiffsDeleteNr', { default = true, fg = del_fg, bg = blended_del }) + vim.api.nvim_set_hl(0, 'DiffsAddNr', { default = true, fg = add_fg, bg = blended_add_text }) + vim.api.nvim_set_hl(0, 'DiffsDeleteNr', { default = true, fg = del_fg, bg = blended_del_text }) vim.api.nvim_set_hl(0, 'DiffsAddText', { default = true, bg = blended_add_text }) vim.api.nvim_set_hl(0, 'DiffsDeleteText', { default = true, bg = blended_del_text }) From 5cfa91039b6b6430c55b03609005185b625e9490 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 15:29:19 -0500 Subject: [PATCH 05/21] fix(highlight): use correct line number gutter colors Problem: DiffsAddNr/DiffsDeleteNr used raw diffAdded/diffRemoved foreground and the word-level blended background, instead of matching the line-level and character-level highlight groups. Solution: set gutter bg to the line-level blend (DiffsAdd/DiffsDelete) and fg to the character-level blend (DiffsAddText/DiffsDeleteText). --- doc/diffs.nvim.txt | 8 +++++--- lua/diffs/init.lua | 8 ++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index 6443063..42009e9 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -404,7 +404,9 @@ always derived from the corresponding `Diff*` groups. All derived groups are computed by alpha-blending a source color into the `Normal` background. Line-level groups blend at 40% alpha for a subtle tint; -character-level and line-number groups blend at 60% for more contrast. +character-level groups blend at 60% for more contrast. Line-number groups +combine both: background from the line-level blend, foreground from the +character-level blend. Fugitive unified diff highlights: ~ *DiffsAdd* @@ -417,11 +419,11 @@ Fugitive unified diff highlights: ~ *DiffsAddNr* DiffsAddNr Line number for `+` lines. Foreground from - `diffAdded`, background from `DiffsAddText`. + `DiffsAddText`, background from `DiffsAdd`. *DiffsDeleteNr* DiffsDeleteNr Line number for `-` lines. Foreground from - `diffRemoved`, background from `DiffsDeleteText`. + `DiffsDeleteText`, background from `DiffsDelete`. *DiffsAddText* DiffsAddText Character-level background for changed characters diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index 76c17d6..cfdfb4b 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -198,8 +198,12 @@ local function compute_highlight_groups() vim.api.nvim_set_hl(0, 'DiffsClear', { default = true, fg = normal.fg or 0xc0c0c0 }) vim.api.nvim_set_hl(0, 'DiffsAdd', { default = true, bg = blended_add }) vim.api.nvim_set_hl(0, 'DiffsDelete', { default = true, bg = blended_del }) - vim.api.nvim_set_hl(0, 'DiffsAddNr', { default = true, fg = add_fg, bg = blended_add_text }) - vim.api.nvim_set_hl(0, 'DiffsDeleteNr', { default = true, fg = del_fg, bg = blended_del_text }) + vim.api.nvim_set_hl(0, 'DiffsAddNr', { default = true, fg = blended_add_text, bg = blended_add }) + vim.api.nvim_set_hl( + 0, + 'DiffsDeleteNr', + { default = true, fg = blended_del_text, bg = blended_del } + ) vim.api.nvim_set_hl(0, 'DiffsAddText', { default = true, bg = blended_add_text }) vim.api.nvim_set_hl(0, 'DiffsDeleteText', { default = true, bg = blended_del_text }) From bcc70280fbe68ef97e9cb42d6a130d75b936427d Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 15:44:56 -0500 Subject: [PATCH 06/21] docs: fix vim.max_lines default in config example Problem: the vimdoc config example showed max_lines = 500 under vim, but the actual default in init.lua is 200. Solution: change the example to match the real default. --- doc/diffs.nvim.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index 42009e9..3db6db6 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -66,7 +66,7 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads: }, vim = { enabled = false, - max_lines = 500, + max_lines = 200, }, intra = { enabled = true, From 8e0c41bf6b5903e61b40900506f95e23359328e9 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 15:45:34 -0500 Subject: [PATCH 07/21] fix(highlight): add default flag to DiffsDiff* groups Problem: DiffsDiff* highlight groups lacked default = true, making them impossible for colorschemes to override, inconsistent with the fugitive unified diff groups which already had it. Solution: add default = true to all four DiffsDiffAdd, DiffsDiffDelete, DiffsDiffChange, and DiffsDiffText nvim_set_hl calls. --- doc/diffs.nvim.txt | 6 +++--- lua/diffs/init.lua | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index 3db6db6..7704b5b 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -398,9 +398,9 @@ diff-related features. ============================================================================== HIGHLIGHT GROUPS *diffs-highlights* -diffs.nvim defines custom highlight groups. Fugitive unified diff groups use -`default = true`, so colorschemes can override them. Diff mode groups are -always derived from the corresponding `Diff*` groups. +diffs.nvim defines custom highlight groups. All groups use `default = true`, +so colorschemes can override them by defining the group before the plugin +loads. All derived groups are computed by alpha-blending a source color into the `Normal` background. Line-level groups blend at 40% alpha for a subtle tint; diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index cfdfb4b..14a4c13 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -219,10 +219,14 @@ local function compute_highlight_groups() local diff_change = resolve_hl('DiffChange') local diff_text = resolve_hl('DiffText') - vim.api.nvim_set_hl(0, 'DiffsDiffAdd', { bg = diff_add.bg }) - vim.api.nvim_set_hl(0, 'DiffsDiffDelete', { fg = diff_delete.fg, bg = diff_delete.bg }) - vim.api.nvim_set_hl(0, 'DiffsDiffChange', { bg = diff_change.bg }) - vim.api.nvim_set_hl(0, 'DiffsDiffText', { bg = diff_text.bg }) + vim.api.nvim_set_hl(0, 'DiffsDiffAdd', { default = true, bg = diff_add.bg }) + vim.api.nvim_set_hl( + 0, + 'DiffsDiffDelete', + { default = true, fg = diff_delete.fg, bg = diff_delete.bg } + ) + vim.api.nvim_set_hl(0, 'DiffsDiffChange', { default = true, bg = diff_change.bg }) + vim.api.nvim_set_hl(0, 'DiffsDiffText', { default = true, bg = diff_text.bg }) end local function init() From b7477e3af24ec499cfedf5cfcf4b3db2dda24653 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 15:46:47 -0500 Subject: [PATCH 08/21] feat(highlight): add configurable blend alpha Problem: the character-level blend intensity was hardcoded to 0.6, giving users no way to tune how strongly changed characters stand out from the line-level background. Solution: add highlights.blend_alpha config option (number, 0-1, default 0.6) with type validation and range check. --- doc/diffs.nvim.txt | 8 ++++++++ lua/diffs/init.lua | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index 7704b5b..cf03a6f 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -56,6 +56,7 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads: highlights = { background = true, gutter = true, + blend_alpha = 0.6, context = { enabled = true, lines = 25, @@ -117,6 +118,13 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads: Highlight line numbers with matching colors. Only visible if line numbers are enabled. + {blend_alpha} (number, default: 0.6) + Alpha value for character-level blend intensity. + Controls how strongly changed characters stand + out from the line-level background. Must be + between 0 and 1 (inclusive). Higher values + produce more vivid character-level highlights. + {context} (table, default: see below) Syntax parsing context options. See |diffs.ContextConfig| for fields. diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index 14a4c13..3397bc1 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -18,6 +18,7 @@ ---@class diffs.Highlights ---@field background boolean ---@field gutter boolean +---@field blend_alpha? number ---@field context diffs.ContextConfig ---@field treesitter diffs.TreesitterConfig ---@field vim diffs.VimConfig @@ -192,8 +193,9 @@ local function compute_highlight_groups() local blended_add = blend_color(add_bg, bg, 0.4) local blended_del = blend_color(del_bg, bg, 0.4) - local blended_add_text = blend_color(add_fg, bg, 0.6) - local blended_del_text = blend_color(del_fg, bg, 0.6) + local alpha = config.highlights.blend_alpha or 0.6 + local blended_add_text = blend_color(add_fg, bg, alpha) + local blended_del_text = blend_color(del_fg, bg, alpha) vim.api.nvim_set_hl(0, 'DiffsClear', { default = true, fg = normal.fg or 0xc0c0c0 }) vim.api.nvim_set_hl(0, 'DiffsAdd', { default = true, bg = blended_add }) @@ -248,6 +250,7 @@ local function init() vim.validate({ ['highlights.background'] = { opts.highlights.background, 'boolean', true }, ['highlights.gutter'] = { opts.highlights.gutter, 'boolean', true }, + ['highlights.blend_alpha'] = { opts.highlights.blend_alpha, 'number', true }, ['highlights.context'] = { opts.highlights.context, 'table', true }, ['highlights.treesitter'] = { opts.highlights.treesitter, 'table', true }, ['highlights.vim'] = { opts.highlights.vim, 'table', true }, @@ -348,6 +351,13 @@ local function init() then error('diffs: highlights.intra.max_lines must be >= 1') end + if + opts.highlights + and opts.highlights.blend_alpha + and (opts.highlights.blend_alpha < 0 or opts.highlights.blend_alpha > 1) + then + error('diffs: highlights.blend_alpha must be >= 0 and <= 1') + end config = vim.tbl_deep_extend('force', default_config, opts) log.set_enabled(config.debug) From a0870a789239d571856883dfea11d6854da5aa98 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 15:47:36 -0500 Subject: [PATCH 09/21] feat(highlight): add highlights.overrides config table Problem: users had no config-level way to override computed highlight groups and had to call nvim_set_hl externally. Solution: add highlights.overrides table that maps group names to highlight definitions. Overrides are applied after all computed groups without default = true, so they always win over both computed defaults and colorscheme definitions. --- doc/diffs.nvim.txt | 18 ++++++++++++++++++ lua/diffs/init.lua | 8 ++++++++ 2 files changed, 26 insertions(+) diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index cf03a6f..391ad05 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -74,6 +74,7 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads: algorithm = 'default', max_lines = 500, }, + overrides = {}, }, fugitive = { horizontal = 'du', @@ -141,6 +142,13 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads: Character-level (intra-line) diff highlighting. See |diffs.IntraConfig| for fields. + {overrides} (table, default: {}) + Map of highlight group names to highlight + definitions (see |nvim_set_hl()|). Applied + after all computed groups without `default`, + so overrides always win over both computed + defaults and colorscheme definitions. + *diffs.ContextConfig* Context config fields: ~ {enabled} (boolean, default: true) @@ -469,6 +477,16 @@ To customize these in your colorscheme: >lua vim.api.nvim_set_hl(0, 'DiffsAdd', { bg = '#2e4a3a' }) vim.api.nvim_set_hl(0, 'DiffsDiffDelete', { link = 'DiffDelete' }) < +Or via `highlights.overrides` in config: >lua + vim.g.diffs = { + highlights = { + overrides = { + DiffsAdd = { bg = '#2e4a3a' }, + DiffsDiffDelete = { link = 'DiffDelete' }, + }, + }, + } +< ============================================================================== HEALTH CHECK *diffs-health* diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index 3397bc1..7fb4e6d 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -19,6 +19,7 @@ ---@field background boolean ---@field gutter boolean ---@field blend_alpha? number +---@field overrides? table ---@field context diffs.ContextConfig ---@field treesitter diffs.TreesitterConfig ---@field vim diffs.VimConfig @@ -229,6 +230,12 @@ local function compute_highlight_groups() ) vim.api.nvim_set_hl(0, 'DiffsDiffChange', { default = true, bg = diff_change.bg }) vim.api.nvim_set_hl(0, 'DiffsDiffText', { default = true, bg = diff_text.bg }) + + if config.highlights.overrides then + for group, hl in pairs(config.highlights.overrides) do + vim.api.nvim_set_hl(0, group, hl) + end + end end local function init() @@ -251,6 +258,7 @@ local function init() ['highlights.background'] = { opts.highlights.background, 'boolean', true }, ['highlights.gutter'] = { opts.highlights.gutter, 'boolean', true }, ['highlights.blend_alpha'] = { opts.highlights.blend_alpha, 'number', true }, + ['highlights.overrides'] = { opts.highlights.overrides, 'table', true }, ['highlights.context'] = { opts.highlights.context, 'table', true }, ['highlights.treesitter'] = { opts.highlights.treesitter, 'table', true }, ['highlights.vim'] = { opts.highlights.vim, 'table', true }, From ae65c50f9261707269d34b5db479c884d7e61250 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 15:53:08 -0500 Subject: [PATCH 10/21] docs(readme): mention blend alpha and highlight overrides --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b4d845..ca3d72e 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,8 @@ syntax highlighting. - Vim syntax fallback for languages without a treesitter parser - Hunk header context highlighting (`@@ ... @@ function foo()`) - Character-level (intra-line) diff highlighting for changed characters -- Configurable debouncing, max lines, and diff prefix concealment +- Configurable debouncing, max lines, diff prefix concealment, blend alpha, and + highlight overrides ## Requirements From c72efec77d7987a0feb0a4be3992b7e9f3ac568b Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 16:48:31 -0500 Subject: [PATCH 11/21] fix(commands): add diff buffer UX improvements Problem: diffs:// buffers could trigger spurious LSP diagnostics, opening multiple diffs from fugitive created redundant splits, and there was no quick way to close diff windows. Solution: disable diagnostics on diff buffers, reuse existing diffs:// windows in the tabpage instead of creating new splits, and add a buffer-local q keymap to close diff windows. --- lua/diffs/commands.lua | 54 +++++++++++++++-- spec/ux_spec.lua | 135 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 spec/ux_spec.lua diff --git a/lua/diffs/commands.lua b/lua/diffs/commands.lua index 4fc795b..dc6cfe2 100644 --- a/lua/diffs/commands.lua +++ b/lua/diffs/commands.lua @@ -3,6 +3,27 @@ local M = {} local git = require('diffs.git') local dbg = require('diffs.log').dbg +---@return integer? +function M.find_diffs_window() + local tabpage = vim.api.nvim_get_current_tabpage() + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(tabpage)) do + if vim.api.nvim_win_is_valid(win) then + local buf = vim.api.nvim_win_get_buf(win) + local name = vim.api.nvim_buf_get_name(buf) + if name:match('^diffs://') then + return win + end + end + end + return nil +end + +---@param bufnr integer +function M.setup_diff_buf(bufnr) + vim.diagnostic.enable(false, { bufnr = bufnr }) + vim.keymap.set('n', 'q', 'close', { buffer = bufnr }) +end + ---@param diff_lines string[] ---@param hunk_position { hunk_header: string, offset: integer } ---@return integer? @@ -96,9 +117,16 @@ function M.gdiff(revision, vertical) vim.api.nvim_buf_set_var(diff_buf, 'diffs_repo_root', repo_root) end - vim.cmd(vertical and 'vsplit' or 'split') - vim.api.nvim_win_set_buf(0, diff_buf) + local existing_win = M.find_diffs_window() + if existing_win then + vim.api.nvim_set_current_win(existing_win) + vim.api.nvim_win_set_buf(existing_win, diff_buf) + else + vim.cmd(vertical and 'vsplit' or 'split') + vim.api.nvim_win_set_buf(0, diff_buf) + end + M.setup_diff_buf(diff_buf) dbg('opened diff buffer %d for %s against %s', diff_buf, rel_path, revision) vim.schedule(function() @@ -190,8 +218,14 @@ function M.gdiff_file(filepath, opts) vim.api.nvim_buf_set_var(diff_buf, 'diffs_old_filepath', old_rel_path) end - vim.cmd(opts.vertical and 'vsplit' or 'split') - vim.api.nvim_win_set_buf(0, diff_buf) + local existing_win = M.find_diffs_window() + if existing_win then + vim.api.nvim_set_current_win(existing_win) + vim.api.nvim_win_set_buf(existing_win, diff_buf) + else + vim.cmd(opts.vertical and 'vsplit' or 'split') + vim.api.nvim_win_set_buf(0, diff_buf) + end if opts.hunk_position then local target_line = M.find_hunk_line(diff_lines, opts.hunk_position) @@ -201,6 +235,7 @@ function M.gdiff_file(filepath, opts) end end + M.setup_diff_buf(diff_buf) dbg('opened diff buffer %d for %s (%s)', diff_buf, rel_path, diff_label) vim.schedule(function() @@ -244,9 +279,16 @@ function M.gdiff_section(repo_root, opts) vim.api.nvim_buf_set_name(diff_buf, 'diffs://' .. diff_label .. ':all') vim.api.nvim_buf_set_var(diff_buf, 'diffs_repo_root', repo_root) - vim.cmd(opts.vertical and 'vsplit' or 'split') - vim.api.nvim_win_set_buf(0, diff_buf) + local existing_win = M.find_diffs_window() + if existing_win then + vim.api.nvim_set_current_win(existing_win) + vim.api.nvim_win_set_buf(existing_win, diff_buf) + else + vim.cmd(opts.vertical and 'vsplit' or 'split') + vim.api.nvim_win_set_buf(0, diff_buf) + end + M.setup_diff_buf(diff_buf) dbg('opened section diff buffer %d (%s)', diff_buf, diff_label) vim.schedule(function() diff --git a/spec/ux_spec.lua b/spec/ux_spec.lua new file mode 100644 index 0000000..9cf0110 --- /dev/null +++ b/spec/ux_spec.lua @@ -0,0 +1,135 @@ +local commands = require('diffs.commands') +local helpers = require('spec.helpers') + +local counter = 0 + +local function create_diffs_buffer(name) + counter = counter + 1 + local bufnr = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { + 'diff --git a/file.lua b/file.lua', + '--- a/file.lua', + '+++ b/file.lua', + '@@ -1,1 +1,2 @@', + ' local x = 1', + '+local y = 2', + }) + vim.api.nvim_set_option_value('buftype', 'nowrite', { buf = bufnr }) + vim.api.nvim_set_option_value('bufhidden', 'wipe', { buf = bufnr }) + vim.api.nvim_set_option_value('swapfile', false, { buf = bufnr }) + vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr }) + vim.api.nvim_set_option_value('filetype', 'diff', { buf = bufnr }) + vim.api.nvim_buf_set_name(bufnr, name or ('diffs://unstaged:file_' .. counter .. '.lua')) + return bufnr +end + +describe('ux', function() + describe('diagnostics', function() + it('disables diagnostics on diff buffers', function() + local bufnr = create_diffs_buffer() + commands.setup_diff_buf(bufnr) + + assert.is_false(vim.diagnostic.is_enabled({ bufnr = bufnr })) + vim.api.nvim_buf_delete(bufnr, { force = true }) + end) + + it('does not affect other buffers', function() + local diff_buf = create_diffs_buffer() + local normal_buf = helpers.create_buffer({ 'hello' }) + + commands.setup_diff_buf(diff_buf) + + assert.is_true(vim.diagnostic.is_enabled({ bufnr = normal_buf })) + vim.api.nvim_buf_delete(diff_buf, { force = true }) + helpers.delete_buffer(normal_buf) + end) + end) + + describe('q keymap', function() + it('sets q keymap on diff buffer', function() + local bufnr = create_diffs_buffer() + commands.setup_diff_buf(bufnr) + + local keymaps = vim.api.nvim_buf_get_keymap(bufnr, 'n') + local has_q = false + for _, km in ipairs(keymaps) do + if km.lhs == 'q' then + has_q = true + break + end + end + assert.is_true(has_q) + vim.api.nvim_buf_delete(bufnr, { force = true }) + end) + + it('q closes the window', function() + local bufnr = create_diffs_buffer() + commands.setup_diff_buf(bufnr) + + vim.cmd('split') + local win = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_buf(win, bufnr) + + local win_count_before = #vim.api.nvim_tabpage_list_wins(0) + + vim.api.nvim_buf_call(bufnr, function() + vim.cmd('normal q') + end) + + local win_count_after = #vim.api.nvim_tabpage_list_wins(0) + assert.equals(win_count_before - 1, win_count_after) + end) + end) + + describe('window reuse', function() + it('returns nil when no diffs window exists', function() + local win = commands.find_diffs_window() + assert.is_nil(win) + end) + + it('finds existing diffs:// window', function() + local bufnr = create_diffs_buffer() + vim.cmd('split') + local expected_win = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_buf(expected_win, bufnr) + + local found = commands.find_diffs_window() + assert.equals(expected_win, found) + + vim.api.nvim_win_close(expected_win, true) + end) + + it('ignores non-diffs buffers', function() + local normal_buf = helpers.create_buffer({ 'hello' }) + vim.cmd('split') + local win = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_buf(win, normal_buf) + + local found = commands.find_diffs_window() + assert.is_nil(found) + + vim.api.nvim_win_close(win, true) + helpers.delete_buffer(normal_buf) + end) + + it('returns first diffs window when multiple exist', function() + local buf1 = create_diffs_buffer() + local buf2 = create_diffs_buffer() + + vim.cmd('split') + local win1 = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_buf(win1, buf1) + + vim.cmd('split') + local win2 = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_buf(win2, buf2) + + local found = commands.find_diffs_window() + assert.is_not_nil(found) + assert.is_true(found == win1 or found == win2) + + vim.api.nvim_win_close(win1, true) + vim.api.nvim_win_close(win2, true) + end) + end) +end) From 6b4953bf41014a5492a9e45e7291b8e56226da67 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 16:54:54 -0500 Subject: [PATCH 12/21] docs: document q keymap and window reuse behavior --- doc/diffs.nvim.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index 391ad05..d0298b9 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -226,6 +226,9 @@ COMMANDS *diffs-commands* code language, plus diff header highlighting for `diff --git`, `---`, `+++`, and `@@` lines. + If a `diffs://` window already exists in the current tabpage, the new + diff replaces its buffer instead of creating another split. + Parameters: ~ {revision} (string, optional) Git revision to diff against. Defaults to HEAD. @@ -259,6 +262,12 @@ Example configuration: >lua vim.keymap.set('n', 'gD', '(diffs-gvdiff)') < +Diff buffer mappings: ~ + *diffs-q* + q Close the diff window. Available in all `diffs://` + buffers created by |:Gdiff|, |:Gvdiff|, |:Ghdiff|, + or the fugitive status keymaps. + ============================================================================== FUGITIVE STATUS KEYMAPS *diffs-fugitive* From 731222d027f16716d802db824d8e65c283bdaf04 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 17:38:34 -0500 Subject: [PATCH 13/21] feat(conflict): detect and resolve inline merge conflict markers Problem: when git hits a merge conflict, users stare at raw <<<<<<< markers with broken treesitter and noisy LSP diagnostics. Existing solutions (git-conflict.nvim) use their own highlighting rather than integrating with diffs.nvim's color blending pipeline. Solution: add conflict.lua module that detects <<<<<<>>>>>> markers (with diff3 ||||||| support), highlights ours/theirs/base regions with blended DiffsConflict* highlight groups, provides resolution keymaps (doo/dot/dob/don) and navigation (]x/[x), suppresses diagnostics while markers are present, and auto-detaches when all conflicts are resolved. Fires DiffsConflictResolved user event on last resolution. --- lua/diffs/conflict.lua | 457 ++++++++++++++++++++++++++++ lua/diffs/init.lua | 89 ++++++ plugin/diffs.lua | 9 + spec/conflict_spec.lua | 655 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1210 insertions(+) create mode 100644 lua/diffs/conflict.lua create mode 100644 spec/conflict_spec.lua diff --git a/lua/diffs/conflict.lua b/lua/diffs/conflict.lua new file mode 100644 index 0000000..b4d94ef --- /dev/null +++ b/lua/diffs/conflict.lua @@ -0,0 +1,457 @@ +---@class diffs.ConflictKeymaps +---@field ours string|false +---@field theirs string|false +---@field both string|false +---@field none string|false +---@field next string|false +---@field prev string|false + +---@class diffs.ConflictConfig +---@field enabled boolean +---@field disable_diagnostics boolean +---@field show_virtual_text boolean +---@field keymaps diffs.ConflictKeymaps + +---@class diffs.ConflictRegion +---@field marker_ours integer +---@field ours_start integer +---@field ours_end integer +---@field marker_base integer? +---@field base_start integer? +---@field base_end integer? +---@field marker_sep integer +---@field theirs_start integer +---@field theirs_end integer +---@field marker_theirs integer + +local M = {} + +local ns = vim.api.nvim_create_namespace('diffs-conflict') + +---@type table +local attached_buffers = {} + +---@type table +local diagnostics_suppressed = {} + +local PRIORITY_LINE_BG = 200 + +---@param lines string[] +---@return diffs.ConflictRegion[] +function M.parse(lines) + local regions = {} + local state = 'idle' + local current = nil + + for i, line in ipairs(lines) do + local idx = i - 1 + + if state == 'idle' then + if line:match('^<<<<<<<') then + current = { marker_ours = idx, ours_start = idx + 1 } + state = 'in_ours' + end + elseif state == 'in_ours' then + if line:match('^|||||||') then + current.ours_end = idx + current.marker_base = idx + current.base_start = idx + 1 + state = 'in_base' + elseif line:match('^=======') then + current.ours_end = idx + current.marker_sep = idx + current.theirs_start = idx + 1 + state = 'in_theirs' + elseif line:match('^<<<<<<<') then + current = { marker_ours = idx, ours_start = idx + 1 } + elseif line:match('^>>>>>>>') then + current = nil + state = 'idle' + end + elseif state == 'in_base' then + if line:match('^=======') then + current.base_end = idx + current.marker_sep = idx + current.theirs_start = idx + 1 + state = 'in_theirs' + elseif line:match('^<<<<<<<') then + current = { marker_ours = idx, ours_start = idx + 1 } + state = 'in_ours' + elseif line:match('^>>>>>>>') then + current = nil + state = 'idle' + end + elseif state == 'in_theirs' then + if line:match('^>>>>>>>') then + current.theirs_end = idx + current.marker_theirs = idx + table.insert(regions, current) + current = nil + state = 'idle' + elseif line:match('^<<<<<<<') then + current = { marker_ours = idx, ours_start = idx + 1 } + state = 'in_ours' + end + end + end + + return regions +end + +---@param bufnr integer +---@return diffs.ConflictRegion[] +local function parse_buffer(bufnr) + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + return M.parse(lines) +end + +---@param bufnr integer +---@param regions diffs.ConflictRegion[] +---@param config diffs.ConflictConfig +local function apply_highlights(bufnr, regions, config) + vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1) + + for _, region in ipairs(regions) do + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_ours, 0, { + end_row = region.marker_ours + 1, + hl_group = 'DiffsConflictMarker', + hl_eol = true, + priority = PRIORITY_LINE_BG, + }) + + if config.show_virtual_text then + 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 + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, line, 0, { + end_row = line + 1, + hl_group = 'DiffsConflictOurs', + hl_eol = true, + priority = PRIORITY_LINE_BG, + }) + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, line, 0, { + number_hl_group = 'DiffsConflictOursNr', + priority = PRIORITY_LINE_BG, + }) + end + + if region.marker_base then + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_base, 0, { + end_row = region.marker_base + 1, + hl_group = 'DiffsConflictMarker', + hl_eol = true, + priority = PRIORITY_LINE_BG, + }) + + for line = region.base_start, region.base_end - 1 do + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, line, 0, { + end_row = line + 1, + hl_group = 'DiffsConflictBase', + hl_eol = true, + priority = PRIORITY_LINE_BG, + }) + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, line, 0, { + number_hl_group = 'DiffsConflictBaseNr', + priority = PRIORITY_LINE_BG, + }) + end + end + + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_sep, 0, { + end_row = region.marker_sep + 1, + hl_group = 'DiffsConflictMarker', + hl_eol = true, + priority = PRIORITY_LINE_BG, + }) + + for line = region.theirs_start, region.theirs_end - 1 do + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, line, 0, { + end_row = line + 1, + hl_group = 'DiffsConflictTheirs', + hl_eol = true, + priority = PRIORITY_LINE_BG, + }) + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, line, 0, { + number_hl_group = 'DiffsConflictTheirsNr', + priority = PRIORITY_LINE_BG, + }) + end + + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_theirs, 0, { + end_row = region.marker_theirs + 1, + hl_group = 'DiffsConflictMarker', + hl_eol = true, + priority = PRIORITY_LINE_BG, + }) + + if config.show_virtual_text then + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_theirs, 0, { + virt_text = { { ' incoming', 'DiffsConflictMarker' } }, + virt_text_pos = 'eol', + }) + end + end +end + +---@param cursor_line integer +---@param regions diffs.ConflictRegion[] +---@return diffs.ConflictRegion? +local function find_conflict_at_cursor(cursor_line, regions) + for _, region in ipairs(regions) do + if cursor_line >= region.marker_ours and cursor_line <= region.marker_theirs then + return region + end + end + return nil +end + +---@param bufnr integer +---@param region diffs.ConflictRegion +---@param replacement string[] +local function replace_region(bufnr, region, replacement) + vim.api.nvim_buf_set_lines( + bufnr, + region.marker_ours, + region.marker_theirs + 1, + false, + replacement + ) +end + +---@param bufnr integer +---@param config diffs.ConflictConfig +local function refresh(bufnr, config) + local regions = parse_buffer(bufnr) + if #regions == 0 then + M.detach(bufnr) + vim.api.nvim_exec_autocmds('User', { pattern = 'DiffsConflictResolved' }) + return + end + apply_highlights(bufnr, regions, config) +end + +---@param bufnr integer +---@param config diffs.ConflictConfig +function M.resolve_ours(bufnr, config) + if not vim.api.nvim_get_option_value('modifiable', { buf = bufnr }) then + vim.notify('[diffs.nvim]: buffer is not modifiable', vim.log.levels.WARN) + return + end + local regions = parse_buffer(bufnr) + local cursor = vim.api.nvim_win_get_cursor(0) + local region = find_conflict_at_cursor(cursor[1] - 1, regions) + if not region then + 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) +end + +---@param bufnr integer +---@param config diffs.ConflictConfig +function M.resolve_theirs(bufnr, config) + if not vim.api.nvim_get_option_value('modifiable', { buf = bufnr }) then + vim.notify('[diffs.nvim]: buffer is not modifiable', vim.log.levels.WARN) + return + end + local regions = parse_buffer(bufnr) + local cursor = vim.api.nvim_win_get_cursor(0) + local region = find_conflict_at_cursor(cursor[1] - 1, regions) + if not region then + 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) +end + +---@param bufnr integer +---@param config diffs.ConflictConfig +function M.resolve_both(bufnr, config) + if not vim.api.nvim_get_option_value('modifiable', { buf = bufnr }) then + vim.notify('[diffs.nvim]: buffer is not modifiable', vim.log.levels.WARN) + return + end + local regions = parse_buffer(bufnr) + local cursor = vim.api.nvim_win_get_cursor(0) + local region = find_conflict_at_cursor(cursor[1] - 1, regions) + if not region then + return + end + local ours = vim.api.nvim_buf_get_lines(bufnr, region.ours_start, region.ours_end, false) + local theirs = vim.api.nvim_buf_get_lines(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 + replace_region(bufnr, region, combined) + refresh(bufnr, config) +end + +---@param bufnr integer +---@param config diffs.ConflictConfig +function M.resolve_none(bufnr, config) + if not vim.api.nvim_get_option_value('modifiable', { buf = bufnr }) then + vim.notify('[diffs.nvim]: buffer is not modifiable', vim.log.levels.WARN) + return + end + local regions = parse_buffer(bufnr) + local cursor = vim.api.nvim_win_get_cursor(0) + local region = find_conflict_at_cursor(cursor[1] - 1, regions) + if not region then + return + end + replace_region(bufnr, region, {}) + refresh(bufnr, config) +end + +---@param bufnr integer +function M.goto_next(bufnr) + local regions = parse_buffer(bufnr) + if #regions == 0 then + return + end + local cursor = vim.api.nvim_win_get_cursor(0) + local cursor_line = cursor[1] - 1 + for _, region in ipairs(regions) do + if region.marker_ours > cursor_line then + vim.api.nvim_win_set_cursor(0, { region.marker_ours + 1, 0 }) + return + end + end + vim.api.nvim_win_set_cursor(0, { regions[1].marker_ours + 1, 0 }) +end + +---@param bufnr integer +function M.goto_prev(bufnr) + local regions = parse_buffer(bufnr) + if #regions == 0 then + return + end + local cursor = vim.api.nvim_win_get_cursor(0) + local cursor_line = cursor[1] - 1 + for i = #regions, 1, -1 do + if regions[i].marker_ours < cursor_line then + vim.api.nvim_win_set_cursor(0, { regions[i].marker_ours + 1, 0 }) + return + end + end + vim.api.nvim_win_set_cursor(0, { regions[#regions].marker_ours + 1, 0 }) +end + +---@param bufnr integer +---@param config diffs.ConflictConfig +local function setup_keymaps(bufnr, config) + local km = config.keymaps + + if km.ours then + vim.keymap.set('n', km.ours, function() + M.resolve_ours(bufnr, config) + end, { buffer = bufnr }) + end + if km.theirs then + vim.keymap.set('n', km.theirs, function() + M.resolve_theirs(bufnr, config) + end, { buffer = bufnr }) + end + if km.both then + vim.keymap.set('n', km.both, function() + M.resolve_both(bufnr, config) + end, { buffer = bufnr }) + end + if km.none then + vim.keymap.set('n', km.none, function() + M.resolve_none(bufnr, config) + end, { buffer = bufnr }) + end + if km.next then + vim.keymap.set('n', km.next, function() + M.goto_next(bufnr) + end, { buffer = bufnr }) + end + if km.prev then + vim.keymap.set('n', km.prev, function() + M.goto_prev(bufnr) + end, { buffer = bufnr }) + end +end + +---@param bufnr integer +function M.detach(bufnr) + vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1) + attached_buffers[bufnr] = nil + + if diagnostics_suppressed[bufnr] then + pcall(vim.diagnostic.enable, true, { bufnr = bufnr }) + diagnostics_suppressed[bufnr] = nil + end +end + +---@param bufnr integer +---@param config diffs.ConflictConfig +function M.attach(bufnr, config) + if attached_buffers[bufnr] then + return + end + + local buftype = vim.api.nvim_get_option_value('buftype', { buf = bufnr }) + if buftype ~= '' then + return + end + + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + local has_marker = false + for _, line in ipairs(lines) do + if line:match('^<<<<<<<') then + has_marker = true + break + end + end + if not has_marker then + return + end + + attached_buffers[bufnr] = true + + local regions = M.parse(lines) + apply_highlights(bufnr, regions, config) + setup_keymaps(bufnr, config) + + if config.disable_diagnostics then + pcall(vim.diagnostic.enable, false, { bufnr = bufnr }) + diagnostics_suppressed[bufnr] = true + end + + vim.api.nvim_create_autocmd({ 'TextChanged', 'TextChangedI' }, { + buffer = bufnr, + callback = function() + if not attached_buffers[bufnr] then + return true + end + refresh(bufnr, config) + end, + }) + + vim.api.nvim_create_autocmd('BufWipeout', { + buffer = bufnr, + callback = function() + attached_buffers[bufnr] = nil + diagnostics_suppressed[bufnr] = nil + end, + }) +end + +---@return integer +function M.get_namespace() + return ns +end + +return M diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index 7fb4e6d..cdc29d1 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -29,12 +29,27 @@ ---@field horizontal string|false ---@field vertical string|false +---@class diffs.ConflictKeymaps +---@field ours string|false +---@field theirs string|false +---@field both string|false +---@field none string|false +---@field next string|false +---@field prev string|false + +---@class diffs.ConflictConfig +---@field enabled boolean +---@field disable_diagnostics boolean +---@field show_virtual_text boolean +---@field keymaps diffs.ConflictKeymaps + ---@class diffs.Config ---@field debug boolean ---@field debounce_ms integer ---@field hide_prefix boolean ---@field highlights diffs.Highlights ---@field fugitive diffs.FugitiveConfig +---@field conflict diffs.ConflictConfig ---@class diffs ---@field attach fun(bufnr?: integer) @@ -109,6 +124,19 @@ local default_config = { horizontal = 'du', vertical = 'dU', }, + conflict = { + enabled = true, + disable_diagnostics = true, + show_virtual_text = true, + keymaps = { + ours = 'doo', + theirs = 'dot', + both = 'dob', + none = 'don', + next = ']x', + prev = '[x', + }, + }, } ---@type diffs.Config @@ -231,6 +259,37 @@ local function compute_highlight_groups() vim.api.nvim_set_hl(0, 'DiffsDiffChange', { default = true, bg = diff_change.bg }) vim.api.nvim_set_hl(0, 'DiffsDiffText', { default = true, bg = diff_text.bg }) + local change_bg = diff_change.bg or 0x3a3a4a + local text_bg = diff_text.bg or 0x4a4a5a + local change_fg = diff_change.fg or diff_text.fg or 0x80a0c0 + + local blended_ours = blend_color(add_bg, bg, 0.4) + local blended_theirs = blend_color(change_bg, bg, 0.4) + local blended_base = blend_color(text_bg, bg, 0.3) + local blended_ours_nr = blend_color(add_fg, bg, alpha) + local blended_theirs_nr = blend_color(change_fg, bg, alpha) + local blended_base_nr = blend_color(change_fg, bg, 0.4) + + vim.api.nvim_set_hl(0, 'DiffsConflictOurs', { default = true, bg = blended_ours }) + 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, + 'DiffsConflictOursNr', + { default = true, fg = blended_ours_nr, bg = blended_ours } + ) + vim.api.nvim_set_hl( + 0, + 'DiffsConflictTheirsNr', + { default = true, fg = blended_theirs_nr, bg = blended_theirs } + ) + vim.api.nvim_set_hl( + 0, + 'DiffsConflictBaseNr', + { default = true, fg = blended_base_nr, bg = blended_base } + ) + if config.highlights.overrides then for group, hl in pairs(config.highlights.overrides) do vim.api.nvim_set_hl(0, group, hl) @@ -324,6 +383,30 @@ local function init() }) end + if opts.conflict then + vim.validate({ + ['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.keymaps'] = { opts.conflict.keymaps, 'table', true }, + }) + + if opts.conflict.keymaps then + local keymap_validator = function(v) + return v == false or type(v) == 'string' + end + for _, key in ipairs({ 'ours', 'theirs', 'both', 'none', 'next', 'prev' }) do + vim.validate({ + ['conflict.keymaps.' .. key] = { + opts.conflict.keymaps[key], + keymap_validator, + 'string or false', + }, + }) + end + end + end + if opts.debounce_ms and opts.debounce_ms < 0 then error('diffs: debounce_ms must be >= 0') end @@ -488,4 +571,10 @@ function M.get_fugitive_config() return config.fugitive end +---@return diffs.ConflictConfig +function M.get_conflict_config() + init() + return config.conflict +end + return M diff --git a/plugin/diffs.lua b/plugin/diffs.lua index 031ed60..e4fc690 100644 --- a/plugin/diffs.lua +++ b/plugin/diffs.lua @@ -30,6 +30,15 @@ vim.api.nvim_create_autocmd('BufReadCmd', { end, }) +vim.api.nvim_create_autocmd('BufReadPost', { + callback = function(args) + local conflict_config = require('diffs').get_conflict_config() + if conflict_config.enabled then + require('diffs.conflict').attach(args.buf, conflict_config) + end + end, +}) + vim.api.nvim_create_autocmd('OptionSet', { pattern = 'diff', callback = function() diff --git a/spec/conflict_spec.lua b/spec/conflict_spec.lua new file mode 100644 index 0000000..128c567 --- /dev/null +++ b/spec/conflict_spec.lua @@ -0,0 +1,655 @@ +local conflict = require('diffs.conflict') +local helpers = require('spec.helpers') + +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_file_buffer(lines) + local bufnr = vim.api.nvim_create_buf(false, false) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines or {}) + return bufnr +end + +local function get_extmarks(bufnr) + return vim.api.nvim_buf_get_extmarks(bufnr, conflict.get_namespace(), 0, -1, { details = true }) +end + +describe('conflict', function() + describe('parse', function() + it('parses a single conflict', function() + local lines = { + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + '>>>>>>> feature', + } + local regions = conflict.parse(lines) + assert.are.equal(1, #regions) + assert.are.equal(0, regions[1].marker_ours) + assert.are.equal(1, regions[1].ours_start) + assert.are.equal(2, regions[1].ours_end) + assert.are.equal(2, regions[1].marker_sep) + assert.are.equal(3, regions[1].theirs_start) + assert.are.equal(4, regions[1].theirs_end) + assert.are.equal(4, regions[1].marker_theirs) + end) + + it('parses multiple conflicts', function() + local lines = { + '<<<<<<< HEAD', + 'a', + '=======', + 'b', + '>>>>>>> feat', + 'normal line', + '<<<<<<< HEAD', + 'c', + '=======', + 'd', + '>>>>>>> feat', + } + local regions = conflict.parse(lines) + assert.are.equal(2, #regions) + assert.are.equal(0, regions[1].marker_ours) + assert.are.equal(6, regions[2].marker_ours) + end) + + it('parses diff3 format', function() + local lines = { + '<<<<<<< HEAD', + 'local x = 1', + '||||||| base', + 'local x = 0', + '=======', + 'local x = 2', + '>>>>>>> feature', + } + local regions = conflict.parse(lines) + assert.are.equal(1, #regions) + assert.are.equal(2, regions[1].marker_base) + assert.are.equal(3, regions[1].base_start) + assert.are.equal(4, regions[1].base_end) + end) + + it('handles empty ours section', function() + local lines = { + '<<<<<<< HEAD', + '=======', + 'local x = 2', + '>>>>>>> feature', + } + local regions = conflict.parse(lines) + assert.are.equal(1, #regions) + assert.are.equal(1, regions[1].ours_start) + assert.are.equal(1, regions[1].ours_end) + end) + + it('handles empty theirs section', function() + local lines = { + '<<<<<<< HEAD', + 'local x = 1', + '=======', + '>>>>>>> feature', + } + local regions = conflict.parse(lines) + assert.are.equal(1, #regions) + assert.are.equal(3, regions[1].theirs_start) + assert.are.equal(3, regions[1].theirs_end) + end) + + it('returns empty for no markers', function() + local lines = { 'local x = 1', 'local y = 2' } + local regions = conflict.parse(lines) + assert.are.equal(0, #regions) + end) + + it('discards malformed markers (no separator)', function() + local lines = { + '<<<<<<< HEAD', + 'local x = 1', + '>>>>>>> feature', + } + local regions = conflict.parse(lines) + assert.are.equal(0, #regions) + end) + + it('discards malformed markers (no end)', function() + local lines = { + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + } + local regions = conflict.parse(lines) + assert.are.equal(0, #regions) + end) + + it('handles trailing text on marker lines', function() + local lines = { + '<<<<<<< HEAD (some text)', + 'local x = 1', + '======= extra', + 'local x = 2', + '>>>>>>> feature-branch/some-thing', + } + local regions = conflict.parse(lines) + assert.are.equal(1, #regions) + end) + + it('handles empty base in diff3', function() + local lines = { + '<<<<<<< HEAD', + 'local x = 1', + '||||||| base', + '=======', + 'local x = 2', + '>>>>>>> feature', + } + local regions = conflict.parse(lines) + assert.are.equal(1, #regions) + assert.are.equal(3, regions[1].base_start) + assert.are.equal(3, regions[1].base_end) + end) + end) + + describe('highlighting', function() + after_each(function() + conflict.detach(vim.api.nvim_get_current_buf()) + end) + + it('applies extmarks for conflict regions', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + + conflict.attach(bufnr, default_config()) + + local extmarks = get_extmarks(bufnr) + assert.is_true(#extmarks > 0) + + local has_ours = false + local has_theirs = false + local has_marker = false + for _, mark in ipairs(extmarks) do + local hl = mark[4] and mark[4].hl_group + if hl == 'DiffsConflictOurs' then + has_ours = true + end + if hl == 'DiffsConflictTheirs' then + has_theirs = true + end + if hl == 'DiffsConflictMarker' then + has_marker = true + end + end + assert.is_true(has_ours) + assert.is_true(has_theirs) + assert.is_true(has_marker) + + helpers.delete_buffer(bufnr) + end) + + it('applies virtual text when enabled', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + + conflict.attach(bufnr, default_config({ show_virtual_text = true })) + + 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(2, virt_text_count) + + helpers.delete_buffer(bufnr) + end) + + it('does not apply virtual text when disabled', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + + conflict.attach(bufnr, default_config({ show_virtual_text = false })) + + 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) + + it('applies number_hl_group to content lines', 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 has_ours_nr = false + local has_theirs_nr = false + for _, mark in ipairs(extmarks) do + local nr = mark[4] and mark[4].number_hl_group + if nr == 'DiffsConflictOursNr' then + has_ours_nr = true + end + if nr == 'DiffsConflictTheirsNr' then + has_theirs_nr = true + end + end + assert.is_true(has_ours_nr) + assert.is_true(has_theirs_nr) + + helpers.delete_buffer(bufnr) + end) + + it('highlights base region in diff3', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'local x = 1', + '||||||| base', + 'local x = 0', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + + conflict.attach(bufnr, default_config()) + + local extmarks = get_extmarks(bufnr) + local has_base = false + for _, mark in ipairs(extmarks) do + if mark[4] and mark[4].hl_group == 'DiffsConflictBase' then + has_base = true + break + end + end + assert.is_true(has_base) + + helpers.delete_buffer(bufnr) + end) + + it('clears extmarks on detach', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + + conflict.attach(bufnr, default_config()) + assert.is_true(#get_extmarks(bufnr) > 0) + + conflict.detach(bufnr) + assert.are.equal(0, #get_extmarks(bufnr)) + + helpers.delete_buffer(bufnr) + end) + end) + + describe('resolution', function() + local function make_conflict_buffer() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + vim.api.nvim_set_current_buf(bufnr) + return bufnr + end + + it('resolve_ours keeps ours content', function() + local bufnr = make_conflict_buffer() + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + + conflict.resolve_ours(bufnr, default_config()) + + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + assert.are.equal(1, #lines) + assert.are.equal('local x = 1', lines[1]) + + helpers.delete_buffer(bufnr) + end) + + it('resolve_theirs keeps theirs content', function() + local bufnr = make_conflict_buffer() + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + + conflict.resolve_theirs(bufnr, default_config()) + + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + assert.are.equal(1, #lines) + assert.are.equal('local x = 2', lines[1]) + + helpers.delete_buffer(bufnr) + end) + + it('resolve_both keeps ours then theirs', function() + local bufnr = make_conflict_buffer() + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + + conflict.resolve_both(bufnr, default_config()) + + local lines = vim.api.nvim_buf_get_lines(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]) + + helpers.delete_buffer(bufnr) + end) + + it('resolve_none removes entire block', function() + local bufnr = make_conflict_buffer() + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + + conflict.resolve_none(bufnr, default_config()) + + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + assert.are.equal(1, #lines) + assert.are.equal('', lines[1]) + + helpers.delete_buffer(bufnr) + end) + + it('does nothing when cursor is outside conflict', function() + local bufnr = create_file_buffer({ + 'normal line', + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + + conflict.resolve_ours(bufnr, default_config()) + + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + assert.are.equal(6, #lines) + + helpers.delete_buffer(bufnr) + end) + + it('resolves one conflict among multiple', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'a', + '=======', + 'b', + '>>>>>>> feat', + 'middle', + '<<<<<<< HEAD', + 'c', + '=======', + 'd', + '>>>>>>> feat', + }) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + + conflict.resolve_ours(bufnr, default_config()) + + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + assert.are.equal('a', lines[1]) + assert.are.equal('middle', lines[2]) + assert.are.equal('<<<<<<< HEAD', lines[3]) + + helpers.delete_buffer(bufnr) + end) + + it('resolve_ours with empty ours section', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + + conflict.resolve_ours(bufnr, default_config()) + + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + assert.are.equal(1, #lines) + assert.are.equal('', lines[1]) + + helpers.delete_buffer(bufnr) + end) + + it('handles diff3 resolution (ignores base)', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'local x = 1', + '||||||| base', + 'local x = 0', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + + conflict.resolve_theirs(bufnr, default_config()) + + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + assert.are.equal(1, #lines) + assert.are.equal('local x = 2', lines[1]) + + helpers.delete_buffer(bufnr) + end) + end) + + describe('navigation', function() + it('goto_next jumps to next conflict', function() + local bufnr = create_file_buffer({ + 'normal', + '<<<<<<< HEAD', + 'a', + '=======', + 'b', + '>>>>>>> feat', + 'middle', + '<<<<<<< HEAD', + 'c', + '=======', + 'd', + '>>>>>>> feat', + }) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + + conflict.goto_next(bufnr) + assert.are.equal(2, vim.api.nvim_win_get_cursor(0)[1]) + + conflict.goto_next(bufnr) + assert.are.equal(8, vim.api.nvim_win_get_cursor(0)[1]) + + helpers.delete_buffer(bufnr) + end) + + it('goto_next wraps to first conflict', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'a', + '=======', + 'b', + '>>>>>>> feat', + }) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 5, 0 }) + + conflict.goto_next(bufnr) + assert.are.equal(1, vim.api.nvim_win_get_cursor(0)[1]) + + helpers.delete_buffer(bufnr) + end) + + it('goto_prev jumps to previous conflict', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'a', + '=======', + 'b', + '>>>>>>> feat', + 'middle', + '<<<<<<< HEAD', + 'c', + '=======', + 'd', + '>>>>>>> feat', + 'end', + }) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 12, 0 }) + + conflict.goto_prev(bufnr) + assert.are.equal(7, vim.api.nvim_win_get_cursor(0)[1]) + + conflict.goto_prev(bufnr) + assert.are.equal(1, vim.api.nvim_win_get_cursor(0)[1]) + + helpers.delete_buffer(bufnr) + end) + + it('goto_prev wraps to last conflict', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'a', + '=======', + 'b', + '>>>>>>> feat', + }) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + + conflict.goto_prev(bufnr) + assert.are.equal(1, vim.api.nvim_win_get_cursor(0)[1]) + + helpers.delete_buffer(bufnr) + end) + + it('goto_next does nothing with no conflicts', function() + local bufnr = create_file_buffer({ 'normal line' }) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + + conflict.goto_next(bufnr) + assert.are.equal(1, vim.api.nvim_win_get_cursor(0)[1]) + + helpers.delete_buffer(bufnr) + end) + end) + + describe('lifecycle', function() + it('attach is idempotent', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'a', + '=======', + 'b', + '>>>>>>> feat', + }) + local cfg = default_config() + conflict.attach(bufnr, cfg) + local count1 = #get_extmarks(bufnr) + conflict.attach(bufnr, cfg) + local count2 = #get_extmarks(bufnr) + assert.are.equal(count1, count2) + conflict.detach(bufnr) + helpers.delete_buffer(bufnr) + end) + + it('skips non-file buffers', function() + local bufnr = helpers.create_buffer({ + '<<<<<<< HEAD', + 'a', + '=======', + 'b', + '>>>>>>> feat', + }) + vim.api.nvim_set_option_value('buftype', 'nofile', { buf = bufnr }) + + conflict.attach(bufnr, default_config()) + assert.are.equal(0, #get_extmarks(bufnr)) + + helpers.delete_buffer(bufnr) + end) + + it('skips buffers without conflict markers', function() + local bufnr = create_file_buffer({ 'local x = 1', 'local y = 2' }) + + conflict.attach(bufnr, default_config()) + assert.are.equal(0, #get_extmarks(bufnr)) + + helpers.delete_buffer(bufnr) + end) + + it('detaches after last conflict resolved', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + vim.api.nvim_set_current_buf(bufnr) + conflict.attach(bufnr, default_config()) + + assert.is_true(#get_extmarks(bufnr) > 0) + + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + conflict.resolve_ours(bufnr, default_config()) + + assert.are.equal(0, #get_extmarks(bufnr)) + + helpers.delete_buffer(bufnr) + end) + end) +end) From 74c2dd4c7a0535cbb6611b08b81c64efde79bc4d Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 17:39:35 -0500 Subject: [PATCH 14/21] docs: document conflict resolution config and highlight groups --- doc/diffs.nvim.txt | 142 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 2 deletions(-) diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index d0298b9..028d8ba 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -20,6 +20,7 @@ Features: ~ - Blended diff background colors that preserve syntax visibility - Optional diff prefix (`+`/`-`/` `) concealment - Gutter (line number) highlighting +- Inline merge conflict marker detection, highlighting, and resolution ============================================================================== REQUIREMENTS *diffs-requirements* @@ -80,6 +81,19 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads: horizontal = 'du', vertical = 'dU', }, + conflict = { + enabled = true, + disable_diagnostics = true, + show_virtual_text = true, + keymaps = { + ours = 'doo', + theirs = 'dot', + both = 'dob', + none = 'don', + next = ']x', + prev = '[x', + }, + }, } < *diffs.Config* @@ -108,6 +122,10 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads: Fugitive status buffer keymap options. See |diffs.FugitiveConfig| for fields. + {conflict} (table, default: see below) + Inline merge conflict resolution options. + See |diffs.ConflictConfig| for fields. + *diffs.Highlights* Highlights table fields: ~ {background} (boolean, default: true) @@ -317,6 +335,94 @@ Configuration: ~ Keymap for unified diff in vertical split. Set to `false` to disable. +============================================================================== +CONFLICT RESOLUTION *diffs-conflict* + +diffs.nvim detects inline merge conflict markers (`<<<<<<<`/`=======`/ +`>>>>>>>`) in working files and provides highlighting and resolution keymaps. +Both standard and diff3 (`|||||||`) formats are supported. + +Conflict regions are detected automatically on `BufReadPost` and re-scanned +on `TextChanged`. When all conflicts in a buffer are resolved, highlighting +is removed and diagnostics are re-enabled. + +Configuration: ~ + *diffs.ConflictConfig* +>lua + vim.g.diffs = { + conflict = { + enabled = true, + disable_diagnostics = true, + show_virtual_text = true, + keymaps = { + ours = 'doo', + theirs = 'dot', + both = 'dob', + none = 'don', + next = ']x', + prev = '[x', + }, + }, + } +< + Fields: ~ + {enabled} (boolean, default: true) + Enable conflict marker detection and + resolution. Set to `false` to disable + entirely. + + {disable_diagnostics} (boolean, default: true) + Suppress LSP diagnostics on buffers with + conflict markers. Markers produce syntax + errors that clutter the diagnostic list. + Diagnostics are re-enabled when all conflicts + are resolved. Set `false` to leave + diagnostics alone. + + {show_virtual_text} (boolean, default: true) + 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 + and navigation. Each value accepts a string + (custom key) or `false` (disabled). + + *diffs.ConflictKeymaps* + Keymap fields: ~ + {ours} (string|false, default: 'doo') + Accept current (ours) change. + + {theirs} (string|false, default: 'dot') + Accept incoming (theirs) change. + + {both} (string|false, default: 'dob') + Accept both changes (ours then theirs). + + {none} (string|false, default: 'don') + Reject both changes (delete entire block). + + {next} (string|false, default: ']x') + Jump to next conflict marker. Wraps around. + + {prev} (string|false, default: '[x') + Jump to previous conflict marker. Wraps + around. + +User events: ~ + *DiffsConflictResolved* + DiffsConflictResolved Fired when the last conflict in a buffer is + resolved. Useful for triggering custom actions + (e.g., auto-staging the file). >lua + vim.api.nvim_create_autocmd('User', { + pattern = 'DiffsConflictResolved', + callback = function() + print('all conflicts resolved!') + end, + }) +< + ============================================================================== API *diffs-api* @@ -414,8 +520,9 @@ conflict: modifying line highlights may produce unexpected results. - git-conflict.nvim (akinsho/git-conflict.nvim) - Provides conflict marker highlighting that may overlap with - diffs.nvim's highlighting in conflict scenarios. + Provides conflict marker highlighting and resolution keymaps. + diffs.nvim now has built-in conflict resolution (see + |diffs-conflict|). Disable one or the other to avoid overlap. If you experience visual conflicts, try disabling the conflicting plugin's diff-related features. @@ -462,6 +569,37 @@ Fugitive unified diff highlights: ~ within `-` lines. Derived by blending `diffRemoved` foreground with `Normal` background at 60% alpha. +Conflict highlights: ~ + *DiffsConflictOurs* + DiffsConflictOurs Background for "ours" (current) content lines. + Derived by blending `DiffAdd` background with + `Normal` at 40% alpha (green tint). + + *DiffsConflictTheirs* + DiffsConflictTheirs Background for "theirs" (incoming) content lines. + Derived by blending `DiffChange` background with + `Normal` at 40% alpha. + + *DiffsConflictBase* + DiffsConflictBase Background for base (ancestor) content lines in + diff3 conflicts. Derived by blending `DiffText` + background with `Normal` at 30% alpha (muted). + + *DiffsConflictMarker* + DiffsConflictMarker Dimmed foreground with bold for `<<<<<<<`, + `=======`, `>>>>>>>`, and `|||||||` marker lines. + + *DiffsConflictOursNr* + DiffsConflictOursNr Line number for "ours" content lines. Foreground + from higher-alpha blend, background from line-level + blend. + + *DiffsConflictTheirsNr* + DiffsConflictTheirsNr Line number for "theirs" content lines. + + *DiffsConflictBaseNr* + DiffsConflictBaseNr Line number for base content lines (diff3). + Diff mode window highlights: ~ These are used for |winhighlight| remapping in `&diff` windows. From 7ae867c413aee71ffb123f486294bad712e0fc93 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 17:45:23 -0500 Subject: [PATCH 15/21] fix(conflict): resolve LuaLS duplicate-doc-field and inject-field errors Problem: lua-language-server reports duplicate @class definitions for ConflictKeymaps and ConflictConfig (defined in both init.lua and conflict.lua), and inject-field errors for the untyped parser table. Solution: remove duplicate @class annotations from conflict.lua (init.lua is the canonical source), and annotate the parser's current variable as diffs.ConflictRegion? so LuaLS knows its shape. --- lua/diffs/conflict.lua | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/lua/diffs/conflict.lua b/lua/diffs/conflict.lua index b4d94ef..6e88c1c 100644 --- a/lua/diffs/conflict.lua +++ b/lua/diffs/conflict.lua @@ -1,17 +1,3 @@ ----@class diffs.ConflictKeymaps ----@field ours string|false ----@field theirs string|false ----@field both string|false ----@field none string|false ----@field next string|false ----@field prev string|false - ----@class diffs.ConflictConfig ----@field enabled boolean ----@field disable_diagnostics boolean ----@field show_virtual_text boolean ----@field keymaps diffs.ConflictKeymaps - ---@class diffs.ConflictRegion ---@field marker_ours integer ---@field ours_start integer @@ -41,6 +27,7 @@ local PRIORITY_LINE_BG = 200 function M.parse(lines) local regions = {} local state = 'idle' + ---@type diffs.ConflictRegion? local current = nil for i, line in ipairs(lines) do From 98a1a4028b369686dd01ee6e01580e1c0e7abab3 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 17:51:47 -0500 Subject: [PATCH 16/21] fix(conflict): resolve LuaLS missing-fields diagnostics Problem: LuaLS reports missing-fields errors because the parser builds ConflictRegion tables incrementally, but the variable is typed as diffs.ConflictRegion? which expects all required fields at construction. Solution: type the work-in-progress variable as table? and cast to diffs.ConflictRegion on insertion into the results array. --- lua/diffs/conflict.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/diffs/conflict.lua b/lua/diffs/conflict.lua index 6e88c1c..6e66d39 100644 --- a/lua/diffs/conflict.lua +++ b/lua/diffs/conflict.lua @@ -27,7 +27,7 @@ local PRIORITY_LINE_BG = 200 function M.parse(lines) local regions = {} local state = 'idle' - ---@type diffs.ConflictRegion? + ---@type table? local current = nil for i, line in ipairs(lines) do @@ -72,7 +72,7 @@ function M.parse(lines) if line:match('^>>>>>>>') then current.theirs_end = idx current.marker_theirs = idx - table.insert(regions, current) + table.insert(regions, current --[[@as diffs.ConflictRegion]]) current = nil state = 'idle' elseif line:match('^<<<<<<<') then From 1108c3352672802acba1f3df299d06de5be0953c Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 17:52:35 -0500 Subject: [PATCH 17/21] refactor(conflict): drop unnecessary @as cast in parser --- lua/diffs/conflict.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/diffs/conflict.lua b/lua/diffs/conflict.lua index 6e66d39..52397fe 100644 --- a/lua/diffs/conflict.lua +++ b/lua/diffs/conflict.lua @@ -72,7 +72,7 @@ function M.parse(lines) if line:match('^>>>>>>>') then current.theirs_end = idx current.marker_theirs = idx - table.insert(regions, current --[[@as diffs.ConflictRegion]]) + table.insert(regions, current) current = nil state = 'idle' elseif line:match('^<<<<<<<') then From bae86c5fd9ef148b1ba4b8f13a22dc9ba12c3bda Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 17:57:53 -0500 Subject: [PATCH 18/21] feat(conflict): show branch names in virtual text labels Problem: virtual text showed generic "current"/"incoming" labels with no indication of which branch each side came from. Solution: extract the branch name from the marker line itself (e.g. <<<<<<< HEAD, >>>>>>> feature) and display as "HEAD (current)" / "feature (incoming)". --- lua/diffs/conflict.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/diffs/conflict.lua b/lua/diffs/conflict.lua index 52397fe..6522026 100644 --- a/lua/diffs/conflict.lua +++ b/lua/diffs/conflict.lua @@ -108,7 +108,7 @@ local function apply_highlights(bufnr, regions, config) if config.show_virtual_text then pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_ours, 0, { - virt_text = { { ' current', 'DiffsConflictMarker' } }, + virt_text = { { ' (current)', 'DiffsConflictMarker' } }, virt_text_pos = 'eol', }) end @@ -177,7 +177,7 @@ local function apply_highlights(bufnr, regions, config) if config.show_virtual_text then pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, region.marker_theirs, 0, { - virt_text = { { ' incoming', 'DiffsConflictMarker' } }, + virt_text = { { ' (incoming)', 'DiffsConflictMarker' } }, virt_text_pos = 'eol', }) end From 35cb13419ce4d092ec65ba4782c8fa363d09c6af Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 19:42:29 -0500 Subject: [PATCH 19/21] fix(conflict): keep TextChanged autocmd alive after resolution Problem: resolving the last conflict called M.detach(), which cleared attached_buffers[bufnr]. The TextChanged callback then returned true, permanently deleting the autocmd. Undo restored conflict markers but nothing re-highlighted or re-suppressed diagnostics. Solution: inline the cleanup in refresh() instead of calling detach(). Keep attached_buffers set so the autocmd survives. Re-suppress diagnostics when conflicts reappear after undo. --- lua/diffs/conflict.lua | 10 +++++++++- spec/conflict_spec.lua | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lua/diffs/conflict.lua b/lua/diffs/conflict.lua index 6522026..f0e47c6 100644 --- a/lua/diffs/conflict.lua +++ b/lua/diffs/conflict.lua @@ -214,11 +214,19 @@ end local function refresh(bufnr, config) local regions = parse_buffer(bufnr) if #regions == 0 then - M.detach(bufnr) + vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1) + if diagnostics_suppressed[bufnr] then + pcall(vim.diagnostic.enable, true, { bufnr = bufnr }) + diagnostics_suppressed[bufnr] = nil + end vim.api.nvim_exec_autocmds('User', { pattern = 'DiffsConflictResolved' }) return end apply_highlights(bufnr, regions, config) + if config.disable_diagnostics and not diagnostics_suppressed[bufnr] then + pcall(vim.diagnostic.enable, false, { bufnr = bufnr }) + diagnostics_suppressed[bufnr] = true + end end ---@param bufnr integer diff --git a/spec/conflict_spec.lua b/spec/conflict_spec.lua index 128c567..75eac23 100644 --- a/spec/conflict_spec.lua +++ b/spec/conflict_spec.lua @@ -631,6 +631,39 @@ describe('conflict', function() helpers.delete_buffer(bufnr) end) + it('re-highlights when markers return after resolution', function() + local bufnr = create_file_buffer({ + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + vim.api.nvim_set_current_buf(bufnr) + local cfg = default_config() + conflict.attach(bufnr, cfg) + + assert.is_true(#get_extmarks(bufnr) > 0) + + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + conflict.resolve_ours(bufnr, cfg) + assert.are.equal(0, #get_extmarks(bufnr)) + + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { + '<<<<<<< HEAD', + 'local x = 1', + '=======', + 'local x = 2', + '>>>>>>> feature', + }) + vim.api.nvim_exec_autocmds('TextChanged', { buffer = bufnr }) + + assert.is_true(#get_extmarks(bufnr) > 0) + + conflict.detach(bufnr) + helpers.delete_buffer(bufnr) + end) + it('detaches after last conflict resolved', function() local bufnr = create_file_buffer({ '<<<<<<< HEAD', From a192830d8c886ba65758fd092daf58733b6213fa Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Feb 2026 19:47:32 -0500 Subject: [PATCH 20/21] fix(conflict): clear stale diagnostics before re-enabling Problem: after resolving all conflicts, vim.diagnostic.enable(true) restored diagnostics that were cached while markers were present, showing errors like "unexpected token end" on clean code. Solution: call vim.diagnostic.reset() before re-enabling to flush stale results and let the LSP re-analyze the resolved buffer. --- README.md | 4 +++- lua/diffs/conflict.lua | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ca3d72e..f0a04a8 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ syntax highlighting. - 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 @@ -63,7 +64,8 @@ luarocks install diffs.nvim compatible, but both plugins modifying line highlights may produce unexpected results - [`git-conflict.nvim`](https://github.com/akinsho/git-conflict.nvim) - - conflict marker highlighting may overlap with `diffs.nvim` + `diffs.nvim` now includes built-in conflict resolution; disable one or the + other to avoid overlap # Acknowledgements diff --git a/lua/diffs/conflict.lua b/lua/diffs/conflict.lua index f0e47c6..f4468d2 100644 --- a/lua/diffs/conflict.lua +++ b/lua/diffs/conflict.lua @@ -216,6 +216,7 @@ local function refresh(bufnr, config) if #regions == 0 then vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1) if diagnostics_suppressed[bufnr] then + pcall(vim.diagnostic.reset, nil, bufnr) pcall(vim.diagnostic.enable, true, { bufnr = bufnr }) diagnostics_suppressed[bufnr] = nil end @@ -385,6 +386,7 @@ function M.detach(bufnr) attached_buffers[bufnr] = nil if diagnostics_suppressed[bufnr] then + pcall(vim.diagnostic.reset, nil, bufnr) pcall(vim.diagnostic.enable, true, { bufnr = bufnr }) diagnostics_suppressed[bufnr] = nil end From f3a72926d288aaf03c4e212e9f82a006fb300320 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 8 Feb 2026 16:28:18 -0500 Subject: [PATCH 21/21] doc: add plug mappings for merge conflict resolution --- doc/diffs.nvim.txt | 35 +++++++++++++++++++++++++++++++++++ lua/diffs/conflict.lua | 42 +++++++++++++----------------------------- plugin/diffs.lua | 25 +++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 29 deletions(-) diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index 028d8ba..7663150 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -280,6 +280,41 @@ Example configuration: >lua vim.keymap.set('n', 'gD', '(diffs-gvdiff)') < + *(diffs-conflict-ours)* +(diffs-conflict-ours) + Accept current (ours) change. Replaces the + conflict block with ours content. + + *(diffs-conflict-theirs)* +(diffs-conflict-theirs) + Accept incoming (theirs) change. Replaces the + conflict block with theirs content. + + *(diffs-conflict-both)* +(diffs-conflict-both) + Accept both changes (ours then theirs). + + *(diffs-conflict-none)* +(diffs-conflict-none) + Reject both changes (delete entire block). + + *(diffs-conflict-next)* +(diffs-conflict-next) + Jump to next conflict marker. Wraps around. + + *(diffs-conflict-prev)* +(diffs-conflict-prev) + Jump to previous conflict marker. Wraps around. + +Example configuration: >lua + vim.keymap.set('n', 'co', '(diffs-conflict-ours)') + vim.keymap.set('n', 'ct', '(diffs-conflict-theirs)') + vim.keymap.set('n', 'cb', '(diffs-conflict-both)') + vim.keymap.set('n', 'cn', '(diffs-conflict-none)') + vim.keymap.set('n', ']x', '(diffs-conflict-next)') + vim.keymap.set('n', '[x', '(diffs-conflict-prev)') +< + Diff buffer mappings: ~ *diffs-q* q Close the diff window. Available in all `diffs://` diff --git a/lua/diffs/conflict.lua b/lua/diffs/conflict.lua index f4468d2..9e62a15 100644 --- a/lua/diffs/conflict.lua +++ b/lua/diffs/conflict.lua @@ -348,35 +348,19 @@ end local function setup_keymaps(bufnr, config) local km = config.keymaps - if km.ours then - vim.keymap.set('n', km.ours, function() - M.resolve_ours(bufnr, config) - end, { buffer = bufnr }) - end - if km.theirs then - vim.keymap.set('n', km.theirs, function() - M.resolve_theirs(bufnr, config) - end, { buffer = bufnr }) - end - if km.both then - vim.keymap.set('n', km.both, function() - M.resolve_both(bufnr, config) - end, { buffer = bufnr }) - end - if km.none then - vim.keymap.set('n', km.none, function() - M.resolve_none(bufnr, config) - end, { buffer = bufnr }) - end - if km.next then - vim.keymap.set('n', km.next, function() - M.goto_next(bufnr) - end, { buffer = bufnr }) - end - if km.prev then - vim.keymap.set('n', km.prev, function() - M.goto_prev(bufnr) - end, { buffer = bufnr }) + local maps = { + { km.ours, '(diffs-conflict-ours)' }, + { km.theirs, '(diffs-conflict-theirs)' }, + { km.both, '(diffs-conflict-both)' }, + { km.none, '(diffs-conflict-none)' }, + { km.next, '(diffs-conflict-next)' }, + { km.prev, '(diffs-conflict-prev)' }, + } + + for _, map in ipairs(maps) do + if map[1] then + vim.keymap.set('n', map[1], map[2], { buffer = bufnr }) + end end end diff --git a/plugin/diffs.lua b/plugin/diffs.lua index e4fc690..5d3c8b2 100644 --- a/plugin/diffs.lua +++ b/plugin/diffs.lua @@ -57,3 +57,28 @@ end, { desc = 'Unified diff (horizontal)' }) vim.keymap.set('n', '(diffs-gvdiff)', function() cmds.gdiff(nil, true) end, { desc = 'Unified diff (vertical)' }) + +local function conflict_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-conflict-ours)', function() + conflict_action(require('diffs.conflict').resolve_ours) +end, { desc = 'Accept current (ours) change' }) +vim.keymap.set('n', '(diffs-conflict-theirs)', function() + conflict_action(require('diffs.conflict').resolve_theirs) +end, { desc = 'Accept incoming (theirs) change' }) +vim.keymap.set('n', '(diffs-conflict-both)', function() + conflict_action(require('diffs.conflict').resolve_both) +end, { desc = 'Accept both changes' }) +vim.keymap.set('n', '(diffs-conflict-none)', function() + conflict_action(require('diffs.conflict').resolve_none) +end, { desc = 'Reject both changes' }) +vim.keymap.set('n', '(diffs-conflict-next)', function() + require('diffs.conflict').goto_next(vim.api.nvim_get_current_buf()) +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' })