feat(mapping): deprecate user mappings and add on_attach (#1424)
This commit is contained in:
@@ -434,6 +434,8 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
||||
sync_root_with_cwd = false,
|
||||
reload_on_bufenter = false,
|
||||
respect_buf_cwd = false,
|
||||
on_attach = "disable", -- function(bufnr). If nil, will use the deprecated mapping strategy
|
||||
remove_keymaps = false, -- boolean (disable totally or not) or list of key (lhs)
|
||||
view = {
|
||||
adaptive_size = false,
|
||||
centralize_selection = false,
|
||||
@@ -445,6 +447,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
||||
number = false,
|
||||
relativenumber = false,
|
||||
signcolumn = "yes",
|
||||
-- @deprecated
|
||||
mappings = {
|
||||
custom_only = false,
|
||||
list = {
|
||||
@@ -605,6 +608,8 @@ end
|
||||
local FIELD_OVERRIDE_TYPECHECK = {
|
||||
width = { string = true, ["function"] = true, number = true },
|
||||
height = { string = true, ["function"] = true, number = true },
|
||||
remove_keymaps = { boolean = true, table = true },
|
||||
on_attach = { ["function"] = true, string = true },
|
||||
}
|
||||
|
||||
local function validate_options(conf)
|
||||
@@ -682,6 +687,7 @@ function M.setup(conf)
|
||||
log.raw("config", "%s\n", vim.inspect(opts))
|
||||
|
||||
require("nvim-tree.actions").setup(opts)
|
||||
require("nvim-tree.keymap").setup(opts)
|
||||
require("nvim-tree.colors").setup()
|
||||
require("nvim-tree.diagnostics").setup(opts)
|
||||
require("nvim-tree.explorer").setup(opts)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
-- @deprecated: new implemention in nvim-tree.keymap. Please do not edit this file.
|
||||
|
||||
local a = vim.api
|
||||
|
||||
local log = require "nvim-tree.log"
|
||||
|
||||
286
lua/nvim-tree/keymap.lua
Normal file
286
lua/nvim-tree/keymap.lua
Normal file
@@ -0,0 +1,286 @@
|
||||
local Api = require "nvim-tree.api"
|
||||
|
||||
local M = {}
|
||||
|
||||
local DEFAULT_KEYMAPS = {
|
||||
{
|
||||
key = { "<CR>", "o", "<2-LeftMouse>" },
|
||||
callback = Api.node.open.edit,
|
||||
desc = "open a file or folder; root will cd to the above directory",
|
||||
},
|
||||
{
|
||||
key = "<C-e>",
|
||||
callback = Api.node.open.replace_tree_buffer,
|
||||
desc = "edit the file in place, effectively replacing the tree explorer",
|
||||
},
|
||||
{
|
||||
key = "O",
|
||||
callback = Api.node.open.no_window_picker,
|
||||
desc = "same as (edit) with no window picker",
|
||||
},
|
||||
{
|
||||
key = { "<C-]>", "<2-RightMouse>" },
|
||||
callback = Api.tree.change_root_to_node,
|
||||
desc = "cd in the directory under the cursor",
|
||||
},
|
||||
{
|
||||
key = "<C-v>",
|
||||
callback = Api.node.open.vertical,
|
||||
desc = "open the file in a vertical split",
|
||||
},
|
||||
{
|
||||
key = "<C-x>",
|
||||
callback = Api.node.open.horizontal,
|
||||
desc = "open the file in a horizontal split",
|
||||
},
|
||||
{
|
||||
key = "<C-t>",
|
||||
callback = Api.node.open.tab,
|
||||
desc = "open the file in a new tab",
|
||||
},
|
||||
{
|
||||
key = "<",
|
||||
callback = Api.node.navigate.sibling.prev,
|
||||
desc = "navigate to the previous sibling of current file/directory",
|
||||
},
|
||||
{
|
||||
key = ">",
|
||||
callback = Api.node.navigate.sibling.next,
|
||||
desc = "navigate to the next sibling of current file/directory",
|
||||
},
|
||||
{
|
||||
key = "P",
|
||||
callback = Api.node.navigate.parent,
|
||||
desc = "move cursor to the parent directory",
|
||||
},
|
||||
{
|
||||
key = "<BS>",
|
||||
callback = Api.node.navigate.parent_close,
|
||||
desc = "close current opened directory or parent",
|
||||
},
|
||||
{
|
||||
key = "<Tab>",
|
||||
callback = Api.node.open.preview,
|
||||
desc = "open the file as a preview (keeps the cursor in the tree)",
|
||||
},
|
||||
{
|
||||
key = "K",
|
||||
callback = Api.node.navigate.sibling.first,
|
||||
desc = "navigate to the first sibling of current file/directory",
|
||||
},
|
||||
{
|
||||
key = "J",
|
||||
callback = Api.node.navigate.sibling.last,
|
||||
desc = "navigate to the last sibling of current file/directory",
|
||||
},
|
||||
{
|
||||
key = "I",
|
||||
callback = Api.tree.toggle_gitignore_filter,
|
||||
desc = "toggle visibility of files/folders hidden via |git.ignore| option",
|
||||
},
|
||||
{
|
||||
key = "H",
|
||||
callback = Api.tree.toggle_hidden_filter,
|
||||
desc = "toggle visibility of dotfiles via |filters.dotfiles| option",
|
||||
},
|
||||
{
|
||||
key = "U",
|
||||
callback = Api.tree.toggle_custom_filter,
|
||||
desc = "toggle visibility of files/folders hidden via |filters.custom| option",
|
||||
},
|
||||
{
|
||||
key = "R",
|
||||
callback = Api.tree.reload,
|
||||
desc = "refresh the tree",
|
||||
},
|
||||
{
|
||||
key = "a",
|
||||
callback = Api.fs.create,
|
||||
desc = "add a file; leaving a trailing `/` will add a directory",
|
||||
},
|
||||
{
|
||||
key = "d",
|
||||
callback = Api.fs.remove,
|
||||
desc = "delete a file (will prompt for confirmation)",
|
||||
},
|
||||
{
|
||||
key = "D",
|
||||
callback = Api.fs.trash,
|
||||
desc = "trash a file via |trash| option",
|
||||
},
|
||||
{
|
||||
key = "r",
|
||||
callback = Api.fs.rename,
|
||||
desc = "rename a file",
|
||||
},
|
||||
{
|
||||
key = "<C-r>",
|
||||
callback = Api.fs.rename_sub,
|
||||
desc = "rename a file and omit the filename on input",
|
||||
},
|
||||
{
|
||||
key = "x",
|
||||
callback = Api.fs.cut,
|
||||
desc = "add/remove file/directory to cut clipboard",
|
||||
},
|
||||
{
|
||||
key = "c",
|
||||
callback = Api.fs.copy.node,
|
||||
desc = "add/remove file/directory to copy clipboard",
|
||||
},
|
||||
{
|
||||
key = "p",
|
||||
callback = Api.fs.paste,
|
||||
desc = "paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation",
|
||||
},
|
||||
{
|
||||
key = "y",
|
||||
callback = Api.fs.copy.filename,
|
||||
desc = "copy name to system clipboard",
|
||||
},
|
||||
{
|
||||
key = "Y",
|
||||
callback = Api.fs.copy.relative_path,
|
||||
desc = "copy relative path to system clipboard",
|
||||
},
|
||||
{
|
||||
key = "gy",
|
||||
callback = Api.fs.copy.absolute_path,
|
||||
desc = "copy absolute path to system clipboard",
|
||||
},
|
||||
{
|
||||
key = "[e",
|
||||
callback = Api.node.navigate.diagnostics.next,
|
||||
desc = "go to next diagnostic item",
|
||||
},
|
||||
{
|
||||
key = "[c",
|
||||
callback = Api.node.navigate.git.next,
|
||||
desc = "go to next git item",
|
||||
},
|
||||
{
|
||||
key = "]e",
|
||||
callback = Api.node.navigate.diagnostics.prev,
|
||||
desc = "go to prev diagnostic item",
|
||||
},
|
||||
{
|
||||
key = "]c",
|
||||
callback = Api.node.navigate.git.prev,
|
||||
desc = "go to prev git item",
|
||||
},
|
||||
{
|
||||
key = "-",
|
||||
callback = Api.tree.change_root_to_parent,
|
||||
desc = "navigate up to the parent directory of the current file/directory",
|
||||
},
|
||||
{
|
||||
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",
|
||||
},
|
||||
{
|
||||
key = "f",
|
||||
callback = Api.live_filter.start,
|
||||
desc = "live filter nodes dynamically based on regex matching.",
|
||||
},
|
||||
{
|
||||
key = "F",
|
||||
callback = Api.live_filter.clear,
|
||||
desc = "clear live filter",
|
||||
},
|
||||
{
|
||||
key = "q",
|
||||
callback = Api.tree.close,
|
||||
desc = "close tree window",
|
||||
},
|
||||
{
|
||||
key = "W",
|
||||
callback = Api.tree.collapse_all,
|
||||
desc = "collapse the whole tree",
|
||||
},
|
||||
{
|
||||
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",
|
||||
},
|
||||
{
|
||||
key = "S",
|
||||
callback = Api.tree.search_node,
|
||||
desc = "prompt the user to enter a path and then expands the tree to match the path",
|
||||
},
|
||||
{
|
||||
key = ".",
|
||||
callback = Api.node.run.cmd,
|
||||
desc = "enter vim command mode with the file the cursor is on",
|
||||
},
|
||||
{
|
||||
key = "<C-k>",
|
||||
callback = Api.node.show_info_popup,
|
||||
desc = "toggle a popup with file infos about the file under the cursor",
|
||||
},
|
||||
{
|
||||
key = "g?",
|
||||
callback = Api.tree.toggle_help,
|
||||
desc = "toggle help",
|
||||
},
|
||||
{
|
||||
key = "m",
|
||||
callback = Api.marks.toggle,
|
||||
desc = "Toggle node in bookmarks",
|
||||
},
|
||||
{
|
||||
key = "bmv",
|
||||
callback = Api.marks.bulk.move,
|
||||
desc = "Move all bookmarked nodes into specified location",
|
||||
},
|
||||
}
|
||||
|
||||
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
|
||||
vim.keymap.set("n", key, km.callback, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function get_keymaps(keys_to_disable)
|
||||
if keys_to_disable == true then
|
||||
return {}
|
||||
end
|
||||
|
||||
if type(keys_to_disable) == "table" and #keys_to_disable > 0 then
|
||||
local new_map = {}
|
||||
for _, m in pairs(DEFAULT_KEYMAPS) do
|
||||
local keys = type(m.key) == "table" and m.key or { m.key }
|
||||
local reminding_keys = {}
|
||||
for _, key in pairs(keys) do
|
||||
local found = false
|
||||
for _, key_to_disable in pairs(keys_to_disable) do
|
||||
if key_to_disable == key then
|
||||
found = true
|
||||
break
|
||||
end
|
||||
end
|
||||
if not found then
|
||||
table.insert(reminding_keys, key)
|
||||
end
|
||||
end
|
||||
if #reminding_keys > 0 then
|
||||
local map = vim.deepcopy(m)
|
||||
map.key = reminding_keys
|
||||
table.insert(new_map, map)
|
||||
end
|
||||
end
|
||||
return new_map
|
||||
end
|
||||
|
||||
return DEFAULT_KEYMAPS
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
M.keymaps = get_keymaps(opts)
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -400,4 +400,10 @@ function M.array_remove(array, item)
|
||||
end
|
||||
end
|
||||
|
||||
function M.inject_node(f)
|
||||
return function()
|
||||
f(require("nvim-tree.lib").get_node_at_cursor())
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -92,7 +92,12 @@ local function create_buffer(bufnr)
|
||||
vim.bo[M.get_bufnr()][option] = value
|
||||
end
|
||||
|
||||
require("nvim-tree.actions").apply_mappings(M.get_bufnr())
|
||||
if type(M.on_attach) == "function" then
|
||||
require("nvim-tree.keymap").set_keymaps(M.get_bufnr())
|
||||
M.on_attach(M.get_bufnr())
|
||||
else
|
||||
require("nvim-tree.actions").apply_mappings(M.get_bufnr())
|
||||
end
|
||||
end
|
||||
|
||||
local function get_size()
|
||||
@@ -426,6 +431,7 @@ function M.setup(opts)
|
||||
M.View.winopts.number = options.number
|
||||
M.View.winopts.relativenumber = options.relativenumber
|
||||
M.View.winopts.signcolumn = options.signcolumn
|
||||
M.on_attach = opts.on_attach
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user