feat(highlight): add treesitter context padding from disk

Problem: treesitter parses each diff hunk in isolation, so incomplete
syntax constructs at hunk boundaries (e.g., a function definition with
no body) produce ERROR nodes and drop captures.

Solution: read N lines from the on-disk file before/after each hunk and
prepend/append them as unmapped padding lines. The line_map guard in
highlight_treesitter skips extmarks for unmapped lines, so padding
provides syntax context without visual output. Controlled by
highlights.context (default 25, 0 to disable). Also applies to the vim
syntax fallback path via a leading_offset filter.
This commit is contained in:
Barrett Ruth 2026-02-07 13:05:53 -05:00
parent ba1f830629
commit 2e1ebdee03
7 changed files with 308 additions and 24 deletions

View file

@ -14,6 +14,7 @@
---@class diffs.Highlights
---@field background boolean
---@field gutter boolean
---@field context integer
---@field treesitter diffs.TreesitterConfig
---@field vim diffs.VimConfig
---@field intra diffs.IntraConfig
@ -80,6 +81,7 @@ local default_config = {
highlights = {
background = true,
gutter = true,
context = 25,
treesitter = {
enabled = true,
max_lines = 500,
@ -231,6 +233,7 @@ local function init()
vim.validate({
['highlights.background'] = { opts.highlights.background, 'boolean', true },
['highlights.gutter'] = { opts.highlights.gutter, 'boolean', true },
['highlights.context'] = { opts.highlights.context, 'number', true },
['highlights.treesitter'] = { opts.highlights.treesitter, 'table', true },
['highlights.vim'] = { opts.highlights.vim, 'table', true },
['highlights.intra'] = { opts.highlights.intra, 'table', true },
@ -291,6 +294,9 @@ local function init()
if opts.debounce_ms and opts.debounce_ms < 0 then
error('diffs: debounce_ms must be >= 0')
end
if opts.highlights and opts.highlights.context and opts.highlights.context < 0 then
error('diffs: highlights.context must be >= 0')
end
if
opts.highlights
and opts.highlights.treesitter