fix(highlight,parser,init): line_hl_group, clear_end, did_filetype, email-quoted diffs

Problem: line backgrounds used `hl_group + end_row` multirow extmarks
vulnerable to adjacent `clear_namespace`. `clear_end` was off by one.
`vim.filetype.match` returned nil for function-handled extensions
(`.sh`, `.bash`, etc.) when `did_filetype() != 0`. Parser didn't
handle email-quoted diff prefixes (`> `).

Solution: use `line_hl_group` single-point extmarks. Fix `clear_end`
to `hunk.start_line + #hunk.lines`. Override `vim.fn.did_filetype`
via `rawset` during retry. Strip `> ` quote prefixes in parser and
store `quote_width` per hunk.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Barrett Ruth 2026-03-04 17:29:19 -05:00
parent 0f0acacf96
commit 5028f9cc0a
Signed by: barrett
GPG key ID: A6C96C9349D2FC81
9 changed files with 552 additions and 52 deletions

238
spec/email_quote_spec.lua Normal file
View file

@ -0,0 +1,238 @@
require('spec.helpers')
local parser = require('diffs.parser')
local highlight = require('diffs.highlight')
local function create_buffer(lines)
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
return bufnr
end
local function delete_buffer(bufnr)
if vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end
local function get_extmarks(bufnr, ns)
return vim.api.nvim_buf_get_extmarks(bufnr, ns, 0, -1, { details = true })
end
local function highlight_opts()
return {
hide_prefix = false,
highlights = {
background = true,
gutter = false,
context = { enabled = false, lines = 0 },
treesitter = { enabled = true, max_lines = 500 },
vim = { enabled = false, max_lines = 200 },
intra = { enabled = false, algorithm = 'default', max_lines = 500 },
priorities = { clear = 198, syntax = 199, line_bg = 200, char_bg = 201 },
},
}
end
describe('parser email-quoted diffs', function()
it('parses a fully email-quoted unified diff', function()
local bufnr = create_buffer({
'> diff --git a/foo.py b/foo.py',
'> index abc1234..def5678 100644',
'> --- a/foo.py',
'> +++ b/foo.py',
'> @@ -0,0 +1,3 @@',
'> +from typing import Annotated, final',
'> +',
'> +class Foo:',
})
local hunks = parser.parse_buffer(bufnr)
assert.are.equal(1, #hunks)
assert.are.equal('foo.py', hunks[1].filename)
assert.are.equal(3, #hunks[1].lines)
assert.are.equal('+from typing import Annotated, final', hunks[1].lines[1])
assert.are.equal(2, hunks[1].quote_width)
delete_buffer(bufnr)
end)
it('parses a quoted diff embedded in an email reply', function()
local bufnr = create_buffer({
'Looks good, one nit:',
'',
'> diff --git a/foo.py b/foo.py',
'> @@ -0,0 +1,3 @@',
'> +from typing import Annotated, final',
'> +',
'> +class Foo:',
'',
'Maybe rename Foo to Bar?',
})
local hunks = parser.parse_buffer(bufnr)
assert.are.equal(1, #hunks)
assert.are.equal('foo.py', hunks[1].filename)
assert.are.equal(3, #hunks[1].lines)
assert.are.equal(2, hunks[1].quote_width)
delete_buffer(bufnr)
end)
it('sets quote_width = 0 on normal (unquoted) diffs', function()
local bufnr = create_buffer({
'diff --git a/bar.lua b/bar.lua',
'@@ -1,2 +1,2 @@',
'-old_line',
'+new_line',
})
local hunks = parser.parse_buffer(bufnr)
assert.are.equal(1, #hunks)
assert.are.equal(0, hunks[1].quote_width)
delete_buffer(bufnr)
end)
it('treats bare > lines as empty quoted lines', function()
local bufnr = create_buffer({
'> diff --git a/foo.py b/foo.py',
'> @@ -1,3 +1,3 @@',
'> -old',
'>',
'> +new',
})
local hunks = parser.parse_buffer(bufnr)
assert.are.equal(1, #hunks)
assert.are.equal(3, #hunks[1].lines)
assert.are.equal('-old', hunks[1].lines[1])
assert.are.equal(' ', hunks[1].lines[2])
assert.are.equal('+new', hunks[1].lines[3])
delete_buffer(bufnr)
end)
it('adjusts header_context_col for quote width', function()
local bufnr = create_buffer({
'> diff --git a/foo.py b/foo.py',
'> @@ -1,2 +1,2 @@ def hello():',
'> -old',
'> +new',
})
local hunks = parser.parse_buffer(bufnr)
assert.are.equal(1, #hunks)
assert.are.equal('def hello():', hunks[1].header_context)
assert.are.equal(#'@@ -1,2 +1,2 @@ ' + 2, hunks[1].header_context_col)
delete_buffer(bufnr)
end)
it('handles deeply nested quotes', function()
local bufnr = create_buffer({
'>> diff --git a/foo.py b/foo.py',
'>> @@ -0,0 +1,2 @@',
'>> +line1',
'>> +line2',
})
local hunks = parser.parse_buffer(bufnr)
assert.are.equal(1, #hunks)
assert.are.equal(3, hunks[1].quote_width)
assert.are.equal('+line1', hunks[1].lines[1])
delete_buffer(bufnr)
end)
end)
describe('email-quoted header highlight suppression', function()
before_each(function()
vim.api.nvim_set_hl(0, 'DiffsClear', { fg = 0xc0c0c0, bg = 0x1e1e2e })
vim.api.nvim_set_hl(0, 'DiffsAdd', { bg = 0x2e4a3a })
vim.api.nvim_set_hl(0, 'DiffsDelete', { bg = 0x4a2e3a })
end)
it('applies DiffsClear to header lines when quote_width > 0', function()
local bufnr = create_buffer({
'> diff --git a/foo.py b/foo.py',
'> index abc1234..def5678 100644',
'> --- a/foo.py',
'> +++ b/foo.py',
'> @@ -0,0 +1,2 @@',
'> +line1',
'> +line2',
})
local hunks = parser.parse_buffer(bufnr)
assert.are.equal(1, #hunks)
local ns = vim.api.nvim_create_namespace('diffs_email_clear_test')
highlight.highlight_hunk(bufnr, ns, hunks[1], highlight_opts())
local extmarks = get_extmarks(bufnr, ns)
local clear_lines = {}
for _, mark in ipairs(extmarks) do
local d = mark[4]
if d and d.hl_group == 'DiffsClear' and mark[3] == 0 then
clear_lines[mark[2]] = true
end
end
assert.is_true(clear_lines[0] ~= nil, 'expected DiffsClear on diff --git line')
assert.is_true(clear_lines[1] ~= nil, 'expected DiffsClear on index line')
assert.is_true(clear_lines[2] ~= nil, 'expected DiffsClear on --- line')
assert.is_true(clear_lines[3] ~= nil, 'expected DiffsClear on +++ line')
delete_buffer(bufnr)
end)
it('applies DiffsClear and diff treesitter to @@ line when quote_width > 0', function()
local bufnr = create_buffer({
'> diff --git a/foo.py b/foo.py',
'> @@ -0,0 +1,2 @@',
'> +line1',
'> +line2',
})
local hunks = parser.parse_buffer(bufnr)
assert.are.equal(1, #hunks)
local ns = vim.api.nvim_create_namespace('diffs_email_at_test')
highlight.highlight_hunk(bufnr, ns, hunks[1], highlight_opts())
local extmarks = get_extmarks(bufnr, ns)
local has_at_clear = false
local has_at_ts = false
for _, mark in ipairs(extmarks) do
local d = mark[4]
if mark[2] == 1 and d then
if d.hl_group == 'DiffsClear' and mark[3] == 0 then
has_at_clear = true
end
if d.hl_group and d.hl_group:match('^@.*%.diff$') and d.priority == 199 then
has_at_ts = true
end
end
end
assert.is_true(has_at_clear, 'expected DiffsClear on @@ line')
assert.is_true(has_at_ts, 'expected diff treesitter capture on @@ line')
delete_buffer(bufnr)
end)
it('does not apply DiffsClear to header lines when quote_width = 0', function()
local bufnr = create_buffer({
'diff --git a/foo.py b/foo.py',
'index abc1234..def5678 100644',
'--- a/foo.py',
'+++ b/foo.py',
'@@ -0,0 +1,2 @@',
'+line1',
'+line2',
})
local hunks = parser.parse_buffer(bufnr)
assert.are.equal(1, #hunks)
local ns = vim.api.nvim_create_namespace('diffs_email_noclear_test')
highlight.highlight_hunk(bufnr, ns, hunks[1], highlight_opts())
local extmarks = get_extmarks(bufnr, ns)
for _, mark in ipairs(extmarks) do
local d = mark[4]
if d and d.hl_group == 'DiffsClear' and mark[3] == 0 and mark[2] < 5 then
error('unexpected DiffsClear at col 0 on header line ' .. mark[2] .. ' with quote_width=0')
end
end
delete_buffer(bufnr)
end)
end)

View file

@ -287,7 +287,7 @@ describe('highlight', function()
local extmarks = get_extmarks(bufnr)
local has_diff_add = false
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].hl_group == 'DiffsAdd' then
if mark[4] and mark[4].line_hl_group == 'DiffsAdd' then
has_diff_add = true
break
end
@ -320,7 +320,7 @@ describe('highlight', function()
local extmarks = get_extmarks(bufnr)
local has_diff_delete = false
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].hl_group == 'DiffsDelete' then
if mark[4] and mark[4].line_hl_group == 'DiffsDelete' then
has_diff_delete = true
break
end
@ -362,6 +362,114 @@ describe('highlight', function()
delete_buffer(bufnr)
end)
it('line bg uses line_hl_group not hl_group with end_row', function()
local bufnr = create_buffer({
'@@ -1,1 +1,2 @@',
' local x = 1',
'+local y = 2',
})
local hunk = {
filename = 'test.lua',
lang = 'lua',
start_line = 1,
lines = { ' local x = 1', '+local y = 2' },
}
highlight.highlight_hunk(
bufnr,
ns,
hunk,
default_opts({ highlights = { background = true } })
)
local extmarks = get_extmarks(bufnr)
for _, mark in ipairs(extmarks) do
local d = mark[4]
assert.is_not_equal('DiffsAdd', d and d.hl_group)
assert.is_not_equal('DiffsDelete', d and d.hl_group)
end
delete_buffer(bufnr)
end)
it('line bg extmark survives adjacent clear_namespace starting at next row', function()
local bufnr = create_buffer({
'diff --git a/foo.py b/foo.py',
'@@ -1,2 +1,2 @@',
'-old',
'+new',
})
local hunk = {
filename = 'foo.py',
header_start_line = 1,
start_line = 2,
lines = { '-old', '+new' },
prefix_width = 1,
quote_width = 0,
}
highlight.highlight_hunk(
bufnr,
ns,
hunk,
default_opts({ highlights = { background = true, treesitter = { enabled = false } } })
)
local last_body_row = hunk.start_line + #hunk.lines - 1
vim.api.nvim_buf_clear_namespace(bufnr, ns, last_body_row + 1, last_body_row + 10)
local marks =
vim.api.nvim_buf_get_extmarks(bufnr, ns, { last_body_row, 0 }, { last_body_row, -1 }, { details = true })
local has_line_bg = false
for _, mark in ipairs(marks) do
if mark[4] and mark[4].line_hl_group == 'DiffsAdd' then
has_line_bg = true
end
end
assert.is_true(has_line_bg)
delete_buffer(bufnr)
end)
it('clear range covers last body line of hunk with header', function()
local bufnr = create_buffer({
'diff --git a/foo.py b/foo.py',
'index abc..def 100644',
'--- a/foo.py',
'+++ b/foo.py',
'@@ -1,3 +1,3 @@',
' ctx',
'-old',
'+new',
})
local hunk = {
filename = 'foo.py',
header_start_line = 1,
start_line = 5,
lines = { ' ctx', '-old', '+new' },
prefix_width = 1,
quote_width = 0,
}
highlight.highlight_hunk(
bufnr,
ns,
hunk,
default_opts({ highlights = { background = true, treesitter = { enabled = false } } })
)
local last_body_row = hunk.start_line + #hunk.lines - 1
local clear_start = hunk.header_start_line - 1
local clear_end = hunk.start_line + #hunk.lines
vim.api.nvim_buf_clear_namespace(bufnr, ns, clear_start, clear_end)
local marks =
vim.api.nvim_buf_get_extmarks(bufnr, ns, { last_body_row, 0 }, { last_body_row, -1 }, { details = false })
assert.are.equal(0, #marks)
delete_buffer(bufnr)
end)
it('still applies background when treesitter disabled', function()
local bufnr = create_buffer({
'@@ -1,1 +1,2 @@',
@ -386,7 +494,7 @@ describe('highlight', function()
local extmarks = get_extmarks(bufnr)
local has_diff_add = false
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].hl_group == 'DiffsAdd' then
if mark[4] and mark[4].line_hl_group == 'DiffsAdd' then
has_diff_add = true
break
end
@ -500,7 +608,7 @@ describe('highlight', function()
local extmarks = get_extmarks(bufnr)
local has_diff_add = false
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].hl_group == 'DiffsAdd' then
if mark[4] and mark[4].line_hl_group == 'DiffsAdd' then
has_diff_add = true
break
end
@ -585,7 +693,7 @@ describe('highlight', function()
local found = false
for _, mark in ipairs(extmarks) do
local d = mark[4]
if d and (d.hl_group == 'DiffsAdd' or d.hl_group == 'DiffsDelete') and d.hl_eol then
if d and (d.line_hl_group == 'DiffsAdd' or d.line_hl_group == 'DiffsDelete') then
found = true
end
end
@ -741,7 +849,7 @@ describe('highlight', function()
if d then
if d.hl_group == 'DiffsClear' then
table.insert(priorities.clear, d.priority)
elseif (d.hl_group == 'DiffsAdd' or d.hl_group == 'DiffsDelete') and d.hl_eol then
elseif (d.line_hl_group == 'DiffsAdd' or d.line_hl_group == 'DiffsDelete') then
table.insert(priorities.line_bg, d.priority)
elseif d.hl_group == 'DiffsAddText' or d.hl_group == 'DiffsDeleteText' then
table.insert(priorities.char_bg, d.priority)
@ -842,8 +950,8 @@ describe('highlight', function()
local line_bgs = {}
for _, mark in ipairs(extmarks) do
local d = mark[4]
if d and (d.hl_group == 'DiffsAdd' or d.hl_group == 'DiffsDelete') and d.hl_eol then
line_bgs[mark[2]] = d.hl_group
if d and (d.line_hl_group == 'DiffsAdd' or d.line_hl_group == 'DiffsDelete') then
line_bgs[mark[2]] = d.line_hl_group
end
end
assert.is_nil(line_bgs[1])
@ -1034,8 +1142,8 @@ describe('highlight', function()
local marker_text = {}
for _, mark in ipairs(extmarks) do
local d = mark[4]
if d and (d.hl_group == 'DiffsAdd' or d.hl_group == 'DiffsDelete') and d.hl_eol then
line_bgs[mark[2]] = d.hl_group
if d and (d.line_hl_group == 'DiffsAdd' or d.line_hl_group == 'DiffsDelete') then
line_bgs[mark[2]] = d.line_hl_group
end
if d and d.number_hl_group then
gutter_hls[mark[2]] = d.number_hl_group

View file

@ -127,6 +127,108 @@ describe('integration', function()
end)
end)
describe('ft_retry_pending', function()
before_each(function()
rawset(vim.fn, 'did_filetype', function() return 1 end)
require('diffs.parser')._test.ft_lang_cache = {}
end)
after_each(function()
rawset(vim.fn, 'did_filetype', nil)
end)
it('sets ft_retry_pending when nil-ft hunks detected under did_filetype', function()
local bufnr = create_buffer({
'diff --git a/app.conf b/app.conf',
'@@ -1,2 +1,2 @@',
' server {',
'- listen 80;',
'+ listen 8080;',
})
diffs.attach(bufnr)
local entry = diffs._test.hunk_cache[bufnr]
assert.is_not_nil(entry)
assert.is_nil(entry.hunks[1].ft)
assert.is_true(diffs._test.ft_retry_pending[bufnr] == true)
delete_buffer(bufnr)
end)
it('clears ft_retry_pending after scheduled callback fires', function()
local bufnr = create_buffer({
'diff --git a/app.conf b/app.conf',
'@@ -1,2 +1,2 @@',
' server {',
'- listen 80;',
'+ listen 8080;',
})
diffs.attach(bufnr)
assert.is_true(diffs._test.ft_retry_pending[bufnr] == true)
local done = false
vim.schedule(function()
done = true
end)
vim.wait(1000, function()
return done
end)
assert.is_nil(diffs._test.ft_retry_pending[bufnr])
delete_buffer(bufnr)
end)
it('invalidates cache after scheduled callback fires', function()
local bufnr = create_buffer({
'diff --git a/app.conf b/app.conf',
'@@ -1,2 +1,2 @@',
' server {',
'- listen 80;',
'+ listen 8080;',
})
diffs.attach(bufnr)
local tick_after_attach = diffs._test.hunk_cache[bufnr].tick
assert.is_true(tick_after_attach >= 0)
local done = false
vim.schedule(function()
done = true
end)
vim.wait(1000, function()
return done
end)
local entry = diffs._test.hunk_cache[bufnr]
assert.are.equal(-1, entry.tick)
assert.is_true(entry.pending_clear)
delete_buffer(bufnr)
end)
it('does not set ft_retry_pending when did_filetype() is zero', function()
rawset(vim.fn, 'did_filetype', nil)
local bufnr = create_buffer({
'diff --git a/test.sh b/test.sh',
'@@ -1,2 +1,3 @@',
' #!/usr/bin/env bash',
'-old line',
'+new line',
})
diffs.attach(bufnr)
assert.is_falsy(diffs._test.ft_retry_pending[bufnr])
delete_buffer(bufnr)
end)
it('does not set ft_retry_pending for files with resolvable ft', function()
local bufnr = create_buffer({
'M test.lua',
'@@ -1,1 +1,2 @@',
' local x = 1',
'+local y = 2',
})
diffs.attach(bufnr)
assert.is_falsy(diffs._test.ft_retry_pending[bufnr])
delete_buffer(bufnr)
end)
end)
describe('extmarks from highlight pipeline', function()
it('DiffsAdd background applied to + lines', function()
local bufnr = create_buffer({
@ -145,7 +247,7 @@ describe('integration', function()
local extmarks = get_extmarks(bufnr, ns)
local has_diff_add = false
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].hl_group == 'DiffsAdd' then
if mark[4] and mark[4].line_hl_group == 'DiffsAdd' then
has_diff_add = true
break
end
@ -171,7 +273,7 @@ describe('integration', function()
local extmarks = get_extmarks(bufnr, ns)
local has_diff_delete = false
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].hl_group == 'DiffsDelete' then
if mark[4] and mark[4].line_hl_group == 'DiffsDelete' then
has_diff_delete = true
break
end
@ -198,10 +300,10 @@ describe('integration', function()
local has_add = false
local has_delete = false
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].hl_group == 'DiffsAdd' then
if mark[4] and mark[4].line_hl_group == 'DiffsAdd' then
has_add = true
end
if mark[4] and mark[4].hl_group == 'DiffsDelete' then
if mark[4] and mark[4].line_hl_group == 'DiffsDelete' then
has_delete = true
end
end
@ -230,8 +332,8 @@ describe('integration', function()
local line_bgs = {}
for _, mark in ipairs(extmarks) do
local d = mark[4]
if d and (d.hl_group == 'DiffsAdd' or d.hl_group == 'DiffsDelete') and d.hl_eol then
line_bgs[mark[2]] = d.hl_group
if d and (d.line_hl_group == 'DiffsAdd' or d.line_hl_group == 'DiffsDelete') then
line_bgs[mark[2]] = d.line_hl_group
end
end
assert.is_nil(line_bgs[1])
@ -313,10 +415,10 @@ describe('integration', function()
local del_lines = {}
for _, mark in ipairs(extmarks) do
local d = mark[4]
if d and d.hl_group == 'DiffsAdd' and d.hl_eol then
if d and d.line_hl_group == 'DiffsAdd' then
add_lines[mark[2]] = true
end
if d and d.hl_group == 'DiffsDelete' and d.hl_eol then
if d and d.line_hl_group == 'DiffsDelete' then
del_lines[mark[2]] = true
end
end

View file

@ -391,6 +391,27 @@ describe('parser', function()
vim.fn.delete(repo_root, 'rf')
end)
it('detects filetype for .sh files when did_filetype() is non-zero', function()
rawset(vim.fn, 'did_filetype', function() return 1 end)
parser._test.ft_lang_cache = {}
local bufnr = create_buffer({
'diff --git a/test.sh b/test.sh',
'@@ -1,3 +1,4 @@',
' #!/usr/bin/env bash',
' set -euo pipefail',
'-echo "running tests..."',
'+echo "running tests with coverage..."',
})
local hunks = parser.parse_buffer(bufnr)
assert.are.equal(1, #hunks)
assert.are.equal('test.sh', hunks[1].filename)
assert.are.equal('sh', hunks[1].ft)
delete_buffer(bufnr)
rawset(vim.fn, 'did_filetype', nil)
end)
it('extracts file line numbers from @@ header', function()
local bufnr = create_buffer({
'M lua/test.lua',