From 1b1b3b9b9ea2ffca731a48f067bbe5d359ad8cfb Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Tue, 7 Feb 2023 11:16:21 +1100 Subject: [PATCH 1/2] fix(#914): remove existence check before create-file, rename-file --- lua/nvim-tree/actions/fs/create-file.lua | 5 ----- lua/nvim-tree/actions/fs/rename-file.lua | 5 ----- 2 files changed, 10 deletions(-) diff --git a/lua/nvim-tree/actions/fs/create-file.lua b/lua/nvim-tree/actions/fs/create-file.lua index cd0a83800f1..3972f9925ba 100644 --- a/lua/nvim-tree/actions/fs/create-file.lua +++ b/lua/nvim-tree/actions/fs/create-file.lua @@ -54,11 +54,6 @@ function M.fn(node) return end - if utils.file_exists(new_file_path) then - notify.warn "Cannot create: file already exists" - return - end - -- create a folder for each path element if the folder does not exist -- if the answer ends with a /, create a file for the last path element local is_last_path_file = not new_file_path:match(utils.path_separator .. "$") diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index 443858ececc..99e25a7efaa 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -16,11 +16,6 @@ local function err_fmt(from, to, reason) end function M.rename(node, to) - if utils.file_exists(to) then - notify.warn(err_fmt(node.absolute_path, 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 From f4ceb1eb4f91f64bfa43b55ae86a7a8d89ed5150 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Tue, 7 Feb 2023 13:05:44 +1100 Subject: [PATCH 2/2] fix(#914): harden buffer rename on paste/rename --- lua/nvim-tree/utils.lua | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 2604add9324..aff98e05ece 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -154,27 +154,35 @@ function M.get_nodes_by_line(nodes_all, line_start) return nodes_by_line end -function M.rename_loaded_buffers(old_path, new_path) +--- Rename and write a buffer if from exists and to does not. +--- @param from_path string absolute path +--- @param to_path string absolute path +function M.rename_loaded_buffers(from_path, to_path) + -- find from then to + local from_buf, to_buf for _, buf in pairs(vim.api.nvim_list_bufs()) do if vim.api.nvim_buf_is_loaded(buf) then - local buf_name = vim.api.nvim_buf_get_name(buf) - local exact_match = buf_name == old_path - local child_match = ( - buf_name:sub(1, #old_path) == old_path and buf_name:sub(#old_path + 1, #old_path + 1) == path_separator - ) - if exact_match or child_match then - vim.api.nvim_buf_set_name(buf, new_path .. buf_name:sub(#old_path + 1)) - -- to avoid the 'overwrite existing file' error message on write for - -- normal files - if vim.api.nvim_buf_get_option(buf, "buftype") == "" then - vim.api.nvim_buf_call(buf, function() - vim.cmd "silent! write!" - vim.cmd "edit" - end) - end + local path = vim.api.nvim_buf_get_name(buf) + if path == from_path then + from_buf = buf + elseif path == to_path then + to_buf = buf end end end + + -- don't clobber + if from_buf and not to_buf then + vim.api.nvim_buf_set_name(from_buf, to_path) + -- to avoid the 'overwrite existing file' error message on write for + -- normal files + if vim.api.nvim_buf_get_option(from_buf, "buftype") == "" then + vim.api.nvim_buf_call(from_buf, function() + vim.cmd "silent! write!" + vim.cmd "edit" + end) + end + end end --- @param path string path to file or directory