Skip to content

Commit 64cc3c1

Browse files
authored
feat(mapping): deprecate user mappings and add on_attach (#1424)
1 parent 5f30a7b commit 64cc3c1

File tree

6 files changed

+387
-130
lines changed

6 files changed

+387
-130
lines changed

doc/nvim-tree-lua.txt

Lines changed: 80 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,34 @@ performance.
542542
Idle milliseconds between filesystem change and action.
543543
Type: `number`, Default: `50` (ms)
544544

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

@@ -599,14 +627,11 @@ Window / buffer setup.
599627
Configuration options for |nvim-tree-mappings|
600628

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

606633
*nvim-tree.view.mappings.list*
607-
A list of keymaps that will extend or override the default keymaps.
608-
Type: `table`
609-
Default: see |nvim-tree-default-mappings|
634+
DEPRECATED: see |nvim-tree.on_attach|
610635

611636
*nvim-tree.renderer*
612637
UI rendering setup
@@ -1045,130 +1070,56 @@ exists.
10451070
==============================================================================
10461071
6. MAPPINGS *nvim-tree-mappings*
10471072

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

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

lua/nvim-tree.lua

Lines changed: 6 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 = "disable", -- function(bufnr). If nil, will use the deprecated mapping strategy
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 = {
@@ -605,6 +608,8 @@ end
605608
local FIELD_OVERRIDE_TYPECHECK = {
606609
width = { string = true, ["function"] = true, number = true },
607610
height = { string = true, ["function"] = true, number = true },
611+
remove_keymaps = { boolean = true, table = true },
612+
on_attach = { ["function"] = true, string = true },
608613
}
609614

610615
local function validate_options(conf)
@@ -682,6 +687,7 @@ function M.setup(conf)
682687
log.raw("config", "%s\n", vim.inspect(opts))
683688

684689
require("nvim-tree.actions").setup(opts)
690+
require("nvim-tree.keymap").setup(opts)
685691
require("nvim-tree.colors").setup()
686692
require("nvim-tree.diagnostics").setup(opts)
687693
require("nvim-tree.explorer").setup(opts)

lua/nvim-tree/actions/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- @deprecated: new implemention in nvim-tree.keymap. Please do not edit this file.
2+
13
local a = vim.api
24

35
local log = require "nvim-tree.log"

0 commit comments

Comments
 (0)