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 function set_map_for(bufnr)
local opts = { noremap = true, silent = true, nowait = true, buffer = bufnr } local opts = { noremap = true, silent = true, nowait = true, buffer = bufnr }
return function(mode, rhs) return function(mode, rhs, desc)
return function(lhs) return function(lhs)
opts.desc = desc
vim.keymap.set(mode or "n", lhs, rhs, opts) vim.keymap.set(mode or "n", lhs, rhs, opts)
end end
end end
@@ -266,7 +267,7 @@ function M.apply_mappings(bufnr)
for _, b in pairs(M.mappings) do for _, b in pairs(M.mappings) do
local rhs = b.cb or run_dispatch(b.action) local rhs = b.cb or run_dispatch(b.action)
if rhs then 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 } local keys = type(b.key) == "table" and b.key or { b.key }
for _, key in pairs(keys) do for _, key in pairs(keys) do

View File

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

View File

@@ -1,83 +1,96 @@
local log = require "nvim-tree.log"
local M = {} local M = {}
local function shorten_lhs(lhs) local function tidy_lhs(lhs)
lhs = lhs:gsub("LeftMouse", "LM") -- nvim_buf_get_keymap replaces leading "<" with "<lt>" e.g. "<lt>CTRL-v>"
lhs = lhs:gsub("RightMouse", "RM") lhs = lhs:gsub("^<lt>", "<")
lhs = lhs:gsub("MiddleMouse", "MM")
lhs = lhs:gsub("ScrollWheelDown", "SWD") -- shorten ctrls
lhs = lhs:gsub("ScrollWheelUp", "SWU") if lhs:lower():match "^<ctrl%-" then
lhs = lhs:gsub("ScrollWheelLeft", "SWL") lhs = lhs:lower():gsub("^<ctrl%-", "<C%-")
lhs = lhs:gsub("ScrollWheelRight", "SWR") end
-- uppercase ctrls
if lhs:match "^<C%-" then
lhs = lhs:upper()
end
return lhs return lhs
end 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() function M.compute_lines()
local help_lines = { "HELP" } local help_lines = { "HELP" }
local help_hl = { { "NvimTreeRootFolder", 0, 0, #help_lines[1] } } 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 = vim.tbl_map(function(bkm)
return { lhs = tidy_lhs(bkm.lhs), desc = bkm.desc }
local processed = {} end, buf_keymaps)
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
table.sort(processed, function(a, b) 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) end)
for _, p in pairs(processed) do
p.lhs = shorten_lhs(p.lhs)
end
local num = 0 local num = 0
for _, val in pairs(processed) do for _, p 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
num = num + 1 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) table.insert(help_lines, bind_string)
local hl_len = math.max(6, string.len(key)) + 2 table.insert(help_hl, { "NvimTreeFolderName", num, 0, 6 })
table.insert(help_hl, { "NvimTreeFolderName", num, 0, hl_len })
if not builtin then table.insert(help_hl, { "NvimTreeFileRenamed", num, 6, -1 })
table.insert(help_hl, { "NvimTreeFileRenamed", num, hl_len, -1 })
end
end
end end
return help_lines, help_hl return help_lines, help_hl
end end