1
1
local api = vim .api
2
2
local luv = vim .loop
3
- local open_mode = luv .constants .O_CREAT + luv .constants .O_WRONLY + luv .constants .O_TRUNC
4
3
5
4
local utils = require ' nvim-tree.utils'
6
5
local view = require ' nvim-tree.view'
@@ -29,13 +28,10 @@ local function create_file(file)
29
28
return
30
29
end
31
30
end
32
- luv .fs_open (file , " w" , open_mode , vim .schedule_wrap (function (err , fd )
31
+ luv .fs_open (file , " w" , 420 , vim .schedule_wrap (function (err , fd )
33
32
if err then
34
33
api .nvim_err_writeln (' Couldn\' t create file ' .. file )
35
34
else
36
- -- FIXME: i don't know why but libuv keeps creating file with executable permissions
37
- -- this is why we need to chmod to default file permissions
38
- luv .fs_chmod (file , 420 )
39
35
luv .fs_close (fd )
40
36
events ._dispatch_file_created (file )
41
37
lib .refresh_tree (true )
@@ -68,44 +64,34 @@ function M.create(node)
68
64
add_into = node .absolute_path :sub (0 , - (# node .name + 1 ))
69
65
end
70
66
71
- local ans = vim .fn .input (' Create file ' .. add_into )
67
+ local ans = vim .fn .input (' Create file ' , add_into )
72
68
utils .clear_prompt ()
73
- if not ans or # ans == 0 then return end
69
+ if not ans or # ans == 0 or luv . fs_access ( ans , ' r ' ) then return end
74
70
75
- if not ans :match (utils .path_separator ) then
76
- return create_file (add_into .. ans )
77
- end
78
-
79
- -- create a foler for each element until / and create a file when element is not ending with /
80
- -- if element is ending with / and it's the last element, we need to manually refresh
81
- local relpath = ' '
71
+ -- create a folder for each path element if the folder does not exist
72
+ -- if the answer ends with a /, create a file for the last entry
73
+ local is_last_path_file = not ans :match (utils .path_separator .. ' $' )
74
+ local path_to_create = ' '
82
75
local idx = 0
83
76
84
- local num_entries = get_num_entries (utils .path_split (ans ))
85
- local first_entry
77
+ local num_entries = get_num_entries (utils .path_split (utils .path_remove_trailing (ans )))
86
78
for path in utils .path_split (ans ) do
87
- if idx == 0 then
88
- first_entry = add_into .. relpath .. path
89
- end
90
79
idx = idx + 1
91
- relpath = relpath .. path
92
- local abs_path = add_into .. relpath
93
- if relpath :match (' .*' .. utils .path_separator .. ' $' ) then
94
- local success = luv .fs_mkdir (abs_path , 493 )
80
+ path_to_create = utils .path_join ({path_to_create , path })
81
+ if is_last_path_file and idx == num_entries then
82
+ create_file (path_to_create )
83
+ elseif not luv .fs_access (path_to_create , " r" ) then
84
+ local success = luv .fs_mkdir (path_to_create , 493 )
95
85
if not success then
96
- api .nvim_err_writeln (' Could not create folder ' .. abs_path )
97
- return
98
- end
99
- if idx == num_entries then
100
- events ._dispatch_folder_created (abs_path )
101
- api .nvim_out_write (' Folder ' .. abs_path .. ' was properly created\n ' )
102
- lib .refresh_tree (true )
86
+ api .nvim_err_writeln (' Could not create folder ' .. path_to_create )
87
+ break
103
88
end
104
- else
105
- create_file (abs_path )
106
89
end
107
90
end
108
- focus_file (first_entry :sub (0 , # first_entry - 1 ))
91
+ api .nvim_out_write (ans .. ' was properly created\n ' )
92
+ events ._dispatch_folder_created (ans )
93
+ lib .refresh_tree (true )
94
+ focus_file (ans )
109
95
end
110
96
111
97
local function clear_buffer (absolute_path )
@@ -117,15 +103,15 @@ local function clear_buffer(absolute_path)
117
103
end
118
104
119
105
local function rename_loaded_buffers (old_name , new_name )
120
- for _ , buf in pairs (api .nvim_list_bufs ()) do
121
- if api .nvim_buf_is_loaded (buf ) then
122
- if api .nvim_buf_get_name (buf ) == old_name then
123
- api .nvim_buf_set_name (buf , new_name )
124
- -- to avoid the 'overwrite existing file' error message on write
125
- vim .api .nvim_buf_call (buf , function () vim .cmd (" silent! w!" ) end )
126
- end
106
+ for _ , buf in pairs (api .nvim_list_bufs ()) do
107
+ if api .nvim_buf_is_loaded (buf ) then
108
+ if api .nvim_buf_get_name (buf ) == old_name then
109
+ api .nvim_buf_set_name (buf , new_name )
110
+ -- to avoid the 'overwrite existing file' error message on write
111
+ vim .api .nvim_buf_call (buf , function () vim .cmd (" silent! w!" ) end )
127
112
end
128
113
end
114
+ end
129
115
end
130
116
131
117
local function remove_dir (cwd )
0 commit comments