diff --git a/doc/diffs.nvim.txt b/doc/diffs.nvim.txt index 73f7cc3..b65e3a6 100644 --- a/doc/diffs.nvim.txt +++ b/doc/diffs.nvim.txt @@ -241,11 +241,13 @@ Configuration is done via `vim.g.diffs`. Set this before the plugin loads: 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. + Alpha value for diff line background intensity. + Controls how strongly diff lines (adds/deletes) + stand out from the editor background. Intra-line + highlights use alpha + 0.3 (capped at 1.0) for + extra contrast. Must be between 0 and 1 + (inclusive). Higher values produce more vivid + backgrounds. {context} (table, default: see below) Syntax parsing context options. diff --git a/spec/highlight_spec.lua b/spec/highlight_spec.lua index c6e1a2a..34101ea 100644 --- a/spec/highlight_spec.lua +++ b/spec/highlight_spec.lua @@ -511,6 +511,83 @@ describe('highlight', function() delete_buffer(bufnr) end) + it('applies DiffsAddNr prefix extmark on + line for pw=1', function() + local bufnr = create_buffer({ + '@@ -1,2 +1,2 @@', + '-old', + '+new', + }) + + local hunk = { + filename = 'test.lua', + lang = 'lua', + start_line = 1, + lines = { '-old', '+new' }, + prefix_width = 1, + } + + highlight.highlight_hunk( + bufnr, + ns, + hunk, + default_opts({ highlights = { background = true, treesitter = { enabled = false } } }) + ) + + local extmarks = get_extmarks(bufnr) + local add_prefix = false + local del_prefix = false + for _, mark in ipairs(extmarks) do + local d = mark[4] + if d and d.end_col == 1 and mark[3] == 0 then + if d.hl_group == 'DiffsAddNr' and mark[2] == 2 then + add_prefix = true + end + if d.hl_group == 'DiffsDeleteNr' and mark[2] == 1 then + del_prefix = true + end + end + end + assert.is_true(add_prefix, 'DiffsAddNr on + prefix') + assert.is_true(del_prefix, 'DiffsDeleteNr on - prefix') + delete_buffer(bufnr) + end) + + it('does not apply prefix extmark on context line', function() + local bufnr = create_buffer({ + '@@ -1,2 +1,2 @@', + ' ctx', + '+new', + }) + + local hunk = { + filename = 'test.lua', + lang = 'lua', + start_line = 1, + lines = { ' ctx', '+new' }, + prefix_width = 1, + } + + highlight.highlight_hunk( + bufnr, + ns, + hunk, + default_opts({ highlights = { background = true, treesitter = { enabled = false } } }) + ) + + local extmarks = get_extmarks(bufnr) + local ctx_prefix = false + for _, mark in ipairs(extmarks) do + local d = mark[4] + if d and mark[2] == 1 and mark[3] == 0 and d.end_col == 1 then + if d.hl_group == 'DiffsAddNr' or d.hl_group == 'DiffsDeleteNr' then + ctx_prefix = true + end + end + end + assert.is_false(ctx_prefix, 'no prefix extmark on context line') + delete_buffer(bufnr) + end) + it('applies vim syntax extmarks when vim.enabled and no TS parser', function() local orig_synID = vim.fn.synID local orig_synIDtrans = vim.fn.synIDtrans diff --git a/spec/init_spec.lua b/spec/init_spec.lua index 398f767..91bbed1 100644 --- a/spec/init_spec.lua +++ b/spec/init_spec.lua @@ -554,7 +554,7 @@ describe('diffs', function() diffs._test.set_hl_retry_pending(false) end) - it('omits DiffsClear.bg when Normal.bg is nil (transparent)', function() + it('uses dark fallback bg for DiffsClear when Normal.bg is nil (transparent)', function() vim.api.nvim_get_hl = function(ns, opts) if opts.name == 'Normal' then return { fg = 0xc0c0c0 } @@ -562,11 +562,38 @@ describe('diffs', function() return saved_get_hl(ns, opts) end diffs._test.compute_highlight_groups() - assert.is_nil(set_calls.DiffsClear.bg) + assert.are.equal(0x1a1a1a, set_calls.DiffsClear.bg) assert.is_table(set_calls.DiffsAdd) assert.is_table(set_calls.DiffsDelete) end) + it('blend_alpha controls DiffsAdd.bg intensity', function() + local saved_config_alpha = diffs._test.get_config().highlights.blend_alpha + diffs._test.get_config().highlights.blend_alpha = 0.3 + vim.api.nvim_get_hl = function(ns, opts) + if opts.name == 'Normal' then + return { fg = 0xc0c0c0, bg = 0x1e1e2e } + end + if opts.name == 'DiffAdd' then + return { bg = 0x1a3a1a } + end + if opts.name == 'DiffDelete' then + return { bg = 0x3a1a1a } + end + return saved_get_hl(ns, opts) + end + diffs._test.compute_highlight_groups() + local bg_03 = set_calls.DiffsAdd.bg + + diffs._test.get_config().highlights.blend_alpha = 0.9 + diffs._test.compute_highlight_groups() + local bg_09 = set_calls.DiffsAdd.bg + + assert.is_not.equal(bg_03, bg_09) + + diffs._test.get_config().highlights.blend_alpha = saved_config_alpha + end) + it('retries once then stops when Normal.bg stays nil', function() vim.api.nvim_get_hl = function(ns, opts) if opts.name == 'Normal' then