Skip to content

Commit 4b9d4da

Browse files
committed
wip
1 parent ed64ae5 commit 4b9d4da

3 files changed

Lines changed: 57 additions & 76 deletions

File tree

lua/neo-tree/sources/filesystem/lib/fs_actions.lua

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -736,15 +736,15 @@ M.delete_nodes = function(paths_to_delete, callback)
736736
end)
737737
end
738738

739-
M.trash_node = function(path, callback, noconfirm)
739+
M.trash_node = function(path, callback)
740740
local _, name = utils.split_path(path)
741741

742742
log.trace("Deleting node:", path)
743743
local _type = "unknown"
744744
local stat = uv.fs_lstat(path)
745745
local children_count = 0
746746
if not stat then
747-
log.warn("Could not read file/dir:", path, stat, ", attempting to delete anyway...")
747+
log.warn("Could not read file/dir:", path, stat, ", attempting to trash anyway...")
748748
-- Guess the type by whether it appears to have an extension
749749
if path:match("%.(.+)$") then
750750
_type = "file"
@@ -775,7 +775,7 @@ M.trash_node = function(path, callback, noconfirm)
775775
end
776776
end
777777

778-
local do_delete = function()
778+
local do_trash = function()
779779
local complete = vim.schedule_wrap(function()
780780
events.fire_event(events.FILE_DELETED, path)
781781
if callback then
@@ -789,80 +789,47 @@ M.trash_node = function(path, callback, noconfirm)
789789
return
790790
end
791791

792-
if _type ~= "directory" then
793-
local success = uv.fs_unlink(path)
794-
if not success then
795-
return log.error("Could not remove file: " .. path)
796-
end
797-
clear_buffer(path)
798-
else
799-
-- first try using native system commands, which are recursive
800-
local success = false
801-
if utils.is_windows then
802-
local delete_ok, result =
803-
utils.execute_command({ "cmd.exe", "/c", "rmdir", "/s", "/q", vim.fn.shellescape(path) })
804-
if not delete_ok then
805-
log.debug("Could not delete directory '", path, "' with rmdir: ", result)
806-
else
807-
log.info("Deleted directory ", path)
808-
success = true
809-
end
810-
else
811-
local delete_ok, result = utils.execute_command({ "rm", "-Rf", path })
812-
if not delete_ok then
813-
log.debug("Could not delete directory '", path, "' with rm: ", result)
814-
else
815-
log.info("Deleted directory ", path)
816-
success = true
817-
end
818-
end
819-
820-
-- Fallback to using libuv if native commands fail
821-
if not success then
822-
success = delete_dir(path)
823-
if not success then
824-
return log.error("Could not remove directory: " .. path)
825-
end
826-
end
792+
local success, err = trash.trash({ path })
793+
if not success then
794+
log.error("Could not trash " .. path, err)
795+
return
827796
end
797+
828798
complete()
829799
end
830800

831-
if noconfirm then
832-
do_delete()
833-
else
834-
local msg = string.format("Are you sure you want to delete '%s'?", name)
835-
if children_count > 0 then
836-
msg = ("WARNING: Dir has %s %s! %s"):format(
837-
children_count,
838-
children_count == 1 and "child" or "children",
839-
msg
840-
)
841-
end
842-
inputs.confirm(msg, function(confirmed)
843-
if confirmed then
844-
do_delete()
845-
end
846-
end)
801+
local msg = string.format("Are you sure you want to trash '%s'?", name)
802+
if children_count > 0 then
803+
msg = ("WARNING: Dir has %s %s! %s"):format(
804+
children_count,
805+
children_count == 1 and "child" or "children",
806+
msg
807+
)
847808
end
809+
inputs.confirm(msg, function(confirmed)
810+
if confirmed then
811+
do_trash()
812+
end
813+
end)
848814
end
849815

850-
---@param paths_to_delete string[]
816+
---@param paths_to_trash string[]
851817
---@param callback fun(path)?
852-
M.trash_nodes = function(paths_to_delete, callback)
853-
local msg = "Are you sure you want to delete " .. #paths_to_delete .. " items?"
818+
M.trash_nodes = function(paths_to_trash, callback)
819+
local msg = "Are you sure you want to delete " .. #paths_to_trash .. " items?"
854820
inputs.confirm(msg, function(confirmed)
855821
if not confirmed then
856822
return
857823
end
858824

859-
for _, path in ipairs(paths_to_delete) do
860-
M.trash_node(path, nil, true)
825+
local success, err = trash.trash(paths_to_trash)
826+
if not success then
827+
log.error(err)
861828
end
862829

863830
if callback then
864831
vim.schedule(function()
865-
callback(paths_to_delete[#paths_to_delete])
832+
callback(paths_to_trash[#paths_to_trash])
866833
end)
867834
end
868835
end)

lua/neo-tree/sources/filesystem/lib/trash-freedesktop.lua

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ local function update_trash_size_cache(trash_dir, files_dir, info_dir)
9999
for line in f:lines() do
100100
local size, mtime, name = line:match("(%d+) (%d+) (.+)")
101101
if size and mtime and name then
102-
hash[name] = {
102+
hash[vim.uri_decode(name, "rfc2396")] = {
103+
103104
size = tonumber(size),
104105
mtime = tonumber(mtime),
105106
seen = false,
@@ -151,24 +152,29 @@ local function update_trash_size_cache(trash_dir, files_dir, info_dir)
151152

152153
-- 3. Write out hash back to temporary directorysizes file
153154
local out = io.open(tmp_cache_path, "w")
154-
if out then
155-
for name, data in pairs(hash) do
156-
if data.seen then
157-
out:write(string.format("%d %d %s\n", data.size, data.mtime, name))
158-
end
155+
if not out then
156+
return nil, "Could not update directorysizes file"
157+
end
158+
for name, data in pairs(hash) do
159+
if data.seen then
160+
out:write(string.format("%d %d %s\n", data.size, data.mtime, name))
159161
end
160-
out:close()
162+
end
163+
out:close()
161164

162-
-- 4. Atomic rename into place
163-
local success, err = uv.fs_rename(tmp_cache_path, cache_file_path)
164-
if not success then
165-
return nil, "Failed to update cache file: " .. (err or "unknown error")
166-
end
165+
-- 4. Atomic rename into place
166+
local success, err = uv.fs_rename(tmp_cache_path, cache_file_path)
167+
if not success then
168+
return nil, "Failed to update cache file: " .. (err or "unknown error")
167169
end
168170

169171
return total_size
170172
end
171173

174+
local function restorer(paths)
175+
return function(paths) end
176+
end
177+
---@type neotree.trash.FunctionGenerator
172178
return function(paths)
173179
if utils.is_windows then
174180
log.warn("Freedesktop trash module does not support Windows.")
@@ -180,11 +186,17 @@ return function(paths)
180186
local setup = ensure_writable_dir(trash_dir)
181187
and ensure_writable_dir(trash_files_dir)
182188
and ensure_writable_dir(trash_info_dir)
189+
183190
if not setup then
184191
return nil
185192
end
193+
194+
if vim.fn.has("nvim-0.10") == 0 then
195+
-- Requires neovim 0.10 for vim.uri module
196+
return nil
197+
end
186198
-- Roughly check that all of these are on the same device.
187-
-- This rough check should be fine because if one of the paths contains a mountpoint then the move will fail anyways.
199+
-- This check should be fine because if one of the paths contains a mountpoint then the move will fail anyways.
188200
local trash_dir_dev = get_dev(trash_dir)
189201
if get_dev(trash_files_dir) ~= trash_dir_dev or get_dev(trash_info_dir) ~= trash_dir_dev then
190202
log.at.warn.format(
@@ -206,7 +218,6 @@ return function(paths)
206218
return nil
207219
end
208220
end
209-
210221
return function()
211222
for i, path in ipairs(paths) do
212223
local _, filename = utils.split_path(path)
@@ -225,7 +236,7 @@ return function(paths)
225236
Path=%s
226237
DeletionDate=%s"
227238
]],
228-
path,
239+
vim.uri_encode(path, "rfc2396"),
229240
os.date("%Y%m%dT%H:%M:%S")
230241
)
231242

@@ -245,7 +256,7 @@ DeletionDate=%s"
245256
return false, "Failed to move file to trash: " .. (move_err or "unknown error")
246257
end
247258
end
248-
update_trash_size_cache(trash_dir, trash_files_dir, trash_info_dir)
259+
assert(update_trash_size_cache(trash_dir, trash_files_dir, trash_info_dir))
249260

250261
return true
251262
end

lua/neo-tree/sources/filesystem/lib/trash.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,7 @@ M.trash = function(paths)
188188
return false, "No trash commands or functions worked."
189189
end
190190

191+
---@param paths string The paths in the actual trash
192+
M.restore = function(paths) end
193+
191194
return M

0 commit comments

Comments
 (0)