chore: refacto create implementation
allows for creating file anywhere on the system fixes adding a file in a grouped folder
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
local api = vim.api
|
local api = vim.api
|
||||||
local luv = vim.loop
|
local luv = vim.loop
|
||||||
local open_mode = luv.constants.O_CREAT + luv.constants.O_WRONLY + luv.constants.O_TRUNC
|
|
||||||
|
|
||||||
local utils = require'nvim-tree.utils'
|
local utils = require'nvim-tree.utils'
|
||||||
local view = require'nvim-tree.view'
|
local view = require'nvim-tree.view'
|
||||||
@@ -29,13 +28,10 @@ local function create_file(file)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
luv.fs_open(file, "w", open_mode, vim.schedule_wrap(function(err, fd)
|
luv.fs_open(file, "w", 420, vim.schedule_wrap(function(err, fd)
|
||||||
if err then
|
if err then
|
||||||
api.nvim_err_writeln('Couldn\'t create file '..file)
|
api.nvim_err_writeln('Couldn\'t create file '..file)
|
||||||
else
|
else
|
||||||
-- FIXME: i don't know why but libuv keeps creating file with executable permissions
|
|
||||||
-- this is why we need to chmod to default file permissions
|
|
||||||
luv.fs_chmod(file, 420)
|
|
||||||
luv.fs_close(fd)
|
luv.fs_close(fd)
|
||||||
events._dispatch_file_created(file)
|
events._dispatch_file_created(file)
|
||||||
lib.refresh_tree(true)
|
lib.refresh_tree(true)
|
||||||
@@ -68,44 +64,34 @@ function M.create(node)
|
|||||||
add_into = node.absolute_path:sub(0, -(#node.name + 1))
|
add_into = node.absolute_path:sub(0, -(#node.name + 1))
|
||||||
end
|
end
|
||||||
|
|
||||||
local ans = vim.fn.input('Create file '..add_into)
|
local ans = vim.fn.input('Create file ', add_into)
|
||||||
utils.clear_prompt()
|
utils.clear_prompt()
|
||||||
if not ans or #ans == 0 then return end
|
if not ans or #ans == 0 or luv.fs_access(ans, 'r') then return end
|
||||||
|
|
||||||
if not ans:match(utils.path_separator) then
|
-- create a folder for each path element if the folder does not exist
|
||||||
return create_file(add_into..ans)
|
-- if the answer ends with a /, create a file for the last entry
|
||||||
end
|
local is_last_path_file = not ans:match(utils.path_separator..'$')
|
||||||
|
local path_to_create = ''
|
||||||
-- create a foler for each element until / and create a file when element is not ending with /
|
|
||||||
-- if element is ending with / and it's the last element, we need to manually refresh
|
|
||||||
local relpath = ''
|
|
||||||
local idx = 0
|
local idx = 0
|
||||||
|
|
||||||
local num_entries = get_num_entries(utils.path_split(ans))
|
local num_entries = get_num_entries(utils.path_split(utils.path_remove_trailing(ans)))
|
||||||
local first_entry
|
|
||||||
for path in utils.path_split(ans) do
|
for path in utils.path_split(ans) do
|
||||||
if idx == 0 then
|
|
||||||
first_entry = add_into..relpath..path
|
|
||||||
end
|
|
||||||
idx = idx + 1
|
idx = idx + 1
|
||||||
relpath = relpath..path
|
path_to_create = utils.path_join({path_to_create, path})
|
||||||
local abs_path = add_into..relpath
|
if is_last_path_file and idx == num_entries then
|
||||||
if relpath:match('.*'..utils.path_separator..'$') then
|
create_file(path_to_create)
|
||||||
local success = luv.fs_mkdir(abs_path, 493)
|
elseif not luv.fs_access(path_to_create, "r") then
|
||||||
|
local success = luv.fs_mkdir(path_to_create, 493)
|
||||||
if not success then
|
if not success then
|
||||||
api.nvim_err_writeln('Could not create folder '..abs_path)
|
api.nvim_err_writeln('Could not create folder '..path_to_create)
|
||||||
return
|
break
|
||||||
end
|
end
|
||||||
if idx == num_entries then
|
end
|
||||||
events._dispatch_folder_created(abs_path)
|
end
|
||||||
api.nvim_out_write('Folder '..abs_path..' was properly created\n')
|
api.nvim_out_write(ans..' was properly created\n')
|
||||||
|
events._dispatch_folder_created(ans)
|
||||||
lib.refresh_tree(true)
|
lib.refresh_tree(true)
|
||||||
end
|
focus_file(ans)
|
||||||
else
|
|
||||||
create_file(abs_path)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
focus_file(first_entry:sub(0, #first_entry - 1))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function clear_buffer(absolute_path)
|
local function clear_buffer(absolute_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user