From 4e853eabcb002650096ef78f098253fe12ba3d8f Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Fri, 20 Jan 2023 02:56:57 -0800 Subject: [PATCH] fix: alternate buffer preservation (#43) --- lua/oil/init.lua | 6 ++++-- lua/oil/util.lua | 10 ++++++++++ tests/altbuf_spec.lua | 13 +++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 31868c9..c7c6545 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -660,7 +660,8 @@ M.setup = function(opts) group = aug, pattern = "*", callback = function() - if vim.bo.filetype ~= "oil" then + local util = require("oil.util") + if not util.is_oil_bufnr(0) then vim.api.nvim_win_set_var(0, "oil_original_buffer", vim.api.nvim_get_current_buf()) vim.api.nvim_win_set_var(0, "oil_original_alternate", vim.fn.bufnr("#")) end @@ -719,7 +720,8 @@ M.setup = function(opts) pattern = "*", nested = true, callback = function(params) - if vim.bo[params.buf].filetype ~= "oil" or vim.w.oil_did_enter then + local util = require("oil.util") + if not util.is_oil_bufnr(params.buf) or vim.w.oil_did_enter then return end -- This new window is a split off of an oil window. We need to transfer the window diff --git a/lua/oil/util.lua b/lua/oil/util.lua index 75000a8..97cacf9 100644 --- a/lua/oil/util.lua +++ b/lua/oil/util.lua @@ -489,6 +489,16 @@ M.run_in_fullscreen_win = function(bufnr, callback) vim.cmd.close({ count = winnr, mods = { noautocmd = true, emsg_silent = true } }) end +---@param bufnr integer +---@return boolean +M.is_oil_bufnr = function(bufnr) + if vim.bo[bufnr].filetype == "oil" then + return true + end + local scheme = M.parse_url(vim.api.nvim_buf_get_name(bufnr)) + return config.adapters[scheme] or config.adapter_aliases[scheme] +end + ---This is a hack so we don't end up in insert mode after starting a task ---@param prev_mode string The vim mode we were in before opening a terminal M.hack_around_termopen_autocmd = function(prev_mode) diff --git a/tests/altbuf_spec.lua b/tests/altbuf_spec.lua index b73cb88..03846e0 100644 --- a/tests/altbuf_spec.lua +++ b/tests/altbuf_spec.lua @@ -16,6 +16,19 @@ a.describe("Alternate buffer", function() assert.equals("foo", vim.fn.expand("#")) end) + a.it("sets previous buffer as alternate when editing url file", function() + vim.cmd.edit({ args = { "foo" } }) + oil.open() + test_util.wait_for_autocmd("BufReadPost") + local readme = fs.join(vim.fn.getcwd(), "README.md") + vim.cmd.edit({ args = { "oil://" .. fs.os_to_posix_path(readme) } }) + -- We're gonna jump around to 2 different buffers + test_util.wait_for_autocmd("BufEnter") + test_util.wait_for_autocmd("BufEnter") + assert.equals(readme, vim.api.nvim_buf_get_name(0)) + assert.equals("foo", vim.fn.expand("#")) + end) + a.it("sets previous buffer as alternate when editing oil://", function() vim.cmd.edit({ args = { "foo" } }) vim.cmd.edit({ args = { "oil://" .. fs.os_to_posix_path(vim.fn.getcwd()) } })