aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorkiyan <yazdani.kiyan@protonmail.com>2022-02-06 14:46:24 +0100
committerKiyan <yazdani.kiyan@protonmail.com>2022-02-06 15:12:34 +0100
commita0b7cece2c5fd0f78fa21aa51c171a270551ed13 (patch)
treec3437085e5179aea902e3615744030bd8e65bf10 /lua
parentrefacto: deprecate used_by in parsers.lua (diff)
downloadnvim-treesitter-a0b7cece2c5fd0f78fa21aa51c171a270551ed13.tar
nvim-treesitter-a0b7cece2c5fd0f78fa21aa51c171a270551ed13.tar.gz
nvim-treesitter-a0b7cece2c5fd0f78fa21aa51c171a270551ed13.tar.bz2
nvim-treesitter-a0b7cece2c5fd0f78fa21aa51c171a270551ed13.tar.lz
nvim-treesitter-a0b7cece2c5fd0f78fa21aa51c171a270551ed13.tar.xz
nvim-treesitter-a0b7cece2c5fd0f78fa21aa51c171a270551ed13.tar.zst
nvim-treesitter-a0b7cece2c5fd0f78fa21aa51c171a270551ed13.zip
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
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-treesitter/configs.lua18
-rw-r--r--lua/nvim-treesitter/highlight.lua42
2 files changed, 39 insertions, 21 deletions
diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua
index 5fddd1620..8f0f885a1 100644
--- a/lua/nvim-treesitter/configs.lua
+++ b/lua/nvim-treesitter/configs.lua
@@ -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)
diff --git a/lua/nvim-treesitter/highlight.lua b/lua/nvim-treesitter/highlight.lua
index 734a17fd8..ce7cf8117 100644
--- a/lua/nvim-treesitter/highlight.lua
+++ b/lua/nvim-treesitter/highlight.lua
@@ -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