custom node keymaps (#855)
This commit is contained in:
committed by
GitHub
parent
0982c6a35a
commit
fa8bb09de4
80
README.md
80
README.md
@@ -219,50 +219,60 @@ highlight NvimTreeFolderIcon guibg=blue
|
|||||||
The `list` option in `view.mappings.list` is a table of
|
The `list` option in `view.mappings.list` is a table of
|
||||||
```lua
|
```lua
|
||||||
-- key can be either a string or a table of string (lhs)
|
-- key can be either a string or a table of string (lhs)
|
||||||
-- cb is the callback that will be called
|
-- action is the name of the 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
|
-- mode is normal by default
|
||||||
|
|
||||||
|
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
|
||||||
|
|
||||||
|
local function print_node_path(node) {
|
||||||
|
print(node.absolute_path)
|
||||||
|
}
|
||||||
|
|
||||||
local list = {
|
local list = {
|
||||||
{ key = {"<CR>", "o" }, cb = ":lua some_func()<cr>", mode = "n"}
|
{ key = {"<CR>", "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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
These are the default bindings:
|
These are the default bindings:
|
||||||
```lua
|
```lua
|
||||||
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
|
|
||||||
-- default mappings
|
-- default mappings
|
||||||
local list = {
|
local list = {
|
||||||
{ key = {"<CR>", "o", "<2-LeftMouse>"}, cb = tree_cb("edit") },
|
{ key = {"<CR>", "o", "<2-LeftMouse>"}, action = "edit" },
|
||||||
{ key = {"<2-RightMouse>", "<C-]>"}, cb = tree_cb("cd") },
|
{ key = {"<2-RightMouse>", "<C-]>"}, action = "cd" },
|
||||||
{ key = "<C-v>", cb = tree_cb("vsplit") },
|
{ key = "<C-v>", action = "vsplit" },
|
||||||
{ key = "<C-x>", cb = tree_cb("split") },
|
{ key = "<C-x>", action = "split" },
|
||||||
{ key = "<C-t>", cb = tree_cb("tabnew") },
|
{ key = "<C-t>", action = "tabnew" },
|
||||||
{ key = "<", cb = tree_cb("prev_sibling") },
|
{ key = "<", action = "prev_sibling" },
|
||||||
{ key = ">", cb = tree_cb("next_sibling") },
|
{ key = ">", action = "next_sibling" },
|
||||||
{ key = "P", cb = tree_cb("parent_node") },
|
{ key = "P", action = "parent_node" },
|
||||||
{ key = "<BS>", cb = tree_cb("close_node") },
|
{ key = "<BS>", action = "close_node" },
|
||||||
{ key = "<Tab>", cb = tree_cb("preview") },
|
{ key = "<Tab>", action = "preview" },
|
||||||
{ key = "K", cb = tree_cb("first_sibling") },
|
{ key = "K", action = "first_sibling" },
|
||||||
{ key = "J", cb = tree_cb("last_sibling") },
|
{ key = "J", action = "last_sibling" },
|
||||||
{ key = "I", cb = tree_cb("toggle_ignored") },
|
{ key = "I", action = "toggle_ignored" },
|
||||||
{ key = "H", cb = tree_cb("toggle_dotfiles") },
|
{ key = "H", action = "toggle_dotfiles" },
|
||||||
{ key = "R", cb = tree_cb("refresh") },
|
{ key = "R", action = "refresh" },
|
||||||
{ key = "a", cb = tree_cb("create") },
|
{ key = "a", action = "create" },
|
||||||
{ key = "d", cb = tree_cb("remove") },
|
{ key = "d", action = "remove" },
|
||||||
{ key = "D", cb = tree_cb("trash") },
|
{ key = "D", action = "trash" },
|
||||||
{ key = "r", cb = tree_cb("rename") },
|
{ key = "r", action = "rename" },
|
||||||
{ key = "<C-r>", cb = tree_cb("full_rename") },
|
{ key = "<C-r>", action = "full_rename" },
|
||||||
{ key = "x", cb = tree_cb("cut") },
|
{ key = "x", action = "cut" },
|
||||||
{ key = "c", cb = tree_cb("copy") },
|
{ key = "c", action = "copy" },
|
||||||
{ key = "p", cb = tree_cb("paste") },
|
{ key = "p", action = "paste" },
|
||||||
{ key = "y", cb = tree_cb("copy_name") },
|
{ key = "y", action = "copy_name" },
|
||||||
{ key = "Y", cb = tree_cb("copy_path") },
|
{ key = "Y", action = "copy_path" },
|
||||||
{ key = "gy", cb = tree_cb("copy_absolute_path") },
|
{ key = "gy", action = "copy_absolute_path" },
|
||||||
{ key = "[c", cb = tree_cb("prev_git_item") },
|
{ key = "[c", action = "prev_git_item" },
|
||||||
{ key = "]c", cb = tree_cb("next_git_item") },
|
{ key = "]c", action = "next_git_item" },
|
||||||
{ key = "-", cb = tree_cb("dir_up") },
|
{ key = "-", action = "dir_up" },
|
||||||
{ key = "s", cb = tree_cb("system_open") },
|
{ key = "s", action = "system_open" },
|
||||||
{ key = "q", cb = tree_cb("close") },
|
{ key = "q", action = "close" },
|
||||||
{ key = "g?", cb = tree_cb("toggle_help") },
|
{ key = "g?", action = "toggle_help" },
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -574,42 +574,61 @@ INFORMATIONS *nvim-tree-info*
|
|||||||
Defaults to:
|
Defaults to:
|
||||||
>
|
>
|
||||||
lua <<EOF
|
lua <<EOF
|
||||||
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
|
|
||||||
local list = {
|
local list = {
|
||||||
{ key = {"<CR>", "o", "<2-LeftMouse>"}, cb = tree_cb("edit") },
|
{ key = {"<CR>", "o", "<2-LeftMouse>"}, action = "edit" },
|
||||||
{ key = {"<2-RightMouse>", "<C-]>"}, cb = tree_cb("cd") },
|
{ key = {"<2-RightMouse>", "<C-]>"}, action = "cd" },
|
||||||
{ key = "<C-v>", cb = tree_cb("vsplit") },
|
{ key = "<C-v>", action = "vsplit" },
|
||||||
{ key = "<C-x>", cb = tree_cb("split") },
|
{ key = "<C-x>", action = "split" },
|
||||||
{ key = "<C-t>", cb = tree_cb("tabnew") },
|
{ key = "<C-t>", action = "tabnew" },
|
||||||
{ key = "<", cb = tree_cb("prev_sibling") },
|
{ key = "<", action = "prev_sibling" },
|
||||||
{ key = ">", cb = tree_cb("next_sibling") },
|
{ key = ">", action = "next_sibling" },
|
||||||
{ key = "P", cb = tree_cb("parent_node") },
|
{ key = "P", action = "parent_node" },
|
||||||
{ key = "<BS>", cb = tree_cb("close_node") },
|
{ key = "<BS>", action = "close_node" },
|
||||||
{ key = "<Tab>", cb = tree_cb("preview") },
|
{ key = "<Tab>", action = "preview" },
|
||||||
{ key = "K", cb = tree_cb("first_sibling") },
|
{ key = "K", action = "first_sibling" },
|
||||||
{ key = "J", cb = tree_cb("last_sibling") },
|
{ key = "J", action = "last_sibling" },
|
||||||
{ key = "I", cb = tree_cb("toggle_ignored") },
|
{ key = "I", action = "toggle_ignored" },
|
||||||
{ key = "H", cb = tree_cb("toggle_dotfiles") },
|
{ key = "H", action = "toggle_dotfiles" },
|
||||||
{ key = "R", cb = tree_cb("refresh") },
|
{ key = "R", action = "refresh" },
|
||||||
{ key = "a", cb = tree_cb("create") },
|
{ key = "a", action = "create" },
|
||||||
{ key = "d", cb = tree_cb("remove") },
|
{ key = "d", action = "remove" },
|
||||||
{ key = "D", cb = tree_cb("trash") },
|
{ key = "D", action = "trash" },
|
||||||
{ key = "r", cb = tree_cb("rename") },
|
{ key = "r", action = "rename" },
|
||||||
{ key = "<C-r>", cb = tree_cb("full_rename") },
|
{ key = "<C-r>", action = "full_rename" },
|
||||||
{ key = "x", cb = tree_cb("cut") },
|
{ key = "x", action = "cut" },
|
||||||
{ key = "c", cb = tree_cb("copy") },
|
{ key = "c", action = "copy" },
|
||||||
{ key = "p", cb = tree_cb("paste") },
|
{ key = "p", action = "paste" },
|
||||||
{ key = "y", cb = tree_cb("copy_name") },
|
{ key = "y", action = "copy_name" },
|
||||||
{ key = "Y", cb = tree_cb("copy_path") },
|
{ key = "Y", action = "copy_path" },
|
||||||
{ key = "gy", cb = tree_cb("copy_absolute_path") },
|
{ key = "gy", action = "copy_absolute_path" },
|
||||||
{ key = "[c", cb = tree_cb("prev_git_item") },
|
{ key = "[c", action = "prev_git_item" },
|
||||||
{ key = "]c", cb = tree_cb("next_git_item") },
|
{ key = "]c", action = "next_git_item" },
|
||||||
{ key = "-", cb = tree_cb("dir_up") },
|
{ key = "-", action = "dir_up" },
|
||||||
{ key = "s", cb = tree_cb("system_open") },
|
{ key = "s", action = "system_open" },
|
||||||
{ key = "q", cb = tree_cb("close") },
|
{ key = "q", action = "close" },
|
||||||
{ key = "g?", cb = tree_cb("toggle_help") },
|
{ key = "g?", action = "toggle_help" },
|
||||||
}
|
}
|
||||||
<
|
<
|
||||||
|
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
|
||||||
|
- 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
|
||||||
|
>
|
||||||
|
lua <<EOF
|
||||||
|
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
|
||||||
|
|
||||||
|
local function print_node_path(node) {
|
||||||
|
print(node.absolute_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
local list = {
|
||||||
|
{ key = {"<CR>", "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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|Features| *nvim-tree-features*
|
|Features| *nvim-tree-features*
|
||||||
|
|
||||||
|
|||||||
@@ -155,29 +155,34 @@ local keypress_funcs = {
|
|||||||
trash = function(node) trash.trash_node(node, _config) end,
|
trash = function(node) trash.trash_node(node, _config) end,
|
||||||
}
|
}
|
||||||
|
|
||||||
function M.on_keypress(mode)
|
function M.on_keypress(action)
|
||||||
if view.is_help_ui() and mode ~= 'toggle_help' then return end
|
if view.is_help_ui() and action ~= 'toggle_help' then return end
|
||||||
local node = lib.get_node_at_cursor()
|
local node = lib.get_node_at_cursor()
|
||||||
if not node then return end
|
if not node then return end
|
||||||
|
|
||||||
if keypress_funcs[mode] then
|
local custom_function = view.View.custom_keypress_funcs[action]
|
||||||
return keypress_funcs[mode](node)
|
local default_function = keypress_funcs[action]
|
||||||
|
|
||||||
|
if type(custom_function) == 'function' then
|
||||||
|
return custom_function(node)
|
||||||
|
elseif default_function then
|
||||||
|
return default_function(node)
|
||||||
end
|
end
|
||||||
|
|
||||||
if node.name == ".." then
|
if node.name == ".." then
|
||||||
return lib.change_dir("..")
|
return lib.change_dir("..")
|
||||||
elseif mode == "cd" and node.entries ~= nil then
|
elseif action == "cd" and node.entries ~= nil then
|
||||||
return lib.change_dir(lib.get_last_group_node(node).absolute_path)
|
return lib.change_dir(lib.get_last_group_node(node).absolute_path)
|
||||||
elseif mode == "cd" then
|
elseif action == "cd" then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if node.link_to and not node.entries then
|
if node.link_to and not node.entries then
|
||||||
lib.open_file(mode, node.link_to)
|
lib.open_file(action, node.link_to)
|
||||||
elseif node.entries ~= nil then
|
elseif node.entries ~= nil then
|
||||||
lib.expand_or_collapse(node)
|
lib.expand_or_collapse(node)
|
||||||
else
|
else
|
||||||
lib.open_file(mode, node.absolute_path)
|
lib.open_file(action, node.absolute_path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ function M.get_icon_state()
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- TODO: remove this once the cb property is not supported in mappings
|
||||||
function M.nvim_tree_callback(callback_name)
|
function M.nvim_tree_callback(callback_name)
|
||||||
return string.format(":lua require'nvim-tree'.on_keypress('%s')<CR>", callback_name)
|
return string.format(":lua require'nvim-tree'.on_keypress('%s')<CR>", callback_name)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
local a = vim.api
|
local a = vim.api
|
||||||
|
local nvim_tree_callback = require'nvim-tree.config'.nvim_tree_callback
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.nvim_tree_callback(callback_name)
|
|
||||||
return string.format(":lua require'nvim-tree'.on_keypress('%s')<CR>", callback_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
M.View = {
|
M.View = {
|
||||||
last_focused_winnr = nil,
|
last_focused_winnr = nil,
|
||||||
bufnr = nil,
|
bufnr = nil,
|
||||||
@@ -44,39 +41,40 @@ M.View = {
|
|||||||
{ name = 'bufhidden', val = 'hide' }
|
{ name = 'bufhidden', val = 'hide' }
|
||||||
},
|
},
|
||||||
mappings = {
|
mappings = {
|
||||||
{ key = {"<CR>", "o", "<2-LeftMouse>"}, cb = M.nvim_tree_callback("edit") },
|
{ key = {"<CR>", "o", "<2-LeftMouse>"}, action = "edit" },
|
||||||
{ key = {"<2-RightMouse>", "<C-]>"}, cb = M.nvim_tree_callback("cd") },
|
{ key = {"<2-RightMouse>", "<C-]>"}, action = "cd" },
|
||||||
{ key = "<C-v>", cb = M.nvim_tree_callback("vsplit") },
|
{ key = "<C-v>", action = "vsplit" },
|
||||||
{ key = "<C-x>", cb = M.nvim_tree_callback("split") },
|
{ key = "<C-x>", action = "split"},
|
||||||
{ key = "<C-t>", cb = M.nvim_tree_callback("tabnew") },
|
{ key = "<C-t>", action = "tabnew" },
|
||||||
{ key = "<", cb = M.nvim_tree_callback("prev_sibling") },
|
{ key = "<", action = "prev_sibling" },
|
||||||
{ key = ">", cb = M.nvim_tree_callback("next_sibling") },
|
{ key = ">", action = "next_sibling" },
|
||||||
{ key = "P", cb = M.nvim_tree_callback("parent_node") },
|
{ key = "P", action = "parent_node" },
|
||||||
{ key = "<BS>", cb = M.nvim_tree_callback("close_node") },
|
{ key = "<BS>", action = "close_node"},
|
||||||
{ key = "<Tab>", cb = M.nvim_tree_callback("preview") },
|
{ key = "<Tab>", action = "preview" },
|
||||||
{ key = "K", cb = M.nvim_tree_callback("first_sibling") },
|
{ key = "K", action = "first_sibling" },
|
||||||
{ key = "J", cb = M.nvim_tree_callback("last_sibling") },
|
{ key = "J", action = "last_sibling" },
|
||||||
{ key = "I", cb = M.nvim_tree_callback("toggle_ignored") },
|
{ key = "I", action = "toggle_ignored" },
|
||||||
{ key = "H", cb = M.nvim_tree_callback("toggle_dotfiles") },
|
{ key = "H", action = "toggle_dotfiles" },
|
||||||
{ key = "R", cb = M.nvim_tree_callback("refresh") },
|
{ key = "R", action = "refresh" },
|
||||||
{ key = "a", cb = M.nvim_tree_callback("create") },
|
{ key = "a", action = "create" },
|
||||||
{ key = "d", cb = M.nvim_tree_callback("remove") },
|
{ key = "d", action = "remove" },
|
||||||
{ key = "D", cb = M.nvim_tree_callback("trash") },
|
{ key = "D", action = "trash"},
|
||||||
{ key = "r", cb = M.nvim_tree_callback("rename") },
|
{ key = "r", action = "rename" },
|
||||||
{ key = "<C-r>", cb = M.nvim_tree_callback("full_rename") },
|
{ key = "<C-r>", action = "full_rename" },
|
||||||
{ key = "x", cb = M.nvim_tree_callback("cut") },
|
{ key = "x", action = "cut" },
|
||||||
{ key = "c", cb = M.nvim_tree_callback("copy") },
|
{ key = "c", action = "copy"},
|
||||||
{ key = "p", cb = M.nvim_tree_callback("paste") },
|
{ key = "p", action = "paste" },
|
||||||
{ key = "y", cb = M.nvim_tree_callback("copy_name") },
|
{ key = "y", action = "copy_name" },
|
||||||
{ key = "Y", cb = M.nvim_tree_callback("copy_path") },
|
{ key = "Y", action = "copy_path" },
|
||||||
{ key = "gy", cb = M.nvim_tree_callback("copy_absolute_path") },
|
{ key = "gy", action = "copy_absolute_path" },
|
||||||
{ key = "[c", cb = M.nvim_tree_callback("prev_git_item") },
|
{ key = "[c", action = "prev_git_item" },
|
||||||
{ key = "]c", cb = M.nvim_tree_callback("next_git_item") },
|
{ key = "]c", action = "next_git_item" },
|
||||||
{ key = "-", cb = M.nvim_tree_callback("dir_up") },
|
{ key = "-", action = "dir_up" },
|
||||||
{ key = "s", cb = M.nvim_tree_callback("system_open") },
|
{ key = "s", action = "system_open" },
|
||||||
{ key = "q", cb = M.nvim_tree_callback("close") },
|
{ key = "q", action = "close"},
|
||||||
{ key = "g?", cb = M.nvim_tree_callback("toggle_help") }
|
{ key = "g?", action = "toggle_help" }
|
||||||
}
|
},
|
||||||
|
custom_keypress_funcs = {},
|
||||||
}
|
}
|
||||||
|
|
||||||
local function wipe_rogue_buffer()
|
local function wipe_rogue_buffer()
|
||||||
@@ -97,12 +95,13 @@ local function create_buffer()
|
|||||||
end
|
end
|
||||||
|
|
||||||
for _, b in pairs(M.View.mappings) do
|
for _, b in pairs(M.View.mappings) do
|
||||||
|
local mapping_rhs = b.cb or nvim_tree_callback(b.action)
|
||||||
if type(b.key) == "table" then
|
if type(b.key) == "table" then
|
||||||
for _, key in pairs(b.key) do
|
for _, key in pairs(b.key) do
|
||||||
a.nvim_buf_set_keymap(M.View.bufnr, b.mode or 'n', key, b.cb, { noremap = true, silent = true, nowait = true })
|
a.nvim_buf_set_keymap(M.View.bufnr, b.mode or 'n', key, mapping_rhs, { noremap = true, silent = true, nowait = true })
|
||||||
end
|
end
|
||||||
elseif b.cb then
|
elseif mapping_rhs then
|
||||||
a.nvim_buf_set_keymap(M.View.bufnr, b.mode or 'n', b.key, b.cb, { noremap = true, silent = true, nowait = true })
|
a.nvim_buf_set_keymap(M.View.bufnr, b.mode or 'n', b.key, mapping_rhs, { noremap = true, silent = true, nowait = true })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -135,6 +134,9 @@ local function merge_mappings(user_mappings)
|
|||||||
else
|
else
|
||||||
table.insert(user_keys, map.key)
|
table.insert(user_keys, map.key)
|
||||||
end
|
end
|
||||||
|
if map.action and type(map.action_cb) == "function" then
|
||||||
|
M.View.custom_keypress_funcs[map.action] = map.action_cb
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local view_mappings = vim.tbl_filter(function(map)
|
local view_mappings = vim.tbl_filter(function(map)
|
||||||
|
|||||||
Reference in New Issue
Block a user