From e3dabec4620843a55fdb03d1aeebf5edaa62107e Mon Sep 17 00:00:00 2001 From: patrick96 Date: Tue, 9 Nov 2021 18:24:35 +0100 Subject: Add condition function to module config The function is called with the language and bufnr, if it returns false, the module is disabled for that buffer. This gives the user more fine-grained control over whether a module is started. --- lua/nvim-treesitter/configs.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'lua/nvim-treesitter/configs.lua') diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index a31d7fd7..95b6ab98 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -335,8 +335,9 @@ M.commands = { } -- @param mod: module (string) --- @param ft: filetype (string) -function M.is_enabled(mod, lang) +-- @param lang: the language of the buffer (string) +-- @param bufnr: the bufnr (number) +function M.is_enabled(mod, lang, bufnr) if not parsers.list[lang] or not parsers.has_parser(lang) then return false end @@ -350,6 +351,10 @@ function M.is_enabled(mod, lang) return false end + if module_config.cond and not module_config.cond(lang, bufnr) then + return false + end + for _, parser in pairs(module_config.disable) do if lang == parser then return false @@ -445,7 +450,7 @@ function M.attach_module(mod_name, bufnr, lang) local 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) then + if resolved_mod and not attached_buffers_by_module.has(mod_name, bufnr) and M.is_enabled(mod_name, lang, bufnr) then attached_buffers_by_module.set(mod_name, bufnr, true) resolved_mod.attach(bufnr, lang) end -- cgit v1.2.3