Skip to content

Commit 7b0ebf8

Browse files
author
baahrens
authored
feat: Use vim.ui.input for rename and create (#1097)
1 parent 6492d43 commit 7b0ebf8

File tree

2 files changed

+63
-59
lines changed

2 files changed

+63
-59
lines changed

lua/nvim-tree/actions/create-file.lua

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ local function get_containing_folder(node)
5050
return node.absolute_path:sub(0, -node_name_size - 1)
5151
end
5252

53-
local function get_input(containing_folder)
54-
local ans = vim.fn.input("Create file ", containing_folder)
55-
utils.clear_prompt()
56-
if not ans or #ans == 0 or utils.file_exists(ans) then
57-
return
58-
end
59-
return ans
60-
end
61-
6253
function M.fn(node)
6354
node = lib.get_last_group_node(node)
6455
if node.name == ".." then
@@ -70,44 +61,53 @@ function M.fn(node)
7061
end
7162

7263
local containing_folder = get_containing_folder(node)
73-
local file = get_input(containing_folder)
74-
if not file then
75-
return
76-
end
7764

78-
-- create a folder for each path element if the folder does not exist
79-
-- if the answer ends with a /, create a file for the last path element
80-
local is_last_path_file = not file:match(utils.path_separator .. "$")
81-
local path_to_create = ""
82-
local idx = 0
65+
local input_opts = { prompt = "Create file", default = containing_folder }
8366

84-
local num_nodes = get_num_nodes(utils.path_split(utils.path_remove_trailing(file)))
85-
local is_error = false
86-
for path in utils.path_split(file) do
87-
idx = idx + 1
88-
local p = utils.path_remove_trailing(path)
89-
if #path_to_create == 0 and vim.fn.has "win32" == 1 then
90-
path_to_create = utils.path_join { p, path_to_create }
91-
else
92-
path_to_create = utils.path_join { path_to_create, p }
67+
vim.ui.input(input_opts, function(new_file_path)
68+
if not new_file_path or new_file_path == containing_folder then
69+
return
9370
end
94-
if is_last_path_file and idx == num_nodes then
95-
create_file(path_to_create)
96-
elseif not utils.file_exists(path_to_create) then
97-
local success = uv.fs_mkdir(path_to_create, 493)
98-
if not success then
99-
a.nvim_err_writeln("Could not create folder " .. path_to_create)
100-
is_error = true
101-
break
71+
72+
if utils.file_exists(new_file_path) then
73+
utils.warn "Cannot create: file already exists"
74+
return
75+
end
76+
77+
-- create a folder for each path element if the folder does not exist
78+
-- if the answer ends with a /, create a file for the last path element
79+
local is_last_path_file = not new_file_path:match(utils.path_separator .. "$")
80+
local path_to_create = ""
81+
local idx = 0
82+
83+
local num_nodes = get_num_nodes(utils.path_split(utils.path_remove_trailing(new_file_path)))
84+
local is_error = false
85+
for path in utils.path_split(new_file_path) do
86+
idx = idx + 1
87+
local p = utils.path_remove_trailing(path)
88+
if #path_to_create == 0 and vim.fn.has "win32" == 1 then
89+
path_to_create = utils.path_join { p, path_to_create }
90+
else
91+
path_to_create = utils.path_join { path_to_create, p }
92+
end
93+
if is_last_path_file and idx == num_nodes then
94+
create_file(path_to_create)
95+
elseif not utils.file_exists(path_to_create) then
96+
local success = uv.fs_mkdir(path_to_create, 493)
97+
if not success then
98+
a.nvim_err_writeln("Could not create folder " .. path_to_create)
99+
is_error = true
100+
break
101+
end
102102
end
103103
end
104-
end
105-
if not is_error then
106-
a.nvim_out_write(file .. " was properly created\n")
107-
end
108-
events._dispatch_folder_created(file)
109-
require("nvim-tree.actions.reloaders").reload_explorer()
110-
focus_file(file)
104+
if not is_error then
105+
a.nvim_out_write(new_file_path .. " was properly created\n")
106+
end
107+
events._dispatch_folder_created(new_file_path)
108+
require("nvim-tree.actions.reloaders").reload_explorer()
109+
focus_file(new_file_path)
110+
end)
111111
end
112112

113113
return M

lua/nvim-tree/actions/rename-file.lua

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,28 @@ function M.fn(with_sub)
1616

1717
local namelen = node.name:len()
1818
local abs_path = with_sub and node.absolute_path:sub(0, namelen * -1 - 1) or node.absolute_path
19-
local new_name = vim.fn.input("Rename " .. node.name .. " to ", abs_path)
20-
utils.clear_prompt()
21-
if not new_name or #new_name == 0 then
22-
return
23-
end
24-
if utils.file_exists(new_name) then
25-
utils.warn "Cannot rename: file already exists"
26-
return
27-
end
2819

29-
local success = uv.fs_rename(node.absolute_path, new_name)
30-
if not success then
31-
return a.nvim_err_writeln("Could not rename " .. node.absolute_path .. " to " .. new_name)
32-
end
33-
a.nvim_out_write(node.absolute_path .. "" .. new_name .. "\n")
34-
utils.rename_loaded_buffers(node.absolute_path, new_name)
35-
events._dispatch_node_renamed(abs_path, new_name)
36-
require("nvim-tree.actions.reloaders").reload_explorer()
20+
local input_opts = { prompt = "Rename to", default = abs_path }
21+
22+
vim.ui.input(input_opts, function(new_file_path)
23+
if not new_file_path then
24+
return
25+
end
26+
27+
if utils.file_exists(new_file_path) then
28+
utils.warn "Cannot rename: file already exists"
29+
return
30+
end
31+
32+
local success = uv.fs_rename(node.absolute_path, new_file_path)
33+
if not success then
34+
return a.nvim_err_writeln("Could not rename " .. node.absolute_path .. " to " .. new_file_path)
35+
end
36+
a.nvim_out_write(node.absolute_path .. "" .. new_file_path .. "\n")
37+
utils.rename_loaded_buffers(node.absolute_path, new_file_path)
38+
events._dispatch_node_renamed(abs_path, new_file_path)
39+
require("nvim-tree.actions.reloaders").reload_explorer()
40+
end)
3741
end
3842
end
3943

0 commit comments

Comments
 (0)