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:
parent
7106bcc291
commit
90b312e8df
3 changed files with 190 additions and 69 deletions
|
|
@ -65,6 +65,7 @@ end
|
||||||
---@field hide_prefix boolean
|
---@field hide_prefix boolean
|
||||||
---@field highlights diffs.Highlights
|
---@field highlights diffs.Highlights
|
||||||
---@field defer_vim_syntax? boolean
|
---@field defer_vim_syntax? boolean
|
||||||
|
---@field syntax_only? boolean
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
---@param ns integer
|
---@param ns integer
|
||||||
|
|
@ -381,7 +382,13 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
||||||
---@type diffs.IntraChanges?
|
---@type diffs.IntraChanges?
|
||||||
local intra = nil
|
local intra = nil
|
||||||
local intra_cfg = opts.highlights.intra
|
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)
|
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)
|
intra = diff.compute_intra_hunks(hunk.lines, intra_cfg.algorithm)
|
||||||
if intra then
|
if intra then
|
||||||
|
|
@ -494,6 +501,7 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
||||||
or content:match('^|||||||')
|
or content:match('^|||||||')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not opts.syntax_only then
|
||||||
if opts.hide_prefix then
|
if opts.hide_prefix then
|
||||||
local virt_hl = (opts.highlights.background and line_hl) or nil
|
local virt_hl = (opts.highlights.background and line_hl) or nil
|
||||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||||
|
|
@ -520,14 +528,6 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if line_len > pw and covered_lines[buf_line] then
|
|
||||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, pw, {
|
|
||||||
end_col = line_len,
|
|
||||||
hl_group = 'DiffsClear',
|
|
||||||
priority = p.clear,
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
if opts.highlights.background and is_diff_line then
|
if opts.highlights.background and is_diff_line then
|
||||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||||
line_hl_group = line_hl,
|
line_hl_group = line_hl,
|
||||||
|
|
@ -569,6 +569,15 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if line_len > pw and covered_lines[buf_line] then
|
||||||
|
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, pw, {
|
||||||
|
end_col = line_len,
|
||||||
|
hl_group = 'DiffsClear',
|
||||||
|
priority = p.clear,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
dbg('hunk %s:%d applied %d extmarks', hunk.filename, hunk.start_line, extmark_count)
|
dbg('hunk %s:%d applied %d extmarks', hunk.filename, hunk.start_line, extmark_count)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -797,18 +797,13 @@ local function init()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local t1 = config.debug and vim.uv.hrtime() or nil
|
local t1 = config.debug and vim.uv.hrtime() or nil
|
||||||
local full_opts = {
|
local syntax_opts = {
|
||||||
hide_prefix = config.hide_prefix,
|
hide_prefix = config.hide_prefix,
|
||||||
highlights = config.highlights,
|
highlights = config.highlights,
|
||||||
|
syntax_only = true,
|
||||||
}
|
}
|
||||||
for _, hunk in ipairs(deferred_syntax) do
|
for _, hunk in ipairs(deferred_syntax) do
|
||||||
local start_row = hunk.start_line - 1
|
highlight.highlight_hunk(bufnr, ns, hunk, syntax_opts)
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
if t1 then
|
if t1 then
|
||||||
dbg('deferred pass: %d hunks in %.2fms', #deferred_syntax, (vim.uv.hrtime() - t1) / 1e6)
|
dbg('deferred pass: %d hunks in %.2fms', #deferred_syntax, (vim.uv.hrtime() - t1) / 1e6)
|
||||||
|
|
|
||||||
|
|
@ -1249,6 +1249,123 @@ describe('highlight', function()
|
||||||
end
|
end
|
||||||
delete_buffer(bufnr)
|
delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('two-pass rendering produces no duplicate extmarks', function()
|
||||||
|
vim.api.nvim_set_hl(0, 'DiffsAddText', { bg = 0x00FF00 })
|
||||||
|
vim.api.nvim_set_hl(0, 'DiffsDeleteText', { bg = 0xFF0000 })
|
||||||
|
vim.api.nvim_set_hl(0, 'DiffsAddNr', { fg = 0x80c080, bg = 0x2e4a3a })
|
||||||
|
vim.api.nvim_set_hl(0, 'DiffsDeleteNr', { fg = 0xc08080, bg = 0x4a2e3a })
|
||||||
|
|
||||||
|
local bufnr = create_buffer({
|
||||||
|
'@@ -1,2 +1,2 @@',
|
||||||
|
'-local x = 1',
|
||||||
|
'+local x = 2',
|
||||||
|
})
|
||||||
|
|
||||||
|
local hunk = {
|
||||||
|
filename = 'test.lua',
|
||||||
|
lang = 'lua',
|
||||||
|
start_line = 1,
|
||||||
|
lines = { '-local x = 1', '+local x = 2' },
|
||||||
|
}
|
||||||
|
|
||||||
|
local fast = default_opts({
|
||||||
|
highlights = {
|
||||||
|
treesitter = { enabled = false },
|
||||||
|
background = true,
|
||||||
|
gutter = true,
|
||||||
|
intra = { enabled = true, algorithm = 'default', max_lines = 500 },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
local syntax = default_opts({
|
||||||
|
highlights = {
|
||||||
|
treesitter = { enabled = true },
|
||||||
|
background = true,
|
||||||
|
gutter = true,
|
||||||
|
intra = { enabled = true, algorithm = 'default', max_lines = 500 },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
syntax.syntax_only = true
|
||||||
|
|
||||||
|
highlight.highlight_hunk(bufnr, ns, hunk, fast)
|
||||||
|
highlight.highlight_hunk(bufnr, ns, hunk, syntax)
|
||||||
|
|
||||||
|
local extmarks = get_extmarks(bufnr)
|
||||||
|
for row = 1, 2 do
|
||||||
|
local line_hl_count = 0
|
||||||
|
local number_hl_count = 0
|
||||||
|
local intra_count = 0
|
||||||
|
for _, mark in ipairs(extmarks) do
|
||||||
|
if mark[2] == row then
|
||||||
|
local d = mark[4]
|
||||||
|
if d.line_hl_group then
|
||||||
|
line_hl_count = line_hl_count + 1
|
||||||
|
end
|
||||||
|
if d.number_hl_group then
|
||||||
|
number_hl_count = number_hl_count + 1
|
||||||
|
end
|
||||||
|
if d.hl_group == 'DiffsAddText' or d.hl_group == 'DiffsDeleteText' then
|
||||||
|
intra_count = intra_count + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert.are.equal(1, line_hl_count, 'row ' .. row .. ' has duplicate line_hl_group')
|
||||||
|
assert.are.equal(1, number_hl_count, 'row ' .. row .. ' has duplicate number_hl_group')
|
||||||
|
assert.is_true(intra_count <= 1, 'row ' .. row .. ' has duplicate intra extmarks')
|
||||||
|
end
|
||||||
|
delete_buffer(bufnr)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('syntax_only pass adds treesitter without duplicating backgrounds', function()
|
||||||
|
local bufnr = create_buffer({
|
||||||
|
'@@ -1,2 +1,3 @@',
|
||||||
|
' local x = 1',
|
||||||
|
'+local y = 2',
|
||||||
|
' return x',
|
||||||
|
})
|
||||||
|
|
||||||
|
local hunk = {
|
||||||
|
filename = 'test.lua',
|
||||||
|
lang = 'lua',
|
||||||
|
start_line = 1,
|
||||||
|
lines = { ' local x = 1', '+local y = 2', ' return x' },
|
||||||
|
}
|
||||||
|
|
||||||
|
local fast = default_opts({
|
||||||
|
highlights = {
|
||||||
|
treesitter = { enabled = false },
|
||||||
|
background = true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
local syntax = default_opts({
|
||||||
|
highlights = {
|
||||||
|
treesitter = { enabled = true },
|
||||||
|
background = true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
syntax.syntax_only = true
|
||||||
|
|
||||||
|
highlight.highlight_hunk(bufnr, ns, hunk, fast)
|
||||||
|
highlight.highlight_hunk(bufnr, ns, hunk, syntax)
|
||||||
|
|
||||||
|
local extmarks = get_extmarks(bufnr)
|
||||||
|
local has_ts = false
|
||||||
|
local line_hl_count = 0
|
||||||
|
for _, mark in ipairs(extmarks) do
|
||||||
|
local d = mark[4]
|
||||||
|
if d and d.hl_group and d.hl_group:match('^@.*%.lua$') then
|
||||||
|
has_ts = true
|
||||||
|
end
|
||||||
|
if d and d.line_hl_group then
|
||||||
|
line_hl_count = line_hl_count + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert.is_true(has_ts)
|
||||||
|
assert.are.equal(1, line_hl_count)
|
||||||
|
delete_buffer(bufnr)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('diff header highlighting', function()
|
describe('diff header highlighting', function()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue