From 8e2807d09e1a5bd226b1ad77a71f3ac6c7fcd36e Mon Sep 17 00:00:00 2001 From: echasnovski Date: Sun, 31 Jan 2021 12:43:08 +0200 Subject: Implement `TSBufToggle`. --- doc/nvim-treesitter.txt | 7 +++++++ lua/nvim-treesitter/configs.lua | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/doc/nvim-treesitter.txt b/doc/nvim-treesitter.txt index b13cdacee..d253194c7 100644 --- a/doc/nvim-treesitter.txt +++ b/doc/nvim-treesitter.txt @@ -229,6 +229,13 @@ A list of modules can be found at |:TSModuleInfo| :TSBufDisable {module}~ Disable {module} on the current buffer +A list of modules can be found at |:TSModuleInfo| + + *:TSBufToggle* +:TSBufToggle {module}~ + +Toggle (enable if disabled, disable if enabled) {module} on the current +buffer. A list of modules can be found at |:TSModuleInfo| *:TSBufEnableAll* diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index b0d38244e..9f7627a00 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -136,6 +136,21 @@ local function disable_all(mod) config_mod.enable = false end +-- Toggles a module for a buffer +-- @param mod path to module +-- @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) + + if attached_buffers_by_module.has(mod, bufnr) then + disable_module(mod, bufnr) + else + enable_module(mod, bufnr, lang) + end +end + -- Recurses through all modules including submodules -- @param accumulator function called for each module -- @param root root configuration table to start at @@ -181,6 +196,13 @@ M.commands = { "-complete=custom,nvim_treesitter#available_modules", }, }, + TSBufToggle = { + run = toggle_module, + args = { + "-nargs=1", + "-complete=custom,nvim_treesitter#available_modules", + }, + }, TSEnableAll = { run = enable_all, args = { -- cgit v1.2.3-70-g09d2