From f6f439ba59b051e16f17d56bf1271af67fb5b662 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 4 Sep 2022 16:28:12 +1000 Subject: [PATCH] chore(mappings): migrate legacy mappings under the hood: remove dispatch --- lua/nvim-tree.lua | 3 - lua/nvim-tree/actions/dispatch.lua | 128 --------- lua/nvim-tree/actions/init.lua | 402 +---------------------------- lua/nvim-tree/view.lua | 2 - scripts/update-help.sh | 26 +- 5 files changed, 18 insertions(+), 543 deletions(-) delete mode 100644 lua/nvim-tree/actions/dispatch.lua diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 54bbfe9e..e4e01177 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -68,9 +68,6 @@ function M.change_root(filepath, bufnr) change_dir.fn(vim.fn.fnamemodify(filepath, ":p:h")) end ----@deprecated -M.on_keypress = require("nvim-tree.actions.dispatch").dispatch - function M.toggle(find_file, no_focus, cwd) if view.is_visible() then view.close() diff --git a/lua/nvim-tree/actions/dispatch.lua b/lua/nvim-tree/actions/dispatch.lua deleted file mode 100644 index 44eff983..00000000 --- a/lua/nvim-tree/actions/dispatch.lua +++ /dev/null @@ -1,128 +0,0 @@ -local view = require "nvim-tree.view" -local lib = require "nvim-tree.lib" - -local M = {} - -local Actions = { - close = view.close, - - -- Tree modifiers - collapse_all = require("nvim-tree.actions.tree-modifiers.collapse-all").fn, - expand_all = require("nvim-tree.actions.tree-modifiers.expand-all").fn, - toggle_dotfiles = require("nvim-tree.actions.tree-modifiers.toggles").dotfiles, - toggle_custom = require("nvim-tree.actions.tree-modifiers.toggles").custom, - toggle_git_ignored = require("nvim-tree.actions.tree-modifiers.toggles").git_ignored, - - -- Filesystem operations - copy_absolute_path = require("nvim-tree.actions.fs.copy-paste").copy_absolute_path, - copy_name = require("nvim-tree.actions.fs.copy-paste").copy_filename, - copy_path = require("nvim-tree.actions.fs.copy-paste").copy_path, - copy = require("nvim-tree.actions.fs.copy-paste").copy, - create = require("nvim-tree.actions.fs.create-file").fn, - cut = require("nvim-tree.actions.fs.copy-paste").cut, - full_rename = require("nvim-tree.actions.fs.rename-file").fn(true), - paste = require("nvim-tree.actions.fs.copy-paste").paste, - trash = require("nvim-tree.actions.fs.trash").fn, - remove = require("nvim-tree.actions.fs.remove-file").fn, - rename = require("nvim-tree.actions.fs.rename-file").fn(false), - - -- Movements in tree - close_node = require("nvim-tree.actions.moves.parent").fn(true), - first_sibling = require("nvim-tree.actions.moves.sibling").fn "first", - last_sibling = require("nvim-tree.actions.moves.sibling").fn "last", - next_diag_item = require("nvim-tree.actions.moves.item").fn("next", "diag"), - next_git_item = require("nvim-tree.actions.moves.item").fn("next", "git"), - next_sibling = require("nvim-tree.actions.moves.sibling").fn "next", - parent_node = require("nvim-tree.actions.moves.parent").fn(false), - prev_diag_item = require("nvim-tree.actions.moves.item").fn("prev", "diag"), - prev_git_item = require("nvim-tree.actions.moves.item").fn("prev", "git"), - prev_sibling = require("nvim-tree.actions.moves.sibling").fn "prev", - - -- Other types - refresh = require("nvim-tree.actions.reloaders.reloaders").reload_explorer, - dir_up = require("nvim-tree.actions.root.dir-up").fn, - search_node = require("nvim-tree.actions.finders.search-node").fn, - run_file_command = require("nvim-tree.actions.node.run-command").run_file_command, - toggle_file_info = require("nvim-tree.actions.node.file-popup").toggle_file_info, - system_open = require("nvim-tree.actions.node.system-open").fn, - toggle_mark = require("nvim-tree.marks").toggle_mark, - bulk_move = require("nvim-tree.marks.bulk-move").bulk_move, -} - -local function handle_action_on_help_ui(action) - if action == "close" or action == "toggle_help" then - require("nvim-tree.actions.tree-modifiers.toggles").help() - end -end - -local function handle_filter_actions(action) - if action == "live_filter" then - require("nvim-tree.live-filter").start_filtering() - elseif action == "clear_live_filter" then - require("nvim-tree.live-filter").clear_filter() - end -end - -local function change_dir_action(node) - if node.name == ".." then - require("nvim-tree.actions.root.change-dir").fn ".." - elseif node.nodes ~= nil then - require("nvim-tree.actions.root.change-dir").fn(lib.get_last_group_node(node).absolute_path) - end -end - -local function open_file(action, node) - local path = node.absolute_path - if node.link_to and not node.nodes then - path = node.link_to - end - require("nvim-tree.actions.node.open-file").fn(action, path) -end - -local function handle_tree_actions(action) - local node = lib.get_node_at_cursor() - if not node then - return - end - - local custom_function = M.custom_keypress_funcs[action] - local defined_action = Actions[action] - - if type(custom_function) == "function" then - return custom_function(node) - elseif defined_action then - return defined_action(node) - end - - local is_parent = node.name == ".." - - if action == "preview" and is_parent then - return - end - - if action == "cd" or is_parent then - return change_dir_action(node) - end - - if node.nodes then - lib.expand_or_collapse(node) - else - open_file(action, node) - end -end - -function M.dispatch(action) - if view.is_help_ui() or action == "toggle_help" then - handle_action_on_help_ui(action) - elseif action == "live_filter" or action == "clear_live_filter" then - handle_filter_actions(action) - else - handle_tree_actions(action) - end -end - -function M.setup(custom_keypress_funcs) - M.custom_keypress_funcs = custom_keypress_funcs -end - -return M diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index e69775a2..edbd9763 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -1,391 +1,6 @@ --- @deprecated: new implementation in nvim-tree.keymap. Please do not edit this file. - -local a = vim.api - local log = require "nvim-tree.log" -local view = require "nvim-tree.view" -local util = require "nvim-tree.utils" --- BEGIN_DEFAULT_MAPPINGS -local DEFAULT_MAPPINGS = { - { - key = { "", "o", "<2-LeftMouse>" }, - action = "edit", - desc = "open a file or folder; root will cd to the above directory", - }, - { - key = "", - action = "edit_in_place", - desc = "edit the file in place, effectively replacing the tree explorer", - }, - { - key = "O", - action = "edit_no_picker", - desc = "same as (edit) with no window picker", - }, - { - key = { "", "<2-RightMouse>" }, - action = "cd", - desc = "cd in the directory under the cursor", - }, - { - key = "", - action = "vsplit", - desc = "open the file in a vertical split", - }, - { - key = "", - action = "split", - desc = "open the file in a horizontal split", - }, - { - key = "", - action = "tabnew", - desc = "open the file in a new tab", - }, - { - key = "<", - action = "prev_sibling", - desc = "navigate to the previous sibling of current file/directory", - }, - { - key = ">", - action = "next_sibling", - desc = "navigate to the next sibling of current file/directory", - }, - { - key = "P", - action = "parent_node", - desc = "move cursor to the parent directory", - }, - { - key = "", - action = "close_node", - desc = "close current opened directory or parent", - }, - { - key = "", - action = "preview", - desc = "open the file as a preview (keeps the cursor in the tree)", - }, - { - key = "K", - action = "first_sibling", - desc = "navigate to the first sibling of current file/directory", - }, - { - key = "J", - action = "last_sibling", - desc = "navigate to the last sibling of current file/directory", - }, - { - key = "I", - action = "toggle_git_ignored", - desc = "toggle visibility of files/folders hidden via |git.ignore| option", - }, - { - key = "H", - action = "toggle_dotfiles", - desc = "toggle visibility of dotfiles via |filters.dotfiles| option", - }, - { - key = "U", - action = "toggle_custom", - desc = "toggle visibility of files/folders hidden via |filters.custom| option", - }, - { - key = "R", - action = "refresh", - desc = "refresh the tree", - }, - { - key = "a", - action = "create", - desc = "add a file; leaving a trailing `/` will add a directory", - }, - { - key = "d", - action = "remove", - desc = "delete a file (will prompt for confirmation)", - }, - { - key = "D", - action = "trash", - desc = "trash a file via |trash| option", - }, - { - key = "r", - action = "rename", - desc = "rename a file", - }, - { - key = "", - action = "full_rename", - desc = "rename a file and omit the filename on input", - }, - { - key = "x", - action = "cut", - desc = "add/remove file/directory to cut clipboard", - }, - { - key = "c", - action = "copy", - desc = "add/remove file/directory to copy clipboard", - }, - { - key = "p", - action = "paste", - desc = "paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation", - }, - { - key = "y", - action = "copy_name", - desc = "copy name to system clipboard", - }, - { - key = "Y", - action = "copy_path", - desc = "copy relative path to system clipboard", - }, - { - key = "gy", - action = "copy_absolute_path", - desc = "copy absolute path to system clipboard", - }, - { - key = "[e", - action = "prev_diag_item", - desc = "go to next diagnostic item", - }, - { - key = "[c", - action = "prev_git_item", - desc = "go to next git item", - }, - { - key = "]e", - action = "next_diag_item", - desc = "go to prev diagnostic item", - }, - { - key = "]c", - action = "next_git_item", - desc = "go to prev git item", - }, - { - key = "-", - action = "dir_up", - desc = "navigate up to the parent directory of the current file/directory", - }, - { - key = "s", - action = "system_open", - desc = "open a file with default system application or a folder with default file manager, using |system_open| option", - }, - { - key = "f", - action = "live_filter", - desc = "live filter nodes dynamically based on regex matching.", - }, - { - key = "F", - action = "clear_live_filter", - desc = "clear live filter", - }, - { - key = "q", - action = "close", - desc = "close tree window", - }, - { - key = "W", - action = "collapse_all", - desc = "collapse the whole tree", - }, - { - key = "E", - action = "expand_all", - desc = "expand the whole tree, stopping after expanding |actions.expand_all.max_folder_discovery| folders; this might hang neovim for a while if running on a big folder", - }, - { - key = "S", - action = "search_node", - desc = "prompt the user to enter a path and then expands the tree to match the path", - }, - { - key = ".", - action = "run_file_command", - desc = "enter vim command mode with the file the cursor is on", - }, - { - key = "", - action = "toggle_file_info", - desc = "toggle a popup with file infos about the file under the cursor", - }, - { - key = "g?", - action = "toggle_help", - desc = "toggle help", - }, - { - key = "m", - action = "toggle_mark", - desc = "Toggle node in bookmarks", - }, - { - key = "bmv", - action = "bulk_move", - desc = "Move all bookmarked nodes into specified location", - }, -} --- END_DEFAULT_MAPPINGS - -local M = { - mappings = {}, - custom_keypress_funcs = {}, -} - -local function set_map_for(bufnr) - local opts = { noremap = true, silent = true, nowait = true, buffer = bufnr } - return function(mode, rhs, desc) - return function(lhs) - opts.desc = desc - vim.keymap.set(mode or "n", lhs, rhs, opts) - end - end -end - -local function run_dispatch(action) - return function() - require("nvim-tree.actions.dispatch").dispatch(action) - end -end - -function M.apply_mappings(bufnr) - local setter_for = set_map_for(bufnr) - for _, b in pairs(M.mappings) do - local rhs = b.cb or run_dispatch(b.action) - if rhs then - local setter = setter_for(b.mode, rhs, b.action) - - local keys = type(b.key) == "table" and b.key or { b.key } - for _, key in pairs(keys) do - setter(key) - end - end - end -end - -local function merge_mappings(user_mappings) - if #user_mappings == 0 then - return M.mappings - end - - local function is_empty(s) - return s == "" - end - - local user_keys = {} - local removed_keys = {} - -- remove default mappings if action is a empty string - 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) - if is_empty(map.action) then - table.insert(removed_keys, key) - end - end - else - table.insert(user_keys, map.key) - if is_empty(map.action) then - table.insert(removed_keys, map.key) - end - end - - if map.action and type(map.action_cb) == "function" then - if not is_empty(map.action) then - M.custom_keypress_funcs[map.action] = map.action_cb - else - util.notify.warn "action can't be empty if action_cb provided" - end - end - end - - local default_map = vim.tbl_filter(function(map) - if type(map.key) == "table" then - local filtered_keys = {} - for _, key in pairs(map.key) do - if not vim.tbl_contains(user_keys, key) and not vim.tbl_contains(removed_keys, key) then - table.insert(filtered_keys, key) - end - end - map.key = filtered_keys - return not vim.tbl_isempty(map.key) - else - return not vim.tbl_contains(user_keys, map.key) and not vim.tbl_contains(removed_keys, map.key) - end - end, M.mappings) - - local user_map = vim.tbl_filter(function(map) - return not is_empty(map.action) - end, user_mappings) - - return vim.fn.extend(default_map, user_map) -end - -local function copy_mappings(user_mappings) - if #user_mappings == 0 then - return M.mappings - end - - for _, map in pairs(user_mappings) do - if map.action and type(map.action_cb) == "function" then - M.custom_keypress_funcs[map.action] = map.action_cb - end - end - - return user_mappings -end - -local function cleanup_existing_mappings() - local bufnr = view.get_bufnr() - if bufnr == nil or not a.nvim_buf_is_valid(bufnr) then - return - end - - for _, b in pairs(M.mappings) do - local keys = type(b.key) == "table" and b.key or { b.key } - for _, key in pairs(keys) do - vim.keymap.del(b.mode or "n", key, { buffer = bufnr }) - end - end -end - -local function filter_mappings(mappings, keys) - if type(keys) == "boolean" and keys then - return {} - elseif type(keys) == "table" then - return vim.tbl_filter(function(m) - if type(m.key) == "table" then - m.key = vim.tbl_filter(function(k) - return not vim.tbl_contains(keys, k) - end, m.key) - return #m.key > 0 - else - return not vim.tbl_contains(keys, m.key) - end - end, vim.deepcopy(mappings)) - else - return vim.deepcopy(mappings) - end -end - -local DEFAULT_MAPPING_CONFIG = { - custom_only = false, - list = {}, -} +local M = {} function M.setup(opts) require("nvim-tree.actions.fs.trash").setup(opts) @@ -399,20 +14,7 @@ function M.setup(opts) require("nvim-tree.actions.fs.copy-paste").setup(opts) require("nvim-tree.actions.tree-modifiers.expand-all").setup(opts) - cleanup_existing_mappings() - - M.mappings = filter_mappings(DEFAULT_MAPPINGS, opts.remove_keymaps) - - 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 = copy_mappings(options.list) - else - M.mappings = merge_mappings(options.list) - end - - require("nvim-tree.actions.dispatch").setup(M.custom_keypress_funcs) - + -- TODO log.line("config", "active mappings") log.raw("config", "%s\n", vim.inspect(M.mappings)) end diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 17ec841c..4d082161 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -95,8 +95,6 @@ local function create_buffer(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 diff --git a/scripts/update-help.sh b/scripts/update-help.sh index 8b284049..9b73897c 100755 --- a/scripts/update-help.sh +++ b/scripts/update-help.sh @@ -19,18 +19,18 @@ sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_OPTS.6.lua }; /${end}/p; d; }" doc/nvim-tree-lua.txt -begin="BEGIN_DEFAULT_MAPPINGS" -end="END_DEFAULT_MAPPINGS" +# begin="BEGIN_DEFAULT_MAPPINGS" +# end="END_DEFAULT_MAPPINGS" -# generate various DEFAULT_MAPPINGS -sed -n -e "/${begin}/,/${end}/{ /${begin}/d; /${end}/d; p; }" lua/nvim-tree/actions/init.lua > /tmp/DEFAULT_MAPPINGS.M.lua -cat /tmp/DEFAULT_MAPPINGS.M.lua scripts/generate_default_mappings.lua | lua +# # generate various DEFAULT_MAPPINGS +# sed -n -e "/${begin}/,/${end}/{ /${begin}/d; /${end}/d; p; }" lua/nvim-tree/actions/init.lua > /tmp/DEFAULT_MAPPINGS.M.lua +# cat /tmp/DEFAULT_MAPPINGS.M.lua scripts/generate_default_mappings.lua | lua -# help -sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_MAPPINGS.lua - }; /${end}/p; d }" doc/nvim-tree-lua.txt -sed -i -e "/^DEFAULT MAPPINGS/,/^>$/{ /^DEFAULT MAPPINGS/{p; r /tmp/DEFAULT_MAPPINGS.help - }; /^>$/p; d }" doc/nvim-tree-lua.txt +# # help +# sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_MAPPINGS.lua +# }; /${end}/p; d }" doc/nvim-tree-lua.txt +# sed -i -e "/^DEFAULT MAPPINGS/,/^>$/{ /^DEFAULT MAPPINGS/{p; r /tmp/DEFAULT_MAPPINGS.help +# }; /^>$/p; d }" doc/nvim-tree-lua.txt # generate various DEFAULT_KEYMAPS begin="BEGIN_DEFAULT_KEYMAPS" @@ -38,3 +38,9 @@ end="END_DEFAULT_KEYMAPS" sed -n -e "/${begin}/,/${end}/{ /${begin}/d; /${end}/d; s/callback = \(.*\),/callback = '\1',/g; p; }" lua/nvim-tree/keymap.lua > /tmp/DEFAULT_KEYMAPS.M.lua cat /tmp/DEFAULT_KEYMAPS.M.lua scripts/generate_default_keymaps.lua | lua +# help +sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_KEYMAPS.on_attach.lua + }; /${end}/p; d }" doc/nvim-tree-lua.txt +sed -i -e "/^DEFAULT KEYMAPS/,/^>$/{ /^DEFAULT KEYMAPS/{p; r /tmp/DEFAULT_KEYMAPS.help + }; /^>$/p; d }" doc/nvim-tree-lua.txt +