feat: highlights, debug fixes, and config validation

This commit is contained in:
Barrett Ruth 2026-02-02 00:22:44 -05:00
parent 00bf84cb05
commit 935eb8f7ed
4 changed files with 69 additions and 57 deletions

View file

@ -1,8 +1,18 @@
local M = {} local M = {}
local debug_enabled = false
---@param enabled boolean
function M.set_debug(enabled)
debug_enabled = enabled
end
---@param msg string ---@param msg string
---@param ... any ---@param ... any
local function dbg(msg, ...) local function dbg(msg, ...)
if not debug_enabled then
return
end
local formatted = string.format(msg, ...) local formatted = string.format(msg, ...)
vim.notify('[fugitive-ts] ' .. formatted, vim.log.levels.DEBUG) vim.notify('[fugitive-ts] ' .. formatted, vim.log.levels.DEBUG)
end end
@ -13,9 +23,8 @@ end
---@param col_offset integer ---@param col_offset integer
---@param text string ---@param text string
---@param lang string ---@param lang string
---@param debug? boolean
---@return integer ---@return integer
local function highlight_text(bufnr, ns, hunk, col_offset, text, lang, debug) local function highlight_text(bufnr, ns, hunk, col_offset, text, lang)
local ok, parser_obj = pcall(vim.treesitter.get_string_parser, text, lang) local ok, parser_obj = pcall(vim.treesitter.get_string_parser, text, lang)
if not ok or not parser_obj then if not ok or not parser_obj then
return 0 return 0
@ -59,7 +68,6 @@ end
---@field max_lines integer ---@field max_lines integer
---@field conceal_prefixes boolean ---@field conceal_prefixes boolean
---@field highlights fugitive-ts.Highlights ---@field highlights fugitive-ts.Highlights
---@field debug boolean
---@param bufnr integer ---@param bufnr integer
---@param ns integer ---@param ns integer
@ -72,7 +80,6 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
end end
if #hunk.lines > opts.max_lines then if #hunk.lines > opts.max_lines then
if opts.debug then
dbg( dbg(
'skipping hunk %s:%d (%d lines > %d max)', 'skipping hunk %s:%d (%d lines > %d max)',
hunk.filename, hunk.filename,
@ -80,7 +87,6 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
#hunk.lines, #hunk.lines,
opts.max_lines opts.max_lines
) )
end
return return
end end
@ -97,25 +103,19 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
local ok, parser_obj = pcall(vim.treesitter.get_string_parser, code, lang) local ok, parser_obj = pcall(vim.treesitter.get_string_parser, code, lang)
if not ok or not parser_obj then if not ok or not parser_obj then
if opts.debug then
dbg('failed to create parser for lang: %s', lang) dbg('failed to create parser for lang: %s', lang)
end
return return
end end
local trees = parser_obj:parse() local trees = parser_obj:parse()
if not trees or #trees == 0 then if not trees or #trees == 0 then
if opts.debug then
dbg('parse returned no trees for lang: %s', lang) dbg('parse returned no trees for lang: %s', lang)
end
return return
end end
local query = vim.treesitter.query.get(lang, 'highlights') local query = vim.treesitter.query.get(lang, 'highlights')
if not query then if not query then
if opts.debug then
dbg('no highlights query for lang: %s', lang) dbg('no highlights query for lang: %s', lang)
end
return return
end end
@ -126,16 +126,9 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
hl_group = 'Normal', hl_group = 'Normal',
priority = 199, priority = 199,
}) })
local header_extmarks = highlight_text( local header_extmarks =
bufnr, highlight_text(bufnr, ns, hunk, hunk.header_context_col, hunk.header_context, lang)
ns, if header_extmarks > 0 then
hunk,
hunk.header_context_col,
hunk.header_context,
lang,
opts.debug
)
if opts.debug and header_extmarks > 0 then
dbg('header %s:%d applied %d extmarks', hunk.filename, hunk.start_line, header_extmarks) dbg('header %s:%d applied %d extmarks', hunk.filename, hunk.start_line, header_extmarks)
end end
end end
@ -196,9 +189,7 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
extmark_count = extmark_count + 1 extmark_count = extmark_count + 1
end end
if opts.debug then
dbg('hunk %s:%d applied %d extmarks', hunk.filename, hunk.start_line, extmark_count) dbg('hunk %s:%d applied %d extmarks', hunk.filename, hunk.start_line, extmark_count)
end end
end
return M return M

View file

