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

@@ -18,6 +18,7 @@ function M.focus()
view.focus(); view.focus();
end end
---@deprecated
function M.on_keypress(...) function M.on_keypress(...)
require'nvim-tree.actions'.on_keypress(...) require'nvim-tree.actions'.on_keypress(...)
end end

View File

@@ -1,13 +1,47 @@
local a = vim.api
local lib = require'nvim-tree.lib' local lib = require'nvim-tree.lib'
local config = require'nvim-tree.config' local config = require'nvim-tree.config'
local view = require'nvim-tree.view' local view = require'nvim-tree.view'
local nvim_tree_callback = require'nvim-tree.config'.nvim_tree_callback
local M = {} local M = {
mappings = {
function M.setup(opts) { key = {"<CR>", "o", "<2-LeftMouse>"}, action = "edit" },
require'nvim-tree.actions.system-open'.setup(opts.system_open) { key = {"<2-RightMouse>", "<C-]>"}, action = "cd" },
require'nvim-tree.actions.trash'.setup(opts.trash) { key = "<C-v>", action = "vsplit" },
end { 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 function go_to(mode)
local icon_state = config.get_icon_state() local icon_state = config.get_icon_state()
@@ -44,10 +78,10 @@ local keypress_funcs = {
dir_up = lib.dir_up, dir_up = lib.dir_up,
close = function() M.close() end, close = function() M.close() end,
preview = function(node) preview = function(node)
if node.name == '..' then
return
end
if node.entries ~= nil then if node.entries ~= nil then
if (node.name == '..') then
return
end
return lib.expand_or_collapse(node) return lib.expand_or_collapse(node)
end end
return require'nvim-tree.actions.open-file'.fn('preview', node.absolute_path) 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() local node = lib.get_node_at_cursor()
if not node then return end 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] local default_function = keypress_funcs[action]
if type(custom_function) == 'function' then if type(custom_function) == 'function' then
@@ -87,4 +121,61 @@ function M.on_keypress(action)
end end
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 return M

View File

@@ -1,5 +1,4 @@
local a = vim.api local a = vim.api
local nvim_tree_callback = require'nvim-tree.config'.nvim_tree_callback
local M = {} local M = {}
@@ -40,41 +39,6 @@ M.View = {
{ name = 'filetype', val = 'NvimTree' }, { name = 'filetype', val = 'NvimTree' },
{ name = 'bufhidden', val = 'hide' } { name = 'bufhidden', val = 'hide' }
}, },
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 wipe_rogue_buffer() local function wipe_rogue_buffer()
@@ -94,16 +58,7 @@ local function create_buffer()
vim.bo[M.View.bufnr][opt.name] = opt.val vim.bo[M.View.bufnr][opt.name] = opt.val
end end
for _, b in pairs(M.View.mappings) do require'nvim-tree.actions'.apply_mappings(M.View.bufnr)
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(M.View.bufnr, b.mode or 'n', key, mapping_rhs, { noremap = true, silent = true, nowait = true })
end
elseif mapping_rhs then
a.nvim_buf_set_keymap(M.View.bufnr, b.mode or 'n', b.key, mapping_rhs, { noremap = true, silent = true, nowait = true })
end
end
end end
local DEFAULT_CONFIG = { local DEFAULT_CONFIG = {
@@ -111,41 +66,11 @@ local DEFAULT_CONFIG = {
height = 30, height = 30,
side = 'left', side = 'left',
auto_resize = false, auto_resize = false,
mappings = {
custom_only = false,
list = {}
},
number = false, number = false,
relativenumber = false, relativenumber = false,
signcolumn = 'yes' signcolumn = 'yes'
} }
local function merge_mappings(user_mappings)
if #user_mappings == 0 then
return M.View.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.View.custom_keypress_funcs[map.action] = map.action_cb
end
end
local view_mappings = vim.tbl_filter(function(map)
return not vim.tbl_contains(user_keys, map.key)
end, M.View.mappings)
return vim.fn.extend(view_mappings, user_mappings)
end
function M.setup(opts) function M.setup(opts)
local options = vim.tbl_deep_extend('force', DEFAULT_CONFIG, opts) local options = vim.tbl_deep_extend('force', DEFAULT_CONFIG, opts)
M.View.side = options.side M.View.side = options.side
@@ -156,11 +81,6 @@ function M.setup(opts)
M.View.winopts.number = options.number M.View.winopts.number = options.number
M.View.winopts.relativenumber = options.relativenumber M.View.winopts.relativenumber = options.relativenumber
M.View.winopts.signcolumn = options.signcolumn M.View.winopts.signcolumn = options.signcolumn
if options.mappings.custom_only then
M.View.mappings = options.mappings.list
else
M.View.mappings = merge_mappings(options.mappings.list)
end
vim.cmd "augroup NvimTreeView" vim.cmd "augroup NvimTreeView"
vim.cmd "au!" vim.cmd "au!"