chore(mappings): add desc to all mappings, show in help, reformat help

This commit is contained in:
Alexander Courtis
2022-08-02 12:09:01 +10:00
parent a8471666c2
commit 2320d173e0
3 changed files with 117 additions and 101 deletions

View File

@@ -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

View File

@@ -8,7 +8,7 @@ local DEFAULT_KEYMAPS = {
key = { "<CR>", "o", "<2-LeftMouse>" },
callback = Api.node.open.edit,
desc = {
long = "open a file or folder; root will cd to the above directory",
long = "Open a file or directory; root will cd to the above directory.",
short = "Open",
},
},
@@ -16,7 +16,7 @@ local DEFAULT_KEYMAPS = {
key = "<C-e>",
callback = Api.node.open.replace_tree_buffer,
desc = {
long = "edit the file in place, effectively replacing the tree explorer",
long = "Open file in place, effectively replacing the tree explorer.",
short = "Open: In Place",
},
},
@@ -24,7 +24,7 @@ local DEFAULT_KEYMAPS = {
key = "O",
callback = Api.node.open.no_window_picker,
desc = {
long = "same as (edit) with no window picker",
long = "Open file with no window picker.",
short = "Open: No Window Picker",
},
},
@@ -32,15 +32,16 @@ local DEFAULT_KEYMAPS = {
key = { "<C-]>", "<2-RightMouse>" },
callback = Api.tree.change_root_to_node,
desc = {
long = "cd in the directory under the cursor",
short = "cd",
long = "cd in the directory under the cursor.",
short = "CD",
},
},
{
key = "<C-v>",
-- key = "<C-v>",
key = "<ctrL-v>",
callback = Api.node.open.vertical,
desc = {
long = "open the file in a vertical split",
long = "Open file in a vertical split.",
short = "Open: Vertical Split",
},
},
@@ -48,7 +49,7 @@ local DEFAULT_KEYMAPS = {
key = "<C-x>",
callback = Api.node.open.horizontal,
desc = {
long = "open the file in a horizontal split",
long = "Open file in a horizontal split.",
short = "Open: Horizontal Split",
},
},
@@ -56,7 +57,7 @@ local DEFAULT_KEYMAPS = {
key = "<C-t>",
callback = Api.node.open.tab,
desc = {
long = "open the file in a new tab",
long = "Open file in a new tab.",
short = "Open: New Tab",
},
},
@@ -64,7 +65,7 @@ local DEFAULT_KEYMAPS = {
key = "<",
callback = Api.node.navigate.sibling.prev,
desc = {
long = "navigate to the previous sibling of current file/directory",
long = "Navigate to the previous sibling.",
short = "Previous Sibling",
},
},
@@ -72,7 +73,7 @@ local DEFAULT_KEYMAPS = {
key = ">",
callback = Api.node.navigate.sibling.next,
desc = {
long = "navigate to the next sibling of current file/directory",
long = "Navigate to the next sibling",
short = "Next Sibling",
},
},
@@ -80,7 +81,7 @@ local DEFAULT_KEYMAPS = {
key = "P",
callback = Api.node.navigate.parent,
desc = {
long = "move cursor to the parent directory",
long = "Move cursor to the parent directory.",
short = "Parent Directory",
},
},
@@ -88,15 +89,15 @@ local DEFAULT_KEYMAPS = {
key = "<BS>",
callback = Api.node.navigate.parent_close,
desc = {
long = "close current opened directory or parent",
short = "Close Folder",
long = "Close current opened directory or parent.",
short = "Close Directory",
},
},
{
key = "<Tab>",
callback = Api.node.open.preview,
desc = {
long = "open the file as a preview (keeps the cursor in the tree)",
long = "Open file as a preview (keeps the cursor in the tree).",
short = "Open Preview",
},
},
@@ -104,7 +105,7 @@ local DEFAULT_KEYMAPS = {
key = "K",
callback = Api.node.navigate.sibling.first,
desc = {
long = "navigate to the first sibling of current file/directory",
long = "Navigate to the first sibling.",
short = "First Sibling",
},
},
@@ -112,7 +113,7 @@ local DEFAULT_KEYMAPS = {
key = "J",
callback = Api.node.navigate.sibling.last,
desc = {
long = "navigate to the last sibling of current file/directory",
long = "Navigate to the last sibling.",
short = "Last Sibling",
},
},
@@ -120,7 +121,7 @@ local DEFAULT_KEYMAPS = {
key = "I",
callback = Api.tree.toggle_gitignore_filter,
desc = {
long = "toggle visibility of files/folders hidden via |git.ignore| option",
long = "Toggle visibility of files/directories hidden via |git.ignore| option.",
short = "Toggle Git Ignore",
},
},
@@ -128,7 +129,7 @@ local DEFAULT_KEYMAPS = {
key = "H",
callback = Api.tree.toggle_hidden_filter,
desc = {
long = "toggle visibility of dotfiles via |filters.dotfiles| option",
long = "Toggle visibility of dotfiles via |filters.dotfiles| option.",
short = "Toggle Dotfiles",
},
},
@@ -136,7 +137,7 @@ local DEFAULT_KEYMAPS = {
key = "U",
callback = Api.tree.toggle_custom_filter,
desc = {
long = "toggle visibility of files/folders hidden via |filters.custom| option",
long = "Toggle visibility of files/directories hidden via |filters.custom| option.",
short = "Toggle Hidden",
},
},
@@ -144,7 +145,7 @@ local DEFAULT_KEYMAPS = {
key = "R",
callback = Api.tree.reload,
desc = {
long = "refresh the tree",
long = "Refresh the tree.",
short = "Refresh",
},
},
@@ -152,7 +153,7 @@ local DEFAULT_KEYMAPS = {
key = "a",
callback = Api.fs.create,
desc = {
long = "add a file; leaving a trailing `/` will add a directory",
long = "Create a file; leaving a trailing `/` will add a directory.",
short = "Create",
},
},
@@ -160,7 +161,7 @@ local DEFAULT_KEYMAPS = {
key = "d",
callback = Api.fs.remove,
desc = {
long = "delete a file (will prompt for confirmation)",
long = "Delete a file, prompting for confirmation.",
short = "Delete",
},
},
@@ -168,7 +169,7 @@ local DEFAULT_KEYMAPS = {
key = "D",
callback = Api.fs.trash,
desc = {
long = "trash a file via |trash| option",
long = "Trash a file via |trash| option.",
short = "Trash",
},
},
@@ -176,7 +177,7 @@ local DEFAULT_KEYMAPS = {
key = "r",
callback = Api.fs.rename,
desc = {
long = "rename a file",
long = "Rename a file or directory.",
short = "Rename",
},
},
@@ -280,7 +281,7 @@ local DEFAULT_KEYMAPS = {
key = "s",
callback = Api.node.run.system,
desc = {
long = "open a file with default system application or a folder with default file manager, using |system_open| option",
long = "Open a file with default system application or a directory with default file manager, using |system_open| option.",
short = "Run System",
},
},
@@ -288,7 +289,7 @@ local DEFAULT_KEYMAPS = {
key = "f",
callback = Api.live_filter.start,
desc = {
long = "live filter nodes dynamically based on regex matching.",
long = "Live filter nodes dynamically based on regex matching.",
short = "Filter",
},
},
@@ -296,7 +297,7 @@ local DEFAULT_KEYMAPS = {
key = "F",
callback = Api.live_filter.clear,
desc = {
long = "clear live filter",
long = "Clear live filter.",
short = "Clean Filter",
},
},
@@ -304,7 +305,7 @@ local DEFAULT_KEYMAPS = {
key = "q",
callback = Api.tree.close,
desc = {
long = "close tree window",
long = "Close tree window.",
short = "Close",
},
},
@@ -312,7 +313,7 @@ local DEFAULT_KEYMAPS = {
key = "W",
callback = Api.tree.collapse_all,
desc = {
long = "collapse the whole tree",
long = "Collapse the whole tree.",
short = "Collapse",
},
},
@@ -320,7 +321,7 @@ local DEFAULT_KEYMAPS = {
key = "E",
callback = Api.tree.expand_all,
desc = {
long = "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",
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",
},
},
@@ -328,7 +329,7 @@ local DEFAULT_KEYMAPS = {
key = "S",
callback = Api.tree.search_node,
desc = {
long = "prompt the user to enter a path and then expands the tree to match the path",
long = "Prompt the user to enter a path and then expands the tree to match the path.",
short = "Search",
},
},
@@ -336,7 +337,7 @@ local DEFAULT_KEYMAPS = {
key = ".",
callback = Api.node.run.cmd,
desc = {
long = "enter vim command mode with the file the cursor is on",
long = "Enter vim command mode with the file the cursor is on.",
short = "Run Command",
},
},
@@ -344,7 +345,7 @@ local DEFAULT_KEYMAPS = {
key = "<C-k>",
callback = Api.node.show_info_popup,
desc = {
long = "toggle a popup with file infos about the file under the cursor",
long = "Toggle a popup with file info about the file under the cursor.",
short = "Info",
},
},
@@ -352,7 +353,7 @@ local DEFAULT_KEYMAPS = {
key = "g?",
callback = Api.tree.toggle_help,
desc = {
long = "toggle help",
long = "Toggle help.",
short = "Help",
},
},
@@ -360,7 +361,7 @@ local DEFAULT_KEYMAPS = {
key = "m",
callback = Api.marks.toggle,
desc = {
long = "Toggle node in bookmarks",
long = "Toggle node in bookmarks.",
short = "Toggle Bookmark",
},
},
@@ -380,6 +381,7 @@ function M.set_keymaps(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

View File

@@ -1,83 +1,96 @@
local log = require "nvim-tree.log"
local M = {}
local function shorten_lhs(lhs)
lhs = lhs:gsub("LeftMouse", "LM")
lhs = lhs:gsub("RightMouse", "RM")
lhs = lhs:gsub("MiddleMouse", "MM")
lhs = lhs:gsub("ScrollWheelDown", "SWD")
lhs = lhs:gsub("ScrollWheelUp", "SWU")
lhs = lhs:gsub("ScrollWheelLeft", "SWL")
lhs = lhs:gsub("ScrollWheelRight", "SWR")
local function tidy_lhs(lhs)
-- nvim_buf_get_keymap replaces leading "<" with "<lt>" e.g. "<lt>CTRL-v>"
lhs = lhs:gsub("^<lt>", "<")
-- shorten ctrls
if lhs:lower():match "^<ctrl%-" then
lhs = lhs:lower():gsub("^<ctrl%-", "<C%-")
end
-- uppercase ctrls
if lhs:match "^<C%-" then
lhs = lhs:upper()
end
return lhs
end
local function shorten_lhs(lhs)
return lhs
:gsub("LeftMouse>$", "LM>")
:gsub("RightMouse>$", "RM>")
:gsub("MiddleMouse>$", "MM>")
:gsub("ScrollWheelDown>$", "SD>")
:gsub("ScrollWheelUp>$", "SU>")
:gsub("ScrollWheelLeft>$", "SL>")
:gsub("ScrollWheelRight>$", "SR>")
end
-- sort lhs roughly as per :help index
local PAT_MOUSE = "^<.*Mouse"
local PAT_CTRL = "^<C\\-"
local PAT_SPECIAL = "^<.+"
local function sort_lhs(a, b)
-- mouse last
if a:match(PAT_MOUSE) and not b:match(PAT_MOUSE) then
return false
elseif not a:match(PAT_MOUSE) and b:match(PAT_MOUSE) then
return true
end
-- ctrl first
if a:match(PAT_CTRL) and not b:match(PAT_CTRL) then
return true
elseif not a:match(PAT_CTRL) and b:match(PAT_CTRL) then
return false
end
-- special next
if a:match(PAT_SPECIAL) and not b:match(PAT_SPECIAL) then
return true
elseif not a:match(PAT_SPECIAL) and b:match(PAT_SPECIAL) then
return false
end
-- non-alpha next
if a:match "^%a" and not b:match "^%a" then
return false
elseif not a:match "^%a" and b:match "^%a" then
return true
end
return a < b
end
function M.compute_lines()
local help_lines = { "HELP" }
local help_hl = { { "NvimTreeRootFolder", 0, 0, #help_lines[1] } }
local buf_keymaps = vim.api.nvim_buf_get_keymap(vim.api.nvim_get_current_buf(), "n")
local buf_keymaps = vim.api.nvim_buf_get_keymap(vim.api.nvim_get_current_buf(), "")
log.line("dev", "%s", vim.inspect(buf_keymaps))
local processed = {}
for _, bkm in ipairs(buf_keymaps) do
local default_keymap = nil
for _, dkm in ipairs(require("nvim-tree.keymap").DEFAULT_KEYMAPS) do
if bkm.callback == dkm.callback then
default_keymap = dkm
end
end
local lhs = shorten_lhs(bkm.lhs)
if default_keymap then
table.insert(processed, { lhs, default_keymap.desc.short })
else
table.insert(processed, { lhs, "<user>" })
end
end
-- local mappings = vim.tbl_filter(function(v)
-- return (v.cb ~= nil and v.cb ~= "") or (v.action ~= nil and v.action ~= "")
-- end, require("nvim-tree.actions").mappings)
-- local processed = {}
-- for _, b in pairs(mappings) do
-- local cb = b.cb
-- local key = b.key
-- local name
-- if cb and cb:sub(1, 35) == require("nvim-tree.config").nvim_tree_callback("test"):sub(1, 35) then
-- name = cb:match "'[^']+'[^']*$"
-- name = name:match "'[^']+'"
-- elseif b.action then
-- name = b.action
-- else
-- name = (b.name ~= nil) and b.name or cb
-- name = '"' .. name .. '"'
-- end
-- table.insert(processed, { key, name, true })
-- end
local processed = vim.tbl_map(function(bkm)
return { lhs = tidy_lhs(bkm.lhs), desc = bkm.desc }
end, buf_keymaps)
table.sort(processed, function(a, b)
return (a[3] == b[3] and (a[2] < b[2] or (a[2] == b[2] and #a[1] < #b[1]))) or (a[3] and not b[3])
return sort_lhs(a.lhs, b.lhs)
end)
for _, p in pairs(processed) do
p.lhs = shorten_lhs(p.lhs)
end
local num = 0
for _, val in pairs(processed) do
local keys = type(val[1]) == "string" and { val[1] } or val[1]
local map_name = val[2]
local builtin = val[3]
for _, key in pairs(keys) do
for _, p in pairs(processed) do
num = num + 1
local bind_string = string.format("%6s : %s", key, map_name)
local bind_string = string.format("%-6.6s %s", shorten_lhs(p.lhs), p.desc)
table.insert(help_lines, bind_string)
local hl_len = math.max(6, string.len(key)) + 2
table.insert(help_hl, { "NvimTreeFolderName", num, 0, hl_len })
table.insert(help_hl, { "NvimTreeFolderName", num, 0, 6 })
if not builtin then
table.insert(help_hl, { "NvimTreeFileRenamed", num, hl_len, -1 })
end
end
table.insert(help_hl, { "NvimTreeFileRenamed", num, 6, -1 })
end
return help_lines, help_hl
end