From 199c7c0f18bc812b8a291f5e5bc3d6d50ad194c8 Mon Sep 17 00:00:00 2001 From: mohamed arish Date: Mon, 29 Jan 2024 20:49:45 +0530 Subject: [PATCH 1/5] Added creating of directories when renaming files --- lua/nvim-tree/actions/fs/rename-file.lua | 63 +++++++++++++++++++----- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index ec87c475920..489a690bba3 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -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, @@ -26,20 +36,48 @@ function M.rename(node, to) local notify_from = notify.render_path(node.absolute_path) local notify_to = notify.render_path(to) - if utils.file_exists(to) then - notify.warn(err_fmt(notify_from, notify_to, "file already exists")) - return + -- create a folder for each path element if the folder does not exist + local idx = 0 + local path_to_create = "" + local is_last_path_file = not to:match(utils.path_remove_trailing(to)) + + 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 + path_to_create = utils.path_join { p, path_to_create } + else + path_to_create = utils.path_join { path_to_create, p } + end + + if is_last_path_file and 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)) + is_error = true + break + 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)) + break + end + is_error = true + events._dispatch_folder_created(to) + end 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 + 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 @@ -62,7 +100,8 @@ function M.fn(default_modifier) -- support for only specific modifiers have been implemented if not ALLOWED_MODIFIERS[modifier] then - notify.warn("Modifier " .. vim.inspect(modifier) .. " is not in allowed list : " .. table.concat(ALLOWED_MODIFIERS, ",")) + notify.warn("Modifier " .. + vim.inspect(modifier) .. " is not in allowed list : " .. table.concat(ALLOWED_MODIFIERS, ",")) return end From f50ce77336a35ffc8758887080818806d048676b Mon Sep 17 00:00:00 2001 From: mohamed arish Date: Mon, 29 Jan 2024 20:57:22 +0530 Subject: [PATCH 2/5] Style check error? fixed --- lua/nvim-tree/actions/fs/rename-file.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index 489a690bba3..0cc098391e9 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -100,8 +100,7 @@ function M.fn(default_modifier) -- support for only specific modifiers have been implemented if not ALLOWED_MODIFIERS[modifier] then - notify.warn("Modifier " .. - vim.inspect(modifier) .. " is not in allowed list : " .. table.concat(ALLOWED_MODIFIERS, ",")) + notify.warn("Modifier " .. vim.inspect(modifier) .. " is not in allowed list : " .. table.concat(ALLOWED_MODIFIERS, ",")) return end From 68da256fea81b891c76b94fa308d7ec30cc5e83a Mon Sep 17 00:00:00 2001 From: mohamed arish Date: Sun, 11 Feb 2024 12:12:12 +0530 Subject: [PATCH 3/5] Forgot, I added back the line of code that creates a directory named as the file and forgot to remove it --- lua/nvim-tree/actions/fs/rename-file.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index 0cc098391e9..2c861dddcee 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -69,7 +69,6 @@ function M.rename(node, to) break end is_error = true - events._dispatch_folder_created(to) end end From c41be1fa7943d4a6cd70db6d5a2cb0dc8bf231e8 Mon Sep 17 00:00:00 2001 From: mohamed arish Date: Mon, 26 Feb 2024 12:00:18 +0530 Subject: [PATCH 4/5] Added a check for file already exists and also switched the is_error values as they were mismatched --- lua/nvim-tree/actions/fs/rename-file.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index 2c861dddcee..005ef2975c8 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -54,21 +54,26 @@ function M.rename(node, to) end if is_last_path_file and idx == num_nodes then + if utils.file_exists(to) then + notify.warn(err_fmt(notify_from, notify_to, "file already exists")) + 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)) - is_error = true - break + 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 = true + is_error = false end end From 645a9dbb4466ac5e91c6ea6bfb21065208e58466 Mon Sep 17 00:00:00 2001 From: mohamed arish Date: Mon, 26 Feb 2024 12:58:17 +0530 Subject: [PATCH 5/5] I don't know how but this somehow fixed the creation of a directory? --- lua/nvim-tree/actions/fs/rename-file.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index 005ef2975c8..d83f3f53e84 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -36,10 +36,14 @@ function M.rename(node, to) local notify_from = notify.render_path(node.absolute_path) local notify_to = notify.render_path(to) + if utils.file_exists(to) then + notify.warn(err_fmt(notify_from, notify_to, "file already exists")) + return + end + -- create a folder for each path element if the folder does not exist local idx = 0 local path_to_create = "" - local is_last_path_file = not to:match(utils.path_remove_trailing(to)) local num_nodes = get_num_nodes(utils.path_split(utils.path_remove_trailing(to))) local is_error = false @@ -53,12 +57,7 @@ function M.rename(node, to) path_to_create = utils.path_join { path_to_create, p } end - if is_last_path_file and idx == num_nodes then - if utils.file_exists(to) then - notify.warn(err_fmt(notify_from, notify_to, "file already exists")) - return - 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)