From 165d57e9309e96c23529f6750caacaa3615938d1 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 18 Sep 2023 18:04:16 -0700 Subject: [PATCH] feat: :Oil --trash can open specific trash directories --- doc/oil.txt | 4 ++-- lua/oil/init.lua | 21 ++++++++++++++++----- scripts/generate.py | 2 +- tests/url_spec.lua | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/doc/oil.txt b/doc/oil.txt index b0e78a24..9174b76c 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -403,7 +403,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* @@ -462,7 +462,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 diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 8b7a8dd4..5ff2eabb 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -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") @@ -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") @@ -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 @@ -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 @@ -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" }) diff --git a/scripts/generate.py b/scripts/generate.py index dabe9bf7..114af0b5 100755 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -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 diff --git a/tests/url_spec.lua b/tests/url_spec.lua index d9ecb309..32d801eb 100644 --- a/tests/url_spec.lua +++ b/tests/url_spec.lua @@ -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,