From 2535aea207faf590512bdb7d332b0440948b7043 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 19 Mar 2026 20:00:44 -0400 Subject: [PATCH 1/2] fix(highlight): use file context for header text treesitter parsing Problem: the `@@ ... @@ end` header context text was parsed by treesitter in isolation. Without surrounding code, tokens like `end` produce ERROR nodes and get no highlight captures. Solution: prepend `hunk.context_before` lines (already computed by `compute_hunk_context`) to give the treesitter string parser enough scope to recognize keywords, closing delimiters, etc. Only captures on the target row produce extmarks. --- lua/diffs/highlight.lua | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lua/diffs/highlight.lua b/lua/diffs/highlight.lua index 806f782..325a588 100644 --- a/lua/diffs/highlight.lua +++ b/lua/diffs/highlight.lua @@ -11,12 +11,20 @@ local diff = require('diffs.diff') ---@param lang string ---@param context_lines? string[] ---@param priorities diffs.PrioritiesConfig +---@param context_before? string[] ---@return integer -local function highlight_text(bufnr, ns, hunk, col_offset, text, lang, context_lines, priorities) - local parse_text = text +local function highlight_text(bufnr, ns, hunk, col_offset, text, lang, context_lines, priorities, context_before) + local prefix_count = 0 + local parts = {} + if context_before and #context_before > 0 then + prefix_count = #context_before + parts[#parts + 1] = table.concat(context_before, '\n') + end + parts[#parts + 1] = text if context_lines and #context_lines > 0 then - parse_text = text .. '\n' .. table.concat(context_lines, '\n') + parts[#parts + 1] = table.concat(context_lines, '\n') end + local parse_text = table.concat(parts, '\n') local ok, parser_obj = pcall(vim.treesitter.get_string_parser, parse_text, lang) if not ok or not parser_obj then @@ -35,10 +43,11 @@ local function highlight_text(bufnr, ns, hunk, col_offset, text, lang, context_l local extmark_count = 0 local header_line = hunk.start_line - 1 + local target_row = prefix_count for id, node, metadata in query:iter_captures(trees[1]:root(), parse_text) do local sr, sc, _, ec = node:range() - if sr == 0 then + if sr == target_row then local capture_name = '@' .. query.captures[id] .. '.' .. lang local buf_sr = header_line @@ -412,7 +421,8 @@ function M.highlight_hunk(bufnr, ns, hunk, opts) hunk.header_context, hunk.lang, new_code, - p + p, + hunk.context_before ) if header_extmarks > 0 then dbg('header %s:%d applied %d extmarks', hunk.filename, hunk.start_line, header_extmarks) From 961037d0e50e57f1a6510e59e446c24c106e636b Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 19 Mar 2026 20:23:04 -0400 Subject: [PATCH 2/2] ci: format --- lua/diffs/highlight.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lua/diffs/highlight.lua b/lua/diffs/highlight.lua index 325a588..cbb544e 100644 --- a/lua/diffs/highlight.lua +++ b/lua/diffs/highlight.lua @@ -13,7 +13,17 @@ local diff = require('diffs.diff') ---@param priorities diffs.PrioritiesConfig ---@param context_before? string[] ---@return integer -local function highlight_text(bufnr, ns, hunk, col_offset, text, lang, context_lines, priorities, context_before) +local function highlight_text( + bufnr, + ns, + hunk, + col_offset, + text, + lang, + context_lines, + priorities, + context_before +) local prefix_count = 0 local parts = {} if context_before and #context_before > 0 then