fix: clear stale gutter extmarks after fugitive section toggle

Problem: Repeatedly toggling `=` in fugitive left green gutter
extmarks on lines between sections. Two issues combined:
(1) carry_forward_highlighted returned an empty table (truthy in
Lua) when zero hunks matched, so pending_clear stayed false and
compressed extmarks from deleted diff lines were never cleared.
(2) The namespace clear in on_buf's pending_clear path was removed
in 2feb8a8, so even when pending_clear was true the extmarks
survived.

Solution: Return nil from carry_forward_highlighted when no hunks
were carried forward, so pending_clear is correctly set to true.
Restore nvim_buf_clear_namespace in on_buf's pending_clear block.
Add process_pending_clear test helper and spec coverage.
This commit is contained in:
Barrett Ruth 2026-02-25 13:16:26 -05:00
parent 6040c054cb
commit 330b3fdc28
2 changed files with 40 additions and 1 deletions

View file

@ -288,6 +288,9 @@ local function carry_forward_highlighted(old_entry, new_hunks)
old_n,
new_n
)
if next(highlighted) == nil then
return nil
end
return highlighted
end
@ -729,6 +732,7 @@ local function init()
ensure_cache(bufnr)
local entry = hunk_cache[bufnr]
if entry and entry.pending_clear then
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
entry.highlighted = {}
entry.pending_clear = false
end
@ -927,12 +931,22 @@ function M.get_conflict_config()
return config.conflict
end
local function process_pending_clear(bufnr)
local entry = hunk_cache[bufnr]
if entry and entry.pending_clear then
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
entry.highlighted = {}
entry.pending_clear = false
end
end
M._test = {
find_visible_hunks = find_visible_hunks,
hunk_cache = hunk_cache,
ensure_cache = ensure_cache,
invalidate_cache = invalidate_cache,
hunks_eq = hunks_eq,
process_pending_clear = process_pending_clear,
}
return M

View file

@ -122,6 +122,31 @@ describe('decoration_provider', function()
assert.is_true(entry.pending_clear)
delete_buffer(bufnr)
end)
it('clears namespace extmarks when on_buf processes pending_clear', function()
local bufnr = create_buffer({
'M test.lua',
'@@ -1,1 +1,2 @@',
' local x = 1',
'+local y = 2',
})
diffs.attach(bufnr)
local ns_id = vim.api.nvim_create_namespace('diffs')
vim.api.nvim_buf_set_extmark(bufnr, ns_id, 0, 0, { line_hl_group = 'DiffAdd' })
assert.are.equal(1, #vim.api.nvim_buf_get_extmarks(bufnr, ns_id, 0, -1, {}))
diffs._test.invalidate_cache(bufnr)
diffs._test.ensure_cache(bufnr)
local entry = diffs._test.hunk_cache[bufnr]
assert.is_true(entry.pending_clear)
diffs._test.process_pending_clear(bufnr)
entry = diffs._test.hunk_cache[bufnr]
assert.is_false(entry.pending_clear)
assert.are.same({}, vim.api.nvim_buf_get_extmarks(bufnr, ns_id, 0, -1, {}))
delete_buffer(bufnr)
end)
end)
describe('BufWipeout cleanup', function()
@ -262,7 +287,7 @@ describe('decoration_provider', function()
local updated = diffs._test.hunk_cache[bufnr]
assert.is_nil(updated.highlighted[1])
assert.is_false(updated.pending_clear)
assert.is_true(updated.pending_clear)
delete_buffer(bufnr)
end)
end)