Skip to content

Commit f68cae2

Browse files
committed
feat(mapping): deprecate user mappings and add on_attach
See the help doc. I think this is better than providing our custom binding methods, idea taken from gitsigns.nvim. Once this is complete, we could remove the whole mapping code and simplify the actions -> dispatch configurations.
1 parent a044818 commit f68cae2

File tree

5 files changed

+133
-129
lines changed

5 files changed

+133
-129
lines changed

doc/nvim-tree-lua.txt

Lines changed: 78 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,32 @@ This will be experimental for a few weeks and will become the default.
543543
Idle milliseconds between filesystem change and action.
544544
Type: `number`, Default: `50` (ms)
545545

546+
*nvim-tree.on_attach*
547+
Function ran when creating the nvim-tree buffer.
548+
This can be used to attach keybindings to the tree buffer.
549+
>
550+
on_attach = function(bufnr)
551+
local inject_node = require("nvim-tree.utils").inject_node
552+
553+
vim.keymap.set("n", "<leader>n", inject_node(function(node)
554+
if node then
555+
print(node.absolute_path)
556+
end
557+
end), { buffer = bufnr, noremap = true })
558+
559+
vim.bo[bufnr].path = "/tmp"
560+
end
561+
<
562+
Type: `function(bufnr)`, Default: `empty_function`
563+
564+
*nvim-tree.remove_keymaps*
565+
This can be used to remove the default mappings in the tree.
566+
- Remove specific keys by passing a `string` table of keys
567+
eg. {"<C-o>", "<CR>", "o"}
568+
- Remove all default mappings by passing `true`
569+
- Ignore by passing `false`
570+
Type: `bool` or `{string}`, Default: `false`
571+
546572
*nvim-tree.view*
547573
Window / buffer setup.
548574

@@ -600,14 +626,11 @@ Window / buffer setup.
600626
Configuration options for |nvim-tree-mappings|
601627

602628
*nvim-tree.view.mappings.custom_only*
603-
Will use only the provided user mappings and not the default otherwise,
604-
extends the default mappings with the provided user mappings.
605-
Type: `boolean`, Default: `false`
629+
DEPRECATED: see |nvim-tree.remove_keymaps|
630+
606631

607632
*nvim-tree.view.mappings.list*
608-
A list of keymaps that will extend or override the default keymaps.
609-
Type: `table`
610-
Default: see |nvim-tree-default-mappings|
633+
DEPRECATED: see |nvim-tree.on_attach|
611634

612635
*nvim-tree.renderer*
613636
UI rendering setup
@@ -1043,130 +1066,56 @@ exists.
10431066
==============================================================================
10441067
6. MAPPINGS *nvim-tree-mappings*
10451068

1046-
The `list` option in `view.mappings.list` is a table of
1069+
Setting your own mapping in the configuration is deprecated, see |nvim-tree.on_attach| now.
1070+
1071+
You can remove mappings with |nvim-tree.remove_keymaps|.
1072+
1073+
`<CR>`, `o`, `<2-LeftMouse>` open a file or folder; root will cd to the above directory
1074+
`<C-e>` edit the file in place, effectively replacing the tree explorer
1075+
`O` same as (edit) with no window picker
1076+
`<C-]>`, `<2-RightMouse>` cd in the directory under the cursor
1077+
`<C-v>` open the file in a vertical split
1078+
`<C-x>` open the file in a horizontal split
1079+
`<C-t>` open the file in a new tab
1080+
`<` navigate to the previous sibling of current file/directory
1081+
`>` navigate to the next sibling of current file/directory
1082+
`P` move cursor to the parent directory
1083+
`<BS>` close current opened directory or parent
1084+
`<Tab>` open the file as a preview (keeps the cursor in the tree)
1085+
`K` navigate to the first sibling of current file/directory
1086+
`J` navigate to the last sibling of current file/directory
1087+
`I` toggle visibility of files/folders hidden via |git.ignore| option
1088+
`H` toggle visibility of dotfiles via |filters.dotfiles| option
1089+
`U` toggle visibility of files/folders hidden via |filters.custom| option
1090+
`R` refresh the tree
1091+
`a` add a file; leaving a trailing `/` will add a directory
1092+
`d` delete a file (will prompt for confirmation)
1093+
`D` trash a file via |trash| option
1094+
`r` rename a file
1095+
`<C-r>` rename a file and omit the filename on input
1096+
`x` add/remove file/directory to cut clipboard
1097+
`c` add/remove file/directory to copy clipboard
1098+
`p` paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation
1099+
`y` copy name to system clipboard
1100+
`Y` copy relative path to system clipboard
1101+
`gy` copy absolute path to system clipboard
1102+
`[e` go to next diagnostic item
1103+
`[c` go to next git item
1104+
`]e` go to prev diagnostic item
1105+
`]c` go to prev git item
1106+
`-` navigate up to the parent directory of the current file/directory
1107+
`s` open a file with default system application or a folder with default file manager, using |system_open| option
1108+
`f` live filter nodes dynamically based on regex matching.
1109+
`F` clear live filter
1110+
`q` close tree window
1111+
`W` collapse the whole tree
1112+
`E` expand the whole tree, stopping after expanding |actions.expand_all.max_folder_discovery| folders; this might hang neovim for a while if running on a big folder
1113+
`S` prompt the user to enter a path and then expands the tree to match the path
1114+
`.` enter vim command mode with the file the cursor is on
1115+
`<C-k>` toggle a popup with file infos about the file under the cursor
1116+
`g?` toggle help
1117+
`m` Toggle node in bookmarks
10471118

