fix(highlight): use hunk body as context for header treesitter parsing

Problem: the header context string (e.g. "function M.setup()") was
parsed in isolation by treesitter, which couldn't recognize "function"
as @keyword.function because the snippet is an incomplete definition
with no body or "end".

Solution: append the already-built new_code lines as trailing context
when parsing the header string, giving treesitter a complete function
definition. Filter captures to row 0 only so body-line captures don't
produce extmarks on the header line.
This commit is contained in:
Barrett Ruth 2026-02-07 15:10:07 -05:00
parent 3b2e0de2a7
commit 38220ab368
2 changed files with 68 additions and 18 deletions

View file

@ -220,6 +220,40 @@ describe('highlight', function()
delete_buffer(bufnr)
end)
it('highlights function keyword in header context', function()
local bufnr = create_buffer({
'@@ -5,3 +5,4 @@ function M.setup()',
' local x = 1',
'+local y = 2',
' return x',
})
local hunk = {
filename = 'test.lua',
lang = 'lua',
start_line = 1,
header_context = 'function M.setup()',
header_context_col = 18,
lines = { ' local x = 1', '+local y = 2', ' return x' },
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local has_keyword_function = false
for _, mark in ipairs(extmarks) do
if mark[2] == 0 and mark[4] and mark[4].hl_group then
local hl = mark[4].hl_group
if hl == '@keyword.function.lua' or hl == '@keyword.lua' then
has_keyword_function = true
break
end
end
end
assert.is_true(has_keyword_function)
delete_buffer(bufnr)
end)
it('does not highlight header when no header_context', function()
local bufnr = create_buffer({
'@@ -10,3 +10,4 @@',