mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 11:06:54 -04:00
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:
parent
e3adb39586
commit
7141393f12
3 changed files with 45 additions and 19 deletions
|
|
@ -136,8 +136,8 @@ Each module can also be enabled or disabled interactively through the following
|
|||
```vim
|
||||
:TSBufEnable {module} " enable 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.
|
||||
:TSDisableAll {module} [{ft}] " disable module on every buffer. If filetype is specified, disable only for this filetype.
|
||||
:TSEnable {module} [{ft}] " enable module on every buffer. If filetype is specified, enable 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
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -269,8 +269,8 @@ Toggle (enable if disabled, disable if enabled) {module} on the current
|
|||
buffer.
|
||||
A list of modules can be found at |:TSModuleInfo|
|
||||
|
||||
*:TSEnableAll*
|
||||
:TSEnableAll {module} [{language}]~
|
||||
*:TSEnable*
|
||||
:TSEnable {module} [{language}]~
|
||||
|
||||
Enable {module} for the session.
|
||||
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 languages can be found at |:TSInstallInfo|
|
||||
|
||||
*:TSDisableAll*
|
||||
:TSDisableAll {module} [{language}]~
|
||||
*:TSDisable*
|
||||
:TSDisable {module} [{language}]~
|
||||
|
||||
Disable {module} for the session.
|
||||
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 languages can be found at |:TSInstallInfo|
|
||||
|
||||
*:TSToggleAll*
|
||||
:TSToggleAll {module} [{language}]~
|
||||
*:TSToggle*
|
||||
:TSToggle {module} [{language}]~
|
||||
|
||||
Toggle (enable if disabled, disable if enabled) {module} for the session.
|
||||
If {language} is specified, toggle module for the session only for this
|
||||
|
|
|
|||
|
|
@ -72,8 +72,22 @@ end
|
|||
-- @param bufnr buffer number, defaults to current buffer
|
||||
-- @param lang language, defaults to current language
|
||||
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()
|
||||
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)
|
||||
end
|
||||
|
||||
|
|
@ -101,19 +115,28 @@ local function enable_all(mod)
|
|||
return
|
||||
end
|
||||
|
||||
enable_mod_conf_autocmd(mod)
|
||||
config_mod.enable = true
|
||||
config_mod.enabled_buffers = nil
|
||||
|
||||
for _, bufnr in pairs(api.nvim_list_bufs()) do
|
||||
enable_module(mod, bufnr)
|
||||
end
|
||||
|
||||
enable_mod_conf_autocmd(mod)
|
||||
config_mod.enable = true
|
||||
end
|
||||
|
||||
-- Disables and detaches the module for a buffer.
|
||||
-- @param mod path to module
|
||||
-- @param bufnr buffer number, defaults to current buffer
|
||||
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()
|
||||
if module.enabled_buffers then
|
||||
module.enabled_buffers[bufnr] = false
|
||||
end
|
||||
M.detach_module(mod, bufnr)
|
||||
end
|
||||
|
||||
|
|
@ -136,16 +159,17 @@ end
|
|||
-- @param mod path to module
|
||||
local function disable_all(mod)
|
||||
local config_mod = M.get_module(mod)
|
||||
if not config_mod or not config_mod.enable then
|
||||
if not config_mod then
|
||||
return
|
||||
end
|
||||
|
||||
config_mod.enabled_buffers = nil
|
||||
disable_mod_conf_autocmd(mod)
|
||||
config_mod.enable = false
|
||||
|
||||
for _, bufnr in pairs(api.nvim_list_bufs()) do
|
||||
disable_module(mod, bufnr)
|
||||
end
|
||||
|
||||
disable_mod_conf_autocmd(mod)
|
||||
config_mod.enable = false
|
||||
end
|
||||
|
||||
-- Toggles a module for a buffer
|
||||
|
|
@ -291,21 +315,21 @@ M.commands = {
|
|||
"-complete=custom,nvim_treesitter#available_modules",
|
||||
},
|
||||
},
|
||||
TSEnableAll = {
|
||||
TSEnable = {
|
||||
run = enable_all,
|
||||
args = {
|
||||
"-nargs=+",
|
||||
"-complete=custom,nvim_treesitter#available_modules",
|
||||
},
|
||||
},
|
||||
TSDisableAll = {
|
||||
TSDisable = {
|
||||
run = disable_all,
|
||||
args = {
|
||||
"-nargs=+",
|
||||
"-complete=custom,nvim_treesitter#available_modules",
|
||||
},
|
||||
},
|
||||
TSToggleAll = {
|
||||
TSToggle = {
|
||||
run = toggle_all,
|
||||
args = {
|
||||
"-nargs=+",
|
||||
|
|
@ -347,7 +371,9 @@ function M.is_enabled(mod, lang, bufnr)
|
|||
return false
|
||||
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
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue