Skip to content

Commit 70fab85

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 80dc86e commit 70fab85

File tree

5 files changed

+131
-123
lines changed

5 files changed

+131
-123
lines changed

doc/nvim-tree-lua.txt

Lines changed: 76 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,32 @@ This will be experimental for a few weeks and will become the default.
539539
Idle milliseconds between filesystem change and action.
540540
Type: `number`, Default: `50` (ms)
541541

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

@@ -596,14 +622,11 @@ Window / buffer setup.
596622
Configuration options for |nvim-tree-mappings|
597623

598624
*nvim-tree.view.mappings.custom_only*
599-
Will use only the provided user mappings and not the default otherwise,
600-
extends the default mappings with the provided user mappings.
601-
Type: `boolean`, Default: `false`
625+
DEPRECATED: see |nvim-tree.remove_keymaps|
626+
602627

603628
*nvim-tree.view.mappings.list*
604-
A list of keymaps that will extend or override the default keymaps.
605-
Type: `table`
606-
Default: see |nvim-tree-default-mappings|
629+
DEPRECATED: see |nvim-tree.on_attach|
607630

608631
*nvim-tree.renderer*
609632
UI rendering setup
@@ -943,128 +966,58 @@ A good functionnality to enable is |nvim-tree.hijack_directories|.
943966
==============================================================================
944967
5. MAPPINGS *nvim-tree-mappings*
945968

946-
The `list` option in `view.mappings.list` is a table of
969+
Setting your own mapping in the configuration is deprecated, see |nvim-tree.on_attach| now.
947970

948-
- `key` can be either a string or a table of string (lhs)
949-
- `action` is the name of the action, set to `""` to remove default action
950-
- `action_cb` is the function that will be called, it receives the node as a parameter. Optional for default actions
951-
- `mode` is normal by default
952-
>
953-
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
954-
955-
local function print_node_path(node) {
956-
print(node.absolute_path)
957-
}
958-
959-
local list = {
960-
{ key = {"<CR>", "o" }, action = "edit", mode = "n"},
961-
{ key = "p", action = "print_path", action_cb = print_node_path },
962-
{ key = "s", cb = tree_cb("vsplit") }, --tree_cb and the cb property are deprecated
963-
{ key = "<2-RightMouse>", action = "" }, -- will remove default cd action
964-
}
965-
<
966-
Mouse support defined in |KeyBindings|
971+
You can remove mappings with |nvim-tree.remove_keymaps|.
967972

968973
DEFAULT MAPPINGS *nvim-tree-default-mappings*
969974

970-
`<CR>` edit open a file or folder; root will cd to the above directory
971-
`o`
972-
`<2-LeftMouse>`
973-
`<C-e>` edit_in_place edit the file in place, effectively replacing the tree explorer
974-
`O` edit_no_picker same as (edit) with no window picker
975-
`<C-]>` cd cd in the directory under the cursor
976-
`<2-RightMouse>`
977-
`<C-v>` vsplit open the file in a vertical split
978-
`<C-x>` split open the file in a horizontal split
979-
`<C-t>` tabnew open the file in a new tab
980-
`<` prev_sibling navigate to the previous sibling of current file/directory
981-
`>` next_sibling navigate to the next sibling of current file/directory
982-
`P` parent_node move cursor to the parent directory
983-
`<BS>` close_node close current opened directory or parent
984-
`<Tab>` preview open the file as a preview (keeps the cursor in the tree)
985-
`K` first_sibling navigate to the first sibling of current file/directory
986-
`J` last_sibling navigate to the last sibling of current file/directory
987-
`I` toggle_git_ignored toggle visibility of files/folders hidden via |git.ignore| option
988-
`H` toggle_dotfiles toggle visibility of dotfiles via |filters.dotfiles| option
989-
`U` toggle_custom toggle visibility of files/folders hidden via |filters.custom| option
990-
`R` refresh refresh the tree
991-
`a` create add a file; leaving a trailing `/` will add a directory
992-
`d` remove delete a file (will prompt for confirmation)
993-
`D` trash trash a file via |trash| option
994-
`r` rename rename a file
995-
`<C-r>` full_rename rename a file and omit the filename on input
996-
`x` cut add/remove file/directory to cut clipboard
997-
`c` copy add/remove file/directory to copy clipboard
998-
`p` paste paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation
999-
`y` copy_name copy name to system clipboard
1000-
`Y` copy_path copy relative path to system clipboard
1001-
`gy` copy_absolute_path copy absolute path to system clipboard
1002-
`[e` prev_diag_item go to next diagnostic item
1003-
`[c` prev_git_item go to next git item
1004-
`]e` next_diag_item go to prev diagnostic item
1005-
`]c` next_git_item go to prev git item
1006-
`-` dir_up navigate up to the parent directory of the current file/directory
1007-
`s` system_open open a file with default system application or a folder with default file manager, using |system_open| option
1008-
`f` live_filter live filter nodes dynamically based on regex matching.
1009-
`F` clear_live_filter clear live filter
1010-
`q` close close tree window
1011-
`W` collapse_all collapse the whole tree
1012-
`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
1013-
`S` search_node prompt the user to enter a path and then expands the tree to match the path
1014-
`.` run_file_command enter vim command mode with the file the cursor is on
1015-
`<C-k>` toggle_file_info toggle a popup with file infos about the file under the cursor
1016-
`g?` toggle_help toggle help
1017-
`m` toggle_mark Toggle node in bookmarks
975+
`<CR>`, `o`, `<2-LeftMouse>` open a file or folder; root will cd to the above directory
976+
`<C-e>` edit the file in place, effectively replacing the tree explorer
977+
`O` same as (edit) with no window picker
978+
`<C-]>`, `<2-RightMouse>` cd in the directory under the cursor
979+
`<C-v>` open the file in a vertical split
980+
`<C-x>` open the file in a horizontal split
981+
`<C-t>` open the file in a new tab
982+
`<` navigate to the previous sibling of current file/directory
983+
`>` navigate to the next sibling of current file/directory
984+
`P` move cursor to the parent directory
985+
`<BS>` close current opened directory or parent
986+
`<Tab>` open the file as a preview (keeps the cursor in the tree)
987+
`K` navigate to the first sibling of current file/directory
988+
`J` navigate to the last sibling of current file/directory
989+
`I` toggle visibility of files/folders hidden via |git.ignore| option
990+
`H` toggle visibility of dotfiles via |filters.dotfiles| option
991+
`U` toggle visibility of files/folders hidden via |filters.custom| option
992+
`R` refresh the tree
993+
`a` add a file; leaving a trailing `/` will add a directory
994+
`d` delete a file (will prompt for confirmation)
995+
`D` trash a file via |trash| option
996+
`r` rename a file
997+
`<C-r>` rename a file and omit the filename on input
998+
`x` add/remove file/directory to cut clipboard
999+
`c` add/remove file/directory to copy clipboard
1000+
`p` paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation
1001+
`y` copy name to system clipboard
1002+
`Y` copy relative path to system clipboard
1003+
`gy` copy absolute path to system clipboard
1004+
`[e` go to next diagnostic item
1005+
`[c` go to next git item
1006+
`]e` go to prev diagnostic item
1007+
`]c` go to prev git item
1008+
`-` navigate up to the parent directory of the current file/directory
1009+
`s` open a file with default system application or a folder with default file manager, using |system_open| option
1010+
`f` live filter nodes dynamically based on regex matching.
1011+
`F` clear live filter
1012+
`q` close tree window
1013+
`W` collapse the whole tree
1014+
`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
1015+
`S` prompt the user to enter a path and then expands the tree to match the path
1016+
`.` enter vim command mode with the file the cursor is on
1017+
`<C-k>` toggle a popup with file infos about the file under the cursor
1018+
`g?` toggle help
1019+
`m` Toggle node in bookmarks
10181020

