Skip to content
Open
Changes from 1 commit
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
Next Next commit
fix: Move existing file out of the way during rename
  • Loading branch information
FlippingBinary committed May 20, 2025
commit cc464dc1b94b731bdc950867db9d95f4bbcc317b
29 changes: 26 additions & 3 deletions lua/blink/cmp/fuzzy/download/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,32 @@ end
--- @param new_path string
function files.rename(old_path, new_path)
return async.task.new(function(resolve, reject)
vim.uv.fs_rename(old_path, new_path, function(err)
if err then return reject(err) end
resolve()
-- Generate a temporary filename with timestamp
local time = os.date("%Y%m%d%H%M%S")
local dirname = vim.fs.dirname(new_path)
local basename = vim.fs.basename(new_path)
local tmpfile = vim.fs.joinpath(dirname or ".", ".trash-" .. time .. "-" .. basename)

-- Try to move new_path out of the way unconditionally
vim.uv.fs_rename(new_path, tmpfile, function(rename_existing_err)
if rename_existing_err then
-- Signal the fact that there is no tmp file to delete
tmpfile = ""
end
-- Now move old_path to new_path
vim.uv.fs_rename(old_path, new_path, function(rename_err)
if rename_err then
return reject(rename_err)
end
-- If we moved the original new_path, try to delete the temp file
if string.len(tmpfile) > 0 then
vim.uv.fs_unlink(tmpfile, function()
-- TODO: either report the error or just automatically delete
-- stray trash files as part of a routine cleanup.
end)
end
resolve()
end)
end)
end)
end
Expand Down