fix(highlight): prevent duplicate extmarks from two-pass rendering
Problem: the deferred pass (pass 2) cleared the hunk range and called `highlight_hunk` with full opts, reapplying line backgrounds, intra-line diffs, prefix DiffsClear, and per-char prefix highlights that pass 1 already set. This doubled extmark counts on affected lines. Solution: add `syntax_only` flag to `diffs.HunkOpts`. When set, `highlight_hunk` skips non-syntax extmarks (line backgrounds, intra-line, prefix DiffsClear, per-char prefix fg, hide_prefix overlay, conflict markers). The deferred pass uses `syntax_only = true` and no longer clears the namespace range, so pass 1's extmarks persist while pass 2 layers treesitter and content DiffsClear on top.
This commit is contained in:
parent
91a856443f
commit
a26e9a522c
3 changed files with 200 additions and 79 deletions
|
|
@ -65,6 +65,7 @@ end
|
|||
---@field hide_prefix boolean
|
||||
---@field highlights diffs.Highlights
|
||||
---@field defer_vim_syntax? boolean
|
||||
---@field syntax_only? boolean
|
||||
|
||||
---@param bufnr integer
|
||||
---@param ns integer
|
||||
|
|
@ -398,7 +399,13 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
|||
---@type diffs.IntraChanges?
|
||||
local intra = nil
|
||||
local intra_cfg = opts.highlights.intra
|
||||
if intra_cfg and intra_cfg.enabled and pw == 1 and #hunk.lines <= intra_cfg.max_lines then
|
||||
if
|
||||
not opts.syntax_only
|
||||
and intra_cfg
|
||||
and intra_cfg.enabled
|
||||
and pw == 1
|
||||
and #hunk.lines <= intra_cfg.max_lines
|
||||
then
|
||||
dbg('computing intra for hunk %s:%d (%d lines)', hunk.filename, hunk.start_line, #hunk.lines)
|
||||
intra = diff.compute_intra_hunks(hunk.lines, intra_cfg.algorithm)
|
||||
if intra then
|
||||
|
|
@ -513,37 +520,80 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
|||
or content:match('^|||||||')
|
||||
end
|
||||
|
||||
if opts.hide_prefix then
|
||||
local virt_hl = (opts.highlights.background and line_hl) or nil
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||
virt_text = { { string.rep(' ', pw + qw), virt_hl } },
|
||||
virt_text_pos = 'overlay',
|
||||
})
|
||||
end
|
||||
if not opts.syntax_only then
|
||||
if opts.hide_prefix then
|
||||
local virt_hl = (opts.highlights.background and line_hl) or nil
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||
virt_text = { { string.rep(' ', pw + qw), virt_hl } },
|
||||
virt_text_pos = 'overlay',
|
||||
})
|
||||
end
|
||||
|
||||
if qw > 0 then
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||
end_col = pw + qw,
|
||||
hl_group = 'DiffsClear',
|
||||
priority = p.clear,
|
||||
})
|
||||
elseif pw > 1 then
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||
end_col = pw,
|
||||
hl_group = 'DiffsClear',
|
||||
priority = p.clear,
|
||||
})
|
||||
end
|
||||
if qw > 0 then
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||
end_col = pw + qw,
|
||||
hl_group = 'DiffsClear',
|
||||
priority = p.clear,
|
||||
})
|
||||
elseif pw > 1 then
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||
end_col = pw,
|
||||
hl_group = 'DiffsClear',
|
||||
priority = p.clear,
|
||||
})
|
||||
end
|
||||
|
||||
if pw > 1 then
|
||||
for ci = 0, pw - 1 do
|
||||
local ch = line:sub(ci + 1, ci + 1)
|
||||
if ch == '+' or ch == '-' then
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, ci + qw, {
|
||||
end_col = ci + qw + 1,
|
||||
hl_group = ch == '+' and '@diff.plus' or '@diff.minus',
|
||||
priority = p.syntax,
|
||||
})
|
||||
if pw > 1 then
|
||||
for ci = 0, pw - 1 do
|
||||
local ch = line:sub(ci + 1, ci + 1)
|
||||
if ch == '+' or ch == '-' then
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, ci + qw, {
|
||||
end_col = ci + qw + 1,
|
||||
hl_group = ch == '+' and '@diff.plus' or '@diff.minus',
|
||||
priority = p.syntax,
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if opts.highlights.background and is_diff_line then
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||
line_hl_group = line_hl,
|
||||
number_hl_group = opts.highlights.gutter and number_hl or nil,
|
||||
priority = p.line_bg,
|
||||
})
|
||||
end
|
||||
|
||||
if is_marker and line_len > pw then
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, pw + qw, {
|
||||
end_col = line_len + qw,
|
||||
hl_group = 'DiffsConflictMarker',
|
||||
priority = p.char_bg,
|
||||
})
|
||||
end
|
||||
|
||||
if char_spans_by_line[i] then
|
||||
local char_hl = has_add and 'DiffsAddText' or 'DiffsDeleteText'
|
||||
for _, span in ipairs(char_spans_by_line[i]) do
|
||||
dbg(
|
||||
'char extmark: line=%d buf_line=%d col=%d..%d hl=%s text="%s"',
|
||||
i,
|
||||
buf_line,
|
||||
span.col_start,
|
||||
span.col_end,
|
||||
char_hl,
|
||||
line:sub(span.col_start + 1, span.col_end)
|
||||
)
|
||||
local ok, err =
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, span.col_start + qw, {
|
||||
end_col = span.col_end + qw,
|
||||
hl_group = char_hl,
|
||||
priority = p.char_bg,
|
||||
})
|
||||
if not ok then
|
||||
dbg('char extmark FAILED: %s', err)
|
||||
end
|
||||
extmark_count = extmark_count + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -555,47 +605,6 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
|||
priority = p.clear,
|
||||
})
|
||||
end
|
||||
|
||||
if opts.highlights.background and is_diff_line then
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||
line_hl_group = line_hl,
|
||||
number_hl_group = opts.highlights.gutter and number_hl or nil,
|
||||
priority = p.line_bg,
|
||||
})
|
||||
end
|
||||
|
||||
if is_marker and line_len > pw then
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, pw + qw, {
|
||||
end_col = line_len + qw,
|
||||
hl_group = 'DiffsConflictMarker',
|
||||
priority = p.char_bg,
|
||||
})
|
||||
end
|
||||
|
||||
if char_spans_by_line[i] then
|
||||
local char_hl = has_add and 'DiffsAddText' or 'DiffsDeleteText'
|
||||
for _, span in ipairs(char_spans_by_line[i]) do
|
||||
dbg(
|
||||
'char extmark: line=%d buf_line=%d col=%d..%d hl=%s text="%s"',
|
||||
i,
|
||||
buf_line,
|
||||
span.col_start,
|
||||
span.col_end,
|
||||
char_hl,
|
||||
line:sub(span.col_start + 1, span.col_end)
|
||||
)
|
||||
local ok, err =
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, span.col_start + qw, {
|
||||
end_col = span.col_end + qw,
|
||||
hl_group = char_hl,
|
||||
priority = p.char_bg,
|
||||
})
|
||||
if not ok then
|
||||
dbg('char extmark FAILED: %s', err)
|
||||
end
|
||||
extmark_count = extmark_count + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
dbg('hunk %s:%d applied %d extmarks', hunk.filename, hunk.start_line, extmark_count)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue