Skip to content

Commit 85b5486

Browse files
committed
feat: :Oil --trash can open specific trash directories
1 parent 59d5a18 commit 85b5486

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

doc/oil.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ toggle_hidden *actions.toggle_hidde
407407
Toggle hidden files and directories
408408

409409
toggle_trash *actions.toggle_trash*
410-
Jump back and forth the trash for the current directory
410+
Jump to and from the trash for the current directory
411411

412412
--------------------------------------------------------------------------------
413413
HIGHLIGHTS *oil-highlights*
@@ -466,7 +466,7 @@ Oil has built-in support for using the system trash. When
466466
`delete_to_trash = true`, any deleted files will be sent to the trash instead
467467
of being permanently deleted. You can browse the trash for a directory using
468468
the `toggle_trash` action (bound to `g\` by default). You can view all files
469-
in the trash with `:Oil --trash`.
469+
in the trash with `:Oil --trash /`.
470470

471471
To restore files, simply delete them from the trash and put them in the desired
472472
destination, the same as any other file operation. If you delete files from the

lua/oil/init.lua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,13 @@ end
150150
---Get the oil url for a given directory
151151
---@private
152152
---@param dir nil|string When nil, use the cwd
153-
---@return nil|string The parent url
153+
---@param use_oil_parent nil|boolean If in an oil buffer, return the parent (default true)
154+
---@return string The parent url
154155
---@return nil|string The basename (if present) of the file/dir we were just in
155-
M.get_url_for_path = function(dir)
156+
M.get_url_for_path = function(dir, use_oil_parent)
157+
if use_oil_parent == nil then
158+
use_oil_parent = true
159+
end
156160
local config = require("oil.config")
157161
local fs = require("oil.fs")
158162
local util = require("oil.util")
@@ -169,15 +173,16 @@ M.get_url_for_path = function(dir)
169173
return config.adapter_to_scheme.files .. path
170174
else
171175
local bufname = vim.api.nvim_buf_get_name(0)
172-
return M.get_buffer_parent_url(bufname)
176+
return M.get_buffer_parent_url(bufname, use_oil_parent)
173177
end
174178
end
175179

176180
---@private
177181
---@param bufname string
182+
---@param use_oil_parent boolean If in an oil buffer, return the parent
178183
---@return string
179184
---@return nil|string
180-
M.get_buffer_parent_url = function(bufname)
185+
M.get_buffer_parent_url = function(bufname, use_oil_parent)
181186
local config = require("oil.config")
182187
local fs = require("oil.fs")
183188
local pathutil = require("oil.pathutil")
@@ -202,6 +207,9 @@ M.get_buffer_parent_url = function(bufname)
202207
return config.adapter_to_scheme.files .. util.addslash(path)
203208
end
204209

210+
if not use_oil_parent then
211+
return bufname
212+
end
205213
local adapter = config.get_adapter_by_scheme(scheme)
206214
local parent_url
207215
if adapter and adapter.get_parent then
@@ -834,6 +842,7 @@ M.setup = function(opts)
834842
config.setup(opts)
835843
set_colors()
836844
vim.api.nvim_create_user_command("Oil", function(args)
845+
local util = require("oil.util")
837846
if args.smods.tab == 1 then
838847
vim.cmd.tabnew()
839848
end
@@ -864,7 +873,9 @@ M.setup = function(opts)
864873
local method = float and "open_float" or "open"
865874
local path = args.fargs[1]
866875
if trash then
867-
path = "oil-trash:///"
876+
local url = M.get_url_for_path(path, false)
877+
local _, new_path = util.parse_url(url)
878+
path = "oil-trash://" .. new_path
868879
end
869880
M[method](path)
870881
end, { desc = "Open oil file browser on a directory", nargs = "*", complete = "dir" })

scripts/generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def get_trash_vimdoc() -> "VimdocSection":
240240
`delete_to_trash = true`, any deleted files will be sent to the trash instead
241241
of being permanently deleted. You can browse the trash for a directory using
242242
the `toggle_trash` action (bound to `g\\` by default). You can view all files
243-
in the trash with `:Oil --trash`.
243+
in the trash with `:Oil --trash /`.
244244
245245
To restore files, simply delete them from the trash and put them in the desired
246246
destination, the same as any other file operation. If you delete files from the

tests/url_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("url", function()
1313
}
1414
for _, case in ipairs(cases) do
1515
local input, expected, expected_basename = unpack(case)
16-
local output, basename = oil.get_buffer_parent_url(input)
16+
local output, basename = oil.get_buffer_parent_url(input, true)
1717
assert.equals(expected, output, string.format('Parent url for path "%s" failed', input))
1818
assert.equals(
1919
expected_basename,

0 commit comments

Comments
 (0)