feat(highlight): warn when hunks exceed max_lines

Problem: when a hunk's highlighted lines exceed `max_lines`, syntax
highlighting is silently skipped. Users have no indication why parts
of their diff lack highlighting.

Solution: add `highlights.warn_max_lines` (default `true`) that emits
a `vim.notify` warning with the hunk index and line count vs threshold.
Also change `max_lines` to count only highlighted (`+`/`-`) lines
rather than total body lines including context.
This commit is contained in:
Barrett Ruth 2026-03-10 16:36:50 -04:00
parent c7cd8fc24c
commit 5199e72bd0
4 changed files with 74 additions and 22 deletions

View file

@ -310,16 +310,26 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
local use_vim = not use_ts and hunk.ft and opts.highlights.vim.enabled
local max_lines = use_ts and opts.highlights.treesitter.max_lines or opts.highlights.vim.max_lines
if (use_ts or use_vim) and #hunk.lines > max_lines then
dbg(
'skipping hunk %s:%d (%d lines > %d max)',
hunk.filename,
hunk.start_line,
#hunk.lines,
max_lines
)
use_ts = false
use_vim = false
if use_ts or use_vim then
local hl_count = 0
for _, line in ipairs(hunk.lines) do
local c = line:sub(1, 1)
if c == '+' or c == '-' then
hl_count = hl_count + 1
end
end
hunk._hl_line_count = hl_count
if hl_count > max_lines then
dbg(
'skipping hunk %s:%d (%d highlighted lines > %d max)',
hunk.filename,
hunk.start_line,
hl_count,
max_lines
)
use_ts = false
use_vim = false
end
end
if use_vim and opts.defer_vim_syntax then
@ -456,7 +466,7 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
and intra_cfg
and intra_cfg.enabled
and pw == 1
and #hunk.lines <= intra_cfg.max_lines
and (hunk._hl_line_count or #hunk.lines) <= intra_cfg.max_lines
then
dbg('computing intra for hunk %s:%d (%d lines)', hunk.filename, hunk.start_line, #hunk.lines)
intra = diff.compute_intra_hunks(hunk.lines, intra_cfg.algorithm)
@ -467,8 +477,8 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
end
elseif intra_cfg and not intra_cfg.enabled then
dbg('intra disabled by config')
elseif intra_cfg and #hunk.lines > intra_cfg.max_lines then
dbg('intra skipped: %d lines > %d max', #hunk.lines, intra_cfg.max_lines)
elseif intra_cfg and (hunk._hl_line_count or #hunk.lines) > intra_cfg.max_lines then
dbg('intra skipped: %d highlighted lines > %d max', hunk._hl_line_count or #hunk.lines, intra_cfg.max_lines)
end
---@type table<integer, diffs.CharSpan[]>