feat: treesitter highlighting for diff headers
Apply treesitter highlighting to diff metadata lines (diff --git, index, ---, +++) using the diff language parser. Header info is attached only to the first hunk of each file to avoid duplicate highlighting. Based on PR #52 by @phanen with fixes: - header_lines now only contains diff metadata, not hunk content - header info attached only to first hunk per file - removed arbitrary hunk count restriction
This commit is contained in:
parent
4008df3558
commit
a25f1e9d84
7 changed files with 292 additions and 5 deletions
|
|
@ -57,8 +57,9 @@ end
|
|||
---@param ns integer
|
||||
---@param hunk diffs.Hunk
|
||||
---@param code_lines string[]
|
||||
---@param col_offset integer?
|
||||
---@return integer
|
||||
local function highlight_treesitter(bufnr, ns, hunk, code_lines)
|
||||
local function highlight_treesitter(bufnr, ns, hunk, code_lines, col_offset)
|
||||
local lang = hunk.lang
|
||||
if not lang then
|
||||
return 0
|
||||
|
|
@ -101,6 +102,8 @@ local function highlight_treesitter(bufnr, ns, hunk, code_lines)
|
|||
end
|
||||
end
|
||||
|
||||
col_offset = col_offset or 1
|
||||
|
||||
local extmark_count = 0
|
||||
for id, node, _ in query:iter_captures(trees[1]:root(), code) do
|
||||
local capture_name = '@' .. query.captures[id]
|
||||
|
|
@ -108,8 +111,8 @@ local function highlight_treesitter(bufnr, ns, hunk, code_lines)
|
|||
|
||||
local buf_sr = hunk.start_line + sr
|
||||
local buf_er = hunk.start_line + er
|
||||
local buf_sc = sc + 1
|
||||
local buf_ec = ec + 1
|
||||
local buf_sc = sc + col_offset
|
||||
local buf_ec = ec + col_offset
|
||||
|
||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_sr, buf_sc, {
|
||||
end_row = buf_er,
|
||||
|
|
@ -257,6 +260,22 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
|||
extmark_count = highlight_vim_syntax(bufnr, ns, hunk, code_lines)
|
||||
end
|
||||
|
||||
if
|
||||
hunk.header_start_line
|
||||
and hunk.header_lines
|
||||
and #hunk.header_lines > 0
|
||||
and opts.highlights.treesitter.enabled
|
||||
then
|
||||
extmark_count = extmark_count
|
||||
+ highlight_treesitter(bufnr, ns, {
|
||||
filename = hunk.filename,
|
||||
start_line = hunk.header_start_line - 1,
|
||||
lang = 'diff',
|
||||
lines = hunk.header_lines,
|
||||
header_lines = {},
|
||||
}, hunk.header_lines, 0)
|
||||
end
|
||||
|
||||
local syntax_applied = extmark_count > 0
|
||||
|
||||
for i, line in ipairs(hunk.lines) do
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
---@field header_context string?
|
||||
---@field header_context_col integer?
|
||||
---@field lines string[]
|
||||
---@field header_start_line integer?
|
||||
---@field header_lines string[]?
|
||||
|
||||
local M = {}
|
||||
|
||||
|
|
@ -58,10 +60,16 @@ function M.parse_buffer(bufnr)
|
|||
local hunk_header_context_col = nil
|
||||
---@type string[]
|
||||
local hunk_lines = {}
|
||||
---@type integer?
|
||||
local hunk_count = nil
|
||||
---@type integer?
|
||||
local header_start = nil
|
||||
---@type string[]
|
||||
local header_lines = {}
|
||||
|
||||
local function flush_hunk()
|
||||
if hunk_start and #hunk_lines > 0 and (current_lang or current_ft) then
|
||||
table.insert(hunks, {
|
||||
local hunk = {
|
||||
filename = current_filename,
|
||||
ft = current_ft,
|
||||
lang = current_lang,
|
||||
|
|
@ -69,7 +77,12 @@ function M.parse_buffer(bufnr)
|
|||
header_context = hunk_header_context,
|
||||
header_context_col = hunk_header_context_col,
|
||||
lines = hunk_lines,
|
||||
})
|
||||
}
|
||||
if hunk_count == 1 and header_start and #header_lines > 0 then
|
||||
hunk.header_start_line = header_start
|
||||
hunk.header_lines = header_lines
|
||||
end
|
||||
table.insert(hunks, hunk)
|
||||
end
|
||||
hunk_start = nil
|
||||
hunk_header_context = nil
|
||||
|
|
@ -89,6 +102,9 @@ function M.parse_buffer(bufnr)
|
|||
elseif current_ft then
|
||||
dbg('file: %s -> ft: %s (no ts parser)', filename, current_ft)
|
||||
end
|
||||
hunk_count = 0
|
||||
header_start = i
|
||||
header_lines = {}
|
||||
elseif line:match('^@@.-@@') then
|
||||
flush_hunk()
|
||||
hunk_start = i
|
||||
|
|
@ -97,6 +113,9 @@ function M.parse_buffer(bufnr)
|
|||
hunk_header_context = context
|
||||
hunk_header_context_col = #prefix
|
||||
end
|
||||
if hunk_count then
|
||||
hunk_count = hunk_count + 1
|
||||
end
|
||||
elseif hunk_start then
|
||||
local prefix = line:sub(1, 1)
|
||||
if prefix == ' ' or prefix == '+' or prefix == '-' then
|
||||
|
|
@ -112,8 +131,12 @@ function M.parse_buffer(bufnr)
|
|||
current_filename = nil
|
||||
current_ft = nil
|
||||
current_lang = nil
|
||||
header_start = nil
|
||||
end
|
||||
end
|
||||
if header_start and not hunk_start then
|
||||
table.insert(header_lines, line)
|
||||
end
|
||||
end
|
||||
|
||||
flush_hunk()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue