From 81cc9c3f62ddbef3687931d119e505643496fa0a Mon Sep 17 00:00:00 2001 From: cdmill <115658917+cdmill@users.noreply.github.com> Date: Wed, 20 Nov 2024 22:06:09 -0700 Subject: [PATCH] feat: option to quite vim if oil is closed as last buffer (#491) * feat: auto-quit vim if oil is closed as last buffer * rename auto_close_vim to auto_close_last_buffer * rework actions.close to be more like actions.cd * fix: configure close action correctly * add type annotation, future proofing * fix: typo * fix: typo * refactor: better type annotations and backwards compatibility --------- Co-authored-by: Steven Arcangeli <506791+stevearc@users.noreply.github.com> --- lua/oil/actions.lua | 11 ++++++++++- lua/oil/init.lua | 16 +++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lua/oil/actions.lua b/lua/oil/actions.lua index 50a266a..8dabe40 100644 --- a/lua/oil/actions.lua +++ b/lua/oil/actions.lua @@ -143,7 +143,16 @@ M.parent = { M.close = { desc = "Close oil and restore original buffer", - callback = oil.close, + callback = function(opts) + opts = opts or {} + oil.close(opts) + end, + parameters = { + exit_if_last_buf = { + type = "boolean", + desc = "Exit vim if oil is closed as the last buffer", + }, + }, } ---@param cmd string diff --git a/lua/oil/init.lua b/lua/oil/init.lua index d673484..7645f35 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -379,8 +379,13 @@ M.open = function(dir) update_preview_window() end +---@class oil.CloseOpts +---@field exit_if_last_buf? boolean Exit vim if this oil buffer is the last open buffer + ---Restore the buffer that was present when oil was opened -M.close = function() +---@param opts? oil.CloseOpts +M.close = function(opts) + opts = opts or {} -- 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 @@ -403,9 +408,14 @@ M.close = function() -- buffer first local oilbuf = vim.api.nvim_get_current_buf() ok = pcall(vim.cmd.bprev) + -- If `bprev` failed, there are no buffers open if not ok then - -- If `bprev` failed, there are no buffers open so we should create a new one with enew - vim.cmd.enew() + -- either exit or create a new blank buffer + if opts.exit_if_last_buf then + vim.cmd.quit() + else + vim.cmd.enew() + end end vim.api.nvim_buf_delete(oilbuf, { force = true }) end