From 507527711fdd8f701544024aeb1a9a068f986d89 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Wed, 14 Dec 2022 22:27:23 +0100 Subject: [PATCH] feat: allow to set keymappings to `false` Fixes #3954 --- README.md | 2 +- lua/nvim-treesitter/configs.lua | 2 +- lua/nvim-treesitter/incremental_selection.lua | 44 ++++++++++--------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index a3db5d966..d07855710 100644 --- a/README.md +++ b/README.md @@ -366,7 +366,7 @@ require'nvim-treesitter.configs'.setup { incremental_selection = { enable = true, keymaps = { - init_selection = "gnn", + init_selection = "gnn", -- set to `false` to disable one of the mappings node_incremental = "grn", scope_incremental = "grc", node_decremental = "grm", diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index cad4bb788..0b9eaa512 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -57,7 +57,7 @@ local builtin_modules = { module_path = "nvim-treesitter.incremental_selection", enable = false, keymaps = { - init_selection = "gnn", + init_selection = "gnn", -- set to `false` to disable one of the mappings node_incremental = "grn", scope_incremental = "grc", node_decremental = "grm", diff --git a/lua/nvim-treesitter/incremental_selection.lua b/lua/nvim-treesitter/incremental_selection.lua index 5deaaf84e..4d4f2aad6 100644 --- a/lua/nvim-treesitter/incremental_selection.lua +++ b/lua/nvim-treesitter/incremental_selection.lua @@ -130,33 +130,37 @@ local FUNCTION_DESCRIPTIONS = { function M.attach(bufnr) local config = configs.get_module "incremental_selection" for funcname, mapping in pairs(config.keymaps) do - local mode - local rhs - if funcname == "init_selection" then - mode = "n" - rhs = M[funcname] - else - mode = "x" - -- We need to move to command mode to access marks '< (visual area start) and '> (visual area end) which are not - -- properly accessible in visual mode. - rhs = string.format(":lua require'nvim-treesitter.incremental_selection'.%s()", funcname) + if mapping then + local mode + local rhs + if funcname == "init_selection" then + mode = "n" + rhs = M[funcname] + else + mode = "x" + -- We need to move to command mode to access marks '< (visual area start) and '> (visual area end) which are not + -- properly accessible in visual mode. + rhs = string.format(":lua require'nvim-treesitter.incremental_selection'.%s()", funcname) + end + vim.keymap.set( + mode, + mapping, + rhs, + { buffer = bufnr, silent = true, noremap = true, desc = FUNCTION_DESCRIPTIONS[funcname] } + ) end - vim.keymap.set( - mode, - mapping, - rhs, - { buffer = bufnr, silent = true, noremap = true, desc = FUNCTION_DESCRIPTIONS[funcname] } - ) end end function M.detach(bufnr) local config = configs.get_module "incremental_selection" for f, mapping in pairs(config.keymaps) do - if f == "init_selection" then - vim.keymap.del("n", mapping, { buffer = bufnr }) - else - vim.keymap.del("x", mapping, { buffer = bufnr }) + if mapping then + if f == "init_selection" then + vim.keymap.del("n", mapping, { buffer = bufnr }) + else + vim.keymap.del("x", mapping, { buffer = bufnr }) + end end end end