chore: expose start and stop highlighter

Decompose highlighter module in small functions to allow exporting a
start and stop functions without the syntax change.
Also fix linter issues in configs.lua
This commit is contained in:
kiyan 2022-02-06 14:46:24 +01:00 committed by Kiyan
parent 58a4897e6d
commit a0b7cece2c
2 changed files with 39 additions and 21 deletions

View file

@ -71,8 +71,8 @@ end
-- @param bufnr buffer number, defaults to current buffer
-- @param lang language, defaults to current language
local function enable_module(mod, bufnr, lang)
local bufnr = bufnr or api.nvim_get_current_buf()
local lang = lang or parsers.get_buf_lang(bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
lang = lang or parsers.get_buf_lang(bufnr)
M.attach_module(mod, bufnr, lang)
end
@ -112,7 +112,7 @@ end
-- @param mod path to module
-- @param bufnr buffer number, defaults to current buffer
local function disable_module(mod, bufnr)
local bufnr = bufnr or api.nvim_get_current_buf()
bufnr = bufnr or api.nvim_get_current_buf()
M.detach_module(mod, bufnr)
end
@ -152,8 +152,8 @@ end
-- @param bufnr buffer number, defaults to current buffer
-- @param lang language, defaults to current language
local function toggle_module(mod, bufnr, lang)
local bufnr = bufnr or api.nvim_get_current_buf()
local lang = lang or parsers.get_buf_lang(bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
lang = lang or parsers.get_buf_lang(bufnr)
if attached_buffers_by_module.has(mod, bufnr) then
disable_module(mod, bufnr)
@ -182,7 +182,7 @@ end
-- @param root root configuration table to start at
-- @param path prefix path
local function recurse_modules(accumulator, root, path)
local root = root or config.modules
root = root or config.modules
for name, module in pairs(root) do
local new_path = path and (path .. "." .. name) or name
@ -449,8 +449,8 @@ end
-- @param bufnr the bufnr
-- @param lang the language of the buffer
function M.attach_module(mod_name, bufnr, lang)
local bufnr = bufnr or api.nvim_get_current_buf()
local lang = lang or parsers.get_buf_lang(bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
lang = lang or parsers.get_buf_lang(bufnr)
local resolved_mod = resolve_module(mod_name)
if resolved_mod and not attached_buffers_by_module.has(mod_name, bufnr) and M.is_enabled(mod_name, lang, bufnr) then
@ -464,7 +464,7 @@ end
-- @param bufnr the bufnr
function M.detach_module(mod_name, bufnr)
local resolved_mod = resolve_module(mod_name)
local bufnr = bufnr or api.nvim_get_current_buf()
bufnr = bufnr or api.nvim_get_current_buf()
if resolved_mod and attached_buffers_by_module.has(mod_name, bufnr) then
attached_buffers_by_module.remove(mod_name, bufnr)

View file

@ -103,30 +103,48 @@ hlmap["type.builtin"] = "TSTypeBuiltin"
hlmap["variable"] = "TSVariable"
hlmap["variable.builtin"] = "TSVariableBuiltin"
function M.attach(bufnr, lang)
local function should_enable_vim_regex(config, lang)
local additional_hl = config.additional_vim_regex_highlighting
local is_table = type(additional_hl) == "table"
return additional_hl and (not is_table or vim.tbl_contains(additional_hl, lang))
end
local function enable_syntax(bufnr)
api.nvim_buf_set_option(bufnr, "syntax", "ON")
end
function M.get_config()
return configs.get_module "highlight"
end
function M.stop(bufnr)
if ts.highlighter.active[bufnr] then
ts.highlighter.active[bufnr]:destroy()
end
end
function M.start(bufnr, lang)
local parser = parsers.get_parser(bufnr, lang)
local config = configs.get_module "highlight"
local config = M.get_config()
for k, v in pairs(config.custom_captures) do
hlmap[k] = v
end
ts.highlighter.new(parser, {})
end
local is_table = type(config.additional_vim_regex_highlighting) == "table"
if
config.additional_vim_regex_highlighting
and (not is_table or vim.tbl_contains(config.additional_vim_regex_highlighting, lang))
then
api.nvim_buf_set_option(bufnr, "syntax", "ON")
function M.attach(bufnr, lang)
M.start(bufnr, lang)
if should_enable_vim_regex(M.get_config(), lang) then
enable_syntax(bufnr)
end
end
function M.detach(bufnr)
if ts.highlighter.active[bufnr] then
ts.highlighter.active[bufnr]:destroy()
end
api.nvim_buf_set_option(bufnr, "syntax", "ON")
M.stop(bufnr)
enable_syntax(bufnr)
end
return M