diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index d755a559ba5..e69775a2601 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -248,8 +248,9 @@ local M = { local function set_map_for(bufnr) local opts = { noremap = true, silent = true, nowait = true, buffer = bufnr } - return function(mode, rhs) + return function(mode, rhs, desc) return function(lhs) + opts.desc = desc vim.keymap.set(mode or "n", lhs, rhs, opts) end end @@ -266,7 +267,7 @@ function M.apply_mappings(bufnr) for _, b in pairs(M.mappings) do local rhs = b.cb or run_dispatch(b.action) if rhs then - local setter = setter_for(b.mode, rhs) + local setter = setter_for(b.mode, rhs, b.action) local keys = type(b.key) == "table" and b.key or { b.key } for _, key in pairs(keys) do diff --git a/lua/nvim-tree/keymap.lua b/lua/nvim-tree/keymap.lua index 2c26a3a4d7c..e863de32951 100644 --- a/lua/nvim-tree/keymap.lua +++ b/lua/nvim-tree/keymap.lua @@ -2,244 +2,386 @@ local Api = require "nvim-tree.api" local M = {} +-- BEGIN_DEFAULT_KEYMAPS local DEFAULT_KEYMAPS = { { key = { "", "o", "<2-LeftMouse>" }, callback = Api.node.open.edit, - desc = "open a file or folder; root will cd to the above directory", + desc = { + long = "Open a file or directory; root will cd to the above directory.", + short = "Open", + }, }, { key = "", callback = Api.node.open.replace_tree_buffer, - desc = "edit the file in place, effectively replacing the tree explorer", + desc = { + long = "Open file in place, effectively replacing the tree explorer.", + short = "Open: In Place", + }, }, { key = "O", callback = Api.node.open.no_window_picker, - desc = "same as (edit) with no window picker", + desc = { + long = "Open file with no window picker.", + short = "Open: No Window Picker", + }, }, { key = { "", "<2-RightMouse>" }, callback = Api.tree.change_root_to_node, - desc = "cd in the directory under the cursor", + desc = { + long = "cd in the directory under the cursor.", + short = "CD", + }, }, { - key = "", + -- key = "", + key = "", callback = Api.node.open.vertical, - desc = "open the file in a vertical split", + desc = { + long = "Open file in a vertical split.", + short = "Open: Vertical Split", + }, }, { key = "", callback = Api.node.open.horizontal, - desc = "open the file in a horizontal split", + desc = { + long = "Open file in a horizontal split.", + short = "Open: Horizontal Split", + }, }, { key = "", callback = Api.node.open.tab, - desc = "open the file in a new tab", + desc = { + long = "Open file in a new tab.", + short = "Open: New Tab", + }, }, { key = "<", callback = Api.node.navigate.sibling.prev, - desc = "navigate to the previous sibling of current file/directory", + desc = { + long = "Navigate to the previous sibling.", + short = "Previous Sibling", + }, }, { key = ">", callback = Api.node.navigate.sibling.next, - desc = "navigate to the next sibling of current file/directory", + desc = { + long = "Navigate to the next sibling", + short = "Next Sibling", + }, }, { key = "P", callback = Api.node.navigate.parent, - desc = "move cursor to the parent directory", + desc = { + long = "Move cursor to the parent directory.", + short = "Parent Directory", + }, }, { key = "", callback = Api.node.navigate.parent_close, - desc = "close current opened directory or parent", + desc = { + long = "Close current opened directory or parent.", + short = "Close Directory", + }, }, { key = "", callback = Api.node.open.preview, - desc = "open the file as a preview (keeps the cursor in the tree)", + desc = { + long = "Open file as a preview (keeps the cursor in the tree).", + short = "Open Preview", + }, }, { key = "K", callback = Api.node.navigate.sibling.first, - desc = "navigate to the first sibling of current file/directory", + desc = { + long = "Navigate to the first sibling.", + short = "First Sibling", + }, }, { key = "J", callback = Api.node.navigate.sibling.last, - desc = "navigate to the last sibling of current file/directory", + desc = { + long = "Navigate to the last sibling.", + short = "Last Sibling", + }, }, { key = "I", callback = Api.tree.toggle_gitignore_filter, - desc = "toggle visibility of files/folders hidden via |git.ignore| option", + desc = { + long = "Toggle visibility of files/directories hidden via |git.ignore| option.", + short = "Toggle Git Ignore", + }, }, { key = "H", callback = Api.tree.toggle_hidden_filter, - desc = "toggle visibility of dotfiles via |filters.dotfiles| option", + desc = { + long = "Toggle visibility of dotfiles via |filters.dotfiles| option.", + short = "Toggle Dotfiles", + }, }, { key = "U", callback = Api.tree.toggle_custom_filter, - desc = "toggle visibility of files/folders hidden via |filters.custom| option", + desc = { + long = "Toggle visibility of files/directories hidden via |filters.custom| option.", + short = "Toggle Hidden", + }, }, { key = "R", callback = Api.tree.reload, - desc = "refresh the tree", + desc = { + long = "Refresh the tree.", + short = "Refresh", + }, }, { key = "a", callback = Api.fs.create, - desc = "add a file; leaving a trailing `/` will add a directory", + desc = { + long = "Create a file; leaving a trailing `/` will add a directory.", + short = "Create", + }, }, { key = "d", callback = Api.fs.remove, - desc = "delete a file (will prompt for confirmation)", + desc = { + long = "Delete a file, prompting for confirmation.", + short = "Delete", + }, }, { key = "D", callback = Api.fs.trash, - desc = "trash a file via |trash| option", + desc = { + long = "Trash a file via |trash| option.", + short = "Trash", + }, }, { key = "r", callback = Api.fs.rename, - desc = "rename a file", + desc = { + long = "Rename a file or directory.", + short = "Rename", + }, }, { key = "", callback = Api.fs.rename_sub, - desc = "rename a file and omit the filename on input", + desc = { + long = "Rename a file or directory and omit the filename on input.", + short = "Rename: Omit Filename", + }, }, { key = "x", callback = Api.fs.cut, - desc = "add/remove file/directory to cut clipboard", + desc = { + long = "Cut file or directory to cut clipboard.", + short = "Cut", + }, }, { key = "c", callback = Api.fs.copy.node, - desc = "add/remove file/directory to copy clipboard", + desc = { + long = "Copy file or directory to copy clipboard.", + short = "Copy", + }, }, { key = "p", callback = Api.fs.paste, - desc = "paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation", + desc = { + long = "Paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation.", + short = "Paste", + }, }, { key = "y", callback = Api.fs.copy.filename, - desc = "copy name to system clipboard", + desc = { + long = "Copy name to system clipboard.", + short = "Copy Name", + }, }, { key = "Y", callback = Api.fs.copy.relative_path, - desc = "copy relative path to system clipboard", + desc = { + long = "Copy relative path to system clipboard.", + short = "Copy Relative Path", + }, }, { key = "gy", callback = Api.fs.copy.absolute_path, - desc = "copy absolute path to system clipboard", + desc = { + long = "Copy absolute path to system clipboard.", + short = "Copy Absolute Path", + }, }, { key = "]e", callback = Api.node.navigate.diagnostics.next, - desc = "go to next diagnostic item", + desc = { + long = "Go to next diagnostic item.", + short = "Next Diagnostic", + }, }, { key = "]c", callback = Api.node.navigate.git.next, - desc = "go to next git item", + desc = { + long = "Go to next git item.", + short = "Next Git", + }, }, { key = "[e", callback = Api.node.navigate.diagnostics.prev, - desc = "go to prev diagnostic item", + desc = { + long = "Go to prev diagnostic item.", + short = "Prev Diagnostic", + }, }, { key = "[c", callback = Api.node.navigate.git.prev, - desc = "go to prev git item", + desc = { + long = "Go to prev git item.", + short = "Prev Git", + }, }, { key = "-", callback = Api.tree.change_root_to_parent, - desc = "navigate up to the parent directory of the current file/directory", + desc = { + long = "Navigate up to the parent directory of the current file/directory.", + short = "Up", + }, }, { key = "s", callback = Api.node.run.system, - desc = "open a file with default system application or a folder with default file manager, using |system_open| option", + desc = { + long = "Open a file with default system application or a directory with default file manager, using |system_open| option.", + short = "Run System", + }, }, { key = "f", callback = Api.live_filter.start, - desc = "live filter nodes dynamically based on regex matching.", + desc = { + long = "Live filter nodes dynamically based on regex matching.", + short = "Filter", + }, }, { key = "F", callback = Api.live_filter.clear, - desc = "clear live filter", + desc = { + long = "Clear live filter.", + short = "Clean Filter", + }, }, { key = "q", callback = Api.tree.close, - desc = "close tree window", + desc = { + long = "Close tree window.", + short = "Close", + }, }, { key = "W", callback = Api.tree.collapse_all, - desc = "collapse the whole tree", + desc = { + long = "Collapse the whole tree.", + short = "Collapse", + }, }, { key = "E", callback = Api.tree.expand_all, - desc = "expand the whole tree, stopping after expanding |callbacks.expand_all.max_folder_discovery| folders; this might hang neovim for a while if running on a big folder", + desc = { + long = "Expand the whole tree, stopping after expanding |callbacks.expand_all.max_folder_discovery| directories; this might hang neovim for a while if running on a big directory.", + short = "Expand All", + }, }, { key = "S", callback = Api.tree.search_node, - desc = "prompt the user to enter a path and then expands the tree to match the path", + desc = { + long = "Prompt the user to enter a path and then expands the tree to match the path.", + short = "Search", + }, }, { key = ".", callback = Api.node.run.cmd, - desc = "enter vim command mode with the file the cursor is on", + desc = { + long = "Enter vim command mode with the file the cursor is on.", + short = "Run Command", + }, }, { key = "", callback = Api.node.show_info_popup, - desc = "toggle a popup with file infos about the file under the cursor", + desc = { + long = "Toggle a popup with file info about the file under the cursor.", + short = "Info", + }, }, { key = "g?", callback = Api.tree.toggle_help, - desc = "toggle help", + desc = { + long = "Toggle help.", + short = "Help", + }, }, { key = "m", callback = Api.marks.toggle, - desc = "Toggle node in bookmarks", + desc = { + long = "Toggle node in bookmarks.", + short = "Toggle Bookmark", + }, }, { key = "bmv", callback = Api.marks.bulk.move, - desc = "Move all bookmarked nodes into specified location", + desc = { + long = "Move all bookmarked nodes into specified location.", + short = "Move Bookmarked", + }, }, } +-- END_DEFAULT_KEYMAPS function M.set_keymaps(bufnr) local opts = { noremap = true, silent = true, nowait = true, buffer = bufnr } for _, km in ipairs(M.keymaps) do local keys = type(km.key) == "table" and km.key or { km.key } for _, key in ipairs(keys) do + opts.desc = km.desc.short vim.keymap.set("n", key, km.callback, opts) end end @@ -287,4 +429,6 @@ function M.setup(opts) M.keymaps = get_keymaps(opts.remove_keymaps) end +M.DEFAULT_KEYMAPS = DEFAULT_KEYMAPS + return M diff --git a/lua/nvim-tree/renderer/help.lua b/lua/nvim-tree/renderer/help.lua index 61f00eb5827..3dddd468b12 100644 --- a/lua/nvim-tree/renderer/help.lua +++ b/lua/nvim-tree/renderer/help.lua @@ -1,48 +1,85 @@ local M = {} +local function tidy_lhs(lhs) + -- nvim_buf_get_keymap replaces leading "<" with "" e.g. "CTRL-v>" + lhs = lhs:gsub("^", "<") + + -- shorten ctrls + if lhs:lower():match "^") + + return lhs +end + +-- sort lhs roughly as per :help index +local PAT_MOUSE = "^<.*Mouse" +local PAT_CTRL = "^$/{ /^DEFAULT MAPPINGS/{p; r /tmp/DEFAULT_MAPPINGS.help }; /^>$/p; d }" doc/nvim-tree-lua.txt + +# generate various DEFAULT_KEYMAPS +begin="BEGIN_DEFAULT_KEYMAPS" +end="END_DEFAULT_KEYMAPS" +sed -n -e "/${begin}/,/${end}/{ /${begin}/d; /${end}/d; s/callback = \(.*\),/callback = '\1',/g; p; }" lua/nvim-tree/keymap.lua > /tmp/DEFAULT_KEYMAPS.M.lua +cat /tmp/DEFAULT_KEYMAPS.M.lua scripts/generate_default_keymaps.lua | lua +