From b36ba91b7a4d05ee43617383f68cf6ed6fc2f08e Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Sun, 5 Mar 2023 23:57:58 -0800 Subject: [PATCH] fix: another case of incorrect alternate buffers (#60) --- lua/oil/init.lua | 1 + lua/oil/util.lua | 5 +++++ tests/altbuf_spec.lua | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 736ae52..d32ce7d 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -405,6 +405,7 @@ M.select = function(opts) vertical = opts.vertical, horizontal = opts.horizontal, split = opts.split, + keepalt = true, } if vim.tbl_isempty(mods) then mods = nil diff --git a/lua/oil/util.lua b/lua/oil/util.lua index 156aa5b..d5bef4f 100644 --- a/lua/oil/util.lua +++ b/lua/oil/util.lua @@ -102,11 +102,16 @@ M.rename_buffer = function(src_bufnr, dest_buf_name) -- rename logic. The only reason we can't use nvim_buf_set_name on files is because vim will -- think that the new buffer conflicts with the file next time it tries to save. if not vim.loop.fs_stat(dest_buf_name) then + local altbuf = vim.fn.bufnr("#") -- This will fail if the dest buf name already exists local ok = pcall(vim.api.nvim_buf_set_name, src_bufnr, dest_buf_name) if ok then -- Renaming the buffer creates a new buffer with the old name. Find it and delete it. vim.api.nvim_buf_delete(vim.fn.bufadd(bufname), {}) + if altbuf and vim.api.nvim_buf_is_valid(altbuf) then + vim.fn.setreg("#", altbuf) + end + return false end end diff --git a/tests/altbuf_spec.lua b/tests/altbuf_spec.lua index 3c0113f..2b74b0a 100644 --- a/tests/altbuf_spec.lua +++ b/tests/altbuf_spec.lua @@ -81,6 +81,30 @@ a.describe("Alternate buffer", function() assert.equals("bar", vim.fn.expand("#")) end) + a.it("preserves alternate when traversing oil dirs", function() + vim.cmd.edit({ args = { "foo" } }) + oil.open() + test_util.wait_for_autocmd("BufReadPost") + a.util.sleep(10) + assert.equals("foo", vim.fn.expand("#")) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + oil.select() + test_util.wait_for_autocmd("BufReadPost") + assert.equals("foo", vim.fn.expand("#")) + end) + + a.it("preserves alternate when opening preview", function() + vim.cmd.edit({ args = { "foo" } }) + oil.open() + test_util.wait_for_autocmd("BufReadPost") + a.util.sleep(10) + assert.equals("foo", vim.fn.expand("#")) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + oil.select({ preview = true }) + test_util.wait_for_autocmd("BufReadPost") + assert.equals("foo", vim.fn.expand("#")) + end) + a.describe("floating window", function() a.it("sets previous buffer as alternate", function() vim.cmd.edit({ args = { "foo" } })