feat(marks): add bulk move action (#1419)

This commit is contained in:
Kiyan 2022-07-17 08:25:11 +02:00 committed by GitHub
parent 208ce0b153
commit 26512c369f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 13 deletions

View File

@ -1015,6 +1015,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
`<C-k>` toggle_file_info toggle a popup with file infos about the file under the cursor `<C-k>` toggle_file_info toggle a popup with file infos about the file under the cursor
`g?` toggle_help toggle help `g?` toggle_help toggle help
`m` toggle_mark Toggle node in bookmarks `m` toggle_mark Toggle node in bookmarks
`bmv` bulk_move Move all bookmarked nodes into specified location
> >
view.mappings.list = { -- BEGIN_DEFAULT_MAPPINGS view.mappings.list = { -- BEGIN_DEFAULT_MAPPINGS
@ -1063,6 +1064,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
{ key = "<C-k>", action = "toggle_file_info" } { key = "<C-k>", action = "toggle_file_info" }
{ key = "g?", action = "toggle_help" } { key = "g?", action = "toggle_help" }
{ key = "m", action = "toggle_mark" } { key = "m", action = "toggle_mark" }
{ key = "bmv", action = "bulk_move" }
} -- END_DEFAULT_MAPPINGS } -- END_DEFAULT_MAPPINGS
< <
============================================================================== ==============================================================================

View File

@ -46,6 +46,7 @@ local Actions = {
toggle_file_info = require("nvim-tree.actions.node.file-popup").toggle_file_info, toggle_file_info = require("nvim-tree.actions.node.file-popup").toggle_file_info,
system_open = require("nvim-tree.actions.node.system-open").fn, system_open = require("nvim-tree.actions.node.system-open").fn,
toggle_mark = require("nvim-tree.marks").toggle_mark, 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) local function handle_action_on_help_ui(action)

View File

@ -7,6 +7,26 @@ local events = require "nvim-tree.events"
local M = {} 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) function M.fn(with_sub)
return function(node) return function(node)
node = lib.get_last_group_node(node) node = lib.get_last_group_node(node)
@ -24,19 +44,7 @@ function M.fn(with_sub)
return return
end end
if utils.file_exists(new_file_path) then M.rename(node, new_file_path)
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)
if M.enable_reload then if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer() require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end end

View File

@ -231,6 +231,11 @@ local DEFAULT_MAPPINGS = {
action = "toggle_mark", action = "toggle_mark",
desc = "Toggle node in bookmarks", desc = "Toggle node in bookmarks",
}, },
{
key = "bmv",
action = "bulk_move",
desc = "Move all bookmarked nodes into specified location",
},
} }
-- END_DEFAULT_MAPPINGS -- END_DEFAULT_MAPPINGS

View File

@ -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

View File

@ -66,6 +66,7 @@ end
function M.setup(opts) function M.setup(opts)
vim.fn.sign_define(SIGN_NAME, { text = opts.renderer.icons.glyphs.bookmark, texthl = "NvimTreeBookmark" }) vim.fn.sign_define(SIGN_NAME, { text = opts.renderer.icons.glyphs.bookmark, texthl = "NvimTreeBookmark" })
require("nvim-tree.marks.bulk-move").setup(opts)
end end
return M return M