From cc464dc1b94b731bdc950867db9d95f4bbcc317b Mon Sep 17 00:00:00 2001 From: Jon Musselwhite <35066367+FlippingBinary@users.noreply.github.com> Date: Tue, 20 May 2025 12:21:49 -0400 Subject: [PATCH 1/2] fix: Move existing file out of the way during rename --- lua/blink/cmp/fuzzy/download/files.lua | 29 +++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lua/blink/cmp/fuzzy/download/files.lua b/lua/blink/cmp/fuzzy/download/files.lua index a32b3c3b5..5e2b305fc 100644 --- a/lua/blink/cmp/fuzzy/download/files.lua +++ b/lua/blink/cmp/fuzzy/download/files.lua @@ -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 From e6247491cf3d477d537f467624e85f81b73c9750 Mon Sep 17 00:00:00 2001 From: Jon Musselwhite <35066367+FlippingBinary@users.noreply.github.com> Date: Thu, 22 May 2025 13:20:19 -0400 Subject: [PATCH 2/2] chore: Stylua corrections --- lua/blink/cmp/fuzzy/download/files.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lua/blink/cmp/fuzzy/download/files.lua b/lua/blink/cmp/fuzzy/download/files.lua index 5e2b305fc..b828e24f3 100644 --- a/lua/blink/cmp/fuzzy/download/files.lua +++ b/lua/blink/cmp/fuzzy/download/files.lua @@ -170,22 +170,20 @@ end function files.rename(old_path, new_path) return async.task.new(function(resolve, reject) -- Generate a temporary filename with timestamp - local time = os.date("%Y%m%d%H%M%S") + 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) + 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 = "" + 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 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()