Skip to content

feat(#2630): file renames can now create directories #2657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 3, 2024
57 changes: 49 additions & 8 deletions lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ local M = {
config = {},
}

---@param iter function iterable
---@return integer
local function get_num_nodes(iter)
local i = 0
for _ in iter do
i = i + 1
end
return i
end

local ALLOWED_MODIFIERS = {
[":p"] = true,
[":p:h"] = true,
Expand All @@ -31,15 +41,46 @@ function M.rename(node, to)
return
end

events._dispatch_will_rename_node(node.absolute_path, to)
local success, err = vim.loop.fs_rename(node.absolute_path, to)
if not success then
notify.warn(err_fmt(notify_from, notify_to, err))
return
-- create a folder for each path element if the folder does not exist
local idx = 0
local path_to_create = ""

local num_nodes = get_num_nodes(utils.path_split(utils.path_remove_trailing(to)))
local is_error = false
for path in utils.path_split(to) do
idx = idx + 1

local p = utils.path_remove_trailing(path)
if #path_to_create == 0 and vim.fn.has "win32" == 1 then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use utils.is_wsl or utils.is_windows

Copy link
Contributor Author

@mohamedarish mohamedarish Feb 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using wezterm terminal running zsh on macOS. I'll change the wsl, windows check on rename.
Should I change it in the create file because it is like what I've done right now?
I'll check into why this is failing :)

Copy link
Member

@alex-courtis alex-courtis Feb 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using wezterm terminal running zsh on macOS. I'll change the wsl, windows check on rename. Should I change it in the create file because it is like what I've done right now? I'll check into why this is failing :)

My apologies, I didn't realise it came from copy. That's fine, no need to change.

We need a big refactor / abstraction for that functionality, but Not Today.

I'd be very grateful for a future PR to do that :)

path_to_create = utils.path_join { p, path_to_create }
else
path_to_create = utils.path_join { path_to_create, p }
end

if idx == num_nodes then
events._dispatch_will_rename_node(node.absolute_path, to)
local success, err = vim.loop.fs_rename(node.absolute_path, to)

if not success then
notify.warn(err_fmt(notify_from, notify_to, err))
return
end
elseif not utils.file_exists(path_to_create) then
local success = vim.loop.fs_mkdir(path_to_create, 493)
if not success then
notify.error("Could not create folder " .. notify.render_path(path_to_create))
is_error = true
break
end
is_error = false
end
end

if not is_error then
notify.info(string.format("%s -> %s", notify_from, notify_to))
utils.rename_loaded_buffers(node.absolute_path, to)
events._dispatch_node_renamed(node.absolute_path, to)
end
notify.info(string.format("%s -> %s", notify_from, notify_to))
utils.rename_loaded_buffers(node.absolute_path, to)
events._dispatch_node_renamed(node.absolute_path, to)
end

---@param default_modifier string|nil
Expand Down