feat: cleanup config options
This commit is contained in:
parent
6c4c1e8e49
commit
69943a09c4
6 changed files with 98 additions and 126 deletions
|
|
@ -65,8 +65,9 @@ local function highlight_text(bufnr, ns, hunk, col_offset, text, lang)
|
|||
end
|
||||
|
||||
---@class fugitive-ts.HunkOpts
|
||||
---@field max_lines integer
|
||||
---@field hide_prefix boolean
|
||||
---@field treesitter fugitive-ts.TreesitterConfig
|
||||
---@field vim fugitive-ts.VimConfig
|
||||
---@field highlights fugitive-ts.Highlights
|
||||
|
||||
---@param bufnr integer
|
||||
|
|
@ -79,13 +80,14 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
|||
return
|
||||
end
|
||||
|
||||
if #hunk.lines > opts.max_lines then
|
||||
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,
|
||||
opts.max_lines
|
||||
max_lines
|
||||
)
|
||||
return
|
||||
end
|
||||
|
|
@ -120,7 +122,7 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
|||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, extmark_opts)
|
||||
end
|
||||
|
||||
if line_len > 1 and opts.highlights.treesitter then
|
||||
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',
|
||||
|
|
@ -129,7 +131,7 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
|
|||
end
|
||||
end
|
||||
|
||||
if not opts.highlights.treesitter then
|
||||
if not opts.treesitter.enabled then
|
||||
return
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,17 +1,22 @@
|
|||
---@class fugitive-ts.Highlights
|
||||
---@field treesitter boolean
|
||||
---@field background boolean
|
||||
---@field gutter boolean
|
||||
---@field vim boolean
|
||||
|
||||
---@class fugitive-ts.TreesitterConfig
|
||||
---@field enabled boolean
|
||||
---@field max_lines integer
|
||||
|
||||
---@class fugitive-ts.VimConfig
|
||||
---@field enabled boolean
|
||||
---@field max_lines integer
|
||||
|
||||
---@class fugitive-ts.Config
|
||||
---@field enabled boolean
|
||||
---@field debug boolean
|
||||
---@field languages table<string, string>
|
||||
---@field disabled_languages string[]
|
||||
---@field debounce_ms integer
|
||||
---@field max_lines_per_hunk integer
|
||||
---@field hide_prefix boolean
|
||||
---@field treesitter fugitive-ts.TreesitterConfig
|
||||
---@field vim fugitive-ts.VimConfig
|
||||
---@field highlights fugitive-ts.Highlights
|
||||
|
||||
---@class fugitive-ts
|
||||
|
|
@ -61,16 +66,19 @@ end
|
|||
local default_config = {
|
||||
enabled = true,
|
||||
debug = false,
|
||||
languages = {},
|
||||
disabled_languages = {},
|
||||
debounce_ms = 0,
|
||||
max_lines_per_hunk = 500,
|
||||
hide_prefix = false,
|
||||
treesitter = {
|
||||
enabled = true,
|
||||
max_lines = 500,
|
||||
},
|
||||
vim = {
|
||||
enabled = false,
|
||||
max_lines = 200,
|
||||
},
|
||||
highlights = {
|
||||
treesitter = true,
|
||||
background = true,
|
||||
gutter = true,
|
||||
vim = false,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -102,12 +110,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)
|
||||
local hunks = parser.parse_buffer(bufnr)
|
||||
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,
|
||||
hide_prefix = config.hide_prefix,
|
||||
treesitter = config.treesitter,
|
||||
vim = config.vim,
|
||||
highlights = config.highlights,
|
||||
})
|
||||
end
|
||||
|
|
@ -192,20 +201,31 @@ function M.setup(opts)
|
|||
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 },
|
||||
hide_prefix = { opts.hide_prefix, 'boolean', true },
|
||||
treesitter = { opts.treesitter, 'table', true },
|
||||
vim = { opts.vim, 'table', true },
|
||||
highlights = { opts.highlights, 'table', true },
|
||||
})
|
||||
|
||||
if opts.treesitter then
|
||||
vim.validate({
|
||||
['treesitter.enabled'] = { opts.treesitter.enabled, 'boolean', true },
|
||||
['treesitter.max_lines'] = { opts.treesitter.max_lines, 'number', true },
|
||||
})
|
||||
end
|
||||
|
||||
if opts.vim then
|
||||
vim.validate({
|
||||
['vim.enabled'] = { opts.vim.enabled, 'boolean', true },
|
||||
['vim.max_lines'] = { opts.vim.max_lines, 'number', true },
|
||||
})
|
||||
end
|
||||
|
||||
if opts.highlights then
|
||||
vim.validate({
|
||||
['highlights.treesitter'] = { opts.highlights.treesitter, 'boolean', true },
|
||||
['highlights.background'] = { opts.highlights.background, 'boolean', true },
|
||||
['highlights.gutter'] = { opts.highlights.gutter, 'boolean', true },
|
||||
['highlights.vim'] = { opts.highlights.vim, 'boolean', true },
|
||||
})
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -26,19 +26,8 @@ local function dbg(msg, ...)
|
|||
end
|
||||
|
||||
---@param filename string
|
||||
---@param custom_langs? table<string, string>
|
||||
---@param disabled_langs? string[]
|
||||
---@return string?
|
||||
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
|
||||
dbg('lang disabled: %s', lang)
|
||||
return nil
|
||||
end
|
||||
return lang
|
||||
end
|
||||
|
||||
local function get_lang_from_filename(filename)
|
||||
local ft = vim.filetype.match({ filename = filename })
|
||||
if not ft then
|
||||
dbg('no filetype for: %s', filename)
|
||||
|
|
@ -47,10 +36,6 @@ local function get_lang_from_filename(filename, custom_langs, disabled_langs)
|
|||
|
||||
local lang = vim.treesitter.language.get_lang(ft)
|
||||
if lang then
|
||||
if disabled_langs and vim.tbl_contains(disabled_langs, lang) then
|
||||
dbg('lang disabled: %s', lang)
|
||||
return nil
|
||||
end
|
||||
local ok = pcall(vim.treesitter.language.inspect, lang)
|
||||
if ok then
|
||||
return lang
|
||||
|
|
@ -64,10 +49,8 @@ local function get_lang_from_filename(filename, custom_langs, disabled_langs)
|
|||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param custom_langs? table<string, string>
|
||||
---@param disabled_langs? string[]
|
||||
---@return fugitive-ts.Hunk[]
|
||||
function M.parse_buffer(bufnr, custom_langs, disabled_langs)
|
||||
function M.parse_buffer(bufnr)
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
---@type fugitive-ts.Hunk[]
|
||||
local hunks = {}
|
||||
|
|
@ -107,7 +90,7 @@ function M.parse_buffer(bufnr, custom_langs, disabled_langs)
|
|||
if filename then
|
||||
flush_hunk()
|
||||
current_filename = filename
|
||||
current_lang = get_lang_from_filename(filename, custom_langs, disabled_langs)
|
||||
current_lang = get_lang_from_filename(filename)
|
||||
if current_lang then
|
||||
dbg('file: %s -> lang: %s', filename, current_lang)
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue