diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 6f1116c1cea..e7ab7c85ba5 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1015,6 +1015,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings `` toggle_file_info toggle a popup with file infos about the file under the cursor `g?` toggle_help toggle help `m` toggle_mark Toggle node in bookmarks +`bmv` bulk_move Move all bookmarked nodes into specified location > view.mappings.list = { -- BEGIN_DEFAULT_MAPPINGS @@ -1063,6 +1064,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings { key = "", action = "toggle_file_info" } { key = "g?", action = "toggle_help" } { key = "m", action = "toggle_mark" } + { key = "bmv", action = "bulk_move" } } -- END_DEFAULT_MAPPINGS < ============================================================================== diff --git a/lua/nvim-tree/actions/dispatch.lua b/lua/nvim-tree/actions/dispatch.lua index 6f6f377a2ec..44eff9834b0 100644 --- a/lua/nvim-tree/actions/dispatch.lua +++ b/lua/nvim-tree/actions/dispatch.lua @@ -46,6 +46,7 @@ local Actions = { 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) diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index 921dead6ec7..a4c55773a9e 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -7,6 +7,26 @@ local events = require "nvim-tree.events" local M = {} +local function err_fmt(from, to, reason) + return string.format("Cannot rename %s -> %s: %s", from, to, reason) +end + +function M.rename(node, to) + if utils.file_exists(to) then + utils.warn(err_fmt(node.absolute_path, to, "file already exists")) + return + end + + local success, err = uv.fs_rename(node.absolute_path, to) + if not success then + return utils.warn(err_fmt(node.absolute_path, to, err)) + end + utils.clear_prompt() + a.nvim_out_write(node.absolute_path .. " ➜ " .. to .. "\n") + utils.rename_loaded_buffers(node.absolute_path, to) + events._dispatch_node_renamed(node.absolute_path, to) +end + function M.fn(with_sub) return function(node) node = lib.get_last_group_node(node) @@ -24,19 +44,7 @@ function M.fn(with_sub) return end - if utils.file_exists(new_file_path) then - utils.warn "Cannot rename: file already exists" - return - end - - local success = uv.fs_rename(node.absolute_path, new_file_path) - if not success then - return a.nvim_err_writeln("Could not rename " .. node.absolute_path .. " to " .. new_file_path) - end - utils.clear_prompt() - a.nvim_out_write(node.absolute_path .. " ➜ " .. new_file_path .. "\n") - utils.rename_loaded_buffers(node.absolute_path, new_file_path) - events._dispatch_node_renamed(abs_path, new_file_path) + M.rename(node, new_file_path) if M.enable_reload then require("nvim-tree.actions.reloaders.reloaders").reload_explorer() end diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index a1d6b163640..6cf7dc4e492 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -231,6 +231,11 @@ local DEFAULT_MAPPINGS = { action = "toggle_mark", desc = "Toggle node in bookmarks", }, + { + key = "bmv", + action = "bulk_move", + desc = "Move all bookmarked nodes into specified location", + }, } -- END_DEFAULT_MAPPINGS diff --git a/lua/nvim-tree/marks/bulk-move.lua b/lua/nvim-tree/marks/bulk-move.lua new file mode 100644 index 00000000000..b778eb6417f --- /dev/null +++ b/lua/nvim-tree/marks/bulk-move.lua @@ -0,0 +1,40 @@ +local Marks = require "nvim-tree.marks" +local Core = require "nvim-tree.core" +local utils = require "nvim-tree.utils" +local FsRename = require "nvim-tree.actions.fs.rename-file" + +local M = {} + +function M.bulk_move() + if #Marks.get_marks() == 0 then + utils.warn "no bookmark to perform bulk move on, aborting." + return + end + + vim.ui.input({ prompt = "Move to: ", default = Core.get_cwd(), completion = "dir" }, function(location) + if not location or location == "" then + return + end + if vim.fn.filewritable(location) ~= 2 then + utils.warn(location .. " is not writable, cannot move.") + return + end + + local marks = Marks.get_marks() + for _, node in pairs(marks) do + local head = vim.fn.fnamemodify(node.absolute_path, ":t") + local to = utils.path_join { location, head } + FsRename.rename(node, to) + end + + if M.enable_reload then + require("nvim-tree.actions.reloaders.reloaders").reload_explorer() + end + end) +end + +function M.setup(opts) + M.enable_reload = not opts.filesystem_watchers.enable +end + +return M diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index 6f4e6800212..e65abcf779c 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -66,6 +66,7 @@ end function M.setup(opts) vim.fn.sign_define(SIGN_NAME, { text = opts.renderer.icons.glyphs.bookmark, texthl = "NvimTreeBookmark" }) + require("nvim-tree.marks.bulk-move").setup(opts) end return M