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

View file

@ -72,15 +72,13 @@ local function highlight_buffer(bufnr)
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
local hunks =
parser.parse_buffer(bufnr, config.languages, config.disabled_languages, config.debug)
local hunks = parser.parse_buffer(bufnr, config.languages, config.disabled_languages)
dbg('found %d hunks in buffer %d', #hunks, bufnr)
for _, hunk in ipairs(hunks) do
highlight.highlight_hunk(bufnr, ns, hunk, {
max_lines = config.max_lines_per_hunk,
conceal_prefixes = config.conceal_prefixes,
highlights = config.highlights,
debug = config.debug,
})
end
end
@ -160,7 +158,31 @@ end
---@param opts? fugitive-ts.Config
function M.setup(opts)
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)
parser.set_debug(config.debug)
highlight.set_debug(config.debug)
end
return M

View file

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

View file

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