diff --git a/lua/oil/adapters/trash/freedesktop.lua b/lua/oil/adapters/trash/freedesktop.lua index 07e8a7f..c669730 100644 --- a/lua/oil/adapters/trash/freedesktop.lua +++ b/lua/oil/adapters/trash/freedesktop.lua @@ -151,7 +151,7 @@ end ---@field info_file string ---@field original_path string ---@field deletion_date number ----@field stat uv_fs_t +---@field stat uv.aliases.fs_stat_table ---@param info_file string ---@param cb fun(err?: string, info?: oil.TrashInfo) @@ -596,8 +596,7 @@ M.perform_action = function(action, cb) if err then cb(err) else - ---@diagnostic disable-next-line: undefined-field - local stat_type = trash_info.stat.type + local stat_type = trash_info.stat.type or "unknown" fs.recursive_copy(stat_type, path, trash_info.trash_file, vim.schedule_wrap(cb)) end end) @@ -625,8 +624,7 @@ M.delete_to_trash = function(path, cb) if err then cb(err) else - ---@diagnostic disable-next-line: undefined-field - local stat_type = trash_info.stat.type + local stat_type = trash_info.stat.type or "unknown" fs.recursive_move(stat_type, path, trash_info.trash_file, vim.schedule_wrap(cb)) end end) diff --git a/lua/oil/adapters/trash/mac.lua b/lua/oil/adapters/trash/mac.lua index 8b2d33a..66cf4c1 100644 --- a/lua/oil/adapters/trash/mac.lua +++ b/lua/oil/adapters/trash/mac.lua @@ -224,7 +224,6 @@ M.delete_to_trash = function(path, cb) end local stat_type = src_stat.type - ---@cast stat_type oil.EntryType fs.recursive_move(stat_type, path, dest, vim.schedule_wrap(cb)) end) ) diff --git a/lua/oil/fs.lua b/lua/oil/fs.lua index ac216a1..a9ae10c 100644 --- a/lua/oil/fs.lua +++ b/lua/oil/fs.lua @@ -1,3 +1,4 @@ +local log = require("oil.log") local M = {} local uv = vim.uv or vim.loop @@ -245,6 +246,37 @@ M.recursive_delete = function(entry_type, path, cb) end, 10000) end +---Move the undofile for the file at src_path to dest_path +---@param src_path string +---@param dest_path string +---@param copy boolean +local move_undofile = vim.schedule_wrap(function(src_path, dest_path, copy) + local undofile = vim.fn.undofile(src_path) + uv.fs_stat( + undofile, + vim.schedule_wrap(function(stat_err) + if stat_err then + -- undofile doesn't exist + return + end + local dest_undofile = vim.fn.undofile(dest_path) + if copy then + uv.fs_copyfile(src_path, dest_path, function(err) + if err then + log.warn("Error copying undofile %s: %s", undofile, err) + end + end) + else + uv.fs_rename(undofile, dest_undofile, function(err) + if err then + log.warn("Error moving undofile %s: %s", undofile, err) + end + end) + end + end) + ) +end) + ---@param entry_type oil.EntryType ---@param src_path string ---@param dest_path string @@ -262,6 +294,7 @@ M.recursive_copy = function(entry_type, src_path, dest_path, cb) end if entry_type ~= "directory" then uv.fs_copyfile(src_path, dest_path, { excl = true }, cb) + move_undofile(src_path, dest_path, true) return end uv.fs_stat(src_path, function(stat_err, src_stat) @@ -333,6 +366,9 @@ M.recursive_move = function(entry_type, src_path, dest_path, cb) end end) else + if entry_type ~= "directory" then + move_undofile(src_path, dest_path, false) + end cb() end end) diff --git a/lua/oil/util.lua b/lua/oil/util.lua index 707aa24..7be1d5e 100644 --- a/lua/oil/util.lua +++ b/lua/oil/util.lua @@ -195,6 +195,18 @@ M.rename_buffer = function(src_bufnr, dest_buf_name) -- Try to delete, but don't if the buffer has changes pcall(vim.api.nvim_buf_delete, src_bufnr, {}) end + -- Renaming a buffer won't load the undo file, so we need to do that manually + if vim.bo[dest_bufnr].undofile then + vim.api.nvim_buf_call(dest_bufnr, function() + vim.cmd.rundo({ + args = { vim.fn.undofile(dest_buf_name) }, + magic = { file = false, bar = false }, + mods = { + emsg_silent = true, + }, + }) + end) + end end) return true end