Skip to content

feat(marks): add bulk move action #1419

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

Merged
merged 1 commit into from
Jul 17, 2022
Merged
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
2 changes: 2 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
`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
Expand Down Expand Up @@ -1063,6 +1064,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
{ key = "<C-k>", action = "toggle_file_info" }
{ key = "g?", action = "toggle_help" }
{ key = "m", action = "toggle_mark" }
{ key = "bmv", action = "bulk_move" }
} -- END_DEFAULT_MAPPINGS
<
==============================================================================
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/actions/dispatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 21 additions & 13 deletions lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

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

nice

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)
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-tree/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 40 additions & 0 deletions lua/nvim-tree/marks/bulk-move.lua
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lua/nvim-tree/marks/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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