Skip to content

Commit

Permalink
feat: :Oil --trash can open specific trash directories
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Oct 3, 2023
1 parent f723e89 commit b62f680
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ toggle_hidden *actions.toggle_hidde
Toggle hidden files and directories

toggle_trash *actions.toggle_trash*
Jump back and forth the trash for the current directory
Jump to and from the trash for the current directory

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

To restore files, simply delete them from the trash and put them in the desired
destination, the same as any other file operation. If you delete files from the
Expand Down
21 changes: 16 additions & 5 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,13 @@ end
---Get the oil url for a given directory
---@private
---@param dir nil|string When nil, use the cwd
---@return nil|string The parent url
---@param use_oil_parent nil|boolean If in an oil buffer, return the parent (default true)
---@return string The parent url
---@return nil|string The basename (if present) of the file/dir we were just in
M.get_url_for_path = function(dir)
M.get_url_for_path = function(dir, use_oil_parent)
if use_oil_parent == nil then
use_oil_parent = true
end
local config = require("oil.config")
local fs = require("oil.fs")
local util = require("oil.util")
Expand All @@ -169,15 +173,16 @@ M.get_url_for_path = function(dir)
return config.adapter_to_scheme.files .. path
else
local bufname = vim.api.nvim_buf_get_name(0)
return M.get_buffer_parent_url(bufname)
return M.get_buffer_parent_url(bufname, use_oil_parent)
end
end

---@private
---@param bufname string
---@param use_oil_parent boolean If in an oil buffer, return the parent
---@return string
---@return nil|string
M.get_buffer_parent_url = function(bufname)
M.get_buffer_parent_url = function(bufname, use_oil_parent)
local config = require("oil.config")
local fs = require("oil.fs")
local pathutil = require("oil.pathutil")
Expand All @@ -202,6 +207,9 @@ M.get_buffer_parent_url = function(bufname)
return config.adapter_to_scheme.files .. util.addslash(path)
end

if not use_oil_parent then
return bufname
end
local adapter = config.get_adapter_by_scheme(scheme)
local parent_url
if adapter and adapter.get_parent then
Expand Down Expand Up @@ -834,6 +842,7 @@ M.setup = function(opts)
config.setup(opts)
set_colors()
vim.api.nvim_create_user_command("Oil", function(args)
local util = require("oil.util")
if args.smods.tab == 1 then
vim.cmd.tabnew()
end
Expand Down Expand Up @@ -864,7 +873,9 @@ M.setup = function(opts)
local method = float and "open_float" or "open"
local path = args.fargs[1]
if trash then
path = "oil-trash:///"
local url = M.get_url_for_path(path, false)
local _, new_path = util.parse_url(url)
path = "oil-trash://" .. new_path
end
M[method](path)
end, { desc = "Open oil file browser on a directory", nargs = "*", complete = "dir" })
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def get_trash_vimdoc() -> "VimdocSection":
`delete_to_trash = true`, any deleted files will be sent to the trash instead
of being permanently deleted. You can browse the trash for a directory using
the `toggle_trash` action (bound to `g\\` by default). You can view all files
in the trash with `:Oil --trash`.
in the trash with `:Oil --trash /`.
To restore files, simply delete them from the trash and put them in the desired
destination, the same as any other file operation. If you delete files from the
Expand Down
2 changes: 1 addition & 1 deletion tests/url_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("url", function()
}
for _, case in ipairs(cases) do
local input, expected, expected_basename = unpack(case)
local output, basename = oil.get_buffer_parent_url(input)
local output, basename = oil.get_buffer_parent_url(input, true)
assert.equals(expected, output, string.format('Parent url for path "%s" failed', input))
assert.equals(
expected_basename,
Expand Down

0 comments on commit b62f680

Please sign in to comment.