fix(highlight): handle nil Normal.bg in blending logic (#175)

Co-authored-by: Barrett Ruth <br.barrettruth@gmail.com>
This commit is contained in:
Tristan Knight 2026-03-08 01:49:26 +00:00 committed by GitHub
parent 595c35d910
commit 53dd5d6325
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 98 additions and 3 deletions

View file

@ -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,
},
},
})

View file

@ -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

View file

@ -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)