refacto: move all keybind logic to actions/init

This commit is contained in:
kiyan
2022-01-21 12:21:23 +01:00
parent ba3f3357eb
commit 14461373e0
3 changed files with 103 additions and 91 deletions

View File

@@ -1,13 +1,47 @@
local a = vim.api
local lib = require'nvim-tree.lib'
local config = require'nvim-tree.config'
local view = require'nvim-tree.view'
local nvim_tree_callback = require'nvim-tree.config'.nvim_tree_callback
local M = {}
function M.setup(opts)
require'nvim-tree.actions.system-open'.setup(opts.system_open)
require'nvim-tree.actions.trash'.setup(opts.trash)
end
local M = {
mappings = {
{ key = {"<CR>", "o", "<2-LeftMouse>"}, action = "edit" },
{ key = {"<2-RightMouse>", "<C-]>"}, action = "cd" },
{ key = "<C-v>", action = "vsplit" },
{ key = "<C-x>", action = "split"},
{ key = "<C-t>", action = "tabnew" },
{ key = "<", action = "prev_sibling" },
{ key = ">", action = "next_sibling" },
{ key = "P", action = "parent_node" },
{ key = "<BS>", action = "close_node"},
{ key = "<Tab>", action = "preview" },
{ key = "K", action = "first_sibling" },
{ key = "J", action = "last_sibling" },
{ key = "I", action = "toggle_ignored" },
{ key = "H", action = "toggle_dotfiles" },
{ key = "R", action = "refresh" },
{ key = "a", action = "create" },
{ key = "d", action = "remove" },
{ key = "D", action = "trash"},
{ key = "r", action = "rename" },
{ key = "<C-r>", action = "full_rename" },
{ key = "x", action = "cut" },
{ key = "c", action = "copy"},
{ key = "p", action = "paste" },
{ key = "y", action = "copy_name" },
{ key = "Y", action = "copy_path" },
{ key = "gy", action = "copy_absolute_path" },
{ key = "[c", action = "prev_git_item" },
{ key = "]c", action = "next_git_item" },
{ key = "-", action = "dir_up" },
{ key = "s", action = "system_open" },
{ key = "q", action = "close"},
{ key = "g?", action = "toggle_help" }
},
custom_keypress_funcs = {},
}
local function go_to(mode)
local icon_state = config.get_icon_state()
@@ -44,10 +78,10 @@ local keypress_funcs = {
dir_up = lib.dir_up,
close = function() M.close() end,
preview = function(node)
if node.name == '..' then
return
end
if node.entries ~= nil then
if (node.name == '..') then
return
end
return lib.expand_or_collapse(node)
end
return require'nvim-tree.actions.open-file'.fn('preview', node.absolute_path)
@@ -61,7 +95,7 @@ function M.on_keypress(action)
local node = lib.get_node_at_cursor()
if not node then return end
local custom_function = view.View.custom_keypress_funcs[action]
local custom_function = M.custom_keypress_funcs[action]
local default_function = keypress_funcs[action]
if type(custom_function) == 'function' then
@@ -87,4 +121,61 @@ function M.on_keypress(action)
end
end
function M.apply_mappings(bufnr)
for _, b in pairs(M.mappings) do
local mapping_rhs = b.cb or nvim_tree_callback(b.action)
if type(b.key) == "table" then
for _, key in pairs(b.key) do
a.nvim_buf_set_keymap(bufnr, b.mode or 'n', key, mapping_rhs, { noremap = true, silent = true, nowait = true })
end
elseif mapping_rhs then
a.nvim_buf_set_keymap(bufnr, b.mode or 'n', b.key, mapping_rhs, { noremap = true, silent = true, nowait = true })
end
end
end
local function merge_mappings(user_mappings)
if #user_mappings == 0 then
return M.mappings
end
local user_keys = {}
for _, map in pairs(user_mappings) do
if type(map.key) == "table" then
for _, key in pairs(map.key) do
table.insert(user_keys, key)
end
else
table.insert(user_keys, map.key)
end
if map.action and type(map.action_cb) == "function" then
M.custom_keypress_funcs[map.action] = map.action_cb
end
end
local mappings = vim.tbl_filter(function(map)
return not vim.tbl_contains(user_keys, map.key)
end, M.mappings)
return vim.fn.extend(mappings, user_mappings)
end
local DEFAULT_MAPPING_CONFIG = {
custom_only = false,
list = {}
}
function M.setup(opts)
require'nvim-tree.actions.system-open'.setup(opts.system_open)
require'nvim-tree.actions.trash'.setup(opts.trash)
local user_map_config = (opts.view or {}).mappings or {}
local options = vim.tbl_deep_extend('force', DEFAULT_MAPPING_CONFIG, user_map_config)
if options.custom_only then
M.mappings = options.mappings.list
else
M.mappings = merge_mappings(options.list)
end
end
return M