fix: clear stale gutter extmarks after fugitive section toggle (#139)

## Problem

Repeatedly toggling `=` in fugitive left green gutter
(`number_hl_group`)
extmarks on lines between sections. When fugitive collapses a diff
section,
Neovim compresses extmarks from deleted lines onto the next surviving
line
(the `M ...` file entry). Two issues prevented cleanup:

1. `carry_forward_highlighted` returned `{}` (truthy in Lua) when zero
hunks
matched, so `pending_clear` stayed `false` and the compressed extmarks
   were never cleared.
2. The `nvim_buf_clear_namespace` call 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 (`next(highlighted) == nil`), 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:20:59 -05:00 committed by GitHub
parent 6040c054cb
commit 749a21ae3c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 2 deletions

View file

@ -247,7 +247,7 @@ end
---@param old_entry diffs.HunkCacheEntry
---@param new_hunks diffs.Hunk[]
---@return table<integer, true>
---@return table<integer, true>?
local function carry_forward_highlighted(old_entry, new_hunks)
local old_hunks = old_entry.hunks
local old_hl = old_entry.highlighted
@ -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)