Skip to content

Commit

Permalink
feat(render): scroll lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Iron-E committed Mar 30, 2023
1 parent 682c213 commit 37f1cef
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions lua/barbar/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ local function slice_groups_left(groups, width)

local new_groups = {}

for _, group in ipairs(utils.list_reverse(groups)) do
for i = #groups, 1, -1 do
local group = groups[i]
local text_width = strwidth(group.text)
accumulated_width = accumulated_width + text_width

if accumulated_width >= width then
local length = text_width - (accumulated_width - width)
local start = text_width - length
local new_group = {hl = group.hl, text = strcharpart(group.text, start, length)}
table_insert(new_groups, 1, new_group)
table_insert(new_groups, 1, {hl = group.hl, text = strcharpart(group.text, start, length)})
break
end

Expand Down Expand Up @@ -526,13 +526,15 @@ local function generate_tabline(bufnrs, refocus)
local current_buffer_position = 0
local items = {}

local scroll_lock = config.options.scroll_lock
local scroll_locked = {}

for i, bufnr in ipairs(bufnrs) do
local activity = Buffer.activities[Buffer.get_activity(bufnr)]
local modified = buf_get_option(bufnr, 'modified')

local buffer_data = state.get_buffer_data(bufnr)
local buffer_hl = hl_tabline('Buffer' .. activity .. (
buf_get_option(bufnr, 'modified') and 'Mod' or ''
))
local buffer_hl = hl_tabline('Buffer' .. activity .. (modified and 'Mod' or ''))
local buffer_name = buffer_data.name or '[no name]'

buffer_data.real_width = Layout.calculate_width(layout.base_widths[i], layout.padding_width)
Expand All @@ -552,15 +554,15 @@ local function generate_tabline(bufnrs, refocus)
local buffer_index = {hl = '', text = ''}
if icons_option.buffer_index then
buffer_index.hl = hl_tabline('Buffer' .. activity .. 'Index')
buffer_index.text = tostring(i) .. ' '
buffer_index.text = i .. ' '
end

--- The buffer number
--- @type barbar.render.group
local buffer_number = {hl = '', text = ''}
if icons_option.buffer_number then
buffer_number.hl = hl_tabline('Buffer' .. activity .. 'Number')
buffer_number.text = tostring(bufnr) .. ' '
buffer_number.text = bufnr .. ' '
end

local button = icons_option.button or ''
Expand Down Expand Up @@ -650,8 +652,15 @@ local function generate_tabline(bufnrs, refocus)

current_buffer_position = current_buffer_position + item.width

if scroll_lock[activity:lower()] or
(modified and scroll_lock.modified) or
(buffer_data.pinned and scroll_lock.pinned)
then
table_insert(scroll_locked, item)
end

local scroll_current = min(scroll.current, max_scroll)
if current_buffer_position < scroll_current then
if current_buffer_position < scroll_current then
goto continue -- HACK: there is no `continue` keyword
end

Expand Down Expand Up @@ -691,19 +700,30 @@ local function generate_tabline(bufnrs, refocus)

for _, item in ipairs(items) do
bufferline_groups = groups_insert(bufferline_groups, item.position, item.groups)

local scroll_locked_idx = utils.index_of(scroll_locked, item)
if scroll_locked_idx then
table.remove(scroll_locked, scroll_locked_idx)
end
end

-- Crop to scroll region

local scroll_current = min(scroll.current, max_scroll)
local buffers_end = layout.actual_width - scroll_current
do
local scroll_current = min(scroll.current, max_scroll)
local buffers_end = layout.actual_width - scroll_current

if buffers_end > layout.buffers_width then
bufferline_groups = slice_groups_right(bufferline_groups, scroll_current + layout.buffers_width)
if buffers_end > layout.buffers_width then
bufferline_groups = slice_groups_right(bufferline_groups, scroll_current + layout.buffers_width)
end

if scroll_current > 0 then
bufferline_groups = slice_groups_left(bufferline_groups, layout.buffers_width)
end
end

if scroll_current > 0 then
bufferline_groups = slice_groups_left(bufferline_groups, layout.buffers_width)
for i = #scroll_locked, 1, -1 do
bufferline_groups = groups_insert(bufferline_groups, 0, scroll_locked[i].groups)
end

result = result ..
Expand Down

0 comments on commit 37f1cef

Please sign in to comment.