chore(mappings): migrate legacy mappings under the hood: map keymap to legacy mappings

This commit is contained in:
Alexander Courtis 2022-09-04 15:56:36 +10:00
parent b4d8ef2364
commit 3eb5e4c942
3 changed files with 67 additions and 54 deletions

View File

@ -11,6 +11,7 @@ local DEFAULT_KEYMAPS = {
long = "Open a file or directory; root will cd to the above directory.",
short = "Open",
},
legacy_action = "edit",
},
{
key = "<C-e>",
@ -19,6 +20,7 @@ local DEFAULT_KEYMAPS = {
long = "Open file in place, effectively replacing the tree explorer.",
short = "Open: In Place",
},
legacy_action = "edit_in_place",
},
{
key = "O",
@ -27,6 +29,7 @@ local DEFAULT_KEYMAPS = {
long = "Open file with no window picker.",
short = "Open: No Window Picker",
},
legacy_action = "edit_no_picker",
},
{
key = { "<C-]>", "<2-RightMouse>" },
@ -35,6 +38,7 @@ local DEFAULT_KEYMAPS = {
long = "cd in the directory under the cursor.",
short = "CD",
},
legacy_action = "cd",
},
{
-- key = "<C-v>",
@ -44,6 +48,7 @@ local DEFAULT_KEYMAPS = {
long = "Open file in a vertical split.",
short = "Open: Vertical Split",
},
legacy_action = "vsplit",
},
{
key = "<C-x>",
@ -52,6 +57,7 @@ local DEFAULT_KEYMAPS = {
long = "Open file in a horizontal split.",
short = "Open: Horizontal Split",
},
legacy_action = "split",
},
{
key = "<C-t>",
@ -60,6 +66,7 @@ local DEFAULT_KEYMAPS = {
long = "Open file in a new tab.",
short = "Open: New Tab",
},
legacy_action = "tabnew",
},
{
key = "<",
@ -68,6 +75,7 @@ local DEFAULT_KEYMAPS = {
long = "Navigate to the previous sibling.",
short = "Previous Sibling",
},
legacy_action = "prev_sibling",
},
{
key = ">",
@ -76,6 +84,7 @@ local DEFAULT_KEYMAPS = {
long = "Navigate to the next sibling",
short = "Next Sibling",
},
legacy_action = "next_sibling",
},
{
key = "P",
@ -84,6 +93,7 @@ local DEFAULT_KEYMAPS = {
long = "Move cursor to the parent directory.",
short = "Parent Directory",
},
legacy_action = "parent_node",
},
{
key = "<BS>",
@ -92,6 +102,7 @@ local DEFAULT_KEYMAPS = {
long = "Close current opened directory or parent.",
short = "Close Directory",
},
legacy_action = "close_node",
},
{
key = "<Tab>",
@ -100,6 +111,7 @@ local DEFAULT_KEYMAPS = {
long = "Open file as a preview (keeps the cursor in the tree).",
short = "Open Preview",
},
legacy_action = "preview",
},
{
key = "K",
@ -108,6 +120,7 @@ local DEFAULT_KEYMAPS = {
long = "Navigate to the first sibling.",
short = "First Sibling",
},
legacy_action = "first_sibling",
},
{
key = "J",
@ -116,6 +129,7 @@ local DEFAULT_KEYMAPS = {
long = "Navigate to the last sibling.",
short = "Last Sibling",
},
legacy_action = "last_sibling",
},
{
key = "I",
@ -124,6 +138,7 @@ local DEFAULT_KEYMAPS = {
long = "Toggle visibility of files/directories hidden via |git.ignore| option.",
short = "Toggle Git Ignore",
},
legacy_action = "toggle_git_ignored",
},
{
key = "H",
@ -132,6 +147,7 @@ local DEFAULT_KEYMAPS = {
long = "Toggle visibility of dotfiles via |filters.dotfiles| option.",
short = "Toggle Dotfiles",
},
legacy_action = "toggle_dotfiles",
},
{
key = "U",
@ -140,6 +156,7 @@ local DEFAULT_KEYMAPS = {
long = "Toggle visibility of files/directories hidden via |filters.custom| option.",
short = "Toggle Hidden",
},
legacy_action = "toggle_custom",
},
{
key = "R",
@ -148,6 +165,7 @@ local DEFAULT_KEYMAPS = {
long = "Refresh the tree.",
short = "Refresh",
},
legacy_action = "refresh",
},
{
key = "a",
@ -156,6 +174,7 @@ local DEFAULT_KEYMAPS = {
long = "Create a file; leaving a trailing `/` will add a directory.",
short = "Create",
},
legacy_action = "create",
},
{
key = "d",
@ -164,6 +183,7 @@ local DEFAULT_KEYMAPS = {
long = "Delete a file, prompting for confirmation.",
short = "Delete",
},
legacy_action = "remove",
},
{
key = "D",
@ -172,6 +192,7 @@ local DEFAULT_KEYMAPS = {
long = "Trash a file via |trash| option.",
short = "Trash",
},
legacy_action = "trash",
},
{
key = "r",
@ -180,6 +201,7 @@ local DEFAULT_KEYMAPS = {
long = "Rename a file or directory.",
short = "Rename",
},
legacy_action = "rename",
},
{
key = "<C-r>",
@ -188,6 +210,7 @@ local DEFAULT_KEYMAPS = {
long = "Rename a file or directory and omit the filename on input.",
short = "Rename: Omit Filename",
},
legacy_action = "full_rename",
},
{
key = "x",
@ -196,6 +219,7 @@ local DEFAULT_KEYMAPS = {
long = "Cut file or directory to cut clipboard.",
short = "Cut",
},
legacy_action = "cut",
},
{
key = "c",
@ -204,6 +228,7 @@ local DEFAULT_KEYMAPS = {
long = "Copy file or directory to copy clipboard.",
short = "Copy",
},
legacy_action = "copy",
},
{
key = "p",
@ -212,6 +237,7 @@ local DEFAULT_KEYMAPS = {
long = "Paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation.",
short = "Paste",
},
legacy_action = "paste",
},
{
key = "y",
@ -220,6 +246,7 @@ local DEFAULT_KEYMAPS = {
long = "Copy name to system clipboard.",
short = "Copy Name",
},
legacy_action = "copy_name",
},
{
key = "Y",
@ -228,6 +255,7 @@ local DEFAULT_KEYMAPS = {
long = "Copy relative path to system clipboard.",
short = "Copy Relative Path",
},
legacy_action = "copy_path",
},
{
key = "gy",
@ -236,6 +264,7 @@ local DEFAULT_KEYMAPS = {
long = "Copy absolute path to system clipboard.",
short = "Copy Absolute Path",
},
legacy_action = "copy_absolute_path",
},
{
key = "]e",
@ -244,6 +273,7 @@ local DEFAULT_KEYMAPS = {
long = "Go to next diagnostic item.",
short = "Next Diagnostic",
},
legacy_action = "next_diag_item",
},
{
key = "]c",
@ -252,6 +282,7 @@ local DEFAULT_KEYMAPS = {
long = "Go to next git item.",
short = "Next Git",
},
legacy_action = "next_git_item",
},
{
key = "[e",
@ -260,6 +291,7 @@ local DEFAULT_KEYMAPS = {
long = "Go to prev diagnostic item.",
short = "Prev Diagnostic",
},
legacy_action = "prev_diag_item",
},
{
key = "[c",
@ -268,6 +300,7 @@ local DEFAULT_KEYMAPS = {
long = "Go to prev git item.",
short = "Prev Git",
},
legacy_action = "prev_git_item",
},
{
key = "-",
@ -276,6 +309,7 @@ local DEFAULT_KEYMAPS = {
long = "Navigate up to the parent directory of the current file/directory.",
short = "Up",
},
legacy_action = "dir_up",
},
{
key = "s",
@ -284,6 +318,7 @@ local DEFAULT_KEYMAPS = {
long = "Open a file with default system application or a directory with default file manager, using |system_open| option.",
short = "Run System",
},
legacy_action = "system_open",
},
{
key = "f",
@ -292,6 +327,7 @@ local DEFAULT_KEYMAPS = {
long = "Live filter nodes dynamically based on regex matching.",
short = "Filter",
},
legacy_action = "live_filter",
},
{
key = "F",
@ -300,6 +336,7 @@ local DEFAULT_KEYMAPS = {
long = "Clear live filter.",
short = "Clean Filter",
},
legacy_action = "clear_live_filter",
},
{
key = "q",
@ -308,6 +345,7 @@ local DEFAULT_KEYMAPS = {
long = "Close tree window.",
short = "Close",
},
legacy_action = "close",
},
{
key = "W",
@ -316,6 +354,7 @@ local DEFAULT_KEYMAPS = {
long = "Collapse the whole tree.",
short = "Collapse",
},
legacy_action = "collapse_all",
},
{
key = "E",
@ -324,6 +363,7 @@ local DEFAULT_KEYMAPS = {
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",
},
legacy_action = "expand_all",
},
{
key = "S",
@ -332,6 +372,7 @@ local DEFAULT_KEYMAPS = {
long = "Prompt the user to enter a path and then expands the tree to match the path.",
short = "Search",
},
legacy_action = "search_node",
},
{
key = ".",
@ -340,6 +381,7 @@ local DEFAULT_KEYMAPS = {
long = "Enter vim command mode with the file the cursor is on.",
short = "Run Command",
},
legacy_action = "run_file_command",
},
{
key = "<C-k>",
@ -348,6 +390,7 @@ local DEFAULT_KEYMAPS = {
long = "Toggle a popup with file info about the file under the cursor.",
short = "Info",
},
legacy_action = "toggle_file_info",
},
{
key = "g?",
@ -356,6 +399,7 @@ local DEFAULT_KEYMAPS = {
long = "Toggle help.",
short = "Help",
},
legacy_action = "toggle_help",
},
{
key = "m",
@ -364,6 +408,7 @@ local DEFAULT_KEYMAPS = {
long = "Toggle node in bookmarks.",
short = "Toggle Bookmark",
},
legacy_action = "toggle_mark",
},
{
key = "bmv",
@ -372,6 +417,7 @@ local DEFAULT_KEYMAPS = {
long = "Move all bookmarked nodes into specified location.",
short = "Move Bookmarked",
},
legacy_action = "bulk_move",
},
}
-- END_DEFAULT_KEYMAPS

