Skip to content

Commit 2d4759f

Browse files
committed
add trash
1 parent fa61e06 commit 2d4759f

File tree

9 files changed

+342
-40
lines changed

9 files changed

+342
-40
lines changed

lua/neo-tree/defaults.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ local config = {
6060
sort_function = nil , -- uses a custom function for sorting files and directories in the tree
6161
use_popups_for_input = true, -- If false, inputs will use vim.ui.input() instead of custom floats.
6262
use_default_mappings = true,
63+
trash = {
64+
cmd = nil -- by default: powershell script on windows, `trash` or `osascript` on macOS, and `gio trash` or `trash` (like trash-cli) on other Unixes
65+
},
6366
-- source_selector provides clickable tabs to switch between sources.
6467
source_selector = {
6568
winbar = false, -- toggle to show selector on winbar
@@ -451,6 +454,8 @@ local config = {
451454
},
452455
["A"] = "add_directory", -- also accepts the config.show_path and config.insert_as options.
453456
["d"] = "delete",
457+
-- ["d"] = "trash",
458+
["T"] = "trash",
454459
["r"] = "rename",
455460
["y"] = "copy_to_clipboard",
456461
["x"] = "cut_to_clipboard",

lua/neo-tree/health/init.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ function M.check_config(config)
5959
function(cfg)
6060
---@class neotree.health.Validator.Generators
6161
local v = {
62+
---@generic T
63+
---@param validator neotree.health.Validator<T>
64+
---@return fun(arr: T[])
6265
array = function(validator)
6366
---@generic T
6467
---@param arr T[]
@@ -177,6 +180,16 @@ function M.check_config(config)
177180
validate("sort_function", cfg.sort_function, "function", true)
178181
validate("use_popups_for_input", cfg.use_popups_for_input, "boolean")
179182
validate("use_default_mappings", cfg.use_default_mappings, "boolean")
183+
validate("trash", cfg.trash, function(trash)
184+
validate("cmd", trash.cmd, function(cmd)
185+
if type(cmd) == "function" then
186+
local cmd_output = cmd({ "neotree/test/update" })
187+
validate("<command output>", cmd_output, v.array("string"), true)
188+
elseif type(cmd) == "table" then
189+
v.array("string")(cmd)
190+
end
191+
end, true)
192+
end)
180193
validate("source_selector", cfg.source_selector, function(ss)
181194
validate("winbar", ss.winbar, "boolean")
182195
validate("statusline", ss.statusline, "boolean")

lua/neo-tree/log.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ log_maker.new = function(config)
100100
or table.concat({ config.plugin_short, prefix }, " ")
101101

102102
local title_opts = { title = config.plugin_short }
103+
103104
---@param message string
104105
---@param level vim.log.levels
105106
local notify = vim.schedule_wrap(function(message, level)
@@ -205,9 +206,7 @@ log_maker.new = function(config)
205206

206207
-- Output to console
207208
if config.use_console and can_log_to_console then
208-
vim.schedule(function()
209-
notify(msg, log_level)
210-
end)
209+
notify(msg, log_level)
211210
end
212211
end
213212
end
@@ -262,6 +261,9 @@ log_maker.new = function(config)
262261
log.error = logfunc(Levels.ERROR, make_string)
263262
---Unused, kept around for compatibility at the moment. Remove in v4.0.
264263
log.fatal = logfunc(Levels.FATAL, make_string)
264+
265+
log.notify = notify
266+
log.levels = Levels
265267
-- tree-sitter queries recognize any .format and highlight it w/ string.format highlights
266268
---@type table<string, { format: fun(fmt: string?, ...: any) }>
267269
log.at = {
@@ -374,8 +376,11 @@ log_maker.new = function(config)
374376
else
375377
errmsg = "assertion failed!"
376378
end
379+
local old = config.use_console
380+
config.use_console = false
377381
log.error(errmsg)
378-
return assert(v, errmsg)
382+
config.use_console = old
383+
error(errmsg, 2)
379384
end
380385

381386
---@param context string

lua/neo-tree/sources/common/commands.lua

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ M.delete = function(state, callback)
704704
fs_actions.delete_node(node.path, callback)
705705
end
706706

707-
---@param callback function
707+
---@param callback fun(path: string)
708708
---@type neotree.TreeCommandVisual
709709
M.delete_visual = function(state, selected_nodes, callback)
710710
local paths_to_delete = {}
@@ -725,6 +725,44 @@ M.delete_visual = function(state, selected_nodes, callback)
725725
fs_actions.delete_nodes(paths_to_delete, callback)
726726
end
727727

728+
M.trash = function(state)
729+
local node = assert(state.tree:get_node())
730+
if node.type ~= "file" and node.type ~= "directory" then
731+
log.warn("The `trash` command can only be used on files and directories")
732+
return
733+
end
734+
if node:get_depth() == 1 then
735+
log.error(
736+
"Will not trash root node "
737+
.. node.path
738+
.. ", please back out of the current directory if you want to trash the root node."
739+
)
740+
return
741+
end
742+
fs_actions.trash_node(node.path)
743+
end
744+
745+
---@param callback fun(path: string)
746+
---@type neotree.TreeCommandVisual
747+
M.trash_visual = function(state, selected_nodes, callback)
748+
local paths_to_trash = {}
749+
for _, node_to_trash in pairs(selected_nodes) do
750+
if node_to_trash:get_depth() == 1 then
751+
log.error(
752+
"Will not trash root node "
753+
.. node_to_trash.path
754+
.. ", please back out of the current directory if you want to trash the root node."
755+
)
756+
return
757+
end
758+
759+
if node_to_trash.type == "file" or node_to_trash.type == "directory" then
760+
table.insert(paths_to_trash, node_to_trash.path)
761+
end
762+
end
763+
fs_actions.trash_nodes(paths_to_trash, callback)
764+
end
765+
728766
M.preview = function(state)
729767
Preview.show(state)
730768
end

lua/neo-tree/sources/common/file-items.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ local function create_item(context, path, _type, bufnr)
199199
if item.type == "link" then
200200
---@cast item neotree.FileItem.Link
201201
item.is_link = true
202-
item.link_to = uv.fs_readlink(path)
202+
item.link_to = uv.fs_realpath(path)
203203
if item.link_to then
204204
local link_to_stat = uv.fs_stat(item.path)
205205
if link_to_stat then

0 commit comments

Comments
 (0)