diff --git a/lua/nvim-tree/actions/create-file.lua b/lua/nvim-tree/actions/create-file.lua index 00861aac2b4..facc59a542a 100644 --- a/lua/nvim-tree/actions/create-file.lua +++ b/lua/nvim-tree/actions/create-file.lua @@ -50,15 +50,6 @@ local function get_containing_folder(node) 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 @@ -70,44 +61,53 @@ function M.fn(node) 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 path element - local is_last_path_file = not file:match(utils.path_separator .. "$") - local path_to_create = "" - local idx = 0 + local input_opts = { prompt = "Create file", default = containing_folder } - local num_nodes = get_num_nodes(utils.path_split(utils.path_remove_trailing(file))) - local is_error = false - 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 - path_to_create = utils.path_join { p, path_to_create } - else - path_to_create = utils.path_join { path_to_create, p } + vim.ui.input(input_opts, function(new_file_path) + if not new_file_path or new_file_path == containing_folder then + return end - if is_last_path_file and idx == num_nodes then - create_file(path_to_create) - elseif not utils.file_exists(path_to_create) then - local success = uv.fs_mkdir(path_to_create, 493) - if not success then - a.nvim_err_writeln("Could not create folder " .. path_to_create) - is_error = true - break + + if utils.file_exists(new_file_path) then + utils.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_file(path_to_create) + elseif not utils.file_exists(path_to_create) then + local success = uv.fs_mkdir(path_to_create, 493) + if not success then + a.nvim_err_writeln("Could not create folder " .. path_to_create) + is_error = true + break + end end end - end - if not is_error then - a.nvim_out_write(file .. " was properly created\n") - end - events._dispatch_folder_created(file) - require("nvim-tree.actions.reloaders").reload_explorer() - focus_file(file) + if not is_error then + a.nvim_out_write(new_file_path .. " was properly created\n") + end + events._dispatch_folder_created(new_file_path) + require("nvim-tree.actions.reloaders").reload_explorer() + focus_file(new_file_path) + end) end return M diff --git a/lua/nvim-tree/actions/rename-file.lua b/lua/nvim-tree/actions/rename-file.lua index f8fb53fd8fb..c33f19afd67 100644 --- a/lua/nvim-tree/actions/rename-file.lua +++ b/lua/nvim-tree/actions/rename-file.lua @@ -16,24 +16,28 @@ function M.fn(with_sub) local namelen = node.name:len() local abs_path = with_sub and node.absolute_path:sub(0, namelen * -1 - 1) or node.absolute_path - local new_name = vim.fn.input("Rename " .. node.name .. " to ", abs_path) - utils.clear_prompt() - if not new_name or #new_name == 0 then - return - end - if utils.file_exists(new_name) then - utils.warn "Cannot rename: file already exists" - return - end - local success = uv.fs_rename(node.absolute_path, new_name) - if not success then - return a.nvim_err_writeln("Could not rename " .. node.absolute_path .. " to " .. new_name) - end - a.nvim_out_write(node.absolute_path .. " ➜ " .. new_name .. "\n") - utils.rename_loaded_buffers(node.absolute_path, new_name) - events._dispatch_node_renamed(abs_path, new_name) - require("nvim-tree.actions.reloaders").reload_explorer() + local input_opts = { prompt = "Rename to", default = abs_path } + + vim.ui.input(input_opts, function(new_file_path) + if not new_file_path then + return + end + + if utils.file_exists(new_file_path) then + utils.warn "Cannot rename: file already exists" + return + end + + local success = uv.fs_rename(node.absolute_path, new_file_path) + if not success then + return a.nvim_err_writeln("Could not rename " .. node.absolute_path .. " to " .. new_file_path) + end + a.nvim_out_write(node.absolute_path .. " ➜ " .. new_file_path .. "\n") + utils.rename_loaded_buffers(node.absolute_path, new_file_path) + events._dispatch_node_renamed(abs_path, new_file_path) + require("nvim-tree.actions.reloaders").reload_explorer() + end) end end