chore: extract functions initialization in create-file
This commit is contained in:
@@ -41,6 +41,22 @@ local function get_num_entries(iter)
|
||||
return i
|
||||
end
|
||||
|
||||
local function get_containing_folder(node)
|
||||
local is_open = vim.g.nvim_tree_create_in_closed_folder == 1 or node.open
|
||||
if node.entries ~= nil and is_open then
|
||||
return utils.path_add_trailing(node.absolute_path)
|
||||
end
|
||||
local node_name_size = #(node.name or '')
|
||||
return node.absolute_path:sub(0, -node_name_size + 1)
|
||||
end
|
||||
|
||||
local function get_input(containing_folder)
|
||||
local ans = vim.fn.input('Create file ', containing_folder)
|
||||
utils.clear_prompt()
|
||||
if not ans or #ans == 0 or utils.file_exists(ans) then return end
|
||||
return ans
|
||||
end
|
||||
|
||||
function M.fn(node)
|
||||
node = lib.get_last_group_node(node)
|
||||
if node.name == '..' then
|
||||
@@ -51,28 +67,19 @@ function M.fn(node)
|
||||
}
|
||||
end
|
||||
|
||||
local node_is_open = vim.g.nvim_tree_create_in_closed_folder == 1 or node.open
|
||||
|
||||
local add_into
|
||||
if node.entries ~= nil and node_is_open then
|
||||
add_into = utils.path_add_trailing(node.absolute_path)
|
||||
else
|
||||
add_into = node.absolute_path:sub(0, -(#(node.name or '') + 1))
|
||||
end
|
||||
|
||||
local ans = vim.fn.input('Create file ', add_into)
|
||||
utils.clear_prompt()
|
||||
if not ans or #ans == 0 or utils.file_exists(ans) then return end
|
||||
local containing_folder = get_containing_folder(node)
|
||||
local file = get_input(containing_folder)
|
||||
if not file then return end
|
||||
|
||||
-- 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
|
||||
local is_last_path_file = not ans:match(utils.path_separator..'$')
|
||||
local is_last_path_file = not file:match(utils.path_separator..'$')
|
||||
local path_to_create = ''
|
||||
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(file)))
|
||||
local is_error = false
|
||||
for path in utils.path_split(ans) do
|
||||
for path in utils.path_split(file) do
|
||||
idx = idx + 1
|
||||
local p = utils.path_remove_trailing(path)
|
||||
if #path_to_create == 0 and vim.fn.has('win32') == 1 then
|
||||
@@ -92,11 +99,11 @@ function M.fn(node)
|
||||
end
|
||||
end
|
||||
if not is_error then
|
||||
a.nvim_out_write(ans..' was properly created\n')
|
||||
a.nvim_out_write(file..' was properly created\n')
|
||||
end
|
||||
events._dispatch_folder_created(ans)
|
||||
events._dispatch_folder_created(file)
|
||||
lib.refresh_tree(function()
|
||||
focus_file(ans)
|
||||
focus_file(file)
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user