1048-
- `key` can be either a string or a table of string (lhs)
1049-
- `action` is the name of the action, set to `""` to remove default action
1050-
- `action_cb` is the function that will be called, it receives the node as a parameter. Optional for default actions
1051-
- `mode` is normal by default
1052-
>
1053-
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
1054-
1055-
local function print_node_path(node) {
1056-
print(node.absolute_path)
1057-
}
1058-
1059-
local list = {
1060-
{ key = {"<CR>", "o" }, action = "edit", mode = "n"},
1061-
{ key = "p", action = "print_path", action_cb = print_node_path },
1062-
{ key = "s", cb = tree_cb("vsplit") }, --tree_cb and the cb property are deprecated
1063-
{ key = "<2-RightMouse>", action = "" }, -- will remove default cd action
1064-
}
1065-
<
1066-
Mouse support defined in |KeyBindings|
1067-
1068-
DEFAULT MAPPINGS *nvim-tree-default-mappings*
1069-
1070-
`<CR>` edit open a file or folder; root will cd to the above directory
1071-
`o`
1072-
`<2-LeftMouse>`
1073-
`<C-e>` edit_in_place edit the file in place, effectively replacing the tree explorer
1074-
`O` edit_no_picker same as (edit) with no window picker
1075-
`<C-]>` cd cd in the directory under the cursor
1076-
`<2-RightMouse>`
1077-
`<C-v>` vsplit open the file in a vertical split
1078-
`<C-x>` split open the file in a horizontal split
1079-
`<C-t>` tabnew open the file in a new tab
1080-
`<` prev_sibling navigate to the previous sibling of current file/directory
1081-
`>` next_sibling navigate to the next sibling of current file/directory
1082-
`P` parent_node move cursor to the parent directory
1083-
`<BS>` close_node close current opened directory or parent
1084-
`<Tab>` preview open the file as a preview (keeps the cursor in the tree)
1085-
`K` first_sibling navigate to the first sibling of current file/directory
1086-
`J` last_sibling navigate to the last sibling of current file/directory
1087-
`I` toggle_git_ignored toggle visibility of files/folders hidden via |git.ignore| option
1088-
`H` toggle_dotfiles toggle visibility of dotfiles via |filters.dotfiles| option
1089-
`U` toggle_custom toggle visibility of files/folders hidden via |filters.custom| option
1090-
`R` refresh refresh the tree
1091-
`a` create add a file; leaving a trailing `/` will add a directory
1092-
`d` remove delete a file (will prompt for confirmation)
1093-
`D` trash trash a file via |trash| option
1094-
`r` rename rename a file
1095-
`<C-r>` full_rename rename a file and omit the filename on input
1096-
`x` cut add/remove file/directory to cut clipboard
1097-
`c` copy add/remove file/directory to copy clipboard
1098-
`p` paste paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation
1099-
`y` copy_name copy name to system clipboard
1100-
`Y` copy_path copy relative path to system clipboard
1101-
`gy` copy_absolute_path copy absolute path to system clipboard
1102-
`[e` prev_diag_item go to next diagnostic item
1103-
`[c` prev_git_item go to next git item
1104-
`]e` next_diag_item go to prev diagnostic item
1105-
`]c` next_git_item go to prev git item
1106-
`-` dir_up navigate up to the parent directory of the current file/directory
1107-
`s` system_open open a file with default system application or a folder with default file manager, using |system_open| option
1108-
`f` live_filter live filter nodes dynamically based on regex matching.
1109-
`F` clear_live_filter clear live filter
1110-
`q` close close tree window
1111-
`W` collapse_all collapse the whole tree
1112-
`E` expand_all expand the whole tree, stopping after expanding |actions.expand_all.max_folder_discovery| folders; this might hang neovim for a while if running on a big folder
1113-
`S` search_node prompt the user to enter a path and then expands the tree to match the path
1114-
`.` run_file_command enter vim command mode with the file the cursor is on
1115-
`<C-k>` toggle_file_info toggle a popup with file infos about the file under the cursor
1116-
`g?` toggle_help toggle help
1117-
`m` toggle_mark Toggle node in bookmarks
1118-
`bmv` bulk_move Move all bookmarked nodes into specified location
1119-
1120-
>
1121-
view.mappings.list = { -- BEGIN_DEFAULT_MAPPINGS
1122-
{ key = { "<CR>", "o", "<2-LeftMouse>" }, action = "edit" },
1123-
{ key = "<C-e>", action = "edit_in_place" },
1124-
{ key = "O", action = "edit_no_picker" },
1125-
{ key = { "<C-]>", "<2-RightMouse>" }, action = "cd" },
1126-
{ key = "<C-v>", action = "vsplit" },
1127-
{ key = "<C-x>", action = "split" },
1128-
{ key = "<C-t>", action = "tabnew" },
1129-
{ key = "<", action = "prev_sibling" },
1130-
{ key = ">", action = "next_sibling" },
1131-
{ key = "P", action = "parent_node" },
1132-
{ key = "<BS>", action = "close_node" },
1133-
{ key = "<Tab>", action = "preview" },
1134-
{ key = "K", action = "first_sibling" },
1135-
{ key = "J", action = "last_sibling" },
1136-
{ key = "I", action = "toggle_git_ignored" },
1137-
{ key = "H", action = "toggle_dotfiles" },
1138-
{ key = "U", action = "toggle_custom" },
1139-
{ key = "R", action = "refresh" },
1140-
{ key = "a", action = "create" },
1141-
{ key = "d", action = "remove" },
1142-
{ key = "D", action = "trash" },
1143-
{ key = "r", action = "rename" },
1144-
{ key = "<C-r>", action = "full_rename" },
1145-
{ key = "x", action = "cut" },
1146-
{ key = "c", action = "copy" },
1147-
{ key = "p", action = "paste" },
1148-
{ key = "y", action = "copy_name" },
1149-
{ key = "Y", action = "copy_path" },
1150-
{ key = "gy", action = "copy_absolute_path" },
1151-
{ key = "[e", action = "prev_diag_item" },
1152-
{ key = "[c", action = "prev_git_item" },
1153-
{ key = "]e", action = "next_diag_item" },
1154-
{ key = "]c", action = "next_git_item" },
1155-
{ key = "-", action = "dir_up" },
1156-
{ key = "s", action = "system_open" },
1157-
{ key = "f", action = "live_filter" },
1158-
{ key = "F", action = "clear_live_filter" },
1159-
{ key = "q", action = "close" },
1160-
{ key = "W", action = "collapse_all" },
1161-
{ key = "E", action = "expand_all" },
1162-
{ key = "S", action = "search_node" },
1163-
{ key = ".", action = "run_file_command" },
1164-
{ key = "<C-k>", action = "toggle_file_info" },
1165-
{ key = "g?", action = "toggle_help" },
1166-
{ key = "m", action = "toggle_mark" },
1167-
{ key = "bmv", action = "bulk_move" },
1168-
} -- END_DEFAULT_MAPPINGS
1169-
<
11701119
==============================================================================
11711120
7. HIGHLIGHT GROUPS *nvim-tree-highlight*
11721121

