@@ -97,81 +97,98 @@ function M.rename(node, to)
9797 end
9898end
9999
100- --- @param default_modifier string | nil
101- --- @return fun ( node : Node , modifier : string )
102- function M .fn (default_modifier )
103- default_modifier = default_modifier or " :t"
104-
105- return function (node , modifier )
106- local explorer = core .get_explorer ()
107- if not explorer then
108- return
109- end
100+ --- @param node Node
101+ --- @param modifier ? string
102+ local function prompt_to_rename (node , modifier )
103+ if not modifier or type (modifier ) ~= " string" then
104+ modifier = " :t"
105+ end
110106
111- if type (node ) ~= " table" then
112- node = explorer :get_node_at_cursor ()
113- end
114- if not node then
107+ local explorer = core .get_explorer ()
108+ if not explorer then
109+ return
110+ end
111+
112+ if type (node ) ~= " table" then
113+ local node_at_cursor = explorer :get_node_at_cursor ()
114+ if not node_at_cursor then
115115 return
116116 end
117+ node = node_at_cursor
118+ end
117119
118- if type (modifier ) ~= " string" then
119- modifier = default_modifier
120- end
120+ -- support for only specific modifiers have been implemented
121+ if not ALLOWED_MODIFIERS [modifier ] then
122+ notify .warn (" Modifier " .. vim .inspect (modifier ) .. " is not in allowed list : " .. table.concat (ALLOWED_MODIFIERS , " ," ))
123+ return
124+ end
121125
122- -- support for only specific modifiers have been implemented
123- if not ALLOWED_MODIFIERS [modifier ] then
124- notify .warn (" Modifier " .. vim .inspect (modifier ) .. " is not in allowed list : " .. table.concat (ALLOWED_MODIFIERS , " ," ))
125- return
126- end
126+ local dir = node :as (DirectoryNode )
127+ if dir then
128+ node = dir :last_group_node ()
129+ end
130+ if node .name == " .." then
131+ return
132+ end
127133
128- local dir = node :as (DirectoryNode )
129- if dir then
130- node = dir :last_group_node ()
131- end
132- if node .name == " .." then
134+ local namelen = node .name :len ()
135+ local directory = node .absolute_path :sub (0 , namelen * - 1 - 1 )
136+ local default_path
137+ local prepend = " "
138+ local append = " "
139+ default_path = vim .fn .fnamemodify (node .absolute_path , modifier )
140+ if modifier :sub (0 , 2 ) == " :t" then
141+ prepend = directory
142+ end
143+ if modifier == " :t:r" then
144+ local extension = vim .fn .fnamemodify (node .name , " :e" )
145+ append = extension :len () == 0 and " " or " ." .. extension
146+ end
147+ if modifier == " :p:h" then
148+ default_path = default_path .. " /"
149+ end
150+
151+ local input_opts = {
152+ prompt = " Rename to " ,
153+ default = default_path ,
154+ completion = " file" ,
155+ }
156+
157+ vim .ui .input (input_opts , function (new_file_path )
158+ utils .clear_prompt ()
159+ if not new_file_path then
133160 return
134161 end
135162
136- local namelen = node .name :len ()
137- local directory = node .absolute_path :sub (0 , namelen * - 1 - 1 )
138- local default_path
139- local prepend = " "
140- local append = " "
141- default_path = vim .fn .fnamemodify (node .absolute_path , modifier )
142- if modifier :sub (0 , 2 ) == " :t" then
143- prepend = directory
144- end
145- if modifier == " :t:r" then
146- local extension = vim .fn .fnamemodify (node .name , " :e" )
147- append = extension :len () == 0 and " " or " ." .. extension
148- end
149- if modifier == " :p:h" then
150- default_path = default_path .. " /"
163+ local full_new_path = prepend .. new_file_path .. append
164+
165+ M .rename (node , full_new_path )
166+ if not M .config .filesystem_watchers .enable then
167+ explorer :reload_explorer ()
151168 end
152169
153- local input_opts = {
154- prompt = " Rename to " ,
155- default = default_path ,
156- completion = " file" ,
157- }
170+ find_file (utils .path_remove_trailing (full_new_path ))
171+ end )
172+ end
158173
159- vim .ui .input (input_opts , function (new_file_path )
160- utils .clear_prompt ()
161- if not new_file_path then
162- return
163- end
174+ --- @param node Node
175+ function M .rename_node (node )
176+ prompt_to_rename (node , " :t" )
177+ end
164178
165- local full_new_path = prepend .. new_file_path .. append
179+ --- @param node Node
180+ function M .rename_sub (node )
181+ prompt_to_rename (node , " :p:h" )
182+ end
166183
167- M . rename ( node , full_new_path )
168- if not M . config . filesystem_watchers . enable then
169- explorer : reload_explorer ( )
170- end
184+ --- @param node Node
185+ function M . rename_basename ( node )
186+ prompt_to_rename ( node , " :t:r " )
187+ end
171188
172- find_file ( utils . path_remove_trailing ( full_new_path ))
173- end )
174- end
189+ --- @param node Node
190+ function M . rename_full ( node )
191+ prompt_to_rename ( node , " :p " )
175192end
176193
177194function M .setup (opts )
0 commit comments