From 41f51508e368e01910bd371e75610d6f301558b0 Mon Sep 17 00:00:00 2001 From: wongxy Date: Sun, 20 Feb 2022 18:29:09 +0800 Subject: [PATCH] fix: use "" as flag to remove default mappings (#1011) --- README.md | 3 ++- doc/nvim-tree-lua.txt | 4 ++-- lua/nvim-tree/actions/init.lua | 19 +++++++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 30a2f103..6ca2733f 100644 --- a/README.md +++ b/README.md @@ -228,7 +228,7 @@ require'nvim-tree'.setup { The `list` option in `view.mappings.list` is a table of ```lua -- key can be either a string or a table of string (lhs) --- action is the name of the action +-- action is the name of the action, set to `""` to remove default action -- action_cb is the function that will be called, it receives the node as a parameter. Optional for default actions -- mode is normal by default @@ -242,6 +242,7 @@ local list = { { key = {"", "o" }, action = "edit", mode = "n"}, { key = "p", action = "print_path", action_cb = print_node_path }, { key = "s", cb = tree_cb("vsplit") }, --tree_cb and the cb property are deprecated + { key = "<2-RightMouse>", action = "" }, -- will remove default cd action } ``` diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 4fde4420..356f156b 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -641,7 +641,7 @@ Defaults to: The `list` option in `view.mappings.list` is a table of - key can be either a string or a table of string (lhs) -- action is the name of the action, set to nil to unmap default actions +- action is the name of the action, set to `""` to remove default action - action_cb is the function that will be called, it receives the node as a parameter. Optional for default actions - mode is normal by default > @@ -656,7 +656,7 @@ The `list` option in `view.mappings.list` is a table of { key = {"", "o" }, action = "edit", mode = "n"}, { key = "p", action = "print_path", action_cb = print_node_path }, { key = "s", cb = tree_cb("vsplit") }, --tree_cb and the cb property are deprecated - { key = "<2-RightMouse>", action = nil }, + { key = "<2-RightMouse>", action = "" }, -- will remove default cd action } diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index 4537a689..74cced10 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -2,6 +2,7 @@ local a = vim.api local lib = require'nvim-tree.lib' local view = require'nvim-tree.view' +local util = require'nvim-tree.utils' local nvim_tree_callback = require'nvim-tree.config'.nvim_tree_callback local M = { @@ -126,24 +127,34 @@ local function merge_mappings(user_mappings) return M.mappings end + local function is_empty(s) + return s == '' + end + local user_keys = {} local removed_keys = {} + -- remove default mappings if action is a empty string for _, map in pairs(user_mappings) do if type(map.key) == "table" then for _, key in pairs(map.key) do table.insert(user_keys, key) - if not map.action then + if is_empty(map.action) then table.insert(removed_keys, key) end end else table.insert(user_keys, map.key) - if not map.action then + if is_empty(map.action) then table.insert(removed_keys, map.key) end end + if map.action and type(map.action_cb) == "function" then - M.custom_keypress_funcs[map.action] = map.action_cb + if not is_empty(map.action) then + M.custom_keypress_funcs[map.action] = map.action_cb + else + util.warn("action can't be empty if action_cb provided") + end end end @@ -163,7 +174,7 @@ local function merge_mappings(user_mappings) end, M.mappings) local user_map = vim.tbl_filter(function(map) - return map.action + return not is_empty(map.action) end, user_mappings) return vim.fn.extend(default_map, user_map)