refactor(highlight): unified per-line extmark builder (#144)

## Problem

`highlight_hunk` applied DiffsClear extmarks across 5 scattered sites
with
ad-hoc column arithmetic. This fragmentation produced the 1-column
DiffsClear
gap on email-quoted body context lines (#142 issue 1). A redundant
`highlight_hunk_vim_syntax` function duplicated the inline vim syntax
path,
and the deferred pass in init.lua double-called it, creating duplicate
scratch
buffers and extmarks.

## Solution

Reorganize `highlight_hunk` into two clean phases:

- **Phase 1** — multi-line syntax computation (treesitter, vim syntax,
diff
grammar, header context text). Sets syntax extmarks only, no DiffsClear.
- **Phase 2** — per-line chrome (DiffsClear, backgrounds, gutter,
overlays,
  intra-line). All non-syntax extmarks consolidated in one pass.

Hoist `new_code` to function scope (needed by `highlight_text` outside
the
`use_ts` block). Hoist `at_raw_line` so Phase 1d and Phase 2b share one
`nvim_buf_get_lines` call.

Delete `highlight_hunk_vim_syntax` (redundant with inline path). Remove
the
double-call from the deferred pass in init.lua.

Extend body prefix DiffsClear `end_col` from `qw` to `pw + qw`, fixing
the
1-column gap where native treesitter background bled through on context
lines
in email-quoted diffs (#142 issue 1).

### Email-quoted diff support

The parser now strips `> ` (and `>> `, etc.) email quote prefixes before
pattern matching, enabling syntax highlighting for diffs embedded in
email
replies and `git-send-email` / sourcehut-style patch review threads.
Each hunk stores `quote_width` so the highlight pipeline can apply
`DiffsClear` at the correct column offsets to suppress native treesitter
on quoted regions.

Closes #141

### #142 status after this PR

| Sub-issue | Status |
|-----------|--------|
| 1. Col gap on context lines | Fixed |
| 2. Bare `>` context lines | Improved, edge case remains |
| 3. Diff prefix marker fg | Not addressed (follow-up) |
This commit is contained in:
Barrett Ruth 2026-03-05 09:01:22 -05:00 committed by GitHub
parent 7a3c4ea01e
commit 7106bcc291
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 919 additions and 102 deletions

View file

@ -163,10 +163,10 @@ describe('parser', function()
end
end)
it('stops hunk at blank line', function()
it('stops hunk at blank line when remaining counts exhausted', function()
local bufnr = create_buffer({
'M test.lua',
'@@ -1,2 +1,3 @@',
'@@ -1,1 +1,2 @@',
' local x = 1',
'+local y = 2',
'',
@ -391,6 +391,29 @@ 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',
@ -506,7 +529,8 @@ describe('parser', function()
assert.are.equal(1, #hunks)
assert.are.equal(1, hunks[1].file_new_start)
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)
end)