Skip to content

Commit

Permalink
feat(commands): add option to wrap when moving buffers at ends (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarshgupta137 committed Jun 2, 2023
1 parent 32d74d5 commit da1875c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/bufferline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ The available configuration are:
show_tab_indicators = true | false,
show_duplicate_prefix = true | false, -- whether to show duplicate buffer prefix
persist_buffer_sort = true, -- whether or not custom sorted buffers should persist
move_wraps_at_ends = false, -- whether or not the move command "wraps" at the first or last position
-- can also be a table containing 2 custom separators
-- [focused and unfocused]. eg: { '|', '|' }
separator_style = "slant" | "slope" | "thick" | "thin" | { 'any', 'any' },
Expand Down
31 changes: 29 additions & 2 deletions lua/bufferline/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ function M.get_current_element_index(current_state, opts)
end
end

---@param current_state bufferline.State
---@return number
local get_last_pinned_index = function(current_state)
for index, item in ipairs(current_state.components) do
local element = item:as_element()
if not groups.is_pinned(element) then return index - 1 end
end
return 0
end

--- Move the buffer at index `from_index` (or current index if not specified) to position `to_index`
--- @param to_index number negative indices are accepted (counting from the right instead of the left, e.g. -1 for the last position, -2 for the second-last, etc.)
--- @param from_index number?
Expand All @@ -169,8 +179,25 @@ end

--- @param direction number
function M.move(direction)
local index = M.get_current_element_index(state)
M.move_to(index + direction, index)
local index, element = M.get_current_element_index(state)
local next_index = index + direction
if not config.options.move_wraps_at_ends or not index then return M.move_to(next_index, index) end

local last_pinned_index = get_last_pinned_index(state)
if groups.is_pinned(element) then
if next_index <= 0 then
next_index = last_pinned_index
elseif next_index > last_pinned_index then
next_index = 1
end
else
if next_index <= last_pinned_index then
next_index = #state.components
elseif next_index > #state.components then
next_index = last_pinned_index + 1
end
end
M.move_to(next_index, index)
end

--- @param direction number
Expand Down
1 change: 1 addition & 0 deletions lua/bufferline/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ local function get_defaults()
enforce_regular_tabs = false,
always_show_bufferline = true,
persist_buffer_sort = true,
move_wraps_at_ends = false,
max_prefix_length = 15,
sort_by = "id",
diagnostics = false,
Expand Down
4 changes: 2 additions & 2 deletions lua/bufferline/groups.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ local group_by_name = group_by("name")
local group_by_priority = group_by("priority")

---@param element bufferline.TabElement
local function is_pinned(element) return get_manual_group(element) == PINNED_ID end
function M.is_pinned(element) return get_manual_group(element) == PINNED_ID end

--- Add a buffer to a group manually
---@param group_name string
Expand Down Expand Up @@ -478,7 +478,7 @@ end
function M.toggle_pin()
local _, element = commands.get_current_element_index(state)
if not element then return end
if is_pinned(element) then
if M.is_pinned(element) then
M.remove_element("pinned", element)
else
M.add_element("pinned", element)
Expand Down
1 change: 1 addition & 0 deletions lua/bufferline/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
---@field public enforce_regular_tabs boolean
---@field public always_show_bufferline boolean
---@field public persist_buffer_sort boolean
---@field public move_wraps_at_ends boolean
---@field public max_prefix_length number
---@field public sort_by string
---@field public diagnostics boolean | 'nvim_lsp' | 'coc'
Expand Down

0 comments on commit da1875c

Please sign in to comment.