From 53dd5d6325990d545ecf671b47bb48ff885aef67 Mon Sep 17 00:00:00 2001 From: Tristan Knight Date: Sun, 8 Mar 2026 01:49:26 +0000 Subject: [PATCH] fix(highlight): handle nil Normal.bg in blending logic (#175) Co-authored-by: Barrett Ruth --- .github/ISSUE_TEMPLATE/bug_report.yaml | 20 ++++++- lua/diffs/init.lua | 8 ++- spec/init_spec.lua | 73 ++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 3 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.yaml b/.github/ISSUE_TEMPLATE/bug_report.yaml index 001e042..798b7a9 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yaml +++ b/.github/ISSUE_TEMPLATE/bug_report.yaml @@ -68,10 +68,26 @@ body: load(vim.fn.system('curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua'))() require('lazy.nvim').setup({ spec = { - 'tpope/vim-fugitive', + { 'barrettruth/midnight.nvim', lazy = false, config = function() vim.cmd.colorscheme('midnight') end }, + { 'tpope/vim-fugitive' }, + { 'NeogitOrg/neogit', dependencies = { 'nvim-lua/plenary.nvim' } }, + { 'lewis6991/gitsigns.nvim', config = true }, + { 'rhysd/committia.vim' }, + { 'nvim-telescope/telescope.nvim', dependencies = { 'nvim-lua/plenary.nvim' } }, { 'barrettruth/diffs.nvim', - opts = {}, + init = function() + vim.g.diffs = { + debug = '/tmp/diffs.log', + integrations = { + fugitive = true, + neogit = true, + gitsigns = true, + committia = true, + telescope = true, + }, + } + end, }, }, }) diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index 048b669..2f8285c 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -510,7 +510,6 @@ local function compute_highlight_groups() if not normal.bg and not hl_retry_pending then hl_retry_pending = true vim.schedule(function() - hl_retry_pending = false compute_highlight_groups() for bufnr, _ in pairs(attached_buffers) do invalidate_cache(bufnr) @@ -1115,6 +1114,13 @@ M._test = { process_pending_clear = process_pending_clear, ft_retry_pending = ft_retry_pending, compute_hunk_context = compute_hunk_context, + compute_highlight_groups = compute_highlight_groups, + get_hl_retry_pending = function() + return hl_retry_pending + end, + set_hl_retry_pending = function(v) + hl_retry_pending = v + end, } return M diff --git a/spec/init_spec.lua b/spec/init_spec.lua index 5b564ae..09c0f24 100644 --- a/spec/init_spec.lua +++ b/spec/init_spec.lua @@ -527,4 +527,77 @@ describe('diffs', function() end) end) end) + + describe('compute_highlight_groups', function() + local saved_get_hl, saved_set_hl, saved_schedule + local set_calls, schedule_cbs + + before_each(function() + saved_get_hl = vim.api.nvim_get_hl + saved_set_hl = vim.api.nvim_set_hl + saved_schedule = vim.schedule + set_calls = {} + schedule_cbs = {} + vim.api.nvim_set_hl = function(_, group, opts) + set_calls[group] = opts + end + vim.schedule = function(cb) + table.insert(schedule_cbs, cb) + end + diffs._test.set_hl_retry_pending(false) + end) + + after_each(function() + vim.api.nvim_get_hl = saved_get_hl + vim.api.nvim_set_hl = saved_set_hl + vim.schedule = saved_schedule + diffs._test.set_hl_retry_pending(false) + end) + + it('sets DiffsClear.bg to a number when Normal.bg is nil', function() + vim.api.nvim_get_hl = function(ns, opts) + if opts.name == 'Normal' then + return { fg = 0xc0c0c0 } + end + return saved_get_hl(ns, opts) + end + diffs._test.compute_highlight_groups() + assert.is_number(set_calls.DiffsClear.bg) + assert.is_table(set_calls.DiffsAdd) + assert.is_table(set_calls.DiffsDelete) + 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 + return { fg = 0xc0c0c0 } + end + return saved_get_hl(ns, opts) + end + diffs._test.compute_highlight_groups() + assert.are.equal(1, #schedule_cbs) + schedule_cbs[1]() + assert.are.equal(1, #schedule_cbs) + assert.is_true(diffs._test.get_hl_retry_pending()) + end) + + it('picks up bg on retry when colorscheme loads late', function() + local call_count = 0 + vim.api.nvim_get_hl = function(ns, opts) + if opts.name == 'Normal' then + call_count = call_count + 1 + if call_count <= 1 then + return { fg = 0xc0c0c0 } + end + return { fg = 0xc0c0c0, bg = 0x1e1e2e } + end + return saved_get_hl(ns, opts) + end + diffs._test.compute_highlight_groups() + assert.are.equal(1, #schedule_cbs) + schedule_cbs[1]() + assert.is_number(set_calls.DiffsClear.bg) + assert.are.equal(1, #schedule_cbs) + end) + end) end)