* add todo * refactor(#2886): multi instance: node class refactoring: extract links, *_git_status (#2944) * extract DirectoryLinkNode and FileLinkNode, move Node methods to children * temporarily move DirectoryNode methods into BaseNode for easier reviewing * move mostly unchanged DirectoryNode methods back to BaseNode * tidy * git.git_status_file takes an array * update git status of links * luacheck hack * safer git_status_dir * refactor(#2886): multi instance: node class refactoring: DirectoryNode:expand_or_collapse (#2957) move expand_or_collapse to DirectoryNode * refactor(#2886): multi instance: node group functions refactoring (#2959) * move last_group_node to DirectoryNode * move add BaseNode:as and more doc * revert parameter name changes * revert parameter name changes * add Class * move group methods into DN * tidy group methods * tidy group methods * tidy group methods * tidy group methods * parent is DirectoryNode * tidy expand all * BaseNode -> Node * move watcher to DirectoryNode * last_group_node is DirectoryNode only * simplify create-file * simplify parent * simplify collapse-all * simplify live-filter * style * move lib.get_cursor_position to Explorer * move lib.get_node_at_cursor to Explorer * move lib.get_nodes to Explorer * move place_cursor_on_node to Explorer * resolve resource leak in purge_all_state * move many autocommands into Explorer * post merge tidy * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * Revert "chore: resolve undefined-field" This reverts commit be546ff18d41f28466b065c857e1e041659bd2c8. * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * Revert "chore: resolve undefined-field" This reverts commite82db1c44d. * chore: resolve undefined-field * chore: class new is now generic * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * Revert "chore: resolve undefined-field" This reverts commit0e9b844d22. * move icon builders into node classes * move icon builders into node classes * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * move folder specifics from icons to Directory * move folder specifics from icons to Directory * move folder specifics from icons to Directory * move folder specifics from icons to Directory * move file specifics from icons to File * clean up sorters * chore: resolve undefined-field * tidy hl icon name * file devicon uses library to fall back * file devicon uses library to fall back * file devicon uses library to fall back
106 lines
3.0 KiB
Lua
106 lines
3.0 KiB
Lua
local utils = require("nvim-tree.utils")
|
|
local events = require("nvim-tree.events")
|
|
local core = require("nvim-tree.core")
|
|
local notify = require("nvim-tree.notify")
|
|
|
|
local find_file = require("nvim-tree.actions.finders.find-file").fn
|
|
|
|
local FileNode = require("nvim-tree.node.file")
|
|
local DirectoryNode = require("nvim-tree.node.directory")
|
|
|
|
local M = {}
|
|
|
|
---@param file string
|
|
local function create_and_notify(file)
|
|
events._dispatch_will_create_file(file)
|
|
local ok, fd = pcall(vim.loop.fs_open, file, "w", 420)
|
|
if not ok or type(fd) ~= "number" then
|
|
notify.error("Couldn't create file " .. notify.render_path(file))
|
|
return
|
|
end
|
|
vim.loop.fs_close(fd)
|
|
events._dispatch_file_created(file)
|
|
end
|
|
|
|
---@param iter function iterable
|
|
---@return integer
|
|
local function get_num_nodes(iter)
|
|
local i = 0
|
|
for _ in iter do
|
|
i = i + 1
|
|
end
|
|
return i
|
|
end
|
|
|
|
---@param node Node?
|
|
function M.fn(node)
|
|
node = node or core.get_explorer()
|
|
if not node then
|
|
return
|
|
end
|
|
|
|
local dir = node:is(FileNode) and node.parent or node:as(DirectoryNode)
|
|
if not dir then
|
|
return
|
|
end
|
|
|
|
dir = dir:last_group_node()
|
|
|
|
local containing_folder = utils.path_add_trailing(dir.absolute_path)
|
|
|
|
local input_opts = {
|
|
prompt = "Create file ",
|
|
default = containing_folder,
|
|
completion = "file",
|
|
}
|
|
|
|
vim.ui.input(input_opts, function(new_file_path)
|
|
utils.clear_prompt()
|
|
if not new_file_path or new_file_path == containing_folder then
|
|
return
|
|
end
|
|
|
|
if utils.file_exists(new_file_path) then
|
|
notify.warn("Cannot create: file already exists")
|
|
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 path element
|
|
local is_last_path_file = not new_file_path:match(utils.path_separator .. "$")
|
|
local path_to_create = ""
|
|
local idx = 0
|
|
|
|
local num_nodes = get_num_nodes(utils.path_split(utils.path_remove_trailing(new_file_path)))
|
|
local is_error = false
|
|
for path in utils.path_split(new_file_path) do
|
|
idx = idx + 1
|
|
local p = utils.path_remove_trailing(path)
|
|
if #path_to_create == 0 and vim.fn.has("win32") == 1 then
|
|
path_to_create = utils.path_join({ p, path_to_create })
|
|
else
|
|
path_to_create = utils.path_join({ path_to_create, p })
|
|
end
|
|
if is_last_path_file and idx == num_nodes then
|
|
create_and_notify(path_to_create)
|
|
elseif not utils.file_exists(path_to_create) then
|
|
local success = vim.loop.fs_mkdir(path_to_create, 493)
|
|
if not success then
|
|
notify.error("Could not create folder " .. notify.render_path(path_to_create))
|
|
is_error = true
|
|
break
|
|
end
|
|
events._dispatch_folder_created(new_file_path)
|
|
end
|
|
end
|
|
if not is_error then
|
|
notify.info(notify.render_path(new_file_path) .. " was properly created")
|
|
end
|
|
|
|
-- synchronously refreshes as we can't wait for the watchers
|
|
find_file(utils.path_remove_trailing(new_file_path))
|
|
end)
|
|
end
|
|
|
|
return M
|