@@ -9,6 +9,16 @@ local M = {
9
9
config = {},
10
10
}
11
11
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
+
12
22
local ALLOWED_MODIFIERS = {
13
23
[" :p" ] = true ,
14
24
[" :p:h" ] = true ,
@@ -31,15 +41,46 @@ function M.rename(node, to)
31
41
return
32
42
end
33
43
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 )
39
83
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 )
43
84
end
44
85
45
86
--- @param default_modifier string | nil
0 commit comments