diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 35fc0c31..15e4ca9e 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -18,6 +18,7 @@ function M.focus() view.focus(); end +---@deprecated function M.on_keypress(...) require'nvim-tree.actions'.on_keypress(...) end diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index 6b5b035e..a11243a8 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -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 = {"", "o", "<2-LeftMouse>"}, action = "edit" }, + { key = {"<2-RightMouse>", ""}, action = "cd" }, + { key = "", action = "vsplit" }, + { key = "", action = "split"}, + { key = "", action = "tabnew" }, + { key = "<", action = "prev_sibling" }, + { key = ">", action = "next_sibling" }, + { key = "P", action = "parent_node" }, + { key = "", action = "close_node"}, + { key = "", 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 = "", 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 diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 15179917..6cb7aa81 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -1,5 +1,4 @@ local a = vim.api -local nvim_tree_callback = require'nvim-tree.config'.nvim_tree_callback local M = {} @@ -40,41 +39,6 @@ M.View = { { name = 'filetype', val = 'NvimTree' }, { name = 'bufhidden', val = 'hide' } }, - mappings = { - { key = {"", "o", "<2-LeftMouse>"}, action = "edit" }, - { key = {"<2-RightMouse>", ""}, action = "cd" }, - { key = "", action = "vsplit" }, - { key = "", action = "split"}, - { key = "", action = "tabnew" }, - { key = "<", action = "prev_sibling" }, - { key = ">", action = "next_sibling" }, - { key = "P", action = "parent_node" }, - { key = "", action = "close_node"}, - { key = "", 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 = "", 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() @@ -94,16 +58,7 @@ local function create_buffer() vim.bo[M.View.bufnr][opt.name] = opt.val end - for _, b in pairs(M.View.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(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 + require'nvim-tree.actions'.apply_mappings(M.View.bufnr) end local DEFAULT_CONFIG = { @@ -111,41 +66,11 @@ local DEFAULT_CONFIG = { height = 30, side = 'left', auto_resize = false, - mappings = { - custom_only = false, - list = {} - }, number = false, relativenumber = false, 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) local options = vim.tbl_deep_extend('force', DEFAULT_CONFIG, opts) M.View.side = options.side @@ -156,11 +81,6 @@ function M.setup(opts) M.View.winopts.number = options.number M.View.winopts.relativenumber = options.relativenumber 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 "au!"