chore(mappings): POC for help and :help on_attach keymaps

This commit is contained in:
Alexander Courtis
2022-08-01 14:17:46 +10:00
parent 451901ca9c
commit 2df271038c
2 changed files with 238 additions and 65 deletions

View File

@@ -2,236 +2,374 @@ local Api = require "nvim-tree.api"
local M = {} local M = {}
local DEFAULT_KEYMAPS = { M.DEFAULT_KEYMAPS = {
{ {
key = { "<CR>", "o", "<2-LeftMouse>" }, key = { "<CR>", "o", "<2-LeftMouse>" },
callback = Api.node.open.edit, callback = Api.node.open.edit,
desc = "open a file or folder; root will cd to the above directory", desc = {
long = "open a file or folder; root will cd to the above directory",
short = "Open",
},
}, },
{ {
key = "<C-e>", key = "<C-e>",
callback = Api.node.open.replace_tree_buffer, callback = Api.node.open.replace_tree_buffer,
desc = "edit the file in place, effectively replacing the tree explorer", desc = {
long = "edit the file in place, effectively replacing the tree explorer",
short = "Open: In Place",
},
}, },
{ {
key = "O", key = "O",
callback = Api.node.open.no_window_picker, callback = Api.node.open.no_window_picker,
desc = "same as (edit) with no window picker", desc = {
long = "same as (edit) with no window picker",
short = "Open: No Window Picker",
},
}, },
{ {
key = { "<C-]>", "<2-RightMouse>" }, key = { "<C-]>", "<2-RightMouse>" },
callback = Api.tree.change_root_to_node, 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 = "<C-v>", key = "<C-v>",
callback = Api.node.open.vertical, callback = Api.node.open.vertical,
desc = "open the file in a vertical split", desc = {
long = "open the file in a vertical split",
short = "Open: Vertical Split",
},
}, },
{ {
key = "<C-x>", key = "<C-x>",
callback = Api.node.open.horizontal, callback = Api.node.open.horizontal,
desc = "open the file in a horizontal split", desc = {
long = "open the file in a horizontal split",
short = "Open: Horizontal Split",
},
}, },
{ {
key = "<C-t>", key = "<C-t>",
callback = Api.node.open.tab, callback = Api.node.open.tab,
desc = "open the file in a new tab", desc = {
long = "open the file in a new tab",
short = "Open: New Tab",
},
}, },
{ {
key = "<", key = "<",
callback = Api.node.navigate.sibling.prev, callback = Api.node.navigate.sibling.prev,
desc = "navigate to the previous sibling of current file/directory", desc = {
long = "navigate to the previous sibling of current file/directory",
short = "Previous Sibling",
},
}, },
{ {
key = ">", key = ">",
callback = Api.node.navigate.sibling.next, callback = Api.node.navigate.sibling.next,
desc = "navigate to the next sibling of current file/directory", desc = {
long = "navigate to the next sibling of current file/directory",
short = "Next Sibling",
},
}, },
{ {
key = "P", key = "P",
callback = Api.node.navigate.parent, callback = Api.node.navigate.parent,
desc = "move cursor to the parent directory", desc = {
long = "move cursor to the parent directory",
short = "Parent Directory",
},
}, },
{ {
key = "<BS>", key = "<BS>",
callback = Api.node.navigate.parent_close, callback = Api.node.navigate.parent_close,
desc = "close current opened directory or parent", desc = {
long = "close current opened directory or parent",
short = "Close Folder",
},
}, },
{ {
key = "<Tab>", key = "<Tab>",
callback = Api.node.open.preview, callback = Api.node.open.preview,
desc = "open the file as a preview (keeps the cursor in the tree)", desc = {
long = "open the file as a preview (keeps the cursor in the tree)",
short = "Open Preview",
},
}, },
{ {
key = "K", key = "K",
callback = Api.node.navigate.sibling.first, callback = Api.node.navigate.sibling.first,
desc = "navigate to the first sibling of current file/directory", desc = {
long = "navigate to the first sibling of current file/directory",
short = "First Sibling",
},
}, },
{ {
key = "J", key = "J",
callback = Api.node.navigate.sibling.last, callback = Api.node.navigate.sibling.last,
desc = "navigate to the last sibling of current file/directory", desc = {
long = "navigate to the last sibling of current file/directory",
short = "Last Sibling",
},
}, },
{ {
key = "I", key = "I",
callback = Api.tree.toggle_gitignore_filter, callback = Api.tree.toggle_gitignore_filter,
desc = "toggle visibility of files/folders hidden via |git.ignore| option", desc = {
long = "toggle visibility of files/folders hidden via |git.ignore| option",
short = "Toggle Git Ignore",
},
}, },
{ {
key = "H", key = "H",
callback = Api.tree.toggle_hidden_filter, 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", key = "U",
callback = Api.tree.toggle_custom_filter, callback = Api.tree.toggle_custom_filter,
desc = "toggle visibility of files/folders hidden via |filters.custom| option", desc = {
long = "toggle visibility of files/folders hidden via |filters.custom| option",
short = "Toggle Hidden",
},
}, },
{ {
key = "R", key = "R",
callback = Api.tree.reload, callback = Api.tree.reload,
desc = "refresh the tree", desc = {
long = "refresh the tree",
short = "Refresh",
},
}, },
{ {
key = "a", key = "a",
callback = Api.fs.create, callback = Api.fs.create,
desc = "add a file; leaving a trailing `/` will add a directory", desc = {
long = "add a file; leaving a trailing `/` will add a directory",
short = "Create",
},
}, },
{ {
key = "d", key = "d",
callback = Api.fs.remove, callback = Api.fs.remove,
desc = "delete a file (will prompt for confirmation)", desc = {
long = "delete a file (will prompt for confirmation)",
short = "Delete",
},
}, },
{ {
key = "D", key = "D",
callback = Api.fs.trash, callback = Api.fs.trash,
desc = "trash a file via |trash| option", desc = {
long = "trash a file via |trash| option",
short = "Trash",
},
}, },
{ {
key = "r", key = "r",
callback = Api.fs.rename, callback = Api.fs.rename,
desc = "rename a file", desc = {
long = "rename a file",
short = "Rename",
},
}, },
{ {
key = "<C-r>", key = "<C-r>",
callback = Api.fs.rename_sub, callback = Api.fs.rename_sub,
desc = "rename a file and omit the filename on input", desc = {
long = "rename a file and omit the filename on input",
short = "Rename - Omit Filename",
},
}, },
{ {
key = "x", key = "x",
callback = Api.fs.cut, callback = Api.fs.cut,
desc = "add/remove file/directory to cut clipboard", desc = {
long = "add/remove file/directory to cut clipboard",
short = "Cut",
},
}, },
{ {
key = "c", key = "c",
callback = Api.fs.copy.node, callback = Api.fs.copy.node,
desc = "add/remove file/directory to copy clipboard", desc = {
long = "add/remove file/directory to copy clipboard",
short = "Copy",
},
}, },
{ {
key = "p", key = "p",
callback = Api.fs.paste, 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", key = "y",
callback = Api.fs.copy.filename, callback = Api.fs.copy.filename,
desc = "copy name to system clipboard", desc = {
long = "copy name to system clipboard",
short = "Copy File Name",
},
}, },
{ {
key = "Y", key = "Y",
callback = Api.fs.copy.relative_path, 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", key = "gy",
callback = Api.fs.copy.absolute_path, 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", key = "[e",
callback = Api.node.navigate.diagnostics.next, callback = Api.node.navigate.diagnostics.next,
desc = "go to next diagnostic item", desc = {
long = "go to next diagnostic item",
short = "Next Diagnostic",
},
}, },
{ {
key = "[c", key = "[c",
callback = Api.node.navigate.git.next, callback = Api.node.navigate.git.next,
desc = "go to next git item", desc = {
long = "go to next git item",
short = "Next Git",
},
}, },
{ {
key = "]e", key = "]e",
callback = Api.node.navigate.diagnostics.prev, callback = Api.node.navigate.diagnostics.prev,
desc = "go to prev diagnostic item", desc = {
long = "go to prev diagnostic item",
short = "Prev Diagnostic",
},
}, },
{ {
key = "]c", key = "]c",
callback = Api.node.navigate.git.prev, callback = Api.node.navigate.git.prev,
desc = "go to prev git item", desc = {
long = "go to prev git item",
short = "Prev Git",
},
}, },
{ {
key = "-", key = "-",
callback = Api.tree.change_root_to_parent, 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", key = "s",
callback = Api.node.run.system, 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 folder with default file manager, using |system_open| option",
short = "Run System",
},
}, },
{ {
key = "f", key = "f",
callback = Api.live_filter.start, 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", key = "F",
callback = Api.live_filter.clear, callback = Api.live_filter.clear,
desc = "clear live filter", desc = {
long = "clear live filter",
short = "Clean Filter",
},
}, },
{ {
key = "q", key = "q",
callback = Api.tree.close, callback = Api.tree.close,
desc = "close tree window", desc = {
long = "close tree window",
short = "Close",
},
}, },
{ {
key = "W", key = "W",
callback = Api.tree.collapse_all, callback = Api.tree.collapse_all,
desc = "collapse the whole tree", desc = {
long = "collapse the whole tree",
short = "Collapse",
},
}, },
{ {
key = "E", key = "E",
callback = Api.tree.expand_all, 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| folders; this might hang neovim for a while if running on a big folder",
short = "Expand All",
},
}, },
{ {
key = "S", key = "S",
callback = Api.tree.search_node, 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 = ".", key = ".",
callback = Api.node.run.cmd, 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 = "<C-k>", key = "<C-k>",
callback = Api.node.show_info_popup, 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 infos about the file under the cursor",
short = "Info",
},
}, },
{ {
key = "g?", key = "g?",
callback = Api.tree.toggle_help, callback = Api.tree.toggle_help,
desc = "toggle help", desc = {
long = "toggle help",
short = "Help",
},
}, },
{ {
key = "m", key = "m",
callback = Api.marks.toggle, callback = Api.marks.toggle,
desc = "Toggle node in bookmarks", desc = {
long = "Toggle node in bookmarks",
short = "Toggle Bookmark",
},
}, },
{ {
key = "bmv", key = "bmv",
callback = Api.marks.bulk.move, 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",
},
}, },
} }
@@ -247,7 +385,7 @@ end
local function filter_default_mappings(keys_to_disable) local function filter_default_mappings(keys_to_disable)
local new_map = {} local new_map = {}
for _, m in pairs(DEFAULT_KEYMAPS) do for _, m in pairs(M.DEFAULT_KEYMAPS) do
local keys = type(m.key) == "table" and m.key or { m.key } local keys = type(m.key) == "table" and m.key or { m.key }
local reminding_keys = {} local reminding_keys = {}
for _, key in pairs(keys) do for _, key in pairs(keys) do
@@ -280,7 +418,7 @@ local function get_keymaps(keys_to_disable)
return filter_default_mappings(keys_to_disable) return filter_default_mappings(keys_to_disable)
end end
return DEFAULT_KEYMAPS return M.DEFAULT_KEYMAPS
end end
function M.setup(opts) function M.setup(opts)

