fix(highlight): prevent duplicate extmarks from two-pass rendering (#145)

## Problem

Two-pass rendering (Pass 1: backgrounds + intra-line; Pass 2:
treesitter) caused Pass 2 to re-apply all extmarks that Pass 1 already
set, doubling the extmark count on affected lines.

## Solution

Add `syntax_only` mode to `highlight_hunk`. When `syntax_only = true`,
only treesitter syntax and content `DiffsClear` extmarks are applied —
backgrounds, intra-line, prefix clears, and per-char prefix highlights
are skipped. Pass 2 now uses `syntax_only = true` and no longer calls
`nvim_buf_clear_namespace`, so Pass 1's extmarks persist while Pass 2
layers syntax on top.

Closes #143
This commit is contained in:
Barrett Ruth 2026-03-05 09:16:04 -05:00 committed by GitHub
parent 7106bcc291
commit 90b312e8df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 190 additions and 69 deletions

View file

@ -797,18 +797,13 @@ local function init()
return
end
local t1 = config.debug and vim.uv.hrtime() or nil
local full_opts = {
local syntax_opts = {
hide_prefix = config.hide_prefix,
highlights = config.highlights,
syntax_only = true,
}
for _, hunk in ipairs(deferred_syntax) do
local start_row = hunk.start_line - 1
local end_row = hunk.start_line + #hunk.lines
if hunk.header_start_line then
start_row = hunk.header_start_line - 1
end
vim.api.nvim_buf_clear_namespace(bufnr, ns, start_row, end_row)
highlight.highlight_hunk(bufnr, ns, hunk, full_opts)
highlight.highlight_hunk(bufnr, ns, hunk, syntax_opts)
end
if t1 then
dbg('deferred pass: %d hunks in %.2fms', #deferred_syntax, (vim.uv.hrtime() - t1) / 1e6)