From d8a1e7ca4e599c43dda1849a66b19d9fbff12310 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Sun, 8 Jan 2023 22:12:17 -0800 Subject: [PATCH] fix: preserve alternate buffer when using floating window (#20) --- lua/oil/init.lua | 4 +++- tests/altbuf_spec.lua | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index d575181..68894b8 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -388,8 +388,10 @@ M.select = function(opts) vertical = opts.vertical, horizontal = opts.horizontal, split = opts.split, - keepalt = true, } + if vim.tbl_isempty(mods) then + mods = nil + end local cmd = opts.split and "split" or "edit" vim.cmd({ cmd = cmd, diff --git a/tests/altbuf_spec.lua b/tests/altbuf_spec.lua index 05726a9..d68ebbf 100644 --- a/tests/altbuf_spec.lua +++ b/tests/altbuf_spec.lua @@ -46,4 +46,35 @@ describe("Alternate buffer", function() vim.cmd.edit({ args = { "bar" } }) assert.equals("foo", vim.fn.expand("#")) end) + + describe("floating window", function() + it("sets previous buffer as alternate", function() + vim.cmd.edit({ args = { "foo" } }) + oil.open_float() + -- This is lazy, but testing the actual select logic is more difficult. We can simply + -- replicated it by closing the current window and then doing the edit + vim.api.nvim_win_close(0, true) + vim.cmd.edit({ args = { "bar" } }) + assert.equals("foo", vim.fn.expand("#")) + end) + + it("preserves alternate buffer if editing the same file", function() + vim.cmd.edit({ args = { "foo" } }) + vim.cmd.edit({ args = { "bar" } }) + oil.open_float() + -- This is lazy, but testing the actual select logic is more difficult. We can simply + -- replicated it by closing the current window and then doing the edit + vim.api.nvim_win_close(0, true) + vim.cmd.edit({ args = { "bar" } }) + assert.equals("foo", vim.fn.expand("#")) + end) + + it("preserves alternate buffer if discarding changes", function() + vim.cmd.edit({ args = { "foo" } }) + vim.cmd.edit({ args = { "bar" } }) + oil.open_float() + oil.close() + assert.equals("foo", vim.fn.expand("#")) + end) + end) end)