Skip to content

fix(#914): remove existence check before create-file, rename-file, harden clobbered buffer renaming #1977

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions lua/nvim-tree/actions/fs/create-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 .. "$")
Expand Down
5 changes: 0 additions & 5 deletions lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 24 additions & 16 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Copy link
Member Author

Choose a reason for hiding this comment

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

All callers use absolute paths hence this fuzzy matching can be removed.

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
Expand Down