feat: oil.select can close oil buffer afterwards (#121)

This commit is contained in:
Steven Arcangeli 2023-06-21 08:36:51 -07:00
parent a82503cd79
commit a465123659
2 changed files with 55 additions and 0 deletions

View file

@ -402,6 +402,7 @@ end
--- split "aboveleft"|"belowright"|"topleft"|"botright" Split modifier
--- preview boolean Open the buffer in a preview window
--- tab boolean Open the buffer in a new tab
--- close boolean Close the original oil buffer once selection is made
M.select = function(opts)
local cache = require("oil.cache")
local config = require("oil.config")
@ -415,6 +416,9 @@ M.select = function(opts)
if opts.tab and (opts.preview or opts.split) then
error("Cannot set preview or split when tab = true")
end
if opts.close and opts.preview then
error("Cannot use close=true with preview=true")
end
local util = require("oil.util")
if util.is_floating_win() and opts.preview then
vim.notify("oil preview doesn't work in a floating window", vim.log.levels.ERROR)
@ -550,6 +554,16 @@ M.select = function(opts)
opts.vertical = true
end
end
if
opts.close
and vim.api.nvim_win_is_valid(prev_win)
and prev_win ~= vim.api.nvim_get_current_win()
then
vim.api.nvim_win_call(prev_win, function()
M.close()
end)
end
end
---@param bufnr integer

View file

@ -58,4 +58,45 @@ a.describe("oil select", function()
assert.equals(3, #vim.api.nvim_tabpage_list_wins(0))
assert.not_equals(winid, vim.api.nvim_get_current_win())
end)
a.describe("close after open", function()
a.it("same window", function()
vim.cmd.edit({ args = { "foo" } })
local bufnr = vim.api.nvim_get_current_buf()
oil.open()
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
-- Go to the bottom, so the cursor is not on a directory
vim.cmd.normal({ args = { "G" } })
oil.select({ close = true })
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
-- This one we actually don't expect the buffer to be the same as the initial buffer, because
-- we opened a file
assert.not_equals(bufnr, vim.api.nvim_get_current_buf())
assert.not_equals("oil", vim.bo.filetype)
end)
a.it("split", function()
vim.cmd.edit({ args = { "foo" } })
local bufnr = vim.api.nvim_get_current_buf()
local winid = vim.api.nvim_get_current_win()
oil.open()
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
oil.select({ vertical = true, close = true })
assert.equals(2, #vim.api.nvim_tabpage_list_wins(0))
assert.equals(bufnr, vim.api.nvim_win_get_buf(winid))
end)
a.it("tab", function()
vim.cmd.edit({ args = { "foo" } })
local bufnr = vim.api.nvim_get_current_buf()
local tabpage = vim.api.nvim_get_current_tabpage()
oil.open()
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
oil.select({ tab = true, close = true })
assert.equals(1, #vim.api.nvim_tabpage_list_wins(0))
assert.equals(2, #vim.api.nvim_list_tabpages())
vim.api.nvim_set_current_tabpage(tabpage)
assert.equals(bufnr, vim.api.nvim_get_current_buf())
end)
end)
end)