Skip to content

Commit

Permalink
feat: focus_on_close = 'previous' (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iron-E authored May 12, 2023
1 parent 4c549cd commit cdb43e6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
10 changes: 7 additions & 3 deletions doc/barbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ~
Expand Down
13 changes: 12 additions & 1 deletion lua/barbar/bbye.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/barbar/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit cdb43e6

Please sign in to comment.