Skip to content

Commit 425f5ee

Browse files
committed
chore: refacto create implementation
allows for creating file anywhere on the system fixes adding a file in a grouped folder
1 parent 27caccb commit 425f5ee

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed

lua/nvim-tree/fs.lua

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local api = vim.api
22
local luv = vim.loop
3-
local open_mode = luv.constants.O_CREAT + luv.constants.O_WRONLY + luv.constants.O_TRUNC
43

54
local utils = require'nvim-tree.utils'
65
local view = require'nvim-tree.view'
@@ -29,13 +28,10 @@ local function create_file(file)
2928
return
3029
end
3130
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)
3332
if err then
3433
api.nvim_err_writeln('Couldn\'t create file '..file)
3534
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)
3935
luv.fs_close(fd)
4036
events._dispatch_file_created(file)
4137
lib.refresh_tree(true)
@@ -68,44 +64,34 @@ function M.create(node)
6864
add_into = node.absolute_path:sub(0, -(#node.name + 1))
6965
end
7066

71-
local ans = vim.fn.input('Create file '..add_into)
67+
local ans = vim.fn.input('Create file ', add_into)
7268
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
7470

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 = ''
8275
local idx = 0
8376

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)))
8678
for path in utils.path_split(ans) do
87-
if idx == 0 then
88-
first_entry = add_into..relpath..path
89-
end
9079
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)
9585
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
10388
end
104-
else
105-
create_file(abs_path)
10689
end
10790
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)
10995
end
11096

11197
local function clear_buffer(absolute_path)
@@ -117,15 +103,15 @@ local function clear_buffer(absolute_path)
117103
end
118104

119105
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)
127112
end
128113
end
114+
end
129115
end
130116

131117
local function remove_dir(cwd)

0 commit comments

Comments
 (0)