feat(parser,highlight): support email-quoted diffs (#141)

Problem: email-quoted diffs (`> diff --git ...`, `> @@ ...`) from
git-send-email workflows produce 0 hunks because the parser matches
patterns against raw lines containing `> ` quote prefixes.

Solution: strip quote prefix before pattern matching in the parser,
store `quote_width` on each hunk, offset all extmark column positions
by `qw` in `highlight.lua`, and expand `pw > 1` guards to
`qw > 0 or pw > 1` for DiffsClear suppression. Clamp body prefix
DiffsClear `end_col` to actual buffer line length for bare `>` lines
where `nvim_buf_set_extmark` would reject out-of-range columns.
This commit is contained in:
Barrett Ruth 2026-03-05 10:28:57 -05:00
parent 70d5bee797
commit a6433da5f4
Signed by: barrett
GPG key ID: A6C96C9349D2FC81
3 changed files with 577 additions and 63 deletions

View file

@ -245,7 +245,7 @@ local function highlight_vim_syntax(
pcall(vim.api.nvim_buf_delete, scratch, { force = true })
local hunk_line_count = #hunk.lines
local col_off = (hunk.prefix_width or 1) - 1
local col_off = (hunk.prefix_width or 1) + (hunk.quote_width or 0) - 1
local extmark_count = 0
for _, span in ipairs(spans) do
local adj = span.line - leading_offset
@ -273,6 +273,7 @@ end
function M.highlight_hunk(bufnr, ns, hunk, opts)
local p = opts.highlights.priorities
local pw = hunk.prefix_width or 1
local qw = hunk.quote_width or 0
local use_ts = hunk.lang and opts.highlights.treesitter.enabled
local use_vim = not use_ts and hunk.ft and opts.highlights.vim.enabled
@ -329,9 +330,9 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
end
extmark_count =
highlight_treesitter(bufnr, ns, new_code, hunk.lang, new_map, pw, covered_lines, p)
highlight_treesitter(bufnr, ns, new_code, hunk.lang, new_map, pw + qw, covered_lines, p)
extmark_count = extmark_count
+ highlight_treesitter(bufnr, ns, old_code, hunk.lang, old_map, pw, covered_lines, p)
+ highlight_treesitter(bufnr, ns, old_code, hunk.lang, old_map, pw + qw, covered_lines, p)
if hunk.header_context and hunk.header_context_col then
local header_extmarks = highlight_text(
@ -370,11 +371,11 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
header_map[i] = hunk.header_start_line - 1 + i
end
extmark_count = extmark_count
+ highlight_treesitter(bufnr, ns, hunk.header_lines, 'diff', header_map, 0, nil, p, pw > 1)
+ highlight_treesitter(bufnr, ns, hunk.header_lines, 'diff', header_map, qw, nil, p, qw > 0 or pw > 1)
end
local at_raw_line
if pw > 1 and opts.highlights.treesitter.enabled then
if (qw > 0 or pw > 1) and opts.highlights.treesitter.enabled then
local at_buf_line = hunk.start_line - 1
at_raw_line = vim.api.nvim_buf_get_lines(bufnr, at_buf_line, at_buf_line + 1, false)[1]
end
@ -420,7 +421,7 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
end
if
pw > 1
(qw > 0 or pw > 1)
and hunk.header_start_line
and hunk.header_lines
and #hunk.header_lines > 0
@ -429,35 +430,37 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
for i = 0, #hunk.header_lines - 1 do
local buf_line = hunk.header_start_line - 1 + i
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
end_col = #hunk.header_lines[i + 1],
end_col = #hunk.header_lines[i + 1] + qw,
hl_group = 'DiffsClear',
priority = p.clear,
})
local hline = hunk.header_lines[i + 1]
if hline:match('^index ') then
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
end_col = 5,
hl_group = '@keyword.diff',
priority = p.syntax,
})
local dot_pos = hline:find('%.%.', 1, false)
if dot_pos then
local rest = hline:sub(dot_pos + 2)
local hash = rest:match('^(%x+)')
if hash then
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, dot_pos + 1, {
end_col = dot_pos + 1 + #hash,
hl_group = '@constant.diff',
priority = p.syntax,
})
if pw > 1 then
local hline = hunk.header_lines[i + 1]
if hline:match('^index ') then
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, qw, {
end_col = 5 + qw,
hl_group = '@keyword.diff',
priority = p.syntax,
})
local dot_pos = hline:find('%.%.', 1, false)
if dot_pos then
local rest = hline:sub(dot_pos + 2)
local hash = rest:match('^(%x+)')
if hash then
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, dot_pos + 1 + qw, {
end_col = dot_pos + 1 + #hash + qw,
hl_group = '@constant.diff',
priority = p.syntax,
})
end
end
end
end
end
end
if pw > 1 and at_raw_line then
if (qw > 0 or pw > 1) and at_raw_line then
local at_buf_line = hunk.start_line - 1
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, at_buf_line, 0, {
end_col = #at_raw_line,
@ -465,7 +468,7 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
priority = p.clear,
})
if opts.highlights.treesitter.enabled then
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, at_buf_line, 0, {
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, at_buf_line, qw, {
end_col = #at_raw_line,
hl_group = '@attribute.diff',
priority = p.syntax,
@ -482,9 +485,16 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
})
end
local raw_body_lines
if qw > 0 then
raw_body_lines =
vim.api.nvim_buf_get_lines(bufnr, hunk.start_line, hunk.start_line + #hunk.lines, false)
end
for i, line in ipairs(hunk.lines) do
local buf_line = hunk.start_line + i - 1
local line_len = #line
local raw_len = raw_body_lines and #raw_body_lines[i] or nil
local prefix = line:sub(1, pw)
local has_add = prefix:find('+', 1, true) ~= nil
local has_del = prefix:find('-', 1, true) ~= nil
@ -505,22 +515,30 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
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), virt_hl } },
virt_text = { { string.rep(' ', pw + qw), virt_hl } },
virt_text_pos = 'overlay',
})
end
if pw > 1 then
if qw > 0 or pw > 1 then
local prefix_end = pw + qw
if raw_len and prefix_end > raw_len then
prefix_end = raw_len
end
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
end_col = pw,
end_col = prefix_end,
hl_group = 'DiffsClear',
priority = p.clear,
})
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, {
end_col = ci + 1,
local char_col = ci + qw
if raw_len and char_col >= raw_len then
break
end
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, char_col, {
end_col = char_col + 1,
hl_group = ch == '+' and '@diff.plus' or '@diff.minus',
priority = p.syntax,
})
@ -537,8 +555,8 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
end
if is_marker and line_len > pw then
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, pw, {
end_col = line_len,
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,
})
@ -556,8 +574,8 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
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, {
end_col = 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,
})
@ -570,8 +588,8 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
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,
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, pw + qw, {
end_col = line_len + qw,
hl_group = 'DiffsClear',
priority = p.clear,
})

