Skip to content

Commit 0df03a4

Browse files
binyomentheHamsta
authored andcommitted
Support disabling mappings
Currently there is no way to disable default mappings. The best you can do is use some unlikely key combinations on the left-hand side so that defaults like `gnd` don't get used. This change allows using `false` to disable mappings, which is a common pattern in other Lua plugins.
1 parent 02ad37c commit 0df03a4

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ require'nvim-treesitter.configs'.setup {
5959
refactor = {
6060
smart_rename = {
6161
enable = true,
62+
-- Assign keymaps to false to disable them, e.g. `smart_rename = false`.
6263
keymaps = {
6364
smart_rename = "grr",
6465
},
@@ -83,6 +84,7 @@ require'nvim-treesitter.configs'.setup {
8384
refactor = {
8485
navigation = {
8586
enable = true,
87+
-- Assign keymaps to false to disable them, e.g. `goto_definition = false`.
8688
keymaps = {
8789
goto_definition = "gnd",
8890
list_definitions = "gnD",

Diff for: lua/nvim-treesitter-refactor/navigation.lua

+7-4
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,20 @@ function M.attach(bufnr)
173173
local config = configs.get_module "refactor.navigation"
174174

175175
for fn_name, mapping in pairs(config.keymaps) do
176-
local cmd = string.format([[:lua require'nvim-treesitter-refactor.navigation'.%s(%d)<CR>]], fn_name, bufnr)
177-
178-
api.nvim_buf_set_keymap(bufnr, "n", mapping, cmd, { silent = true, noremap = true, desc = fn_name })
176+
if mapping then
177+
local cmd = string.format([[:lua require'nvim-treesitter-refactor.navigation'.%s(%d)<CR>]], fn_name, bufnr)
178+
api.nvim_buf_set_keymap(bufnr, "n", mapping, cmd, { silent = true, noremap = true, desc = fn_name })
179+
end
179180
end
180181
end
181182

182183
function M.detach(bufnr)
183184
local config = configs.get_module "refactor.navigation"
184185

185186
for _, mapping in pairs(config.keymaps) do
186-
api.nvim_buf_del_keymap(bufnr, "n", mapping)
187+
if mapping then
188+
api.nvim_buf_del_keymap(bufnr, "n", mapping)
189+
end
187190
end
188191
end
189192

Diff for: lua/nvim-treesitter-refactor/smart_rename.lua

+7-3
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,20 @@ function M.attach(bufnr)
5757
local config = configs.get_module "refactor.smart_rename"
5858

5959
for fn_name, mapping in pairs(config.keymaps) do
60-
local cmd = string.format([[:lua require'nvim-treesitter-refactor.smart_rename'.%s(%d)<CR>]], fn_name, bufnr)
61-
api.nvim_buf_set_keymap(bufnr, "n", mapping, cmd, { silent = true, noremap = true, desc = fn_name })
60+
if mapping then
61+
local cmd = string.format([[:lua require'nvim-treesitter-refactor.smart_rename'.%s(%d)<CR>]], fn_name, bufnr)
62+
api.nvim_buf_set_keymap(bufnr, "n", mapping, cmd, { silent = true, noremap = true, desc = fn_name })
63+
end
6264
end
6365
end
6466

6567
function M.detach(bufnr)
6668
local config = configs.get_module "refactor.smart_rename"
6769

6870
for _, mapping in pairs(config.keymaps) do
69-
api.nvim_buf_del_keymap(bufnr, "n", mapping)
71+
if mapping then
72+
api.nvim_buf_del_keymap(bufnr, "n", mapping)
73+
end
7074
end
7175
end
7276

0 commit comments

Comments
 (0)