diff --git a/lua/fugitive-ts/highlight.lua b/lua/fugitive-ts/highlight.lua index b5adb88..a3f0213 100644 --- a/lua/fugitive-ts/highlight.lua +++ b/lua/fugitive-ts/highlight.lua @@ -73,95 +73,35 @@ end ---@param bufnr integer ---@param ns integer ---@param hunk fugitive-ts.Hunk ----@param opts fugitive-ts.HunkOpts -function M.highlight_hunk(bufnr, ns, hunk, opts) +---@param code_lines string[] +---@return integer +local function highlight_treesitter(bufnr, ns, hunk, code_lines) local lang = hunk.lang if not lang then - return - end - - local max_lines = opts.treesitter.max_lines - if #hunk.lines > max_lines then - dbg( - 'skipping hunk %s:%d (%d lines > %d max)', - hunk.filename, - hunk.start_line, - #hunk.lines, - max_lines - ) - return - end - - for i, line in ipairs(hunk.lines) do - local buf_line = hunk.start_line + i - 1 - local line_len = #line - local prefix = line:sub(1, 1) - - local is_diff_line = prefix == '+' or prefix == '-' - local line_hl = is_diff_line and (prefix == '+' and 'FugitiveTsAdd' or 'FugitiveTsDelete') - or nil - local number_hl = is_diff_line and (prefix == '+' and 'FugitiveTsAddNr' or 'FugitiveTsDeleteNr') - or nil - - if opts.hide_prefix then - local virt_hl = (opts.highlights.background and line_hl) or nil - pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, { - virt_text = { { ' ', virt_hl } }, - virt_text_pos = 'overlay', - }) - end - - if opts.highlights.background and is_diff_line then - local extmark_opts = { - line_hl_group = line_hl, - priority = 198, - } - if opts.highlights.gutter then - extmark_opts.number_hl_group = number_hl - end - pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, extmark_opts) - end - - if line_len > 1 and opts.treesitter.enabled then - pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 1, { - end_col = line_len, - hl_group = 'Normal', - priority = 199, - }) - end - end - - if not opts.treesitter.enabled then - return - end - - ---@type string[] - local code_lines = {} - for _, line in ipairs(hunk.lines) do - table.insert(code_lines, line:sub(2)) + return 0 end local code = table.concat(code_lines, '\n') if code == '' then - return + return 0 end local ok, parser_obj = pcall(vim.treesitter.get_string_parser, code, lang) if not ok or not parser_obj then dbg('failed to create parser for lang: %s', lang) - return + return 0 end local trees = parser_obj:parse() if not trees or #trees == 0 then dbg('parse returned no trees for lang: %s', lang) - return + return 0 end local query = vim.treesitter.query.get(lang, 'highlights') if not query then dbg('no highlights query for lang: %s', lang) - return + return 0 end if hunk.header_context and hunk.header_context_col then @@ -197,6 +137,161 @@ function M.highlight_hunk(bufnr, ns, hunk, opts) extmark_count = extmark_count + 1 end + return extmark_count +end + +---@param bufnr integer +---@param ns integer +---@param hunk fugitive-ts.Hunk +---@param code_lines string[] +---@return integer +local function highlight_vim_syntax(bufnr, ns, hunk, code_lines) + local ft = hunk.ft + if not ft then + return 0 + end + + if #code_lines == 0 then + return 0 + end + + local scratch = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(scratch, 0, -1, false, code_lines) + vim.api.nvim_set_option_value('bufhidden', 'wipe', { buf = scratch }) + + local extmark_count = 0 + + vim.api.nvim_buf_call(scratch, function() + vim.cmd('setlocal syntax=' .. ft) + vim.cmd('redraw') + + for i, line in ipairs(code_lines) do + local col = 1 + local line_len = #line + + while col <= line_len do + local syn_id = vim.fn.synID(i, col, 1) + if syn_id == 0 then + col = col + 1 + else + local hl_name = vim.fn.synIDattr(vim.fn.synIDtrans(syn_id), 'name') + local span_start = col + + col = col + 1 + while col <= line_len do + local next_id = vim.fn.synID(i, col, 1) + if next_id == 0 then + break + end + local next_name = vim.fn.synIDattr(vim.fn.synIDtrans(next_id), 'name') + if next_name ~= hl_name then + break + end + col = col + 1 + end + + if hl_name ~= '' then + local buf_line = hunk.start_line + i - 1 + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, span_start, { + end_col = col, + hl_group = hl_name, + priority = 200, + }) + extmark_count = extmark_count + 1 + end + end + end + end + end) + + vim.api.nvim_buf_delete(scratch, { force = true }) + + return extmark_count +end + +---@param bufnr integer +---@param ns integer +---@param hunk fugitive-ts.Hunk +---@param opts fugitive-ts.HunkOpts +function M.highlight_hunk(bufnr, ns, hunk, opts) + local use_ts = hunk.lang and opts.treesitter.enabled + local use_vim = not use_ts and hunk.ft and opts.vim.enabled + + if not use_ts and not use_vim and not hunk.ft then + return + end + + local max_lines = use_ts and opts.treesitter.max_lines or opts.vim.max_lines + if (use_ts or use_vim) and #hunk.lines > max_lines then + dbg( + 'skipping hunk %s:%d (%d lines > %d max)', + hunk.filename, + hunk.start_line, + #hunk.lines, + max_lines + ) + use_ts = false + use_vim = false + end + + local apply_syntax = use_ts or use_vim + + for i, line in ipairs(hunk.lines) do + local buf_line = hunk.start_line + i - 1 + local line_len = #line + local prefix = line:sub(1, 1) + + local is_diff_line = prefix == '+' or prefix == '-' + local line_hl = is_diff_line and (prefix == '+' and 'FugitiveTsAdd' or 'FugitiveTsDelete') + or nil + local number_hl = is_diff_line and (prefix == '+' and 'FugitiveTsAddNr' or 'FugitiveTsDeleteNr') + or nil + + if opts.hide_prefix then + local virt_hl = (opts.highlights.background and line_hl) or nil + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, { + virt_text = { { ' ', virt_hl } }, + virt_text_pos = 'overlay', + }) + end + + if opts.highlights.background and is_diff_line then + local extmark_opts = { + line_hl_group = line_hl, + priority = 198, + } + if opts.highlights.gutter then + extmark_opts.number_hl_group = number_hl + end + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, extmark_opts) + end + + if line_len > 1 and apply_syntax then + pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 1, { + end_col = line_len, + hl_group = 'Normal', + priority = 199, + }) + end + end + + if not apply_syntax then + return + end + + ---@type string[] + local code_lines = {} + for _, line in ipairs(hunk.lines) do + table.insert(code_lines, line:sub(2)) + end + + local extmark_count = 0 + if use_ts then + extmark_count = highlight_treesitter(bufnr, ns, hunk, code_lines) + elseif use_vim then + extmark_count = highlight_vim_syntax(bufnr, ns, hunk, code_lines) + end + dbg('hunk %s:%d applied %d extmarks', hunk.filename, hunk.start_line, extmark_count) end diff --git a/lua/fugitive-ts/parser.lua b/lua/fugitive-ts/parser.lua index 3bf9346..025186b 100644 --- a/lua/fugitive-ts/parser.lua +++ b/lua/fugitive-ts/parser.lua @@ -1,6 +1,7 @@ ---@class fugitive-ts.Hunk ---@field filename string ----@field lang string +---@field ft string? +---@field lang string? ---@field start_line integer ---@field header_context string? ---@field header_context_col integer? @@ -27,13 +28,17 @@ end ---@param filename string ---@return string? -local function get_lang_from_filename(filename) +local function get_ft_from_filename(filename) local ft = vim.filetype.match({ filename = filename }) if not ft then dbg('no filetype for: %s', filename) - return nil end + return ft +end +---@param ft string +---@return string? +local function get_lang_from_ft(ft) local lang = vim.treesitter.language.get_lang(ft) if lang then local ok = pcall(vim.treesitter.language.inspect, lang) @@ -44,7 +49,6 @@ local function get_lang_from_filename(filename) else dbg('no ts lang for filetype: %s', ft) end - return nil end @@ -58,6 +62,8 @@ function M.parse_buffer(bufnr) ---@type string? local current_filename = nil ---@type string? + local current_ft = nil + ---@type string? local current_lang = nil ---@type integer? local hunk_start = nil @@ -69,9 +75,10 @@ function M.parse_buffer(bufnr) local hunk_lines = {} local function flush_hunk() - if hunk_start and #hunk_lines > 0 and current_lang then + if hunk_start and #hunk_lines > 0 and (current_lang or current_ft) then table.insert(hunks, { filename = current_filename, + ft = current_ft, lang = current_lang, start_line = hunk_start, header_context = hunk_header_context, @@ -90,9 +97,12 @@ function M.parse_buffer(bufnr) if filename then flush_hunk() current_filename = filename - current_lang = get_lang_from_filename(filename) + current_ft = get_ft_from_filename(filename) + current_lang = current_ft and get_lang_from_ft(current_ft) or nil if current_lang then dbg('file: %s -> lang: %s', filename, current_lang) + elseif current_ft then + dbg('file: %s -> ft: %s (no ts parser)', filename, current_ft) end elseif line:match('^@@.-@@') then flush_hunk() @@ -109,6 +119,7 @@ function M.parse_buffer(bufnr) elseif line == '' or line:match('^[MADRC%?!]%s+') or line:match('^%a') then flush_hunk() current_filename = nil + current_ft = nil current_lang = nil end end diff --git a/spec/highlight_spec.lua b/spec/highlight_spec.lua index 1982d43..6b35cac 100644 --- a/spec/highlight_spec.lua +++ b/spec/highlight_spec.lua @@ -31,20 +31,25 @@ describe('highlight', function() local function default_opts(overrides) local opts = { - max_lines = 500, hide_prefix = false, + treesitter = { + enabled = true, + max_lines = 500, + }, + vim = { + enabled = false, + max_lines = 200, + }, highlights = { - treesitter = true, background = false, gutter = false, - vim = false, }, } if overrides then for k, v in pairs(overrides) do - if k == 'highlights' then - for hk, hv in pairs(v) do - opts.highlights[hk] = hv + if type(v) == 'table' and type(opts[k]) == 'table' then + for sk, sv in pairs(v) do + opts[k][sk] = sv end else opts[k] = v @@ -478,7 +483,7 @@ describe('highlight', function() bufnr, ns, hunk, - default_opts({ highlights = { treesitter = false, background = true } }) + default_opts({ treesitter = { enabled = false }, highlights = { background = true } }) ) local extmarks = get_extmarks(bufnr) @@ -511,7 +516,7 @@ describe('highlight', function() bufnr, ns, hunk, - default_opts({ highlights = { treesitter = false, background = true } }) + default_opts({ treesitter = { enabled = false }, highlights = { background = true } }) ) local extmarks = get_extmarks(bufnr) diff --git a/spec/parser_spec.lua b/spec/parser_spec.lua index acbd38f..6532b25 100644 --- a/spec/parser_spec.lua +++ b/spec/parser_spec.lua @@ -48,6 +48,7 @@ describe('parser', function() assert.are.equal(1, #hunks) assert.are.equal('lua/test.lua', hunks[1].filename) + assert.are.equal('lua', hunks[1].ft) assert.are.equal('lua', hunks[1].lang) assert.are.equal(3, hunks[1].start_line) assert.are.equal(3, #hunks[1].lines) @@ -156,6 +157,25 @@ describe('parser', function() delete_buffer(bufnr) end) + it('emits hunk with ft when no ts parser available', function() + local bufnr = create_buffer({ + 'M test.xyz_no_parser', + '@@ -1,1 +1,2 @@', + ' some content', + '+more content', + }) + + vim.filetype.add({ extension = { xyz_no_parser = 'xyz_no_parser_ft' } }) + + local hunks = parser.parse_buffer(bufnr) + + assert.are.equal(1, #hunks) + assert.are.equal('xyz_no_parser_ft', hunks[1].ft) + assert.is_nil(hunks[1].lang) + assert.are.equal(2, #hunks[1].lines) + delete_buffer(bufnr) + end) + it('stops hunk at next file header', function() local bufnr = create_buffer({ 'M test.lua',