Compare commits

...

8 commits

Author SHA1 Message Date
a26e9a522c 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.
2026-03-05 01:27:00 -05:00
91a856443f
ci: format 2026-03-05 01:10:25 -05:00
d2921c6fdd feat(highlight): support combined diff highlighting
Problem: Combined diffs (`pw > 1`) had no DiffsClear on headers
or `@@@`, no per-char prefix coloring, native TS junk on body
prefixes, and `force_high_priority` flattened all diff captures
to p199 making `@punctuation.special` (cyan) win over
`@diff.minus`/`@diff.plus` (red/green) on `---`/`+++` lines. The
`index` keyword and result hash after `..` were also uncolored.

Solution: Extend `qw > 0` guards to `qw > 0 or pw > 1` for
headers, `@@@`, and body prefixes. Add per-char `@diff.plus` /
`@diff.minus` at p199 on body prefix columns. Apply
`@attribute.diff` on `@@@` lines. Preserve relative priority
offsets in `highlight_treesitter` via `priorities.syntax +
meta_prio - 100`. Add manual `@keyword.diff` and `@constant.diff`
captures on combined `index` lines.
2026-03-05 01:06:51 -05:00
bb87ae279b feat(parser): support combined diff format
Problem: `parse_buffer` didn't recognize `diff --combined` /
`diff --cc` headers, couldn't parse `@@@` old-side ranges, and
synthesized empty body lines as single-space regardless of prefix
width.

Solution: Add `diff --combined` and `diff --cc` filename patterns,
parse old-side `-start,count` from `@@@` lines, remove
`is_unified_diff` guard on empty body lines, and use
`string.rep(' ', hunk_prefix_width)` for synthetic context lines.
2026-03-05 01:06:37 -05:00
38e0187998 ci: disable library file diagnostics in .luarc.json 2026-03-05 01:06:27 -05:00
194f29159a
fix: format 2026-03-04 17:33:31 -05:00
e5d9b4b7bd
ci: formatting 2026-03-04 17:32:55 -05:00
13c0ea1e92
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.
2026-03-04 17:30:25 -05:00
11 changed files with 1327 additions and 136 deletions

View file

@ -2,8 +2,14 @@
"runtime.version": "LuaJIT", "runtime.version": "LuaJIT",
"runtime.path": ["lua/?.lua", "lua/?/init.lua"], "runtime.path": ["lua/?.lua", "lua/?/init.lua"],
"diagnostics.globals": ["vim", "jit"], "diagnostics.globals": ["vim", "jit"],
"workspace.library": ["$VIMRUNTIME/lua", "${3rd}/luv/library", "${3rd}/busted/library", "${3rd}/luassert/library"], "workspace.library": [
"$VIMRUNTIME/lua",
"${3rd}/luv/library",
"${3rd}/busted/library",
"${3rd}/luassert/library"
],
"workspace.checkThirdParty": false, "workspace.checkThirdParty": false,
"diagnostics.libraryFiles": "Disable",
"workspace.ignoreDir": [".direnv"], "workspace.ignoreDir": [".direnv"],
"completion.callSnippet": "Replace" "completion.callSnippet": "Replace"
} }

1
.styluaignore Normal file
View file

@ -0,0 +1 @@
.direnv/

View file

@ -17,6 +17,8 @@ with language-aware syntax highlighting.
- `:Gdiff` unified diff against any revision - `:Gdiff` unified diff against any revision
- Background-only diff colors for `&diff` buffers - Background-only diff colors for `&diff` buffers
- Inline merge conflict detection, highlighting, and resolution - Inline merge conflict detection, highlighting, and resolution
- Email-quoted diff highlighting (`> diff ...` prefixes, arbitrary nesting
depth)
- Vim syntax fallback, configurable blend/priorities - Vim syntax fallback, configurable blend/priorities
## Requirements ## Requirements

View file

@ -13,7 +13,8 @@
... ...
}: }:
let let
forEachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system}); forEachSystem =
f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system});
in in
{ {
formatter = forEachSystem (pkgs: pkgs.nixfmt-tree); formatter = forEachSystem (pkgs: pkgs.nixfmt-tree);
@ -23,7 +24,12 @@
let let
ts-plugin = pkgs.vimPlugins.nvim-treesitter.withPlugins (p: [ p.diff ]); ts-plugin = pkgs.vimPlugins.nvim-treesitter.withPlugins (p: [ p.diff ]);
diff-grammar = pkgs.vimPlugins.nvim-treesitter-parsers.diff; diff-grammar = pkgs.vimPlugins.nvim-treesitter-parsers.diff;
luaEnv = pkgs.luajit.withPackages (ps: with ps; [ busted nlua ]); luaEnv = pkgs.luajit.withPackages (
ps: with ps; [
busted
nlua
]
);
busted-with-grammar = pkgs.writeShellScriptBin "busted" '' busted-with-grammar = pkgs.writeShellScriptBin "busted" ''
nvim_bin=$(which nvim) nvim_bin=$(which nvim)
tmpdir=$(mktemp -d) tmpdir=$(mktemp -d)

View file

@ -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
@ -74,6 +75,7 @@ end
---@param col_offset integer ---@param col_offset integer
---@param covered_lines? table<integer, true> ---@param covered_lines? table<integer, true>
---@param priorities diffs.PrioritiesConfig ---@param priorities diffs.PrioritiesConfig
---@param force_high_priority? boolean
---@return integer ---@return integer
local function highlight_treesitter( local function highlight_treesitter(
bufnr, bufnr,
@ -83,7 +85,8 @@ local function highlight_treesitter(
line_map, line_map,
col_offset, col_offset,
covered_lines, covered_lines,
priorities priorities,
force_high_priority
) )
local code = table.concat(code_lines, '\n') local code = table.concat(code_lines, '\n')
if code == '' then if code == '' then
@ -123,8 +126,9 @@ local function highlight_treesitter(
local buf_sc = sc + col_offset local buf_sc = sc + col_offset
local buf_ec = ec + col_offset local buf_ec = ec + col_offset
local meta_prio = tonumber(metadata.priority) or 100
local priority = tree_lang == 'diff' local priority = tree_lang == 'diff'
and (col_offset > 0 and priorities.syntax or (tonumber(metadata.priority) or 100)) and ((col_offset > 0 or force_high_priority) and (priorities.syntax + meta_prio - 100) or meta_prio)
or priorities.syntax or priorities.syntax
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_sr, buf_sc, { pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_sr, buf_sc, {
@ -367,14 +371,24 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
header_map[i] = hunk.header_start_line - 1 + i header_map[i] = hunk.header_start_line - 1 + i
end end
extmark_count = extmark_count extmark_count = extmark_count
+ highlight_treesitter(bufnr, ns, hunk.header_lines, 'diff', header_map, qw, nil, p) + highlight_treesitter(
bufnr,
ns,
hunk.header_lines,
'diff',
header_map,
qw,
nil,
p,
qw > 0 or pw > 1
)
end end
local at_raw_line local at_raw_line
if qw > 0 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 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] at_raw_line = vim.api.nvim_buf_get_lines(bufnr, at_buf_line, at_buf_line + 1, false)[1]
if at_raw_line then if qw > 0 and at_raw_line then
local at_logical = at_raw_line:sub(qw + 1) local at_logical = at_raw_line:sub(qw + 1)
local at_map = { [0] = at_buf_line } local at_map = { [0] = at_buf_line }
extmark_count = extmark_count extmark_count = extmark_count
@ -385,7 +399,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
@ -417,7 +437,7 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
end end
if if
qw > 0 (qw > 0 or pw > 1)
and hunk.header_start_line and hunk.header_start_line
and hunk.header_lines and hunk.header_lines
and #hunk.header_lines > 0 and #hunk.header_lines > 0
@ -430,16 +450,46 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
hl_group = 'DiffsClear', hl_group = 'DiffsClear',
priority = p.clear, priority = p.clear,
}) })
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 = qw + 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, qw + dot_pos + 1, {
end_col = qw + dot_pos + 1 + #hash,
hl_group = '@constant.diff',
priority = p.syntax,
})
end
end
end
end
end end
end end
if qw > 0 and at_raw_line then if (qw > 0 or pw > 1) and at_raw_line then
local at_buf_line = hunk.start_line - 1 local at_buf_line = hunk.start_line - 1
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, 0, {
end_col = #at_raw_line, end_col = #at_raw_line,
hl_group = 'DiffsClear', hl_group = 'DiffsClear',
priority = p.clear, priority = p.clear,
}) })
if pw > 1 and opts.highlights.treesitter.enabled then
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,
})
end
end end
if use_ts and hunk.header_context and hunk.header_context_col then if use_ts and hunk.header_context and hunk.header_context_col then
@ -470,6 +520,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, {
@ -484,30 +535,34 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
hl_group = 'DiffsClear', hl_group = 'DiffsClear',
priority = p.clear, priority = p.clear,
}) })
end elseif pw > 1 then
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
if line_len > pw and covered_lines[buf_line] then end_col = pw,
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, pw + qw, {
end_col = line_len + qw,
hl_group = 'DiffsClear', hl_group = 'DiffsClear',
priority = p.clear, priority = p.clear,
}) })
end end
if opts.highlights.background and is_diff_line then if pw > 1 then
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, { for ci = 0, pw - 1 do
end_row = buf_line + 1, local ch = line:sub(ci + 1, ci + 1)
hl_group = line_hl, if ch == '+' or ch == '-' then
hl_eol = true, pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, ci + qw, {
priority = p.line_bg, end_col = ci + qw + 1,
}) hl_group = ch == '+' and '@diff.plus' or '@diff.minus',
if opts.highlights.gutter then priority = p.syntax,
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
number_hl_group = number_hl,
priority = p.line_bg,
}) })
end end
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 if is_marker and line_len > pw then
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, pw + qw, { pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, pw + qw, {
@ -529,7 +584,8 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
char_hl, char_hl,
line:sub(span.col_start + 1, span.col_end) 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, { local ok, err =
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, span.col_start + qw, {
end_col = span.col_end + qw, end_col = span.col_end + qw,
hl_group = char_hl, hl_group = char_hl,
priority = p.char_bg, priority = p.char_bg,
@ -542,6 +598,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 + qw, {
end_col = line_len + qw,
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

View file

@ -765,7 +765,7 @@ local function init()
if not entry.highlighted[i] then if not entry.highlighted[i] then
local hunk = entry.hunks[i] local hunk = entry.hunks[i]
local clear_start = hunk.start_line - 1 local clear_start = hunk.start_line - 1
local clear_end = clear_start + #hunk.lines local clear_end = hunk.start_line + #hunk.lines
if hunk.header_start_line then if hunk.header_start_line then
clear_start = hunk.header_start_line - 1 clear_start = hunk.header_start_line - 1
end end
@ -789,22 +789,21 @@ local function init()
end end
local cur = hunk_cache[bufnr] local cur = hunk_cache[bufnr]
if not cur or cur.tick ~= tick then if not cur or cur.tick ~= tick then
dbg('deferred syntax stale: cur.tick=%s captured=%d', cur and tostring(cur.tick) or 'nil', tick) dbg(
'deferred syntax stale: cur.tick=%s captured=%d',
cur and tostring(cur.tick) or 'nil',
tick
)
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 = start_row + #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)
@ -953,6 +952,7 @@ M._test = {
invalidate_cache = invalidate_cache, invalidate_cache = invalidate_cache,
hunks_eq = hunks_eq, hunks_eq = hunks_eq,
process_pending_clear = process_pending_clear, process_pending_clear = process_pending_clear,
ft_retry_pending = ft_retry_pending,
} }
return M return M

View file

@ -13,6 +13,7 @@
---@field file_new_start integer? ---@field file_new_start integer?
---@field file_new_count integer? ---@field file_new_count integer?
---@field prefix_width integer ---@field prefix_width integer
---@field quote_width integer
---@field repo_root string? ---@field repo_root string?
local M = {} local M = {}
@ -60,6 +61,15 @@ local function get_ft_from_filename(filename, repo_root)
end end
local ft = vim.filetype.match({ filename = filename }) local ft = vim.filetype.match({ filename = filename })
if not ft and vim.fn.did_filetype() ~= 0 then
dbg('retrying filetype match for %s (clearing did_filetype)', filename)
local saved = rawget(vim.fn, 'did_filetype')
rawset(vim.fn, 'did_filetype', function()
return 0
end)
ft = vim.filetype.match({ filename = filename })
rawset(vim.fn, 'did_filetype', saved)
end
if ft then if ft then
dbg('filetype from filename: %s', ft) dbg('filetype from filename: %s', ft)
return ft return ft
@ -125,6 +135,18 @@ end
function M.parse_buffer(bufnr) function M.parse_buffer(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local repo_root = get_repo_root(bufnr) 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[] ---@type diffs.Hunk[]
local hunks = {} local hunks = {}
@ -163,6 +185,7 @@ function M.parse_buffer(bufnr)
---@type integer? ---@type integer?
local new_remaining = nil local new_remaining = nil
local is_unified_diff = false local is_unified_diff = false
local current_quote_width = 0
local function flush_hunk() local function flush_hunk()
if hunk_start and #hunk_lines > 0 then if hunk_start and #hunk_lines > 0 then
@ -175,6 +198,7 @@ function M.parse_buffer(bufnr)
header_context_col = hunk_header_context_col, header_context_col = hunk_header_context_col,
lines = hunk_lines, lines = hunk_lines,
prefix_width = hunk_prefix_width, prefix_width = hunk_prefix_width,
quote_width = current_quote_width,
file_old_start = file_old_start, file_old_start = file_old_start,
file_old_count = file_old_count, file_old_count = file_old_count,
file_new_start = file_new_start, file_new_start = file_new_start,
@ -200,18 +224,33 @@ function M.parse_buffer(bufnr)
end end
for i, line in ipairs(lines) do for i, line in ipairs(lines) do
local diff_git_file = line:match('^diff %-%-git a/.+ b/(.+)$') local logical = line
local neogit_file = line:match('^modified%s+(.+)$') if quote_prefix then
or (not line:match('^new file mode') and line:match('^new file%s+(.+)$')) if line:sub(1, quote_width) == quote_prefix then
or (not line:match('^deleted file mode') and line:match('^deleted%s+(.+)$')) logical = line:sub(quote_width + 1)
or line:match('^renamed%s+(.+)$') elseif line:match('^>+$') then
or line:match('^copied%s+(.+)$') logical = ''
local bare_file = not hunk_start and line:match('^([^%s]+%.[^%s]+)$') end
local filename = line:match('^[MADRCU%?!]%s+(.+)$') or diff_git_file or neogit_file or bare_file 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 if filename then
is_unified_diff = diff_git_file ~= nil is_unified_diff = diff_git_file ~= nil
flush_hunk() flush_hunk()
current_filename = filename current_filename = filename
current_quote_width = (logical ~= line) and quote_width or 0
local cache_key = (repo_root or '') .. '\0' .. filename local cache_key = (repo_root or '') .. '\0' .. filename
local cached = ft_lang_cache[cache_key] local cached = ft_lang_cache[cache_key]
if cached then if cached then
@ -233,13 +272,13 @@ function M.parse_buffer(bufnr)
hunk_prefix_width = 1 hunk_prefix_width = 1
header_start = i header_start = i
header_lines = {} header_lines = {}
elseif line:match('^@@+') then elseif logical:match('^@@+') then
flush_hunk() flush_hunk()
hunk_start = i hunk_start = i
local at_prefix = line:match('^(@@+)') local at_prefix = logical:match('^(@@+)')
hunk_prefix_width = #at_prefix - 1 hunk_prefix_width = #at_prefix - 1
if #at_prefix == 2 then 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 if hs then
file_old_start = tonumber(hs) file_old_start = tonumber(hs)
file_old_count = tonumber(hc) or 1 file_old_count = tonumber(hc) or 1
@ -249,24 +288,31 @@ function M.parse_buffer(bufnr)
new_remaining = file_new_count new_remaining = file_new_count
end end
else else
local hs2, hc2 = 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 = logical:match('%+(%d+),?(%d*) @@')
if hs2 then if hs2 then
file_new_start = tonumber(hs2) file_new_start = tonumber(hs2)
file_new_count = tonumber(hc2) or 1 file_new_count = tonumber(hc2) or 1
new_remaining = file_new_count
end end
end end
local at_end, context = line:match('^(@@+.-@@+%s*)(.*)') local at_end, context = logical:match('^(@@+.-@@+%s*)(.*)')
if context and context ~= '' then if context and context ~= '' then
hunk_header_context = context hunk_header_context = context
hunk_header_context_col = #at_end hunk_header_context_col = #at_end + current_quote_width
end end
if hunk_count then if hunk_count then
hunk_count = hunk_count + 1 hunk_count = hunk_count + 1
end end
elseif hunk_start then elseif hunk_start then
local prefix = line:sub(1, 1) local prefix = logical:sub(1, 1)
if prefix == ' ' or prefix == '+' or prefix == '-' then 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 if old_remaining and (prefix == ' ' or prefix == '-') then
old_remaining = old_remaining - 1 old_remaining = old_remaining - 1
end end
@ -274,22 +320,21 @@ function M.parse_buffer(bufnr)
new_remaining = new_remaining - 1 new_remaining = new_remaining - 1
end end
elseif elseif
line == '' logical == ''
and is_unified_diff
and old_remaining and old_remaining
and old_remaining > 0 and old_remaining > 0
and new_remaining and new_remaining
and new_remaining > 0 and new_remaining > 0
then then
table.insert(hunk_lines, ' ') table.insert(hunk_lines, string.rep(' ', hunk_prefix_width))
old_remaining = old_remaining - 1 old_remaining = old_remaining - 1
new_remaining = new_remaining - 1 new_remaining = new_remaining - 1
elseif elseif
line == '' logical == ''
or line:match('^[MADRC%?!]%s+') or logical:match('^[MADRC%?!]%s+')
or line:match('^diff ') or logical:match('^diff ')
or line:match('^index ') or logical:match('^index ')
or line:match('^Binary ') or logical:match('^Binary ')
then then
flush_hunk() flush_hunk()
current_filename = nil current_filename = nil
@ -299,7 +344,7 @@ function M.parse_buffer(bufnr)
end end
end end
if header_start and not hunk_start then if header_start and not hunk_start then
table.insert(header_lines, line) table.insert(header_lines, logical)
end end
end end

237
spec/email_quote_spec.lua Normal file
View file

@ -0,0 +1,237 @@
require('spec.helpers')
local highlight = require('diffs.highlight')
local parser = require('diffs.parser')
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 extmarks = get_extmarks(bufnr)
local has_diff_add = false local has_diff_add = false
for _, mark in ipairs(extmarks) do 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 has_diff_add = true
break break
end end
@ -320,7 +320,7 @@ describe('highlight', function()
local extmarks = get_extmarks(bufnr) local extmarks = get_extmarks(bufnr)
local has_diff_delete = false local has_diff_delete = false
for _, mark in ipairs(extmarks) do 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 has_diff_delete = true
break break
end end
@ -362,6 +362,124 @@ describe('highlight', function()
delete_buffer(bufnr) delete_buffer(bufnr)
end) 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() it('still applies background when treesitter disabled', function()
local bufnr = create_buffer({ local bufnr = create_buffer({
'@@ -1,1 +1,2 @@', '@@ -1,1 +1,2 @@',
@ -386,7 +504,7 @@ describe('highlight', function()
local extmarks = get_extmarks(bufnr) local extmarks = get_extmarks(bufnr)
local has_diff_add = false local has_diff_add = false
for _, mark in ipairs(extmarks) do 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 has_diff_add = true
break break
end end
@ -500,7 +618,7 @@ describe('highlight', function()
local extmarks = get_extmarks(bufnr) local extmarks = get_extmarks(bufnr)
local has_diff_add = false local has_diff_add = false
for _, mark in ipairs(extmarks) do 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 has_diff_add = true
break break
end end
@ -585,7 +703,7 @@ describe('highlight', function()
local found = false local found = false
for _, mark in ipairs(extmarks) do for _, mark in ipairs(extmarks) do
local d = mark[4] 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 found = true
end end
end end
@ -741,7 +859,7 @@ describe('highlight', function()
if d then if d then
if d.hl_group == 'DiffsClear' then if d.hl_group == 'DiffsClear' then
table.insert(priorities.clear, d.priority) 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) table.insert(priorities.line_bg, d.priority)
elseif d.hl_group == 'DiffsAddText' or d.hl_group == 'DiffsDeleteText' then elseif d.hl_group == 'DiffsAddText' or d.hl_group == 'DiffsDeleteText' then
table.insert(priorities.char_bg, d.priority) table.insert(priorities.char_bg, d.priority)
@ -842,8 +960,8 @@ describe('highlight', function()
local line_bgs = {} local line_bgs = {}
for _, mark in ipairs(extmarks) do for _, mark in ipairs(extmarks) do
local d = mark[4] 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
line_bgs[mark[2]] = d.hl_group line_bgs[mark[2]] = d.line_hl_group
end end
end end
assert.is_nil(line_bgs[1]) assert.is_nil(line_bgs[1])
@ -946,11 +1064,16 @@ describe('highlight', function()
highlight.highlight_hunk(bufnr, ns, hunk, default_opts()) highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr) local extmarks = get_extmarks(bufnr)
local content_clear_count = 0
for _, mark in ipairs(extmarks) do for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].hl_group == 'DiffsClear' then if mark[4] and mark[4].hl_group == 'DiffsClear' then
assert.are.equal(2, mark[3]) assert.is_true(mark[3] == 0 or mark[3] == 2, 'DiffsClear at unexpected col ' .. mark[3])
if mark[3] == 2 then
content_clear_count = content_clear_count + 1
end end
end end
end
assert.are.equal(2, content_clear_count)
delete_buffer(bufnr) delete_buffer(bufnr)
end) end)
@ -1034,8 +1157,8 @@ describe('highlight', function()
local marker_text = {} local marker_text = {}
for _, mark in ipairs(extmarks) do for _, mark in ipairs(extmarks) do
local d = mark[4] 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
line_bgs[mark[2]] = d.hl_group line_bgs[mark[2]] = d.line_hl_group
end end
if d and d.number_hl_group then if d and d.number_hl_group then
gutter_hls[mark[2]] = d.number_hl_group gutter_hls[mark[2]] = d.number_hl_group
@ -1128,6 +1251,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()
@ -1245,6 +1485,467 @@ describe('highlight', function()
assert.are.equal(0, header_extmarks) assert.are.equal(0, header_extmarks)
delete_buffer(bufnr) delete_buffer(bufnr)
end) end)
it('does not apply DiffsClear to header lines for non-quoted diffs', function()
local bufnr = create_buffer({
'diff --git a/parser.lua b/parser.lua',
'index 3e8afa0..018159c 100644',
'--- a/parser.lua',
'+++ b/parser.lua',
'@@ -1,2 +1,3 @@',
' local M = {}',
'+local x = 1',
})
local hunk = {
filename = 'parser.lua',
lang = 'lua',
start_line = 5,
lines = { ' local M = {}', '+local x = 1' },
header_start_line = 1,
header_lines = {
'diff --git a/parser.lua b/parser.lua',
'index 3e8afa0..018159c 100644',
'--- a/parser.lua',
'+++ b/parser.lua',
},
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
for _, mark in ipairs(extmarks) do
local d = mark[4]
if d and d.hl_group == 'DiffsClear' and mark[3] == 0 and mark[2] < 4 then
error('unexpected DiffsClear on header row ' .. mark[2] .. ' for non-quoted diff')
end
end
delete_buffer(bufnr)
end)
it('preserves diff grammar treesitter on headers for non-quoted diffs', function()
local bufnr = create_buffer({
'diff --git a/parser.lua b/parser.lua',
'--- a/parser.lua',
'+++ b/parser.lua',
'@@ -1,2 +1,3 @@',
' local M = {}',
'+local x = 1',
})
local hunk = {
filename = 'parser.lua',
lang = 'lua',
start_line = 4,
lines = { ' local M = {}', '+local x = 1' },
header_start_line = 1,
header_lines = {
'diff --git a/parser.lua b/parser.lua',
'--- a/parser.lua',
'+++ b/parser.lua',
},
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local header_ts_count = 0
for _, mark in ipairs(extmarks) do
local d = mark[4]
if mark[2] < 3 and d and d.hl_group and d.hl_group:match('^@.*%.diff$') then
header_ts_count = header_ts_count + 1
end
end
assert.is_true(header_ts_count > 0, 'expected diff grammar treesitter on header lines')
delete_buffer(bufnr)
end)
it('applies syntax extmarks to combined diff body lines', function()
local bufnr = create_buffer({
'@@@ -1,2 -1,2 +1,3 @@@',
' local M = {}',
'+ local x = 1',
' -local y = 2',
})
local hunk = {
filename = 'test.lua',
lang = 'lua',
prefix_width = 2,
start_line = 1,
lines = { ' local M = {}', '+ local x = 1', ' -local y = 2' },
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local syntax_on_body = 0
for _, mark in ipairs(extmarks) do
local d = mark[4]
if mark[2] >= 1 and d and d.hl_group and d.hl_group:match('^@.*%.lua$') then
syntax_on_body = syntax_on_body + 1
end
end
assert.is_true(syntax_on_body > 0, 'expected lua treesitter syntax on combined diff body')
delete_buffer(bufnr)
end)
it('applies DiffsClear and per-char diff fg to combined diff body prefixes', function()
local bufnr = create_buffer({
'@@@',
' unchanged',
'+ added',
' -removed',
'++both',
})
local hunk = {
filename = 'test.lua',
lang = 'lua',
prefix_width = 2,
start_line = 1,
lines = { ' unchanged', '+ added', ' -removed', '++both' },
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local prefix_clears = {}
local plus_marks = {}
local minus_marks = {}
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 and d.end_col == 2 then
prefix_clears[mark[2]] = true
end
if d.hl_group == '@diff.plus' and d.priority == 199 then
if not plus_marks[mark[2]] then
plus_marks[mark[2]] = {}
end
table.insert(plus_marks[mark[2]], mark[3])
end
if d.hl_group == '@diff.minus' and d.priority == 199 then
if not minus_marks[mark[2]] then
minus_marks[mark[2]] = {}
end
table.insert(minus_marks[mark[2]], mark[3])
end
end
end
assert.is_true(prefix_clears[1] ~= nil, 'DiffsClear on context prefix')
assert.is_true(prefix_clears[2] ~= nil, 'DiffsClear on add prefix')
assert.is_true(prefix_clears[3] ~= nil, 'DiffsClear on del prefix')
assert.is_true(prefix_clears[4] ~= nil, 'DiffsClear on both-add prefix')
assert.is_true(plus_marks[2] ~= nil, '@diff.plus on + in "+ added"')
assert.are.equal(0, plus_marks[2][1])
assert.is_true(minus_marks[3] ~= nil, '@diff.minus on - in " -removed"')
assert.are.equal(1, minus_marks[3][1])
assert.is_true(plus_marks[4] ~= nil, '@diff.plus on ++ in "++both"')
assert.are.equal(2, #plus_marks[4])
assert.is_nil(plus_marks[1], 'no @diff.plus on context " unchanged"')
assert.is_nil(minus_marks[1], 'no @diff.minus on context " unchanged"')
delete_buffer(bufnr)
end)
it('applies DiffsClear to headers for combined diffs', function()
local bufnr = create_buffer({
'diff --combined lua/merge/target.lua',
'index abc1234,def5678..ghi9012',
'--- a/lua/merge/target.lua',
'+++ b/lua/merge/target.lua',
'@@@ -1,2 -1,2 +1,3 @@@',
' local M = {}',
'+ local x = 1',
})
local hunk = {
filename = 'lua/merge/target.lua',
lang = 'lua',
prefix_width = 2,
start_line = 5,
lines = { ' local M = {}', '+ local x = 1' },
header_start_line = 1,
header_lines = {
'diff --combined lua/merge/target.lua',
'index abc1234,def5678..ghi9012',
'--- a/lua/merge/target.lua',
'+++ b/lua/merge/target.lua',
},
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local clear_lines = {}
for _, mark in ipairs(extmarks) do
local d = mark[4]
if d and d.hl_group == 'DiffsClear' and mark[3] == 0 and mark[2] < 4 then
clear_lines[mark[2]] = true
end
end
assert.is_true(clear_lines[0] ~= nil, 'DiffsClear on diff --combined line')
assert.is_true(clear_lines[1] ~= nil, 'DiffsClear on index line')
assert.is_true(clear_lines[2] ~= nil, 'DiffsClear on --- line')
assert.is_true(clear_lines[3] ~= nil, 'DiffsClear on +++ line')
delete_buffer(bufnr)
end)
it('applies @attribute.diff at syntax priority to @@@ line for combined diffs', function()
local bufnr = create_buffer({
'@@@ -1,2 -1,2 +1,3 @@@',
' local M = {}',
'+ local x = 1',
})
local hunk = {
filename = 'test.lua',
lang = 'lua',
prefix_width = 2,
start_line = 1,
lines = { ' local M = {}', '+ local x = 1' },
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local has_attr = false
for _, mark in ipairs(extmarks) do
local d = mark[4]
if mark[2] == 0 and d and d.hl_group == '@attribute.diff' and (d.priority or 0) >= 199 then
has_attr = true
end
end
assert.is_true(has_attr, '@attribute.diff at p>=199 on @@@ line')
delete_buffer(bufnr)
end)
it('applies DiffsClear to @@@ line for combined diffs', function()
local bufnr = create_buffer({
'@@@ -1,2 -1,2 +1,3 @@@',
' local M = {}',
'+ local x = 1',
})
local hunk = {
filename = 'test.lua',
lang = 'lua',
prefix_width = 2,
start_line = 1,
lines = { ' local M = {}', '+ local x = 1' },
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local has_at_clear = false
for _, mark in ipairs(extmarks) do
local d = mark[4]
if mark[2] == 0 and d and d.hl_group == 'DiffsClear' and mark[3] == 0 then
has_at_clear = true
end
end
assert.is_true(has_at_clear, 'DiffsClear on @@@ line')
delete_buffer(bufnr)
end)
it('applies header diff grammar at syntax priority for combined diffs', function()
local bufnr = create_buffer({
'diff --combined lua/merge/target.lua',
'index abc1234,def5678..ghi9012',
'--- a/lua/merge/target.lua',
'+++ b/lua/merge/target.lua',
'@@@ -1,2 -1,2 +1,3 @@@',
' local M = {}',
'+ local x = 1',
})
local hunk = {
filename = 'lua/merge/target.lua',
lang = 'lua',
prefix_width = 2,
start_line = 5,
lines = { ' local M = {}', '+ local x = 1' },
header_start_line = 1,
header_lines = {
'diff --combined lua/merge/target.lua',
'index abc1234,def5678..ghi9012',
'--- a/lua/merge/target.lua',
'+++ b/lua/merge/target.lua',
},
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local high_prio_diff = {}
for _, mark in ipairs(extmarks) do
local d = mark[4]
if
mark[2] < 4
and d
and d.hl_group
and d.hl_group:match('^@.*%.diff$')
and (d.priority or 0) >= 199
then
high_prio_diff[mark[2]] = true
end
end
assert.is_true(high_prio_diff[2] ~= nil, 'diff grammar at p>=199 on --- line')
assert.is_true(high_prio_diff[3] ~= nil, 'diff grammar at p>=199 on +++ line')
delete_buffer(bufnr)
end)
it('@diff.minus wins over @punctuation.special on combined diff headers', function()
local bufnr = create_buffer({
'diff --combined lua/merge/target.lua',
'index abc1234,def5678..ghi9012',
'--- a/lua/merge/target.lua',
'+++ b/lua/merge/target.lua',
'@@@ -1,2 -1,2 +1,3 @@@',
' local M = {}',
'+ local x = 1',
})
local hunk = {
filename = 'lua/merge/target.lua',
lang = 'lua',
prefix_width = 2,
start_line = 5,
lines = { ' local M = {}', '+ local x = 1' },
header_start_line = 1,
header_lines = {
'diff --combined lua/merge/target.lua',
'index abc1234,def5678..ghi9012',
'--- a/lua/merge/target.lua',
'+++ b/lua/merge/target.lua',
},
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local minus_prio, punct_prio_minus = 0, 0
local plus_prio, punct_prio_plus = 0, 0
for _, mark in ipairs(extmarks) do
local d = mark[4]
if d and d.hl_group then
if mark[2] == 2 then
if d.hl_group == '@diff.minus.diff' then
minus_prio = math.max(minus_prio, d.priority or 0)
elseif d.hl_group == '@punctuation.special.diff' then
punct_prio_minus = math.max(punct_prio_minus, d.priority or 0)
end
elseif mark[2] == 3 then
if d.hl_group == '@diff.plus.diff' then
plus_prio = math.max(plus_prio, d.priority or 0)
elseif d.hl_group == '@punctuation.special.diff' then
punct_prio_plus = math.max(punct_prio_plus, d.priority or 0)
end
end
end
end
assert.is_true(
minus_prio > punct_prio_minus,
'@diff.minus.diff should beat @punctuation.special.diff on --- line'
)
assert.is_true(
plus_prio > punct_prio_plus,
'@diff.plus.diff should beat @punctuation.special.diff on +++ line'
)
delete_buffer(bufnr)
end)
it('applies @keyword.diff on index word for combined diffs', function()
local bufnr = create_buffer({
'diff --combined lua/merge/target.lua',
'index abc1234,def5678..ghi9012',
'--- a/lua/merge/target.lua',
'+++ b/lua/merge/target.lua',
'@@@ -1,2 -1,2 +1,3 @@@',
' local M = {}',
'+ local x = 1',
})
local hunk = {
filename = 'lua/merge/target.lua',
lang = 'lua',
prefix_width = 2,
start_line = 5,
lines = { ' local M = {}', '+ local x = 1' },
header_start_line = 1,
header_lines = {
'diff --combined lua/merge/target.lua',
'index abc1234,def5678..ghi9012',
'--- a/lua/merge/target.lua',
'+++ b/lua/merge/target.lua',
},
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local has_keyword = false
for _, mark in ipairs(extmarks) do
local d = mark[4]
if
mark[2] == 1
and d
and d.hl_group == '@keyword.diff'
and mark[3] == 0
and (d.end_col or 0) == 5
then
has_keyword = true
end
end
assert.is_true(has_keyword, '@keyword.diff at row 1, cols 0-5')
delete_buffer(bufnr)
end)
it('applies @constant.diff on result hash for combined diffs', function()
local bufnr = create_buffer({
'diff --combined lua/merge/target.lua',
'index abc1234,def5678..ghi9012',
'--- a/lua/merge/target.lua',
'+++ b/lua/merge/target.lua',
'@@@ -1,2 -1,2 +1,3 @@@',
' local M = {}',
'+ local x = 1',
})
local hunk = {
filename = 'lua/merge/target.lua',
lang = 'lua',
prefix_width = 2,
start_line = 5,
lines = { ' local M = {}', '+ local x = 1' },
header_start_line = 1,
header_lines = {
'diff --combined lua/merge/target.lua',
'index abc1234,def5678..ghi9012',
'--- a/lua/merge/target.lua',
'+++ b/lua/merge/target.lua',
},
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local has_constant = false
for _, mark in ipairs(extmarks) do
local d = mark[4]
if mark[2] == 1 and d and d.hl_group == '@constant.diff' and (d.priority or 0) >= 199 then
has_constant = true
end
end
assert.is_true(has_constant, '@constant.diff on result hash')
delete_buffer(bufnr)
end)
end) end)
describe('extmark priority', function() describe('extmark priority', function()

View file

@ -127,6 +127,110 @@ describe('integration', function()
end) end)
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() describe('extmarks from highlight pipeline', function()
it('DiffsAdd background applied to + lines', function() it('DiffsAdd background applied to + lines', function()
local bufnr = create_buffer({ local bufnr = create_buffer({
@ -145,7 +249,7 @@ describe('integration', function()
local extmarks = get_extmarks(bufnr, ns) local extmarks = get_extmarks(bufnr, ns)
local has_diff_add = false local has_diff_add = false
for _, mark in ipairs(extmarks) do 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 has_diff_add = true
break break
end end
@ -171,7 +275,7 @@ describe('integration', function()
local extmarks = get_extmarks(bufnr, ns) local extmarks = get_extmarks(bufnr, ns)
local has_diff_delete = false local has_diff_delete = false
for _, mark in ipairs(extmarks) do 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 has_diff_delete = true
break break
end end
@ -198,10 +302,10 @@ describe('integration', function()
local has_add = false local has_add = false
local has_delete = false local has_delete = false
for _, mark in ipairs(extmarks) do 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 has_add = true
end 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 has_delete = true
end end
end end
@ -230,8 +334,8 @@ describe('integration', function()
local line_bgs = {} local line_bgs = {}
for _, mark in ipairs(extmarks) do for _, mark in ipairs(extmarks) do
local d = mark[4] 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
line_bgs[mark[2]] = d.hl_group line_bgs[mark[2]] = d.line_hl_group
end end
end end
assert.is_nil(line_bgs[1]) assert.is_nil(line_bgs[1])
@ -313,10 +417,10 @@ describe('integration', function()
local del_lines = {} local del_lines = {}
for _, mark in ipairs(extmarks) do for _, mark in ipairs(extmarks) do
local d = mark[4] 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 add_lines[mark[2]] = true
end 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 del_lines[mark[2]] = true
end end
end end

View file

@ -163,10 +163,10 @@ describe('parser', function()
end end
end) end)
it('stops hunk at blank line', function() it('stops hunk at blank line when remaining counts exhausted', function()
local bufnr = create_buffer({ local bufnr = create_buffer({
'M test.lua', 'M test.lua',
'@@ -1,2 +1,3 @@', '@@ -1,1 +1,2 @@',
' local x = 1', ' local x = 1',
'+local y = 2', '+local y = 2',
'', '',
@ -391,6 +391,29 @@ describe('parser', function()
vim.fn.delete(repo_root, 'rf') vim.fn.delete(repo_root, 'rf')
end) 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() it('extracts file line numbers from @@ header', function()
local bufnr = create_buffer({ local bufnr = create_buffer({
'M lua/test.lua', 'M lua/test.lua',
@ -506,7 +529,8 @@ describe('parser', function()
assert.are.equal(1, #hunks) assert.are.equal(1, #hunks)
assert.are.equal(1, hunks[1].file_new_start) assert.are.equal(1, hunks[1].file_new_start)
assert.are.equal(9, hunks[1].file_new_count) assert.are.equal(9, hunks[1].file_new_count)
assert.is_nil(hunks[1].file_old_start) assert.are.equal(1, hunks[1].file_old_start)
assert.are.equal(3, hunks[1].file_old_count)
delete_buffer(bufnr) delete_buffer(bufnr)
end) end)