@@ -50,15 +50,6 @@ local function get_containing_folder(node)
50
50
return node .absolute_path :sub (0 , - node_name_size - 1 )
51
51
end
52
52
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
-
62
53
function M .fn (node )
63
54
node = lib .get_last_group_node (node )
64
55
if node .name == " .." then
@@ -70,44 +61,53 @@ function M.fn(node)
70
61
end
71
62
72
63
local containing_folder = get_containing_folder (node )
73
- local file = get_input (containing_folder )
74
- if not file then
75
- return
76
- end
77
64
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 }
83
66
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
93
70
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
102
102
end
103
103
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 )
111
111
end
112
112
113
113
return M
0 commit comments