fix(modules): enabling disabling per buffer and globally

When a module is disabled by default in the config, running
TSBufEnable will not enable the module because the is_enabled
function will always return false, thus the module not being enabled.
Also, disabling/enabling the buffers is flaky.

This commit adds per buffer check when the module is not disabled. It
also makes the enable and disable more indempotent.

i've also renamed TS*All to TS*.

Fixes #2754
This commit is contained in:
kiyan 2022-04-09 14:41:07 +02:00 committed by Christian Clason
parent e3adb39586
commit 7141393f12
3 changed files with 45 additions and 19 deletions

View file

@ -136,8 +136,8 @@ Each module can also be enabled or disabled interactively through the following
```vim ```vim
:TSBufEnable {module} " enable module on current buffer :TSBufEnable {module} " enable module on current buffer
:TSBufDisable {module} " disable module on current buffer :TSBufDisable {module} " disable module on current buffer
:TSEnableAll {module} [{ft}] " enable module on every buffer. If filetype is specified, enable only for this filetype. :TSEnable {module} [{ft}] " enable module on every buffer. If filetype is specified, enable only for this filetype.
:TSDisableAll {module} [{ft}] " disable module on every buffer. If filetype is specified, disable only for this filetype. :TSDisable {module} [{ft}] " disable module on every buffer. If filetype is specified, disable only for this filetype.
:TSModuleInfo [{module}] " list information about modules state for each filetype :TSModuleInfo [{module}] " list information about modules state for each filetype
``` ```

View file

@ -269,8 +269,8 @@ Toggle (enable if disabled, disable if enabled) {module} on the current
buffer. buffer.
A list of modules can be found at |:TSModuleInfo| A list of modules can be found at |:TSModuleInfo|
*:TSEnableAll* *:TSEnable*
:TSEnableAll {module} [{language}]~ :TSEnable {module} [{language}]~
Enable {module} for the session. Enable {module} for the session.
If {language} is specified, enable module for the session only for this If {language} is specified, enable module for the session only for this
@ -278,8 +278,8 @@ particular language.
A list of modules can be found at |:TSModuleInfo| A list of modules can be found at |:TSModuleInfo|
A list of languages can be found at |:TSInstallInfo| A list of languages can be found at |:TSInstallInfo|
*:TSDisableAll* *:TSDisable*
:TSDisableAll {module} [{language}]~ :TSDisable {module} [{language}]~
Disable {module} for the session. Disable {module} for the session.
If {language} is specified, disable module for the session only for this If {language} is specified, disable module for the session only for this
@ -287,8 +287,8 @@ particular language.
A list of modules can be found at |:TSModuleInfo| A list of modules can be found at |:TSModuleInfo|
A list of languages can be found at |:TSInstallInfo| A list of languages can be found at |:TSInstallInfo|
*:TSToggleAll* *:TSToggle*
:TSToggleAll {module} [{language}]~ :TSToggle {module} [{language}]~
Toggle (enable if disabled, disable if enabled) {module} for the session. Toggle (enable if disabled, disable if enabled) {module} for the session.
If {language} is specified, toggle module for the session only for this If {language} is specified, toggle module for the session only for this

View file

@ -72,8 +72,22 @@ end
-- @param bufnr buffer number, defaults to current buffer -- @param bufnr buffer number, defaults to current buffer
-- @param lang language, defaults to current language -- @param lang language, defaults to current language
local function enable_module(mod, bufnr, lang) local function enable_module(mod, bufnr, lang)
local module = M.get_module(mod)
if not module then
return
end
bufnr = bufnr or api.nvim_get_current_buf() bufnr = bufnr or api.nvim_get_current_buf()
lang = lang or parsers.get_buf_lang(bufnr) lang = lang or parsers.get_buf_lang(bufnr)
if not module.enable then
if module.enabled_buffers then
module.enabled_buffers[bufnr] = true
else
module.enabled_buffers = { [bufnr] = true }
end
end
M.attach_module(mod, bufnr, lang) M.attach_module(mod, bufnr, lang)
end end
@ -101,19 +115,28 @@ local function enable_all(mod)
return return
end end
enable_mod_conf_autocmd(mod)
config_mod.enable = true
config_mod.enabled_buffers = nil
for _, bufnr in pairs(api.nvim_list_bufs()) do for _, bufnr in pairs(api.nvim_list_bufs()) do
enable_module(mod, bufnr) enable_module(mod, bufnr)
end end
enable_mod_conf_autocmd(mod)
config_mod.enable = true
end end
-- Disables and detaches the module for a buffer. -- Disables and detaches the module for a buffer.
-- @param mod path to module -- @param mod path to module
-- @param bufnr buffer number, defaults to current buffer -- @param bufnr buffer number, defaults to current buffer
local function disable_module(mod, bufnr) local function disable_module(mod, bufnr)
local module = M.get_module(mod)
if not module then
return
end
bufnr = bufnr or api.nvim_get_current_buf() bufnr = bufnr or api.nvim_get_current_buf()
if module.enabled_buffers then
module.enabled_buffers[bufnr] = false
end
M.detach_module(mod, bufnr) M.detach_module(mod, bufnr)
end end
@ -136,16 +159,17 @@ end
-- @param mod path to module -- @param mod path to module
local function disable_all(mod) local function disable_all(mod)
local config_mod = M.get_module(mod) local config_mod = M.get_module(mod)
if not config_mod or not config_mod.enable then if not config_mod then
return return
end end
config_mod.enabled_buffers = nil
disable_mod_conf_autocmd(mod)
config_mod.enable = false
for _, bufnr in pairs(api.nvim_list_bufs()) do for _, bufnr in pairs(api.nvim_list_bufs()) do
disable_module(mod, bufnr) disable_module(mod, bufnr)
end end
disable_mod_conf_autocmd(mod)
config_mod.enable = false
end end
-- Toggles a module for a buffer -- Toggles a module for a buffer
@ -291,21 +315,21 @@ M.commands = {
"-complete=custom,nvim_treesitter#available_modules", "-complete=custom,nvim_treesitter#available_modules",
}, },
}, },
TSEnableAll = { TSEnable = {
run = enable_all, run = enable_all,
args = { args = {
"-nargs=+", "-nargs=+",
"-complete=custom,nvim_treesitter#available_modules", "-complete=custom,nvim_treesitter#available_modules",
}, },
}, },
TSDisableAll = { TSDisable = {
run = disable_all, run = disable_all,
args = { args = {
"-nargs=+", "-nargs=+",
"-complete=custom,nvim_treesitter#available_modules", "-complete=custom,nvim_treesitter#available_modules",
}, },
}, },
TSToggleAll = { TSToggle = {
run = toggle_all, run = toggle_all,
args = { args = {
"-nargs=+", "-nargs=+",
@ -347,7 +371,9 @@ function M.is_enabled(mod, lang, bufnr)
return false return false
end end
if not module_config.enable or not module_config.is_supported(lang) then local buffer_enabled = module_config.enabled_buffers and module_config.enabled_buffers[bufnr]
local config_enabled = module_config.enable or buffer_enabled
if not config_enabled or not module_config.is_supported(lang) then
return false return false
end end