Skip to content

Commit bd5bc12

Browse files
committed
refactor(#3255): wire fs.rename API impl directly to functions
1 parent 6d5bf5f commit bd5bc12

File tree

2 files changed

+82
-65
lines changed

2 files changed

+82
-65
lines changed

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

Lines changed: 77 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -97,81 +97,98 @@ function M.rename(node, to)
9797
end
9898
end
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")
175192
end
176193

177194
function M.setup(opts)

lua/nvim-tree/api/impl/post.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ function M.hydrate(api)
194194
api.fs.create = wrap_node_or_nil(actions.fs.create_file.fn)
195195
api.fs.remove = wrap_node_or_visual(actions.fs.remove_file.fn)
196196
api.fs.trash = wrap_node_or_visual(actions.fs.trash.fn)
197-
api.fs.rename_node = wrap_node(actions.fs.rename_file.fn(":t"))
198-
api.fs.rename = wrap_node(actions.fs.rename_file.fn(":t"))
199-
api.fs.rename_sub = wrap_node(actions.fs.rename_file.fn(":p:h"))
200-
api.fs.rename_basename = wrap_node(actions.fs.rename_file.fn(":t:r"))
201-
api.fs.rename_full = wrap_node(actions.fs.rename_file.fn(":p"))
197+
api.fs.rename_node = wrap_node(actions.fs.rename_file.rename_node)
198+
api.fs.rename = wrap_node(actions.fs.rename_file.rename_node)
199+
api.fs.rename_sub = wrap_node(actions.fs.rename_file.rename_sub)
200+
api.fs.rename_basename = wrap_node(actions.fs.rename_file.rename_basename)
201+
api.fs.rename_full = wrap_node(actions.fs.rename_file.rename_full)
202202
api.fs.cut = wrap_node_or_visual(wrap_explorer_member("clipboard", "cut"))
203203
api.fs.paste = wrap_node(wrap_explorer_member("clipboard", "paste"))
204204
api.fs.clear_clipboard = wrap_explorer_member("clipboard", "clear_clipboard")

0 commit comments

Comments
 (0)