Skip to content

Commit

Permalink
fix: set alternate buffer when inside oil (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Feb 24, 2023
1 parent 647c2d3 commit f1131b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,12 @@ M.setup = function(opts)
local scheme = util.parse_url(bufname)
if scheme and config.adapters[scheme] then
require("oil.view").maybe_set_cursor()
-- While we are in an oil buffer, set the alternate file to the buffer we were in prior to
-- opening oil
local has_orig, orig_buffer = pcall(vim.api.nvim_win_get_var, 0, "oil_original_buffer")
if has_orig and vim.api.nvim_buf_is_valid(orig_buffer) then
vim.fn.setreg("#", orig_buffer)
end
elseif vim.fn.isdirectory(bufname) == 0 then
-- Only run this logic if we are *not* in an oil buffer (and it's not a directory, which
-- will be replaced by an oil:// url)
Expand Down
11 changes: 11 additions & 0 deletions tests/altbuf_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ a.describe("Alternate buffer", function()
assert.equals("foo", vim.fn.expand("#"))
end)

a.it("sets previous buffer as alternate when inside oil buffer", function()
vim.cmd.edit({ args = { "foo" } })
oil.open()
test_util.wait_for_autocmd("BufReadPost")
assert.equals("foo", vim.fn.expand("#"))
vim.cmd.edit({ args = { "bar" } })
assert.equals("foo", vim.fn.expand("#"))
oil.open()
assert.equals("bar", vim.fn.expand("#"))
end)

a.describe("floating window", function()
a.it("sets previous buffer as alternate", function()
vim.cmd.edit({ args = { "foo" } })
Expand Down

0 comments on commit f1131b5

Please sign in to comment.