Skip to content

Commit 5e94dff

Browse files
committed
feat(marks): add bulk rename action
1 parent 89becc7 commit 5e94dff

File tree

6 files changed

+70
-13
lines changed

6 files changed

+70
-13
lines changed

doc/nvim-tree-lua.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
10151015
`<C-k>` toggle_file_info toggle a popup with file infos about the file under the cursor
10161016
`g?` toggle_help toggle help
10171017
`m` toggle_mark Toggle node in bookmarks
1018+
`bmv` bulk_move Move all bookmarked nodes into specified location
10181019

10191020
>
10201021
view.mappings.list = { -- BEGIN_DEFAULT_MAPPINGS
@@ -1063,6 +1064,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
10631064
{ key = "<C-k>", action = "toggle_file_info" }
10641065
{ key = "g?", action = "toggle_help" }
10651066
{ key = "m", action = "toggle_mark" }
1067+
{ key = "bmv", action = "bulk_move" }
10661068
} -- END_DEFAULT_MAPPINGS
10671069
<
10681070
==============================================================================

lua/nvim-tree/actions/dispatch.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ local Actions = {
4646
toggle_file_info = require("nvim-tree.actions.node.file-popup").toggle_file_info,
4747
system_open = require("nvim-tree.actions.node.system-open").fn,
4848
toggle_mark = require("nvim-tree.marks").toggle_mark,
49+
bulk_move = require("nvim-tree.marks.bulk-move").bulk_move,
4950
}
5051

5152
local function handle_action_on_help_ui(action)

lua/nvim-tree/actions/fs/rename-file.lua

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ local events = require "nvim-tree.events"
77

88
local M = {}
99

10+
local function err_fmt(from, to, reason)
11+
return string.format("Cannot rename %s -> %s: %s", from, to, reason)
12+
end
13+
14+
function M.rename(node, to)
15+
if utils.file_exists(to) then
16+
utils.warn(err_fmt(node.absolute_path, to, "file already exists"))
17+
return
18+
end
19+
20+
local success, err = uv.fs_rename(node.absolute_path, to)
21+
if not success then
22+
return utils.warn(err_fmt(node.absolute_path, to, err))
23+
end
24+
utils.clear_prompt()
25+
a.nvim_out_write(node.absolute_path .. "" .. to .. "\n")
26+
utils.rename_loaded_buffers(node.absolute_path, to)
27+
events._dispatch_node_renamed(node.absolute_path, to)
28+
end
29+
1030
function M.fn(with_sub)
1131
return function(node)
1232
node = lib.get_last_group_node(node)
@@ -24,19 +44,7 @@ function M.fn(with_sub)
2444
return
2545
end
2646

27-
if utils.file_exists(new_file_path) then
28-
utils.warn "Cannot rename: file already exists"
29-
return
30-
end
31-
32-
local success = uv.fs_rename(node.absolute_path, new_file_path)
33-
if not success then
34-
return a.nvim_err_writeln("Could not rename " .. node.absolute_path .. " to " .. new_file_path)
35-
end
36-
utils.clear_prompt()
37-
a.nvim_out_write(node.absolute_path .. "" .. new_file_path .. "\n")
38-
utils.rename_loaded_buffers(node.absolute_path, new_file_path)
39-
events._dispatch_node_renamed(abs_path, new_file_path)
47+
M.rename(node, new_file_path)
4048
if M.enable_reload then
4149
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
4250
end

lua/nvim-tree/actions/init.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ local DEFAULT_MAPPINGS = {
231231
action = "toggle_mark",
232232
desc = "Toggle node in bookmarks",
233233
},
234+
{
235+
key = "bmv",
236+
action = "bulk_move",
237+
desc = "Move all bookmarked nodes into specified location",
238+
},
234239
}
235240
-- END_DEFAULT_MAPPINGS
236241

lua/nvim-tree/marks/bulk-move.lua

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
local Marks = require "nvim-tree.marks"
2+
local Core = require "nvim-tree.core"
3+
local utils = require "nvim-tree.utils"
4+
local FsRename = require "nvim-tree.actions.fs.rename-file"
5+
6+
local M = {}
7+
8+
function M.bulk_move()
9+
if #Marks.get_marks() == 0 then
10+
utils.warn "no bookmark to perform bulk move on, aborting."
11+
return
12+
end
13+
14+
vim.ui.input({ prompt = "Move to: ", default = Core.get_cwd(), completion = "dir" }, function(location)
15+
if not location or location == "" then
16+
return
17+
end
18+
if vim.fn.filewritable(location) ~= 2 then
19+
utils.warn(location .. " is not writable, cannot move.")
20+
return
21+
end
22+
23+
local marks = Marks.get_marks()
24+
for _, node in pairs(marks) do
25+
local head = vim.fn.fnamemodify(node.absolute_path, ":t")
26+
local to = utils.path_join { location, head }
27+
FsRename.rename(node, to)
28+
end
29+
30+
if M.enable_reload then
31+
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
32+
end
33+
end)
34+
end
35+
36+
function M.setup(opts)
37+
M.enable_reload = not opts.filesystem_watchers.enable
38+
end
39+
40+
return M

lua/nvim-tree/marks/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ end
6666

6767
function M.setup(opts)
6868
vim.fn.sign_define(SIGN_NAME, { text = opts.renderer.icons.glyphs.bookmark, texthl = "NvimTreeBookmark" })
69+
require("nvim-tree.marks.bulk-move").setup(opts)
6970
end
7071

7172
return M

0 commit comments

Comments
 (0)