diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 67d462f..ba64860 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -410,6 +410,11 @@ end ---@param opts? oil.CloseOpts M.close = function(opts) opts = opts or {} + local mode = vim.api.nvim_get_mode().mode + if mode:match("^[vVsS\22\19]") or mode:match("^no") then + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, true, true), "n", false) + return + end -- If we're in a floating oil window, close it and try to restore focus to the original window if vim.w.is_oil_win then local original_winid = vim.w.oil_original_win diff --git a/tests/close_spec.lua b/tests/close_spec.lua new file mode 100644 index 0000000..05f1d1a --- /dev/null +++ b/tests/close_spec.lua @@ -0,0 +1,49 @@ +require("plenary.async").tests.add_to_env() +local oil = require("oil") +local test_util = require("tests.test_util") + +a.describe("close", function() + a.before_each(function() + test_util.reset_editor() + end) + a.after_each(function() + test_util.reset_editor() + end) + + a.it("does not close buffer from visual mode", function() + oil.open() + test_util.wait_for_autocmd({ "User", pattern = "OilEnter" }) + assert.equals("oil", vim.bo.filetype) + test_util.feedkeys({ "V" }, 10) + oil.close() + assert.equals("oil", vim.bo.filetype) + test_util.feedkeys({ "" }, 10) + end) + + a.it("does not close buffer from operator-pending mode", function() + oil.open() + test_util.wait_for_autocmd({ "User", pattern = "OilEnter" }) + assert.equals("oil", vim.bo.filetype) + vim.api.nvim_feedkeys("d", "n", false) + a.util.sleep(20) + local mode = vim.api.nvim_get_mode().mode + if mode:match("^no") then + oil.close() + assert.equals("oil", vim.bo.filetype) + end + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes("", true, true, true), + "n", + false + ) + a.util.sleep(20) + end) + + a.it("closes buffer from normal mode", function() + oil.open() + test_util.wait_for_autocmd({ "User", pattern = "OilEnter" }) + assert.equals("oil", vim.bo.filetype) + oil.close() + assert.not_equals("oil", vim.bo.filetype) + end) +end)