feat: Use vim.ui.input for rename and create (#1097)

This commit is contained in:
baahrens
2022-03-22 20:28:58 +01:00
committed by GitHub
parent 6492d43fae
commit 7b0ebf8b17
2 changed files with 63 additions and 59 deletions

View File

@@ -50,15 +50,6 @@ local function get_containing_folder(node)
return node.absolute_path:sub(0, -node_name_size - 1) return node.absolute_path:sub(0, -node_name_size - 1)
end 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) function M.fn(node)
node = lib.get_last_group_node(node) node = lib.get_last_group_node(node)
if node.name == ".." then if node.name == ".." then
@@ -70,20 +61,28 @@ function M.fn(node)
end end
local containing_folder = get_containing_folder(node) local containing_folder = get_containing_folder(node)
local file = get_input(containing_folder)
if not file then local input_opts = { prompt = "Create file", default = containing_folder }
vim.ui.input(input_opts, function(new_file_path)
if not new_file_path or new_file_path == containing_folder then
return
end
if utils.file_exists(new_file_path) then
utils.warn "Cannot create: file already exists"
return return
end 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 path element -- 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 is_last_path_file = not new_file_path:match(utils.path_separator .. "$")
local path_to_create = "" local path_to_create = ""
local idx = 0 local idx = 0
local num_nodes = get_num_nodes(utils.path_split(utils.path_remove_trailing(file))) local num_nodes = get_num_nodes(utils.path_split(utils.path_remove_trailing(new_file_path)))
local is_error = false local is_error = false
for path in utils.path_split(file) do for path in utils.path_split(new_file_path) do
idx = idx + 1 idx = idx + 1
local p = utils.path_remove_trailing(path) local p = utils.path_remove_trailing(path)
if #path_to_create == 0 and vim.fn.has "win32" == 1 then if #path_to_create == 0 and vim.fn.has "win32" == 1 then
@@ -103,11 +102,12 @@ function M.fn(node)
end end
end end
if not is_error then if not is_error then
a.nvim_out_write(file .. " was properly created\n") a.nvim_out_write(new_file_path .. " was properly created\n")
end end
events._dispatch_folder_created(file) events._dispatch_folder_created(new_file_path)
require("nvim-tree.actions.reloaders").reload_explorer() require("nvim-tree.actions.reloaders").reload_explorer()
focus_file(file) focus_file(new_file_path)
end)
end end
return M return M

View File

@@ -16,24 +16,28 @@ function M.fn(with_sub)
local namelen = node.name:len() local namelen = node.name:len()
local abs_path = with_sub and node.absolute_path:sub(0, namelen * -1 - 1) or node.absolute_path 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() local input_opts = { prompt = "Rename to", default = abs_path }
if not new_name or #new_name == 0 then
vim.ui.input(input_opts, function(new_file_path)
if not new_file_path then
return return
end end
if utils.file_exists(new_name) then
if utils.file_exists(new_file_path) then
utils.warn "Cannot rename: file already exists" utils.warn "Cannot rename: file already exists"
return return
end end
local success = uv.fs_rename(node.absolute_path, new_name) local success = uv.fs_rename(node.absolute_path, new_file_path)
if not success then if not success then
return a.nvim_err_writeln("Could not rename " .. node.absolute_path .. " to " .. new_name) return a.nvim_err_writeln("Could not rename " .. node.absolute_path .. " to " .. new_file_path)
end end
a.nvim_out_write(node.absolute_path .. "" .. new_name .. "\n") a.nvim_out_write(node.absolute_path .. "" .. new_file_path .. "\n")
utils.rename_loaded_buffers(node.absolute_path, new_name) utils.rename_loaded_buffers(node.absolute_path, new_file_path)
events._dispatch_node_renamed(abs_path, new_name) events._dispatch_node_renamed(abs_path, new_file_path)
require("nvim-tree.actions.reloaders").reload_explorer() require("nvim-tree.actions.reloaders").reload_explorer()
end)
end end
end end