Skip to content

Commit e05a96e

Browse files
committed
feat(marks): add bulk rename action
1 parent 6a49a03 commit e05a96e

File tree

6 files changed

+61
-13
lines changed

6 files changed

+61
-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_rename 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_rename" }
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_rename = require("nvim-tree.marks.bulk-rename").bulk_rename,
4950
}
5051

5152
local function handle_action_on_help_ui(action)

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

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

88
local M = {}
99

10+
function M.rename(node, to)
11+
if utils.file_exists(to) then
12+
utils.warn "Cannot rename: file already exists"
13+
return
14+
end
15+
16+
local success = uv.fs_rename(node.absolute_path, to)
17+
if not success then
18+
return a.nvim_err_writeln("Could not rename " .. node.absolute_path .. " to " .. to)
19+
end
20+
utils.clear_prompt()
21+
a.nvim_out_write(node.absolute_path .. "" .. to .. "\n")
22+
utils.rename_loaded_buffers(node.absolute_path, to)
23+
events._dispatch_node_renamed(node.absolute_path, to)
24+
end
25+
1026
function M.fn(with_sub)
1127
return function(node)
1228
node = lib.get_last_group_node(node)
@@ -24,19 +40,7 @@ function M.fn(with_sub)
2440
return
2541
end
2642

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)
43+
M.rename(node, new_file_path)
4044
if M.enable_reload then
4145
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
4246
end

lua/nvim-tree/actions/init.lua

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

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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_rename()
9+
vim.ui.input({ prompt = "Location: ", default = Core.get_cwd(), completion = "dir" }, function(location)
10+
if not location or location == "" then
11+
return
12+
end
13+
if vim.fn.filewritable(location) ~= 2 then
14+
utils.warn(location .. " is not writable, cannot perform bulk action.")
15+
return
16+
end
17+
18+
local marks = Marks.get_marks()
19+
for _, node in pairs(marks) do
20+
local head = vim.fn.fnamemodify(node.absolute_path, ":t")
21+
local to = utils.path_join { location, head }
22+
FsRename.rename(node, to)
23+
end
24+
25+
if M.enable_reload then
26+
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
27+
end
28+
end)
29+
end
30+
31+
function M.setup(opts)
32+
M.enable_reload = not opts.filesystem_watchers.enable
33+
end
34+
35+
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-rename").setup(opts)
6970
end
7071

7172
return M

0 commit comments

Comments
 (0)