1019-
>
1020-
view.mappings.list = { -- BEGIN_DEFAULT_MAPPINGS
1021-
{ key = { "<CR>", "o", "<2-LeftMouse>" }, action = "edit" }
1022-
{ key = "<C-e>", action = "edit_in_place" }
1023-
{ key = "O", action = "edit_no_picker" }
1024-
{ key = { "<C-]>", "<2-RightMouse>" }, action = "cd" }
1025-
{ key = "<C-v>", action = "vsplit" }
1026-
{ key = "<C-x>", action = "split" }
1027-
{ key = "<C-t>", action = "tabnew" }
1028-
{ key = "<", action = "prev_sibling" }
1029-
{ key = ">", action = "next_sibling" }
1030-
{ key = "P", action = "parent_node" }
1031-
{ key = "<BS>", action = "close_node" }
1032-
{ key = "<Tab>", action = "preview" }
1033-
{ key = "K", action = "first_sibling" }
1034-
{ key = "J", action = "last_sibling" }
1035-
{ key = "I", action = "toggle_git_ignored" }
1036-
{ key = "H", action = "toggle_dotfiles" }
1037-
{ key = "U", action = "toggle_custom" }
1038-
{ key = "R", action = "refresh" }
1039-
{ key = "a", action = "create" }
1040-
{ key = "d", action = "remove" }
1041-
{ key = "D", action = "trash" }
1042-
{ key = "r", action = "rename" }
1043-
{ key = "<C-r>", action = "full_rename" }
1044-
{ key = "x", action = "cut" }
1045-
{ key = "c", action = "copy" }
1046-
{ key = "p", action = "paste" }
1047-
{ key = "y", action = "copy_name" }
1048-
{ key = "Y", action = "copy_path" }
1049-
{ key = "gy", action = "copy_absolute_path" }
1050-
{ key = "[e", action = "prev_diag_item" }
1051-
{ key = "[c", action = "prev_git_item" }
1052-
{ key = "]e", action = "next_diag_item" }
1053-
{ key = "]c", action = "next_git_item" }
1054-
{ key = "-", action = "dir_up" }
1055-
{ key = "s", action = "system_open" }
1056-
{ key = "f", action = "live_filter" }
1057-
{ key = "F", action = "clear_live_filter" }
1058-
{ key = "q", action = "close" }
1059-
{ key = "W", action = "collapse_all" }
1060-
{ key = "E", action = "expand_all" }
1061-
{ key = "S", action = "search_node" }
1062-
{ key = ".", action = "run_file_command" }
1063-
{ key = "<C-k>", action = "toggle_file_info" }
1064-
{ key = "g?", action = "toggle_help" }
1065-
{ key = "m", action = "toggle_mark" }
1066-
} -- END_DEFAULT_MAPPINGS
1067-
<
10681021
==============================================================================
10691022
6. HIGHLIGHT GROUPS *nvim-tree-highlight*
10701023

lua/nvim-tree.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
421421
sync_root_with_cwd = false,
422422
reload_on_bufenter = false,
423423
respect_buf_cwd = false,
424+
on_attach = function() end,
425+
remove_keymaps = false, -- boolean (disable totally or not) or list of key (lhs)
424426
view = {
425427
adaptive_size = false,
426428
centralize_selection = false,
@@ -432,6 +434,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
432434
number = false,
433435
relativenumber = false,
434436
signcolumn = "yes",
437+
-- @deprecated
435438
mappings = {
436439
custom_only = false,
437440
list = {

lua/nvim-tree/actions/init.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ function M.apply_mappings(bufnr)
269269
end
270270
end
271271

272+
-- @deprecated
272273
local function merge_mappings(user_mappings)
273274
if #user_mappings == 0 then
274275
return M.mappings
@@ -327,6 +328,7 @@ local function merge_mappings(user_mappings)
327328
return vim.fn.extend(default_map, user_map)
328329
end
329330

331+
-- @deprecated
330332
local function copy_mappings(user_mappings)
331333
if #user_mappings == 0 then
332334
return M.mappings
@@ -356,10 +358,45 @@ local function cleanup_existing_mappings()
356358
end
357359

358360
local DEFAULT_MAPPING_CONFIG = {
361+
-- @deprecated
359362
custom_only = false,
363+
-- @deprecated
360364
list = {},
361365
}
362366

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

374411
cleanup_existing_mappings()
375412
M.mappings = vim.deepcopy(DEFAULT_MAPPINGS)
413+
remove_keymaps(opts.remove_keymaps)
376414

415+
-- @deprecated
377416
local user_map_config = (opts.view or {}).mappings or {}
417+
-- @deprecated
378418
local options = vim.tbl_deep_extend("force", DEFAULT_MAPPING_CONFIG, user_map_config)
419+
-- @deprecated
379420
if options.custom_only then
421+
-- @deprecated
380422
M.mappings = copy_mappings(options.list)
381423
else
424+
-- @deprecated
382425
M.mappings = merge_mappings(options.list)
383426
end
384427

428+
-- @deprecated
385429
require("nvim-tree.actions.dispatch").setup(M.custom_keypress_funcs)
386430

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

lua/nvim-tree/utils.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,10 @@ function M.focus_file(path)
335335
require("nvim-tree.view").set_cursor { i + 1, 1 }
336336
end
337337

338+
function M.inject_node(f)
339+
return function()
340+
f(require("nvim-tree.lib").get_node_at_cursor())
341+
end
342+
end
343+
338344
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()
@@ -425,6 +426,7 @@ function M.setup(opts)
425426
M.View.winopts.number = options.number
426427
M.View.winopts.relativenumber = options.relativenumber
427428
M.View.winopts.signcolumn = options.signcolumn
429+
M.on_attach = opts.on_attach
428430
end
429431

430432
return M

0 commit comments

Comments
 (0)