lua/nvim-tree.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
434434
sync_root_with_cwd = false,
435435
reload_on_bufenter = false,
436436
respect_buf_cwd = false,
437+
on_attach = function() end,
438+
remove_keymaps = false, -- boolean (disable totally or not) or list of key (lhs)
437439
view = {
438440
adaptive_size = false,
439441
centralize_selection = false,
@@ -445,6 +447,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
445447
number = false,
446448
relativenumber = false,
447449
signcolumn = "yes",
450+
-- @deprecated
448451
mappings = {
449452
custom_only = false,
450453
list = {

lua/nvim-tree/actions/init.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ function M.apply_mappings(bufnr)
274274
end
275275
end
276276

277+
-- @deprecated
277278
local function merge_mappings(user_mappings)
278279
if #user_mappings == 0 then
279280
return M.mappings
@@ -332,6 +333,7 @@ local function merge_mappings(user_mappings)
332333
return vim.fn.extend(default_map, user_map)
333334
end
334335

336+
-- @deprecated
335337
local function copy_mappings(user_mappings)
336338
if #user_mappings == 0 then
337339
return M.mappings
@@ -361,10 +363,45 @@ local function cleanup_existing_mappings()
361363
end
362364

363365
local DEFAULT_MAPPING_CONFIG = {
366+
-- @deprecated
364367
custom_only = false,
368+
-- @deprecated
365369
list = {},
366370
}
367371

372+
local function remove_keymaps(keys_to_disable)
373+
if keys_to_disable == true then
374+
M.mappings = {}
375+
return
376+
end
377+
378+
if type(keys_to_disable) == "table" and #keys_to_disable > 0 then
379+
local new_map = {}
380+
for _, m in pairs(M.mappings) do
381+
local keys = type(m.key) == "table" and m.key or { m.key }
382+
local reminding_keys = {}
383+
for _, key in pairs(keys) do
384+
local found = false
385+
for _, key_to_disable in pairs(keys_to_disable) do
386+
if key_to_disable == key then
387+
found = true
388+
break
389+
end
390+
end
391+
if not found then
392+
table.insert(reminding_keys, key)
393+
end
394+
end
395+
if #reminding_keys > 0 then
396+
local map = vim.deepcopy(m)
397+
map.key = reminding_keys
398+
table.insert(new_map, map)
399+
end
400+
end
401+
M.mappings = new_map
402+
end
403+
end
404+
368405
function M.setup(opts)
369406
require("nvim-tree.actions.fs.trash").setup(opts)
370407
require("nvim-tree.actions.node.system-open").setup(opts)
@@ -378,15 +415,22 @@ function M.setup(opts)
378415

379416
cleanup_existing_mappings()
380417
M.mappings = vim.deepcopy(DEFAULT_MAPPINGS)
418+
remove_keymaps(opts.remove_keymaps)
381419

420+
-- @deprecated
382421
local user_map_config = (opts.view or {}).mappings or {}
422+
-- @deprecated
383423
local options = vim.tbl_deep_extend("force", DEFAULT_MAPPING_CONFIG, user_map_config)
424+
-- @deprecated
384425
if options.custom_only then
426+
-- @deprecated
385427
M.mappings = copy_mappings(options.list)
386428
else
429+
-- @deprecated
387430
M.mappings = merge_mappings(options.list)
388431
end
389432

433+
-- @deprecated
390434
require("nvim-tree.actions.dispatch").setup(M.custom_keypress_funcs)
391435

392436
log.line("config", "active mappings")

lua/nvim-tree/utils.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,10 @@ function M.clear_prompt()
379379
end
380380
end
381381

382+
function M.inject_node(f)
383+
return function()
384+
f(require("nvim-tree.lib").get_node_at_cursor())
385+
end
386+
end
387+
382388
return M

lua/nvim-tree/view.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ local function create_buffer(bufnr)
9393
end
9494

9595
require("nvim-tree.actions").apply_mappings(M.get_bufnr())
96+
M.on_attach(M.get_bufnr())
9697
end
9798

9899
local function get_size()
@@ -426,6 +427,7 @@ function M.setup(opts)
426427
M.View.winopts.number = options.number
427428
M.View.winopts.relativenumber = options.relativenumber
428429
M.View.winopts.signcolumn = options.signcolumn
430+
M.on_attach = opts.on_attach
429431
end
430432

431433
return M

0 commit comments

Comments
 (0)