Skip to content

Commit 32dd3e3

Browse files
committed
feat: most moves and copies will copy the undofile (#583)
1 parent 5313690 commit 32dd3e3

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

lua/oil/adapters/trash/freedesktop.lua

+3-5
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ end
151151
---@field info_file string
152152
---@field original_path string
153153
---@field deletion_date number
154-
---@field stat uv_fs_t
154+
---@field stat uv.aliases.fs_stat_table
155155

156156
---@param info_file string
157157
---@param cb fun(err?: string, info?: oil.TrashInfo)
@@ -596,8 +596,7 @@ M.perform_action = function(action, cb)
596596
if err then
597597
cb(err)
598598
else
599-
---@diagnostic disable-next-line: undefined-field
600-
local stat_type = trash_info.stat.type
599+
local stat_type = trash_info.stat.type or "unknown"
601600
fs.recursive_copy(stat_type, path, trash_info.trash_file, vim.schedule_wrap(cb))
602601
end
603602
end)
@@ -625,8 +624,7 @@ M.delete_to_trash = function(path, cb)
625624
if err then
626625
cb(err)
627626
else
628-
---@diagnostic disable-next-line: undefined-field
629-
local stat_type = trash_info.stat.type
627+
local stat_type = trash_info.stat.type or "unknown"
630628
fs.recursive_move(stat_type, path, trash_info.trash_file, vim.schedule_wrap(cb))
631629
end
632630
end)

lua/oil/adapters/trash/mac.lua

-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ M.delete_to_trash = function(path, cb)
224224
end
225225

226226
local stat_type = src_stat.type
227-
---@cast stat_type oil.EntryType
228227
fs.recursive_move(stat_type, path, dest, vim.schedule_wrap(cb))
229228
end)
230229
)

lua/oil/fs.lua

+36
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local log = require("oil.log")
12
local M = {}
23

34
local uv = vim.uv or vim.loop
@@ -245,6 +246,37 @@ M.recursive_delete = function(entry_type, path, cb)
245246
end, 10000)
246247
end
247248

249+
---Move the undofile for the file at src_path to dest_path
250+
---@param src_path string
251+
---@param dest_path string
252+
---@param copy boolean
253+
local move_undofile = vim.schedule_wrap(function(src_path, dest_path, copy)
254+
local undofile = vim.fn.undofile(src_path)
255+
uv.fs_stat(
256+
undofile,
257+
vim.schedule_wrap(function(stat_err)
258+
if stat_err then
259+
-- undofile doesn't exist
260+
return
261+
end
262+
local dest_undofile = vim.fn.undofile(dest_path)
263+
if copy then
264+
uv.fs_copyfile(src_path, dest_path, function(err)
265+
if err then
266+
log.warn("Error copying undofile %s: %s", undofile, err)
267+
end
268+
end)
269+
else
270+
uv.fs_rename(undofile, dest_undofile, function(err)
271+
if err then
272+
log.warn("Error moving undofile %s: %s", undofile, err)
273+
end
274+
end)
275+
end
276+
end)
277+
)
278+
end)
279+
248280
---@param entry_type oil.EntryType
249281
---@param src_path string
250282
---@param dest_path string
@@ -262,6 +294,7 @@ M.recursive_copy = function(entry_type, src_path, dest_path, cb)
262294
end
263295
if entry_type ~= "directory" then
264296
uv.fs_copyfile(src_path, dest_path, { excl = true }, cb)
297+
move_undofile(src_path, dest_path, true)
265298
return
266299
end
267300
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)
333366
end
334367
end)
335368
else
369+
if entry_type ~= "directory" then
370+
move_undofile(src_path, dest_path, false)
371+
end
336372
cb()
337373
end
338374
end)

lua/oil/util.lua

+12
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ M.rename_buffer = function(src_bufnr, dest_buf_name)
195195
-- Try to delete, but don't if the buffer has changes
196196
pcall(vim.api.nvim_buf_delete, src_bufnr, {})
197197
end
198+
-- Renaming a buffer won't load the undo file, so we need to do that manually
199+
if vim.bo[dest_bufnr].undofile then
200+
vim.api.nvim_buf_call(dest_bufnr, function()
201+
vim.cmd.rundo({
202+
args = { vim.fn.undofile(dest_buf_name) },
203+
magic = { file = false, bar = false },
204+
mods = {
205+
emsg_silent = true,
206+
},
207+
})
208+
end)
209+
end
198210
end)
199211
return true
200212
end

0 commit comments

Comments
 (0)