From c7629620a5b044a4fd456116020ff6a07c78fddc Mon Sep 17 00:00:00 2001 From: Bryan Date: Fri, 1 Nov 2024 22:32:02 -0600 Subject: [PATCH] If file is managed by git then rename using git-mv instead of filesystem mv --- lua/genghis/support/lsp-rename.lua | 42 +++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/lua/genghis/support/lsp-rename.lua b/lua/genghis/support/lsp-rename.lua index 5207b7a..a68c1aa 100644 --- a/lua/genghis/support/lsp-rename.lua +++ b/lua/genghis/support/lsp-rename.lua @@ -39,13 +39,49 @@ function M.lspSupportsRenaming() return false end +---@param filePath string +local function isGitManagedFile(filePath) + local obj = vim.system({ "git", "ls-files", "--error-unmatch", filePath }, { text = true }) + :wait() + if obj.code == 0 then + return true + else + return false + end +end + +---@param oldFilePath string +---@param newFilePath string +local function git_mv(oldFilePath, newFilePath) + local obj = vim.system({ "git", "mv", oldFilePath, newFilePath }, { text = true }):wait() + if obj.code == 0 then + return true, obj.stderr + else + return false, obj.stderr + end +end + ---@param oldFilePath string ---@param newFilePath string function M.moveFile(oldFilePath, newFilePath) local u = require("genghis.support.utils") - - local renamed, _ = vim.uv.fs_rename(oldFilePath, newFilePath) - if renamed then return true end + if isGitManagedFile(oldFilePath) then + local renamed, git_mv_error = git_mv(oldFilePath, newFilePath) + if renamed then + return true + else + u.notify( + ("File managed by git.\nFailed to rename using git mv %q:\n%q\nAttempting to rename using fs."):format( + oldFilePath, + git_mv_error + ), + "error" + ) + end + else + local renamed, _ = vim.uv.fs_rename(oldFilePath, newFilePath) + if renamed then return true end + end ---try `fs_copyfile` to support moving across partitions local copied, copiedError = vim.uv.fs_copyfile(oldFilePath, newFilePath)