View File

@@ -1,27 +1,62 @@
local log = require "nvim-tree.log"
local M = {} 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")
return lhs
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 mappings = vim.tbl_filter(function(v)
return (v.cb ~= nil and v.cb ~= "") or (v.action ~= nil and v.action ~= "") local buf_keymaps = vim.api.nvim_buf_get_keymap(vim.api.nvim_get_current_buf(), "n")
end, require("nvim-tree.actions").mappings)
log.line("dev", "%s", vim.inspect(buf_keymaps))
local processed = {} local processed = {}
for _, b in pairs(mappings) do for _, bkm in ipairs(buf_keymaps) do
local cb = b.cb local default_keymap = nil
local key = b.key for _, dkm in ipairs(require("nvim-tree.keymap").DEFAULT_KEYMAPS) do
local name if bkm.callback == dkm.callback then
if cb and cb:sub(1, 35) == require("nvim-tree.config").nvim_tree_callback("test"):sub(1, 35) then default_keymap = dkm
name = cb:match "'[^']+'[^']*$" end
name = name:match "'[^']+'" end
elseif b.action then local lhs = shorten_lhs(bkm.lhs)
name = b.action if default_keymap then
else table.insert(processed, { lhs, default_keymap.desc.short })
name = (b.name ~= nil) and b.name or cb else
name = '"' .. name .. '"' table.insert(processed, { lhs, "<user>" })
end end
table.insert(processed, { key, name, true })
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 (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])
end) end)