@ -72,15 +72,13 @@ local function highlight_buffer(bufnr)
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1) vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
local hunks = local hunks = parser.parse_buffer(bufnr, config.languages, config.disabled_languages)
parser.parse_buffer(bufnr, config.languages, config.disabled_languages, config.debug)
dbg('found %d hunks in buffer %d', #hunks, bufnr) dbg('found %d hunks in buffer %d', #hunks, bufnr)
for _, hunk in ipairs(hunks) do for _, hunk in ipairs(hunks) do
highlight.highlight_hunk(bufnr, ns, hunk, { highlight.highlight_hunk(bufnr, ns, hunk, {
max_lines = config.max_lines_per_hunk, max_lines = config.max_lines_per_hunk,
conceal_prefixes = config.conceal_prefixes, conceal_prefixes = config.conceal_prefixes,
highlights = config.highlights, highlights = config.highlights,
debug = config.debug,
}) })
end end
end end
@ -160,7 +158,31 @@ end
---@param opts? fugitive-ts.Config ---@param opts? fugitive-ts.Config
function M.setup(opts) function M.setup(opts)
opts = opts or {} opts = opts or {}
vim.validate({
enabled = { opts.enabled, 'boolean', true },
debug = { opts.debug, 'boolean', true },
languages = { opts.languages, 'table', true },
disabled_languages = { opts.disabled_languages, 'table', true },
debounce_ms = { opts.debounce_ms, 'number', true },
max_lines_per_hunk = { opts.max_lines_per_hunk, 'number', true },
conceal_prefixes = { opts.conceal_prefixes, 'boolean', true },
highlights = { opts.highlights, 'table', true },
})
if opts.highlights then
vim.validate({
['highlights.treesitter'] = { opts.highlights.treesitter, 'boolean', true },
['highlights.headers'] = { opts.highlights.headers, 'boolean', true },
['highlights.background'] = { opts.highlights.background, 'boolean', true },
['highlights.linenr'] = { opts.highlights.linenr, 'boolean', true },
['highlights.vim'] = { opts.highlights.vim, 'boolean', true },
})
end
config = vim.tbl_deep_extend('force', default_config, opts) config = vim.tbl_deep_extend('force', default_config, opts)
parser.set_debug(config.debug)
highlight.set_debug(config.debug)
end end
return M return M

View file

@ -8,9 +8,19 @@
local M = {} local M = {}
local debug_enabled = false
---@param enabled boolean
function M.set_debug(enabled)
debug_enabled = enabled
end
---@param msg string ---@param msg string
---@param ... any ---@param ... any
local function dbg(msg, ...) local function dbg(msg, ...)
if not debug_enabled then
return
end
local formatted = string.format(msg, ...) local formatted = string.format(msg, ...)
vim.notify('[fugitive-ts] ' .. formatted, vim.log.levels.DEBUG) vim.notify('[fugitive-ts] ' .. formatted, vim.log.levels.DEBUG)
end end
@ -18,15 +28,12 @@ end
---@param filename string ---@param filename string
---@param custom_langs? table<string, string> ---@param custom_langs? table<string, string>
---@param disabled_langs? string[] ---@param disabled_langs? string[]
---@param debug? boolean
---@return string? ---@return string?
local function get_lang_from_filename(filename, custom_langs, disabled_langs, debug) local function get_lang_from_filename(filename, custom_langs, disabled_langs)
if custom_langs and custom_langs[filename] then if custom_langs and custom_langs[filename] then
local lang = custom_langs[filename] local lang = custom_langs[filename]
if disabled_langs and vim.tbl_contains(disabled_langs, lang) then if disabled_langs and vim.tbl_contains(disabled_langs, lang) then
if debug then
dbg('lang disabled: %s', lang) dbg('lang disabled: %s', lang)
end
return nil return nil
end end
return lang return lang
@ -34,28 +41,22 @@ local function get_lang_from_filename(filename, custom_langs, disabled_langs, de
local ft = vim.filetype.match({ filename = filename }) local ft = vim.filetype.match({ filename = filename })
if not ft then if not ft then
if debug then
dbg('no filetype for: %s', filename) dbg('no filetype for: %s', filename)
end
return nil return nil
end end
local lang = vim.treesitter.language.get_lang(ft) local lang = vim.treesitter.language.get_lang(ft)
if lang then if lang then
if disabled_langs and vim.tbl_contains(disabled_langs, lang) then if disabled_langs and vim.tbl_contains(disabled_langs, lang) then
if debug then
dbg('lang disabled: %s', lang) dbg('lang disabled: %s', lang)
end
return nil return nil
end end
local ok = pcall(vim.treesitter.language.inspect, lang) local ok = pcall(vim.treesitter.language.inspect, lang)
if ok then if ok then
return lang return lang
end end
if debug then
dbg('no parser for lang: %s (ft: %s)', lang, ft) dbg('no parser for lang: %s (ft: %s)', lang, ft)
end else
elseif debug then
dbg('no ts lang for filetype: %s', ft) dbg('no ts lang for filetype: %s', ft)
end end
@ -65,9 +66,8 @@ end
---@param bufnr integer ---@param bufnr integer
---@param custom_langs? table<string, string> ---@param custom_langs? table<string, string>
---@param disabled_langs? string[] ---@param disabled_langs? string[]
---@param debug? boolean
---@return fugitive-ts.Hunk[] ---@return fugitive-ts.Hunk[]
function M.parse_buffer(bufnr, custom_langs, disabled_langs, debug) function M.parse_buffer(bufnr, custom_langs, disabled_langs)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
---@type fugitive-ts.Hunk[] ---@type fugitive-ts.Hunk[]
local hunks = {} local hunks = {}
@ -107,8 +107,8 @@ function M.parse_buffer(bufnr, custom_langs, disabled_langs, debug)
if filename then if filename then
flush_hunk() flush_hunk()
current_filename = filename current_filename = filename
current_lang = get_lang_from_filename(filename, custom_langs, disabled_langs, debug) current_lang = get_lang_from_filename(filename, custom_langs, disabled_langs)
if debug and current_lang then if current_lang then
dbg('file: %s -> lang: %s', filename, current_lang) dbg('file: %s -> lang: %s', filename, current_lang)
end end
elseif line:match('^@@.-@@') then elseif line:match('^@@.-@@') then

View file

@ -36,7 +36,6 @@ describe('highlight', function()
linenr = false, linenr = false,
vim = false, vim = false,
}, },
debug = false,
} }
if overrides then if overrides then
for k, v in pairs(overrides) do for k, v in pairs(overrides) do