refacto: change fs_access to fs_stat to check file existence in fs
This commit is contained in:
@@ -11,6 +11,13 @@ local clipboard = {
|
|||||||
copy = {}
|
copy = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
--- @param path string path to file or directory
|
||||||
|
--- @return boolean
|
||||||
|
local function exist(path)
|
||||||
|
local result, error = luv.fs_stat(path)
|
||||||
|
return error == nil
|
||||||
|
end
|
||||||
|
|
||||||
local function focus_file(file)
|
local function focus_file(file)
|
||||||
local _, i = utils.find_node(
|
local _, i = utils.find_node(
|
||||||
lib.Tree.entries,
|
lib.Tree.entries,
|
||||||
@@ -20,7 +27,7 @@ local function focus_file(file)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function create_file(file)
|
local function create_file(file)
|
||||||
if luv.fs_access(file, "r") ~= false then
|
if exist(file) then
|
||||||
print(file..' already exists. Overwrite? y/n')
|
print(file..' already exists. Overwrite? y/n')
|
||||||
local ans = utils.get_user_input_char()
|
local ans = utils.get_user_input_char()
|
||||||
utils.clear_prompt()
|
utils.clear_prompt()
|
||||||
@@ -69,7 +76,7 @@ function M.create(node)
|
|||||||
|
|
||||||
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 or luv.fs_access(ans, 'r') then return end
|
if not ans or #ans == 0 or exist(ans) then return end
|
||||||
|
|
||||||
-- create a folder for each path element if the folder does not exist
|
-- create a folder for each path element if the folder does not exist
|
||||||
-- if the answer ends with a /, create a file for the last entry
|
-- if the answer ends with a /, create a file for the last entry
|
||||||
@@ -78,6 +85,7 @@ function M.create(node)
|
|||||||
local idx = 0
|
local idx = 0
|
||||||
|
|
||||||
local num_entries = get_num_entries(utils.path_split(utils.path_remove_trailing(ans)))
|
local num_entries = get_num_entries(utils.path_split(utils.path_remove_trailing(ans)))
|
||||||
|
local is_error = false
|
||||||
for path in utils.path_split(ans) do
|
for path in utils.path_split(ans) do
|
||||||
idx = idx + 1
|
idx = idx + 1
|
||||||
local p = utils.path_remove_trailing(path)
|
local p = utils.path_remove_trailing(path)
|
||||||
@@ -88,15 +96,18 @@ function M.create(node)
|
|||||||
end
|
end
|
||||||
if is_last_path_file and idx == num_entries then
|
if is_last_path_file and idx == num_entries then
|
||||||
create_file(path_to_create)
|
create_file(path_to_create)
|
||||||
elseif not luv.fs_access(path_to_create, "r") then
|
elseif not exist(path_to_create) then
|
||||||
local success = luv.fs_mkdir(path_to_create, 493)
|
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 '..path_to_create)
|
api.nvim_err_writeln('Could not create folder '..path_to_create)
|
||||||
|
is_error = true
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if not is_error then
|
||||||
api.nvim_out_write(ans..' was properly created\n')
|
api.nvim_out_write(ans..' was properly created\n')
|
||||||
|
end
|
||||||
events._dispatch_folder_created(ans)
|
events._dispatch_folder_created(ans)
|
||||||
lib.refresh_tree()
|
lib.refresh_tree()
|
||||||
focus_file(ans)
|
focus_file(ans)
|
||||||
@@ -295,7 +306,7 @@ function M.rename(with_sub)
|
|||||||
if not new_name or #new_name == 0 then
|
if not new_name or #new_name == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if luv.fs_access(new_name, 'R') then
|
if exist(new_name) then
|
||||||
utils.warn("Cannot rename: file already exists")
|
utils.warn("Cannot rename: file already exists")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user