From cdb43e65249e9c6ff53d86fbda0fde43dc441317 Mon Sep 17 00:00:00 2001 From: Iron-E <36409591+Iron-E@users.noreply.github.com> Date: Fri, 12 May 2023 18:06:37 +0000 Subject: [PATCH] feat: `focus_on_close = 'previous'` (#484) --- README.md | 2 +- doc/barbar.txt | 10 +++++++--- lua/barbar/bbye.lua | 13 ++++++++++++- lua/barbar/config.lua | 4 ++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5bc59fcd..b2e6a458 100644 --- a/README.md +++ b/README.md @@ -282,7 +282,7 @@ require'barbar'.setup { exclude_name = {'package.json'}, -- A buffer to this direction will be focused (if it exists) when closing the current buffer. - -- Valid options are 'left' (the default) and 'right' + -- Valid options are 'left' (the default), 'previous', and 'right' focus_on_close = 'left', -- Hide inactive buffers and file extensions. Other options are `alternate`, `current`, and `visible`. diff --git a/doc/barbar.txt b/doc/barbar.txt index d90ca815..5b4a89b8 100644 --- a/doc/barbar.txt +++ b/doc/barbar.txt @@ -176,9 +176,13 @@ exclude_name ~ *barbar-setup.focus_on_close* focus_on_close ~ - `'left'|'right'` (default: `'left'`) - A buffer to this direction will be focused (if it exists) when closing the - current buffer. + `'left'|'previous'|'right'` (default: `'left'`) + The algorithm to use for getting the next buffer after closing the current + one: + + - `'left'`: focus the buffer to the left of the current buffer. + - `'previous'`: focus the previous buffer. + - `'right'`: focus the buffer to the right of the current buffer. *barbar-setup.hide* hide ~ diff --git a/lua/barbar/bbye.lua b/lua/barbar/bbye.lua index 9f539db1..f9bc1f04 100644 --- a/lua/barbar/bbye.lua +++ b/lua/barbar/bbye.lua @@ -74,13 +74,22 @@ local enew = vim.api.nvim_cmd and --- @param force boolean function(force) command("enew" .. (force and '!' or '')) end +--- Get the bufnr that will be focused when the buffer with `closing_number` closes. --- @param closing_number integer --- @return nil|integer bufnr of the buffer to focus local function get_focus_on_close(closing_number) local focus_on_close = config.options.focus_on_close local state_bufnrs = state.buffers - if #state_bufnrs < 1 then -- all of the buffers are excluded or unlisted + if focus_on_close == 'previous' then + local previous = bufnr('#') + if previous > -1 then + return previous + end + end + + -- Edge case: all of the buffers are excluded or unlisted + if #state_bufnrs < 1 then local open_bufnrs = list_bufs() local start, end_, step @@ -104,6 +113,7 @@ local function get_focus_on_close(closing_number) state_bufnrs = list.reverse(state.buffers) end + -- Next, try to get the buffer to focus by "looking" left or right of the current buffer local index = list.index_of(state_bufnrs, closing_number) if index then index = index - 1 @@ -114,6 +124,7 @@ local function get_focus_on_close(closing_number) end end + -- If all else fails, choose the first available listed buffer for _, buffer_number in ipairs(state_bufnrs) do if buffer_number ~= closing_number then return buffer_number diff --git a/lua/barbar/config.lua b/lua/barbar/config.lua index f4eb6db7..243cf735 100644 --- a/lua/barbar/config.lua +++ b/lua/barbar/config.lua @@ -155,7 +155,7 @@ local DEPRECATED_OPTIONS = { --- @field clickable boolean --- @field exclude_ft string[] --- @field exclude_name string[] ---- @field focus_on_close side +--- @field focus_on_close side|'previous' --- @field hide barbar.config.options.hide --- @field highlight_alternate boolean --- @field highlight_inactive_file_icons boolean @@ -272,7 +272,7 @@ function config.setup(options) clickable = true, exclude_ft = {}, exclude_name = {}, - focus_on_close = 'left', + focus_on_close = 'previous', hide = {}, highlight_alternate = false, highlight_inactive_file_icons = false,