Skip to content

Commit

Permalink
fix: set alternate when using floating windows (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
iovis committed Dec 9, 2024
1 parent 3c2de37 commit f6314c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,25 @@ M.select = function(opts, callback)
if callback then
callback(err)
end

-- Set alternate for buffer
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
if vim.api.nvim_get_current_buf() ~= orig_buffer then
-- If we are editing a new file after navigating around oil, set the alternate buffer
-- to be the last buffer we were in before opening oil
vim.fn.setreg("#", orig_buffer)
else
-- If we are editing the same buffer that we started oil from, set the alternate to be
-- what it was before we opened oil
local has_orig_alt, alt_buffer =
pcall(vim.api.nvim_win_get_var, 0, "oil_original_alternate")
if has_orig_alt and vim.api.nvim_buf_is_valid(alt_buffer) then
vim.fn.setreg("#", alt_buffer)
end
end
end
end
if not opts.split and (opts.horizontal or opts.vertical) then
if opts.horizontal then
Expand Down
12 changes: 12 additions & 0 deletions tests/altbuf_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,17 @@ a.describe("Alternate buffer", function()
oil.close()
assert.equals("foo", vim.fn.expand("#"))
end)

a.it("preserves alternate when traversing to a new file", function()
vim.cmd.edit({ args = { "foo" } })
oil.open_float()
test_util.wait_for_autocmd({ "User", pattern = "OilEnter" })
assert.equals("foo", vim.fn.expand("#"))
local last_item = vim.fn.line("$")
vim.api.nvim_win_set_cursor(0, { last_item, 5 })
oil.select()
test_util.wait_for_autocmd("BufEnter")
assert.equals("foo", vim.fn.expand("#"))
end)
end)
end)

0 comments on commit f6314c6

Please sign in to comment.