View file

@ -13,6 +13,7 @@
---@field file_new_start integer?
---@field file_new_count integer?
---@field prefix_width integer
---@field quote_width integer
---@field repo_root string?
local M = {}
@ -135,6 +136,17 @@ function M.parse_buffer(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local repo_root = get_repo_root(bufnr)
local quote_prefix = nil
local quote_width = 0
for _, l in ipairs(lines) do
local qp = l:match('^(>+ )diff %-%-') or l:match('^(>+ )@@ %-')
if qp then
quote_prefix = qp
quote_width = #qp
break
end
end
---@type diffs.Hunk[]
local hunks = {}
@ -172,6 +184,7 @@ function M.parse_buffer(bufnr)
local old_remaining = nil
---@type integer?
local new_remaining = nil
local current_quote_width = 0
local function flush_hunk()
if hunk_start and #hunk_lines > 0 then
@ -184,6 +197,7 @@ function M.parse_buffer(bufnr)
header_context_col = hunk_header_context_col,
lines = hunk_lines,
prefix_width = hunk_prefix_width,
quote_width = current_quote_width,
file_old_start = file_old_start,
file_old_count = file_old_count,
file_new_start = file_new_start,
@ -209,19 +223,29 @@ function M.parse_buffer(bufnr)
end
for i, line in ipairs(lines) do
local diff_git_file = line:match('^diff %-%-git a/.+ b/(.+)$')
or line:match('^diff %-%-combined (.+)$')
or line:match('^diff %-%-cc (.+)$')
local neogit_file = line:match('^modified%s+(.+)$')
or (not line:match('^new file mode') and line:match('^new file%s+(.+)$'))
or (not line:match('^deleted file mode') and line:match('^deleted%s+(.+)$'))
or line:match('^renamed%s+(.+)$')
or line:match('^copied%s+(.+)$')
local bare_file = not hunk_start and line:match('^([^%s]+%.[^%s]+)$')
local filename = line:match('^[MADRCU%?!]%s+(.+)$') or diff_git_file or neogit_file or bare_file
local logical = line
if quote_prefix then
if line:sub(1, quote_width) == quote_prefix then
logical = line:sub(quote_width + 1)
elseif line:match('^>+$') then
logical = ''
end
end
local diff_git_file = logical:match('^diff %-%-git a/.+ b/(.+)$')
or logical:match('^diff %-%-combined (.+)$')
or logical:match('^diff %-%-cc (.+)$')
local neogit_file = logical:match('^modified%s+(.+)$')
or (not logical:match('^new file mode') and logical:match('^new file%s+(.+)$'))
or (not logical:match('^deleted file mode') and logical:match('^deleted%s+(.+)$'))
or logical:match('^renamed%s+(.+)$')
or logical:match('^copied%s+(.+)$')
local bare_file = not hunk_start and logical:match('^([^%s]+%.[^%s]+)$')
local filename = logical:match('^[MADRCU%?!]%s+(.+)$') or diff_git_file or neogit_file or bare_file
if filename then
flush_hunk()
current_filename = filename
current_quote_width = (logical ~= line) and quote_width or 0
local cache_key = (repo_root or '') .. '\0' .. filename
local cached = ft_lang_cache[cache_key]
if cached then
@ -243,13 +267,13 @@ function M.parse_buffer(bufnr)
hunk_prefix_width = 1
header_start = i
header_lines = {}
elseif line:match('^@@+') then
elseif logical:match('^@@+') then
flush_hunk()
hunk_start = i
local at_prefix = line:match('^(@@+)')
local at_prefix = logical:match('^(@@+)')
hunk_prefix_width = #at_prefix - 1
if #at_prefix == 2 then
local hs, hc, hs2, hc2 = line:match('^@@ %-(%d+),?(%d*) %+(%d+),?(%d*) @@')
local hs, hc, hs2, hc2 = logical:match('^@@ %-(%d+),?(%d*) %+(%d+),?(%d*) @@')
if hs then
file_old_start = tonumber(hs)
file_old_count = tonumber(hc) or 1
@ -259,31 +283,31 @@ function M.parse_buffer(bufnr)
new_remaining = file_new_count
end
else
local hs, hc = line:match('%-(%d+),?(%d*)')
local hs, hc = logical:match('%-(%d+),?(%d*)')
if hs then
file_old_start = tonumber(hs)
file_old_count = tonumber(hc) or 1
old_remaining = file_old_count
end
local hs2, hc2 = line:match('%+(%d+),?(%d*) @@')
local hs2, hc2 = logical:match('%+(%d+),?(%d*) @@')
if hs2 then
file_new_start = tonumber(hs2)
file_new_count = tonumber(hc2) or 1
new_remaining = file_new_count
end
end
local at_end, context = line:match('^(@@+.-@@+%s*)(.*)')
local at_end, context = logical:match('^(@@+.-@@+%s*)(.*)')
if context and context ~= '' then
hunk_header_context = context
hunk_header_context_col = #at_end
hunk_header_context_col = #at_end + current_quote_width
end
if hunk_count then
hunk_count = hunk_count + 1
end
elseif hunk_start then
local prefix = line:sub(1, 1)
local prefix = logical:sub(1, 1)
if prefix == ' ' or prefix == '+' or prefix == '-' then
table.insert(hunk_lines, line)
table.insert(hunk_lines, logical)
if old_remaining and (prefix == ' ' or prefix == '-') then
old_remaining = old_remaining - 1
end
@ -291,7 +315,7 @@ function M.parse_buffer(bufnr)
new_remaining = new_remaining - 1
end
elseif
line == ''
logical == ''
and old_remaining
and old_remaining > 0
and new_remaining
@ -301,11 +325,11 @@ function M.parse_buffer(bufnr)
old_remaining = old_remaining - 1
new_remaining = new_remaining - 1
elseif
line == ''
or line:match('^[MADRC%?!]%s+')
or line:match('^diff ')
or line:match('^index ')
or line:match('^Binary ')
logical == ''
or logical:match('^[MADRC%?!]%s+')
or logical:match('^diff ')
or logical:match('^index ')
or logical:match('^Binary ')
then
flush_hunk()
current_filename = nil
@ -315,7 +339,7 @@ function M.parse_buffer(bufnr)
end
end
if header_start and not hunk_start then
table.insert(header_lines, line)
table.insert(header_lines, logical)
end
end