Skip to content

chore(mappings): migrate legacy mappings under the hood #1476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,9 @@ function M.setup(conf)
validate_options(conf)

local opts = merge_options(conf)
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw
legacy.move_mappings_to_keymap(opts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach: moving the mappings after merge means we only have to do this once.


local netrw_disabled = opts.disable_netrw or opts.hijack_netrw
_config.root_dirs = opts.root_dirs
_config.prefer_startup_root = opts.prefer_startup_root
_config.update_focused_file = opts.update_focused_file
Expand Down
16 changes: 12 additions & 4 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ local function inject_node(f)
end
end

Api.tree.open = require("nvim-tree").open
Api.tree.toggle = require("nvim-tree").toggle
Api.tree.open = function(...)
require("nvim-tree").open(...)
end
Api.tree.toggle = function(...)
require("nvim-tree").toggle(...)
end
Api.tree.close = require("nvim-tree.view").close
Api.tree.focus = require("nvim-tree").focus
Api.tree.focus = function()
require("nvim-tree").focus()
end
Api.tree.reload = require("nvim-tree.actions.reloaders.reloaders").reload_explorer
Api.tree.change_root = require("nvim-tree").change_dir
Api.tree.change_root = function(...)
require("nvim-tree").change_dir(...)
end
Api.tree.change_root_to_node = inject_node(function(node)
if node.name == ".." then
require("nvim-tree.actions.root.change-dir").fn ".."
Expand Down
94 changes: 94 additions & 0 deletions lua/nvim-tree/legacy.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local utils = require "nvim-tree.utils"
local Api = require "nvim-tree.api"

local M = {}

Expand Down Expand Up @@ -297,6 +298,99 @@ local function removed(opts)
end
end

local OLD_MAPPING_TABLE = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could be lazy and move this to DEFAULT_KEYMAPS with a legacy_action field.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, also i've noticed from some PRs on nvim-treesitter that vim.keymap.set takes an optional "desc" argument which we should use so the nvim-tree will work in which-key and allow us to use that in the help menu

edit = Api.node.open.edit,
edit_in_place = Api.node.open.replace_tree_buffer,
edit_no_picker = Api.node.open.no_window_picker,
cd = Api.tree.change_root_to_node,
vsplit = Api.node.open.vertical,
split = Api.node.open.horizontal,
tabnew = Api.node.open.tab,
preview = Api.node.open.preview,
prev_sibling = Api.node.navigate.sibling.prev,
next_sibling = Api.node.navigate.sibling.next,
parent_node = Api.node.navigate.parent,
close_node = Api.node.navigate.parent_close,
first_sibling = Api.node.navigate.sibling.first,
last_sibling = Api.node.navigate.sibling.last,
toggle_git_ignored = Api.tree.toggle_gitignore_filter,
toggle_dotfiles = Api.tree.toggle_hidden_filter,
toggle_custom = Api.tree.toggle_custom_filter,
refresh = Api.tree.reload,
create = Api.fs.create,
remove = Api.fs.remove,
trash = Api.fs.trash,
rename = Api.fs.rename,
full_rename = Api.fs.rename_sub,
cut = Api.fs.cut,
copy = Api.fs.copy.node,
paste = Api.fs.paste,
copy_name = Api.fs.copy.filename,
copy_path = Api.fs.copy.relative_path,
copy_absolute_path = Api.fs.copy.absolute_path,
prev_git_item = Api.node.navigate.git.prev,
next_git_item = Api.node.navigate.git.next,
prev_diag_item = Api.node.navigate.diagnostics.prev,
next_diag_item = Api.node.navigate.diagnostics.next,
dir_up = Api.tree.change_root_to_parent,
system_open = Api.node.run.system,
live_filter = Api.live_filter.start,
clear_live_filter = Api.live_filter.clear,
close = Api.tree.close,
collapse_all = Api.tree.collapse_all,
expand_all = Api.tree.expand_all,
search_node = Api.tree.search_node,
run_file_command = Api.node.run.cmd,
toggle_file_info = Api.node.show_info_popup,
toggle_help = Api.tree.toggle_help,
toggle_mark = Api.marks.toggle,
bulk_move = Api.marks.bulk.move,
}

function M.move_mappings_to_keymap(opts)
if opts.on_attach == "disable" and opts.view and opts.view.mappings then
local custom_only, list = opts.view.mappings.custom_only, opts.view.mappings.list
if custom_only then
opts.remove_keymaps = true
opts.view.mappings.custom_only = nil
end
if list then
if not custom_only then
opts.remove_keymaps = {}
end
local call_list = {}
opts.on_attach = function(bufnr)
for _, el in pairs(call_list) do
vim.keymap.set(el.mode or "n", el.key, el.callback, { buffer = bufnr, remap = false, silent = true })
end
end
for _, map in pairs(list) do
local keys = type(map.key) == "table" and map.key or { map.key }
local mode = map.mode or "n"
local callback
if map.action ~= "" then
if map.action_cb then
callback = map.action_cb
else
callback = OLD_MAPPING_TABLE[map.action]
end
end

for _, k in pairs(keys) do
if not custom_only and not vim.tbl_contains(opts.remove_keymaps, k) then
table.insert(opts.remove_keymaps, k)
end

if callback then
table.insert(call_list, { mode = mode, key = k, callback = callback })
end
end
end
opts.view.mappings.list = nil
end
end
end

function M.migrate_legacy_options(opts)
-- g: options
local msg
Expand Down