-
-
Notifications
You must be signed in to change notification settings - Fork 624
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 = {} | ||
|
||
|
@@ -297,6 +298,99 @@ local function removed(opts) | |
end | ||
end | ||
|
||
local OLD_MAPPING_TABLE = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could be lazy and move this to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.