From 18c7a3119839adc4599d838726deae662859c8b2 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 14 Aug 2023 17:41:55 +1000 Subject: [PATCH] chore: remove legacy view.mappings.list (#2371) --- README.md | 4 - doc/nvim-tree-lua.txt | 105 -------- lua/nvim-tree.lua | 11 - lua/nvim-tree/api.lua | 10 - lua/nvim-tree/commands.lua | 10 - lua/nvim-tree/config.lua | 11 - lua/nvim-tree/keymap-legacy.lua | 421 -------------------------------- lua/nvim-tree/legacy.lua | 9 - scripts/update-help.sh | 6 +- 9 files changed, 1 insertion(+), 586 deletions(-) delete mode 100644 lua/nvim-tree/config.lua delete mode 100644 lua/nvim-tree/keymap-legacy.lua diff --git a/README.md b/README.md index e4e0ef35..98a9c5be 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,6 @@ Take a look at the [wiki](https://github.com/nvim-tree/nvim-tree.lua/wiki) for S Community support: [matrix](https://matrix.to/#/#nvim-tree:matrix.org) -## New Mapping Method 2023-02-27 - -[:help nvim-tree.view.mappings](doc/nvim-tree-lua.txt) have been deprecated in favour of [:help nvim-tree.on_attach](doc/nvim-tree-lua.txt). Please visit [Migrating To on_attach](https://github.com/nvim-tree/nvim-tree.lua/wiki/Migrating-To-on_attach) to transition. - ## Requirements [neovim >=0.8.0](https://github.com/neovim/neovim/wiki/Installing-Neovim) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 5185bbcc..4e30ae84 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -21,7 +21,6 @@ CONTENTS *nvim-tree* 5.9 API Commands |nvim-tree-api.commands| 6. Mappings |nvim-tree-mappings| 6.1 Default Mappings |nvim-tree-mappings-default| - 6.2 Legacy Mappings |nvim-tree-mappings-legacy| 7. Highlight Groups |nvim-tree-highlight| 8. Events |nvim-tree-events| 9. Bookmarks |nvim-tree-bookmarks| @@ -285,14 +284,6 @@ Show the mappings: `g?` Calls: `api.tree.collapse_all(true)` -*:NvimTreeGenerateOnAttach* - - Creates and opens a new file `nvim-tree-on-attach.lua` in |stdpath| `"cache"`, - usually `$XDG_CACHE_HOME/nvim`. Contains |nvim-tree.on_attach| function based - on your |nvim-tree.view.mappings|, |nvim-tree.remove_keymaps| as well as the - defaults. - See https://github.com/nvim-tree/nvim-tree.lua/wiki/Migrating-To-on_attach - ============================================================================== 4. SETUP *nvim-tree-setup* @@ -324,7 +315,6 @@ applying configuration. reload_on_bufenter = false, respect_buf_cwd = false, on_attach = "default", - remove_keymaps = false, select_prompts = false, view = { centralize_selection = false, @@ -337,12 +327,6 @@ applying configuration. number = false, relativenumber = false, signcolumn = "yes", - mappings = { - custom_only = false, - list = { - -- user mappings go here - }, - }, float = { enable = false, quit_on_focus_loss = true, @@ -816,15 +800,6 @@ specific mappings. See |nvim-tree-mappings|. When on_attach is not a function, |nvim-tree-mappings-default| will be called. Type: `function(bufnr) | string`, Default: `"default"` -*nvim-tree.remove_keymaps* -Deprecated: please see |nvim-tree-mappings-legacy| -This can be used to remove the default mappings in the tree. -- Remove specific keys by passing a `string` table of keys - eg. {"", "", "o", ""} -- Remove all default mappings by passing `true` -- Ignore by passing `false` - Type: `bool` or `{string}`, Default: `false` - *nvim-tree.select_prompts* Use |vim.ui.select| style prompts. Necessary when using a UI prompt decorator such as dressing.nvim or telescope-ui-select.nvim @@ -894,20 +869,6 @@ Window / buffer setup. Show diagnostic sign column. Value can be `"yes"`, `"auto"`, `"no"`. Type: `string`, Default: `"yes"` - *nvim-tree.view.mappings* - Deprecated: please see |nvim-tree-mappings-legacy| - - *nvim-tree.view.mappings.custom_only* - Will use only the provided user mappings and not the default otherwise, - extends the default mappings with the provided user mappings. - Overrides |nvim-tree.remove_keymaps| - Type: `boolean`, Default: `false` - - *nvim-tree.view.mappings.list* - A list of keymaps that will extend or override the default keymaps. - Type: `table` - Default: see |nvim-tree-mappings-legacy| - *nvim-tree.view.float* Configuration options for floating window @@ -1827,21 +1788,6 @@ config.mappings.default_on_attach({bufnr}) Parameters: ~ • {bufnr} (number) nvim-tree buffer number passed to |nvim-tree.on_attach| -config.mappings.active() *nvim-tree-api.config.mappings.active()* - Deprecated: only functions when using legacy |nvim-tree.view.mappings| - Retrieve a clone of the currently active mappings: defaults + user. - Changing the active mappings will require a call to |nvim-tree-setup| - - Return: ~ - (table) as per |nvim-tree.view.mappings.list| - -config.mappings.default() *nvim-tree-api.config.mappings.default()* - Deprecated: only functions when using legacy |nvim-tree.view.mappings| - Retrieve a clone of the default mappings. - - Return: ~ - (table) as per |nvim-tree.view.mappings.list| - *nvim-tree-api.config.mappings.get_keymap()* config.mappings.get_keymap() Retrieves all buffer local mappings for nvim-tree. @@ -2019,57 +1965,6 @@ Alternatively, you may apply these default mappings from your |nvim-tree.on_atta -- your removals and mappings go here end < -============================================================================== - 6.2 LEGACY MAPPINGS *nvim-tree-mappings-legacy* - -nvim-tree mappings were provided via the deprecated |nvim-tree.view.mappings| -and |nvim-tree.remove_keymaps| - -These are ignored when |nvim-tree.on_attach| is present. - -You are encouraged to migrate you existing legacy mappings to -|nvim-tree.on_attach| using |:NvimTreeGenerateOnAttach| - -Please visit https://github.com/nvim-tree/nvim-tree.lua/wiki/Migrating-To-on_attach - -`view.mappings.list` is a table of: - {key} (string|table of string) mandatory |{lhs}|. - - {action} (string) mandatory name of the action. - `""` to remove an action with {key}. The case of {key} must - exactly match the action you are removing e.g. `''` to - remove the default preview action. - An arbitrary description when using `action_cb`. - - {action_cb} (function) optional custom function that will be called. - Receives the node as a parameter. - Non-empty description for `action` is required. - - {mode} (string) optional mode as per |nvim_set_keymap|. - Default `'n'`. - -Examples: -> - local function print_node_path(node) - print(node.absolute_path) - end - - ---- - - view = { - mappings = { - list = { - -- remove a default mapping for cd - { key = "<2-RightMouse>", action = "" }, - - -- add multiple normal mode mappings for edit - { key = { "", "o" }, action = "edit", mode = "n" }, - - -- custom action - { key = "p", action = "print_the_node_path", action_cb = print_node_path }, - - ---- -< ============================================================================== 7. HIGHLIGHT GROUPS *nvim-tree-highlight* diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index f1164a62..e5ffef50 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -12,7 +12,6 @@ local reloaders = require "nvim-tree.actions.reloaders.reloaders" local git = require "nvim-tree.git" local filters = require "nvim-tree.explorer.filters" local modified = require "nvim-tree.modified" -local keymap_legacy = require "nvim-tree.keymap-legacy" local find_file = require "nvim-tree.actions.tree.find-file" local open = require "nvim-tree.actions.tree.open" local events = require "nvim-tree.events" @@ -380,7 +379,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS reload_on_bufenter = false, respect_buf_cwd = false, on_attach = "default", - remove_keymaps = false, select_prompts = false, view = { centralize_selection = false, @@ -393,12 +391,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS number = false, relativenumber = false, signcolumn = "yes", - mappings = { - custom_only = false, - list = { - -- user mappings go here - }, - }, float = { enable = false, quit_on_focus_loss = true, @@ -619,7 +611,6 @@ local FIELD_OVERRIDE_TYPECHECK = { width = { string = true, ["function"] = true, number = true, ["table"] = true }, max = { string = true, ["function"] = true, number = true }, min = { string = true, ["function"] = true, number = true }, - remove_keymaps = { boolean = true, table = true }, on_attach = { ["function"] = true, string = true }, sorter = { ["function"] = true, string = true }, root_folder_label = { ["function"] = true, string = true, boolean = true }, @@ -721,8 +712,6 @@ function M.setup(conf) log.raw("config", "%s\n", vim.inspect(opts)) end - keymap_legacy.generate_legacy_on_attach(opts) - require("nvim-tree.actions").setup(opts) require("nvim-tree.keymap").setup(opts) require("nvim-tree.colors").setup() diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index fd2f4e55..e708339f 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -210,16 +210,6 @@ Api.marks.navigate.select = wrap(require("nvim-tree.marks.navigation").select) Api.config.mappings.default_on_attach = require("nvim-tree.keymap").default_on_attach -Api.config.mappings.active = wrap(function() - notify.warn "api.config.mappings.active is deprecated in favor of config.mappings.get_keymap" - return require("nvim-tree.keymap-legacy").active_mappings_clone() -end) - -Api.config.mappings.default = function() - notify.warn "api.config.mappings.default is deprecated in favor of config.mappings.get_keymap_default" - return require("nvim-tree.keymap-legacy").default_mappings_clone() -end - Api.config.mappings.get_keymap = wrap(function() return require("nvim-tree.keymap").get_keymap() end) diff --git a/lua/nvim-tree/commands.lua b/lua/nvim-tree/commands.lua index d71937aa..ea622c03 100644 --- a/lua/nvim-tree/commands.lua +++ b/lua/nvim-tree/commands.lua @@ -1,4 +1,3 @@ -local keymap_legacy = require "nvim-tree.keymap-legacy" local api = require "nvim-tree.api" local view = require "nvim-tree.view" @@ -121,15 +120,6 @@ local CMDS = { api.tree.collapse_all(true) end, }, - { - name = "NvimTreeGenerateOnAttach", - opts = { - desc = "nvim-tree: generate on_attach function from deprecated view.mappings", - }, - command = function() - keymap_legacy.cmd_generate_on_attach() - end, - }, } function M.get() diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua deleted file mode 100644 index 1884796b..00000000 --- a/lua/nvim-tree/config.lua +++ /dev/null @@ -1,11 +0,0 @@ --- INFO: DEPRECATED FILE, DO NOT ADD ANYTHING IN THERE --- keeping to avoid breaking user configs. Will remove during a weekend. -local M = {} - --- TODO: remove this once the cb property is not supported in mappings, following view.mapping.list removal -function M.nvim_tree_callback(callback_name) - -- generate_on_attach_.* will map this as per mappings.list..action - return callback_name -end - -return M diff --git a/lua/nvim-tree/keymap-legacy.lua b/lua/nvim-tree/keymap-legacy.lua deleted file mode 100644 index 48a8153b..00000000 --- a/lua/nvim-tree/keymap-legacy.lua +++ /dev/null @@ -1,421 +0,0 @@ -local api = require "nvim-tree.api" -local open_file = require "nvim-tree.actions.node.open-file" -local keymap = require "nvim-tree.keymap" -local notify = require "nvim-tree.notify" -local utils = require "nvim-tree.utils" - -local M = { - -- only populated when legacy mappings active - on_attach_lua = nil, - - -- API config.mappings.active .default - legacy_default = {}, - legacy_active = {}, - - -- used by generated on_attach - on_attach = { - list = {}, - unmapped_keys = {}, - remove_defaults = false, - }, -} - -local BEGIN_ON_ATTACH = [[ --- --- This function has been generated from your --- view.mappings.list --- view.mappings.custom_only --- remove_keymaps --- --- You should add this function to your configuration and set on_attach = on_attach in the nvim-tree setup call. --- --- Although care was taken to ensure correctness and completeness, your review is required. --- --- Please check for the following issues in auto generated content: --- "Mappings removed" is as you expect --- "Mappings migrated" are correct --- --- Please see https://github.com/nvim-tree/nvim-tree.lua/wiki/Migrating-To-on_attach for assistance in migrating. --- - -local function on_attach(bufnr) - local api = require('nvim-tree.api') - - local function opts(desc) - return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true } - end -]] - -local END_ON_ATTACH = [[ -end -]] - -local REMOVAL_COMMENT_ON_ATTACH = [[ - - - -- Mappings removed via: - -- remove_keymaps - -- OR - -- view.mappings.list..action = "" - -- - -- The dummy set before del is done for safety, in case a default mapping does not exist. - -- - -- You might tidy things by removing these along with their default mapping. -]] - -local CUSTOM_COMMENT_ON_ATTACH = [[ - - - -- Mappings migrated from view.mappings.list - -- - -- You will need to insert "your code goes here" for any mappings with a custom action_cb -]] - -local NO_DEFAULTS_COMMENT_ON_ATTACH = [[ - - - -- Default mappings not inserted as: - -- remove_keymaps = true - -- OR - -- view.mappings.custom_only = true -]] - -local DEFAULT_ON_ATTACH = [[ - - -- Default mappings. Feel free to modify or remove as you wish. - -- - -- BEGIN_DEFAULT_ON_ATTACH - vim.keymap.set('n', '', api.tree.change_root_to_node, opts('CD')) - vim.keymap.set('n', '', api.node.open.replace_tree_buffer, opts('Open: In Place')) - vim.keymap.set('n', '', api.node.show_info_popup, opts('Info')) - vim.keymap.set('n', '', api.fs.rename_sub, opts('Rename: Omit Filename')) - vim.keymap.set('n', '', api.node.open.tab, opts('Open: New Tab')) - vim.keymap.set('n', '', api.node.open.vertical, opts('Open: Vertical Split')) - vim.keymap.set('n', '', api.node.open.horizontal, opts('Open: Horizontal Split')) - vim.keymap.set('n', '', api.node.navigate.parent_close, opts('Close Directory')) - vim.keymap.set('n', '', api.node.open.edit, opts('Open')) - vim.keymap.set('n', '', api.node.open.preview, opts('Open Preview')) - vim.keymap.set('n', '>', api.node.navigate.sibling.next, opts('Next Sibling')) - vim.keymap.set('n', '<', api.node.navigate.sibling.prev, opts('Previous Sibling')) - vim.keymap.set('n', '.', api.node.run.cmd, opts('Run Command')) - vim.keymap.set('n', '-', api.tree.change_root_to_parent, opts('Up')) - vim.keymap.set('n', 'a', api.fs.create, opts('Create')) - vim.keymap.set('n', 'bd', api.marks.bulk.delete, opts('Delete Bookmarked')) - vim.keymap.set('n', 'bmv', api.marks.bulk.move, opts('Move Bookmarked')) - vim.keymap.set('n', 'B', api.tree.toggle_no_buffer_filter, opts('Toggle Filter: No Buffer')) - vim.keymap.set('n', 'c', api.fs.copy.node, opts('Copy')) - vim.keymap.set('n', 'C', api.tree.toggle_git_clean_filter, opts('Toggle Filter: Git Clean')) - vim.keymap.set('n', '[c', api.node.navigate.git.prev, opts('Prev Git')) - vim.keymap.set('n', ']c', api.node.navigate.git.next, opts('Next Git')) - vim.keymap.set('n', 'd', api.fs.remove, opts('Delete')) - vim.keymap.set('n', 'D', api.fs.trash, opts('Trash')) - vim.keymap.set('n', 'E', api.tree.expand_all, opts('Expand All')) - vim.keymap.set('n', 'e', api.fs.rename_basename, opts('Rename: Basename')) - vim.keymap.set('n', ']e', api.node.navigate.diagnostics.next, opts('Next Diagnostic')) - vim.keymap.set('n', '[e', api.node.navigate.diagnostics.prev, opts('Prev Diagnostic')) - vim.keymap.set('n', 'F', api.live_filter.clear, opts('Clean Filter')) - vim.keymap.set('n', 'f', api.live_filter.start, opts('Filter')) - vim.keymap.set('n', 'g?', api.tree.toggle_help, opts('Help')) - vim.keymap.set('n', 'gy', api.fs.copy.absolute_path, opts('Copy Absolute Path')) - vim.keymap.set('n', 'H', api.tree.toggle_hidden_filter, opts('Toggle Filter: Dotfiles')) - vim.keymap.set('n', 'I', api.tree.toggle_gitignore_filter, opts('Toggle Filter: Git Ignore')) - vim.keymap.set('n', 'J', api.node.navigate.sibling.last, opts('Last Sibling')) - vim.keymap.set('n', 'K', api.node.navigate.sibling.first, opts('First Sibling')) - vim.keymap.set('n', 'm', api.marks.toggle, opts('Toggle Bookmark')) - vim.keymap.set('n', 'o', api.node.open.edit, opts('Open')) - vim.keymap.set('n', 'O', api.node.open.no_window_picker, opts('Open: No Window Picker')) - vim.keymap.set('n', 'p', api.fs.paste, opts('Paste')) - vim.keymap.set('n', 'P', api.node.navigate.parent, opts('Parent Directory')) - vim.keymap.set('n', 'q', api.tree.close, opts('Close')) - vim.keymap.set('n', 'r', api.fs.rename, opts('Rename')) - vim.keymap.set('n', 'R', api.tree.reload, opts('Refresh')) - vim.keymap.set('n', 's', api.node.run.system, opts('Run System')) - vim.keymap.set('n', 'S', api.tree.search_node, opts('Search')) - vim.keymap.set('n', 'U', api.tree.toggle_custom_filter, opts('Toggle Filter: Hidden')) - vim.keymap.set('n', 'W', api.tree.collapse_all, opts('Collapse')) - vim.keymap.set('n', 'x', api.fs.cut, opts('Cut')) - vim.keymap.set('n', 'y', api.fs.copy.filename, opts('Copy Name')) - vim.keymap.set('n', 'Y', api.fs.copy.relative_path, opts('Copy Relative Path')) - vim.keymap.set('n', '<2-LeftMouse>', api.node.open.edit, opts('Open')) - vim.keymap.set('n', '<2-RightMouse>', api.tree.change_root_to_node, opts('CD')) - -- END_DEFAULT_ON_ATTACH -]] - --- stylua: ignore start -local LEGACY_MAPPINGS = { - edit = { key = { "", "o", "<2-LeftMouse>" }, desc = "Open", fn = api.node.open.edit, n = "api.node.open.edit" }, - edit_in_place = { key = "", desc = "Open: In Place", fn = api.node.open.replace_tree_buffer, n = "api.node.open.replace_tree_buffer" }, - edit_no_picker = { key = "O", desc = "Open: No Window Picker", fn = api.node.open.no_window_picker, n = "api.node.open.no_window_picker" }, - cd = { key = { "", "<2-RightMouse>" }, desc = "CD", fn = api.tree.change_root_to_node, n = "api.tree.change_root_to_node" }, - vsplit = { key = "", desc = "Open: Vertical Split", fn = api.node.open.vertical, n = "api.node.open.vertical" }, - split = { key = "", desc = "Open: Horizontal Split", fn = api.node.open.horizontal, n = "api.node.open.horizontal" }, - tabnew = { key = "", desc = "Open: New Tab", fn = api.node.open.tab, n = "api.node.open.tab" }, - prev_sibling = { key = "<", desc = "Previous Sibling", fn = api.node.navigate.sibling.prev, n = "api.node.navigate.sibling.prev" }, - next_sibling = { key = ">", desc = "Next Sibling", fn = api.node.navigate.sibling.next, n = "api.node.navigate.sibling.next" }, - parent_node = { key = "P", desc = "Parent Directory", fn = api.node.navigate.parent, n = "api.node.navigate.parent" }, - close_node = { key = "", desc = "Close Directory", fn = api.node.navigate.parent_close, n = "api.node.navigate.parent_close" }, - preview = { key = "", desc = "Open Preview", fn = api.node.open.preview, n = "api.node.open.preview" }, - first_sibling = { key = "K", desc = "First Sibling", fn = api.node.navigate.sibling.first, n = "api.node.navigate.sibling.first" }, - last_sibling = { key = "J", desc = "Last Sibling", fn = api.node.navigate.sibling.last, n = "api.node.navigate.sibling.last" }, - toggle_git_ignored = { key = "I", desc = "Toggle Git Ignore", fn = api.tree.toggle_gitignore_filter, n = "api.tree.toggle_gitignore_filter" }, - toggle_no_buffer = { key = "B", desc = "Toggle No Buffer", fn = api.tree.toggle_no_buffer_filter, n = "api.tree.toggle_no_buffer_filter" }, - toggle_git_clean = { key = "C", desc = "Toggle Git Clean", fn = api.tree.toggle_git_clean_filter, n = "api.tree.toggle_git_clean_filter" }, - toggle_dotfiles = { key = "H", desc = "Toggle Dotfiles", fn = api.tree.toggle_hidden_filter, n = "api.tree.toggle_hidden_filter" }, - toggle_custom = { key = "U", desc = "Toggle Hidden", fn = api.tree.toggle_custom_filter, n = "api.tree.toggle_custom_filter" }, - refresh = { key = "R", desc = "Refresh", fn = api.tree.reload, n = "api.tree.reload" }, - create = { key = "a", desc = "Create", fn = api.fs.create, n = "api.fs.create" }, - remove = { key = "d", desc = "Delete", fn = api.fs.remove, n = "api.fs.remove" }, - trash = { key = "D", desc = "Trash", fn = api.fs.trash, n = "api.fs.trash" }, - rename = { key = "r", desc = "Rename", fn = api.fs.rename, n = "api.fs.rename" }, - full_rename = { key = "", desc = "Rename: Omit Filename", fn = api.fs.rename_sub, n = "api.fs.rename_sub" }, - rename_basename = { key = "e", desc = "Rename: Basename", fn = api.fs.rename_basename, n = "api.fs.rename_basename" }, - cut = { key = "x", desc = "Cut", fn = api.fs.cut, n = "api.fs.cut" }, - copy = { key = "c", desc = "Copy", fn = api.fs.copy.node, n = "api.fs.copy.node" }, - paste = { key = "p", desc = "Paste", fn = api.fs.paste, n = "api.fs.paste" }, - copy_name = { key = "y", desc = "Copy Name", fn = api.fs.copy.filename, n = "api.fs.copy.filename" }, - copy_path = { key = "Y", desc = "Copy Relative Path", fn = api.fs.copy.relative_path, n = "api.fs.copy.relative_path" }, - copy_absolute_path = { key = "gy", desc = "Copy Absolute Path", fn = api.fs.copy.absolute_path, n = "api.fs.copy.absolute_path" }, - next_diag_item = { key = "]e", desc = "Next Diagnostic", fn = api.node.navigate.diagnostics.next, n = "api.node.navigate.diagnostics.next" }, - next_git_item = { key = "]c", desc = "Next Git", fn = api.node.navigate.git.next, n = "api.node.navigate.git.next" }, - prev_diag_item = { key = "[e", desc = "Prev Diagnostic", fn = api.node.navigate.diagnostics.prev, n = "api.node.navigate.diagnostics.prev" }, - prev_git_item = { key = "[c", desc = "Prev Git", fn = api.node.navigate.git.prev, n = "api.node.navigate.git.prev" }, - dir_up = { key = "-", desc = "Up", fn = api.tree.change_root_to_parent, n = "api.tree.change_root_to_parent" }, - system_open = { key = "s", desc = "Run System", fn = api.node.run.system, n = "api.node.run.system" }, - live_filter = { key = "f", desc = "Filter", fn = api.live_filter.start, n = "api.live_filter.start" }, - clear_live_filter = { key = "F", desc = "Clean Filter", fn = api.live_filter.clear, n = "api.live_filter.clear" }, - close = { key = "q", desc = "Close", fn = api.tree.close, n = "api.tree.close" }, - collapse_all = { key = "W", desc = "Collapse", fn = api.tree.collapse_all, n = "api.tree.collapse_all" }, - expand_all = { key = "E", desc = "Expand All", fn = api.tree.expand_all, n = "api.tree.expand_all" }, - search_node = { key = "S", desc = "Search", fn = api.tree.search_node, n = "api.tree.search_node" }, - run_file_command = { key = ".", desc = "Run Command", fn = api.node.run.cmd, n = "api.node.run.cmd" }, - toggle_file_info = { key = "", desc = "Info", fn = api.node.show_info_popup, n = "api.node.show_info_popup" }, - toggle_help = { key = "g?", desc = "Help", fn = api.tree.toggle_help, n = "api.tree.toggle_help" }, - toggle_mark = { key = "m", desc = "Toggle Bookmark", fn = api.marks.toggle, n = "api.marks.toggle" }, - bulk_move = { key = "bmv", desc = "Move Bookmarked", fn = api.marks.bulk.move, n = "api.marks.bulk.move" }, -} --- stylua: ignore end - -local function all_mapped_keys(list) - local mapped_keys = {} - for _, map in pairs(list) do - if map.action ~= "" then - local keys = type(map.key) == "table" and map.key or { map.key } - for _, key in ipairs(keys) do - table.insert(mapped_keys, key) - end - end - end - return mapped_keys -end - -local function all_unmapped_keys(list, remove_keys) - local unmapped_keys = vim.deepcopy(remove_keys) - for _, map in pairs(list) do - if map.action == "" then - local keys = type(map.key) == "table" and map.key or { map.key } - for _, key in ipairs(keys) do - table.insert(unmapped_keys, key) - end - end - end - return unmapped_keys -end - -local function generate_on_attach_function(list, unmapped_keys, remove_defaults) - M.on_attach.list = vim.deepcopy(list) - M.on_attach.unmapped_keys = vim.deepcopy(unmapped_keys) - M.on_attach.remove_defaults = remove_defaults - - return function(bufnr) - -- apply defaults first - if not M.on_attach.remove_defaults then - keymap.default_on_attach(bufnr) - end - - -- explicit removals - for _, key in ipairs(M.on_attach.unmapped_keys) do - vim.keymap.set("n", key, "", { buffer = bufnr }) - vim.keymap.del("n", key, { buffer = bufnr }) - end - - -- mappings - for _, m in ipairs(M.on_attach.list) do - local keys = type(m.key) == "table" and m.key or { m.key } - for _, k in ipairs(keys) do - local legacy_mapping = LEGACY_MAPPINGS[m.action] or LEGACY_MAPPINGS[m.cb] - if legacy_mapping then - -- straight action or cb, which generated an action string at setup time - vim.keymap.set( - m.mode or "n", - k, - legacy_mapping.fn, - { desc = m.cb or m.action, buffer = bufnr, noremap = true, silent = true, nowait = true } - ) - elseif type(m.action_cb) == "function" then - -- action_cb - vim.keymap.set(m.mode or "n", k, function() - m.action_cb(api.tree.get_node_under_cursor()) - end, { - desc = m.action or "no description", - buffer = bufnr, - noremap = true, - silent = true, - nowait = true, - }) - end - end - end - end -end - -local function generate_on_attach_lua(list, unmapped_keys, remove_defaults) - local lua = BEGIN_ON_ATTACH - - if remove_defaults then - -- no defaults - lua = lua .. NO_DEFAULTS_COMMENT_ON_ATTACH - else - -- defaults with explicit removals - lua = lua .. "\n" .. DEFAULT_ON_ATTACH - if #unmapped_keys > 0 then - lua = lua .. REMOVAL_COMMENT_ON_ATTACH - end - for _, key in ipairs(unmapped_keys) do - lua = lua .. string.format([[ vim.keymap.set('n', '%s', '', { buffer = bufnr })]], key) .. "\n" - lua = lua .. string.format([[ vim.keymap.del('n', '%s', { buffer = bufnr })]], key) .. "\n" - end - end - - -- list - if #list > 0 then - lua = lua .. CUSTOM_COMMENT_ON_ATTACH - end - for _, m in ipairs(list) do - local keys = type(m.key) == "table" and m.key or { m.key } - for _, k in ipairs(keys) do - local legacy_mapping = LEGACY_MAPPINGS[m.action] or LEGACY_MAPPINGS[m.cb] - if legacy_mapping then - lua = lua - .. string.format( - [[ vim.keymap.set('%s', '%s', %s, opts('%s'))]], - m.mode or "n", - k, - legacy_mapping.n, - legacy_mapping.desc - ) - .. "\n" - elseif type(m.action_cb) == "function" then - lua = lua .. string.format([[ vim.keymap.set('%s', '%s', function()]], m.mode or "n", k) .. "\n" - lua = lua .. [[ local node = api.tree.get_node_under_cursor()]] .. "\n" - lua = lua .. [[ -- your code goes here]] .. "\n" - lua = lua .. string.format([[ end, opts('%s'))]], m.action or "no description") .. "\n\n" - end - end - end - - return lua .. "\n" .. END_ON_ATTACH -end - -local function generate_legacy_default_mappings() - local mappings = {} - - for a, m in pairs(LEGACY_MAPPINGS) do - table.insert(mappings, { - action = a, - desc = m.desc, - key = m.key, - }) - end - - return mappings -end - -local function generate_legacy_active_mappings(list, defaults, unmapped_keys, mapped_keys, remove_defaults) - local filtered_defaults - - if remove_defaults then - -- - -- unmap all defaults - -- - filtered_defaults = {} - else - -- - -- unmap defaults by removal and override - -- - local to_unmap = vim.fn.extend(unmapped_keys, mapped_keys) - filtered_defaults = vim.tbl_filter(function(m) - if type(m.key) == "table" then - m.key = vim.tbl_filter(function(k) - return not vim.tbl_contains(to_unmap, k) - end, m.key) - return #m.key > 0 - else - return not vim.tbl_contains(to_unmap, m.key) - end - end, vim.deepcopy(defaults)) - end - - -- - -- remove user action = "" - -- - local user_map = vim.tbl_filter(function(map) - return map.action ~= "" - end, list) - - -- - -- merge - -- - return vim.fn.extend(filtered_defaults, user_map) -end - -function M.generate_legacy_on_attach(opts) - M.on_attach_lua = nil - - if type(opts.on_attach) == "function" then - return - end - - local list = opts.view and opts.view.mappings and opts.view.mappings.list or {} - local remove_keymaps = type(opts.remove_keymaps) == "table" and opts.remove_keymaps or {} - local remove_defaults = opts.remove_keymaps == true - or opts.view and opts.view.mappings and opts.view.mappings.custom_only - - -- do nothing unless the user has configured something - if #list == 0 and #remove_keymaps == 0 and not remove_defaults then - return - end - - notify.info "view.mappings.list has been deprecated in favour of on_attach. Please run :NvimTreeGenerateOnAttach and visit https://github.com/nvim-tree/nvim-tree.lua/wiki/Migrating-To-on_attach" - - local mapped_keys = all_mapped_keys(list) - local unmapped_keys = all_unmapped_keys(list, remove_keymaps) - - opts.on_attach = generate_on_attach_function(list, unmapped_keys, remove_defaults) - M.on_attach_lua = generate_on_attach_lua(list, unmapped_keys, remove_defaults) - - M.legacy_default = generate_legacy_default_mappings() - M.legacy_active = generate_legacy_active_mappings(list, M.legacy_default, unmapped_keys, mapped_keys, remove_defaults) -end - -function M.cmd_generate_on_attach() - if not M.on_attach_lua then - notify.info "No view.mappings.list for on_attach generation." - return - end - - local name = utils.path_join { vim.fn.stdpath "cache", "nvim-tree-on-attach.lua" } - local file = io.output(name) - io.write(M.on_attach_lua) - io.close(file) - open_file.fn("edit", name) -end - -function M.active_mappings_clone() - return vim.deepcopy(M.legacy_active) -end - -function M.default_mappings_clone() - return vim.deepcopy(M.legacy_default) -end - -return M diff --git a/lua/nvim-tree/legacy.lua b/lua/nvim-tree/legacy.lua index 08aad198..58a6a035 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -4,15 +4,6 @@ local notify = require "nvim-tree.notify" local M = {} local function refactored(opts) - -- mapping actions - if opts.view and opts.view.mappings and opts.view.mappings.list then - for _, m in pairs(opts.view.mappings.list) do - if m.action == "toggle_ignored" then - m.action = "toggle_git_ignored" - end - end - end - -- 2022/06/20 utils.move_missing_val(opts, "update_focused_file", "update_cwd", opts, "update_focused_file", "update_root", true) utils.move_missing_val(opts, "", "update_cwd", opts, "", "sync_root_with_cwd", true) diff --git a/scripts/update-help.sh b/scripts/update-help.sh index e4682d92..522a5a44 100755 --- a/scripts/update-help.sh +++ b/scripts/update-help.sh @@ -1,7 +1,7 @@ #!/bin/sh # run after changing nvim-tree.lua DEFAULT_OPTS or keymap.lua M.default_on_attach -# scrapes and updates nvim-tree-lua.txt and keymap-legacy.lua +# scrapes and updates nvim-tree-lua.txt # run from repository root: scripts/update-help.sh @@ -35,10 +35,6 @@ sed -n -e "/${begin}/,/${end}/{ /${begin}/d; /${end}/d; p; }" lua/nvim-tree/keym sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_ON_ATTACH.lua }; /${end}/p; d; }" doc/nvim-tree-lua.txt -# legacy keymap lua -sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_ON_ATTACH.lua - }; /${end}/p; d; }" lua/nvim-tree/keymap-legacy.lua - # help human echo > /tmp/DEFAULT_ON_ATTACH.help sed -E "s/^ *vim.keymap.set\('n', '(.*)',.*api(.*),.*opts\('(.*)'.*$/'\`\1\`' '\3' '|nvim-tree-api\2()|'/g