View File

@ -1,5 +1,6 @@
local utils = require "nvim-tree.utils"
local Api = require "nvim-tree.api"
local DEFAULT_KEYMAPS = require("nvim-tree.keymap").DEFAULT_KEYMAPS
local M = {}
@ -298,55 +299,6 @@ local function removed(opts)
end
end
local OLD_MAPPING_TABLE = {
edit = Api.node.open.edit,
edit_in_place = Api.node.open.replace_tree_buffer,
edit_no_picker = Api.node.open.no_window_picker,
cd = Api.tree.change_root_to_node,
vsplit = Api.node.open.vertical,
split = Api.node.open.horizontal,
tabnew = Api.node.open.tab,
preview = Api.node.open.preview,
prev_sibling = Api.node.navigate.sibling.prev,
next_sibling = Api.node.navigate.sibling.next,
parent_node = Api.node.navigate.parent,
close_node = Api.node.navigate.parent_close,
first_sibling = Api.node.navigate.sibling.first,
last_sibling = Api.node.navigate.sibling.last,
toggle_git_ignored = Api.tree.toggle_gitignore_filter,
toggle_dotfiles = Api.tree.toggle_hidden_filter,
toggle_custom = Api.tree.toggle_custom_filter,
refresh = Api.tree.reload,
create = Api.fs.create,
remove = Api.fs.remove,
trash = Api.fs.trash,
rename = Api.fs.rename,
full_rename = Api.fs.rename_sub,
cut = Api.fs.cut,
copy = Api.fs.copy.node,
paste = Api.fs.paste,
copy_name = Api.fs.copy.filename,
copy_path = Api.fs.copy.relative_path,
copy_absolute_path = Api.fs.copy.absolute_path,
prev_git_item = Api.node.navigate.git.prev,
next_git_item = Api.node.navigate.git.next,
prev_diag_item = Api.node.navigate.diagnostics.prev,
next_diag_item = Api.node.navigate.diagnostics.next,
dir_up = Api.tree.change_root_to_parent,
system_open = Api.node.run.system,
live_filter = Api.live_filter.start,
clear_live_filter = Api.live_filter.clear,
close = Api.tree.close,
collapse_all = Api.tree.collapse_all,
expand_all = Api.tree.expand_all,
search_node = Api.tree.search_node,
run_file_command = Api.node.run.cmd,
toggle_file_info = Api.node.show_info_popup,
toggle_help = Api.tree.toggle_help,
toggle_mark = Api.marks.toggle,
bulk_move = Api.marks.bulk.move,
}
function M.move_mappings_to_keymap(opts)
if opts.on_attach == "disable" and opts.view and opts.view.mappings then
local custom_only, list = opts.view.mappings.custom_only, opts.view.mappings.list
@ -355,24 +307,35 @@ function M.move_mappings_to_keymap(opts)
opts.view.mappings.custom_only = nil
end
if list then
local keymap_by_action = utils.key_by(DEFAULT_KEYMAPS, "legacy_action")
if not custom_only then
opts.remove_keymaps = {}
end
local call_list = {}
opts.on_attach = function(bufnr)
for _, el in pairs(call_list) do
vim.keymap.set(el.mode or "n", el.key, el.callback, { buffer = bufnr, remap = false, silent = true })
if el.callback then
vim.keymap.set(el.mode or "n", el.key, el.callback, { buffer = bufnr, remap = false, silent = true })
elseif el.keymap then
vim.keymap.set(
el.mode or "n",
el.key,
el.keymap.callback,
{ buffer = bufnr, remap = false, silent = true, desc = el.keymap.desc.short }
)
end
end
end
for _, map in pairs(list) do
local keys = type(map.key) == "table" and map.key or { map.key }
local mode = map.mode or "n"
local callback
local keymap
if map.action ~= "" then
if map.action_cb then
callback = map.action_cb
else
callback = OLD_MAPPING_TABLE[map.action]
elseif keymap_by_action[map.action] then
keymap = keymap_by_action[map.action]
end
end
@ -383,6 +346,8 @@ function M.move_mappings_to_keymap(opts)
if callback then
table.insert(call_list, { mode = mode, key = k, callback = callback })
elseif keymap then
table.insert(call_list, { mode = mode, key = k, keymap = keymap })
end
end
end

View File

@ -342,7 +342,9 @@ end
function M.key_by(tbl, key)
local keyed = {}
for _, val in ipairs(tbl) do
keyed[val[key]] = val
if val[key] then
keyed[val[key]] = val
end
end
return keyed
end