diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 9147e8e15d6..53dbf4f29f4 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -7,6 +7,7 @@ local colors = require "nvim-tree.colors" local renderer = require "nvim-tree.renderer" local view = require "nvim-tree.view" local utils = require "nvim-tree.utils" +local actions = require "nvim-tree.actions" local change_dir = require "nvim-tree.actions.root.change-dir" local legacy = require "nvim-tree.legacy" local core = require "nvim-tree.core" @@ -319,6 +320,7 @@ local function setup_vim_commands() api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function() collapse_all.fn(true) end, { bar = true }) + api.nvim_create_user_command("NvimTreeMappingsToOnAttach", actions.mappings_to_on_attach, {}) end function M.change_dir(name) diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index e3b9366fccb..1ba0f107b10 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -395,4 +395,99 @@ function M.setup(opts) log.raw("config", "%s\n", vim.inspect(M.mappings)) end +-- TODO move to legacy +local ACTION_TO_API = { + 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", + 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", + preview = "Api.node.open.preview", + 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_diag_item = "Api.node.navigate.diagnostics.next", + prev_git_item = "Api.node.navigate.git.next", + next_diag_item = "Api.node.navigate.diagnostics.prev", + next_git_item = "Api.node.navigate.git.prev", + 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.mappings_to_on_attach() + local keymaps = {} + local max_key = 0 + local max_fn = 0 + + for _, m in ipairs(M.mappings) do + if ACTION_TO_API[m.action] then + local keys = type(m.key) == "table" and m.key or { m.key } + for _, k in ipairs(keys) do + local fn = ACTION_TO_API[m.action] + table.insert(keymaps, { + key = k, + fn = fn, + }) + max_key = math.max(#k, max_key) + max_fn = math.max(#fn, max_fn) + end + end + end + + local out = [[ +local Api = require("nvim-tree.api") + +local function on_attach(bufnr, mode, opts) +]] + + local fmt = + string.format("%%s vim.keymap.set(mode, %%-%d.%ds %%-%d.%ds, opts)\n", max_key + 3, max_key + 3, max_fn, max_fn) + for _, m in ipairs(keymaps) do + out = string.format(fmt, out, "'" .. m.key .. "',", m.fn) + end + out = out .. [[ +end +]] + + local FILE_NAME = "/tmp/nvim-tree-on_attach.lua" + os.remove(FILE_NAME) + local file = io.open(FILE_NAME, "a") + io.output(file) + io.write(out) + io.close(file) + + print("wrote on_attach to " .. FILE_NAME) +end + return M diff --git a/lua/nvim-tree/keymap.lua b/lua/nvim-tree/keymap.lua index f8cd5fc6bfd..f9ab7c726c7 100644 --- a/lua/nvim-tree/keymap.lua +++ b/lua/nvim-tree/keymap.lua @@ -280,7 +280,7 @@ local function get_keymaps(keys_to_disable) end function M.setup(opts) - M.keymaps = get_keymaps(opts) + M.keymaps = get_keymaps(opts, opts.remove_keymaps) end return M diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 4e5f1c85abc..46107301750 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -94,7 +94,9 @@ 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()) + -- TODO sync mode and opts with set_keymaps + local opts = { noremap = true, silent = true, nowait = true, buffer = M.get_bufnr() } + M.on_attach(M.get_bufnr(), "n", opts) else require("nvim-tree.actions").apply_mappings(M.get_bufnr()) end