Skip to content

Commit 24c0512

Browse files
authored
Merge branch 'master' into master
2 parents e7dcecb + efafd73 commit 24c0512

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

Diff for: lua/nvim-tree/actions/fs/rename-file.lua

+49-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ local M = {
99
config = {},
1010
}
1111

12+
---@param iter function iterable
13+
---@return integer
14+
local function get_num_nodes(iter)
15+
local i = 0
16+
for _ in iter do
17+
i = i + 1
18+
end
19+
return i
20+
end
21+
1222
local ALLOWED_MODIFIERS = {
1323
[":p"] = true,
1424
[":p:h"] = true,
@@ -31,15 +41,46 @@ function M.rename(node, to)
3141
return
3242
end
3343

34-
events._dispatch_will_rename_node(node.absolute_path, to)
35-
local success, err = vim.loop.fs_rename(node.absolute_path, to)
36-
if not success then
37-
notify.warn(err_fmt(notify_from, notify_to, err))
38-
return
44+
-- create a folder for each path element if the folder does not exist
45+
local idx = 0
46+
local path_to_create = ""
47+
48+
local num_nodes = get_num_nodes(utils.path_split(utils.path_remove_trailing(to)))
49+
local is_error = false
50+
for path in utils.path_split(to) do
51+
idx = idx + 1
52+
53+
local p = utils.path_remove_trailing(path)
54+
if #path_to_create == 0 and vim.fn.has "win32" == 1 then
55+
path_to_create = utils.path_join { p, path_to_create }
56+
else
57+
path_to_create = utils.path_join { path_to_create, p }
58+
end
59+
60+
if idx == num_nodes then
61+
events._dispatch_will_rename_node(node.absolute_path, to)
62+
local success, err = vim.loop.fs_rename(node.absolute_path, to)
63+
64+
if not success then
65+
notify.warn(err_fmt(notify_from, notify_to, err))
66+
return
67+
end
68+
elseif not utils.file_exists(path_to_create) then
69+
local success = vim.loop.fs_mkdir(path_to_create, 493)
70+
if not success then
71+
notify.error("Could not create folder " .. notify.render_path(path_to_create))
72+
is_error = true
73+
break
74+
end
75+
is_error = false
76+
end
77+
end
78+
79+
if not is_error then
80+
notify.info(string.format("%s -> %s", notify_from, notify_to))
81+
utils.rename_loaded_buffers(node.absolute_path, to)
82+
events._dispatch_node_renamed(node.absolute_path, to)
3983
end
40-
notify.info(string.format("%s -> %s", notify_from, notify_to))
41-
utils.rename_loaded_buffers(node.absolute_path, to)
42-
events._dispatch_node_renamed(node.absolute_path, to)
4384
end
4485

4586
---@param default_modifier string|nil

0 commit comments

Comments
 (0)