From a4651236594cd7717c9b75c43ede0ed5fd4a7dc9 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Wed, 21 Jun 2023 08:36:51 -0700 Subject: [PATCH] feat: oil.select can close oil buffer afterwards (#121) --- lua/oil/init.lua | 14 ++++++++++++++ tests/select_spec.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 77fd10b..0459544 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -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 diff --git a/tests/select_spec.lua b/tests/select_spec.lua index 80196a2..9a33490 100644 --- a/tests/select_spec.lua +++ b/tests/select_spec.lua @@ -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)