Skip to content

Commit

Permalink
ref: local mlocal Groups
Browse files Browse the repository at this point in the history
Also creates a clear naming distinction between `group_clumps` and
`groups`.
  • Loading branch information
Iron-E committed Apr 10, 2023
1 parent cdfa74f commit de928aa
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 153 deletions.
6 changes: 3 additions & 3 deletions autoload/bufferline.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ endfunc
"========================

function! bufferline#update(...)
call luaeval("require'barbar.render'.update(_A)", get(a:, 1, v:null))
call luaeval("require'barbar.ui.render'.update(_A)", get(a:, 1, v:null))
endfu

function! bufferline#update_async(...)
call timer_start(get(a:, 2, 1), {-> luaeval("require'barbar.render'.update(_A)", get(a:, 1, v:null))})
call timer_start(get(a:, 2, 1), {-> luaeval("require'barbar.ui.render'.update(_A)", get(a:, 1, v:null))})
endfu

function! bufferline#render(update_names) abort
Expand Down Expand Up @@ -43,7 +43,7 @@ function! bufferline#order_by_window_number()
endfunc

function! bufferline#close(abuf)
call luaeval("require'barbar.render'.close_buffer_animated(_A)", a:abuf)
call luaeval("require'barbar.ui.render'.close_buffer_animated(_A)", a:abuf)
endfunc

function! bufferline#close_direct(abuf)
Expand Down
2 changes: 1 addition & 1 deletion lua/barbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local api = require'barbar.api'
local bbye = require'barbar.bbye'
local events = require'barbar.events'
local notify = require'barbar.utils'.notify
local render = require'barbar.render'
local render = require'barbar.ui.render'
local state = require'barbar.state'
local utils = require'barbar.utils'

Expand Down
4 changes: 2 additions & 2 deletions lua/barbar/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ local bbye = require'barbar.bbye'
local Buffer = require'barbar.buffer'
local config = require'barbar.config'
local JumpMode = require'barbar.jump_mode'
local Layout = require'barbar.layout'
local render = require'barbar.render'
local Layout = require'barbar.ui.layout'
local render = require'barbar.ui.render'
local state = require'barbar.state'
local utils = require'barbar.utils'

Expand Down
4 changes: 2 additions & 2 deletions lua/barbar/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ local bbye = require'barbar.bbye'
local config = require'barbar.config'
local highlight = require'barbar.highlight'
local JumpMode = require'barbar.jump_mode'
local render = require'barbar.render'
local render = require'barbar.ui.render'
local state = require'barbar.state'
local utils = require'barbar.utils'

Expand All @@ -48,7 +48,7 @@ function events.augroups(clear)
end

return create_augroup('barbar_misc', {clear = clear}),
create_augroup('barbar_render', {clear = clear})
create_augroup('barbar.ui.render', {clear = clear})
end

--- What to do when clicking a buffer close button
Expand Down
20 changes: 10 additions & 10 deletions lua/barbar/layout.lua → lua/barbar/ui/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ local SLASH_LEN = #'/'
--- The length of one space (`#' '`)
local SPACE_LEN = #' '

--- @class barbar.layout.data
--- @class barbar.ui.layout.data
--- @field total_width integer the total width of the tabline, equals to &columns
--- @field left barbar.layout.data.side left offset data
--- @field right barbar.layout.data.side right offset data
--- @field buffers barbar.layout.data.buffers buffer data
--- @field tabpages barbar.layout.data.tabpages tabpage data
--- @field left barbar.ui.layout.data.side left offset data
--- @field right barbar.ui.layout.data.side right offset data
--- @field buffers barbar.ui.layout.data.buffers buffer data
--- @field tabpages barbar.ui.layout.data.tabpages tabpage data

--- @class barbar.layout.data.side
--- @class barbar.ui.layout.data.side
--- @field width integer the amount of space allocated

--- @class barbar.layout.data.buffers
--- @class barbar.ui.layout.data.buffers
--- @field width integer the amount of space allocated to the buffers
--- @field pinned_width integer the amount of space used by pinned buffers
--- @field unpinned_width integer the amount of space used by pinned buffers
Expand All @@ -46,16 +46,16 @@ local SPACE_LEN = #' '
--- @field base_widths integer[] the minimum amount of space taken up by each buffer
--- @field scroll_max integer the maximum position which can be scrolled to

--- @class barbar.layout.data.tabpages
--- @class barbar.ui.layout.data.tabpages
--- @field width integer the amount of space allocated to the tabpage indicator


--- @class barbar.Layout
--- @class barbar.ui.layout
--- @field buffers integer[] different from `state.buffers` in that the `hide` option is respected. Only updated when calling `calculate_buffers_width`.
local Layout = { buffers = {} }

--- Calculate the current layout of the bufferline.
--- @return barbar.layout.data
--- @return barbar.ui.layout.data
function Layout.calculate()

local total_width = get_option('columns')
Expand Down
97 changes: 47 additions & 50 deletions lua/barbar/groups.lua → lua/barbar/ui/nodes.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
--
-- groups.lua
-- nodes.lua
--

local table_insert = table.insert
local strcharpart = vim.fn.strcharpart --- @type function
local strwidth = vim.api.nvim_strwidth --- @type function

local utils = require'barbar.utils'
--- @class barbar.ui.Nodes
--- @see barbar.ui.node
--- Operations on `node`s.
local Nodes = {}

--- @class barbar.groups
local m = {}

--- Sums the width of the groups
--- @param groups barbar.render.group[]
--- Sums the width of the nodes
--- @param nodes barbar.ui.node[]
--- @return integer
function m.width(groups)
function Nodes.width(nodes)
local result = 0
for _, group in ipairs(groups) do
for _, group in ipairs(nodes) do
result = result + strwidth(group.text)
end
return result
end

--- Concatenates some `groups` into a valid tabline string.
--- @param groups barbar.render.group[]
--- Concatenates some `nodes` into a valid tabline string.
--- @param nodes barbar.ui.node[]
--- @return string
function m.to_string(groups)
function Nodes.to_string(nodes)
local result = ''

for _, group in ipairs(groups) do
for _, group in ipairs(nodes) do
-- NOTE: We have to escape the text in case it contains '%', which is a special character to the
-- tabline.
-- To escape '%', we make it '%%'. It just so happens that '%' is also a special character
Expand All @@ -39,49 +39,46 @@ function m.to_string(groups)
return result
end

--- Concatenates some `groups` into a raw string.
--- Concatenates some `nodes` into a raw string.
--- @param nodes barbar.ui.node[]
--- For debugging purposes.
--- @param groups barbar.render.group[]
--- @return string
--- @diagnostic disable-next-line:unused-function,unused-local
function m.to_raw_string(groups)
function Nodes.to_raw_string(nodes)
local result = ''

for _, group in ipairs(groups) do
for _, group in ipairs(nodes) do
result = result .. group.text
end

return result
end

--- Insert `other` into `groups` at the `position`.
--- @param groups barbar.render.group[]
--- Insert `other` into `nodes` at the `position`.
--- @param nodes barbar.ui.node[]
--- @param position integer
--- @param group barbar.render.group
--- @return barbar.render.group[] with_insertions
function m.insert(groups, position, group)
return m.insert_many(groups, position, { group })
--- @return barbar.ui.node[] with_insertions
function Nodes.insert(nodes, position, group)
return Nodes.insert_many(nodes, position, { group })
end

--- Insert `others` into `groups` at the `position`.
--- @param groups barbar.render.group[]
--- Insert `others` into `nodes` at the `position`.
--- @param nodes barbar.ui.node[]
--- @param position integer
--- @param others barbar.render.group[]
--- @return barbar.render.group[] with_insertions
function m.insert_many(groups, position, others)

--- @param others barbar.ui.node[]
--- @return barbar.ui.node[] with_insertions
function Nodes.insert_many(nodes, position, others)
if position < 0 then
local others_width = m.width(others)
local others_width = Nodes.width(others)
local others_end = position + others_width

if others_end < 0 then
return groups
return nodes
end

local available_width = others_end

position = 0
others = m.slice_left(others, available_width)
others = Nodes.slice_left(others, available_width)
end


Expand All @@ -90,8 +87,8 @@ function m.insert_many(groups, position, others)
local new_groups = {}

local i = 1
while i <= #groups do
local group = groups[i]
while i <= #nodes do
local group = nodes[i]
local group_width = strwidth(group.text)

-- While we haven't found the position...
Expand All @@ -112,7 +109,7 @@ function m.insert_many(groups, position, others)
})
end

-- Add new other groups
-- Add new other nodes
local others_width = 0
for _, other in ipairs(others) do
local other_width = strwidth(other.text)
Expand All @@ -122,10 +119,10 @@ function m.insert_many(groups, position, others)

local end_position = position + others_width

-- Then, resume adding previous groups
-- Then, resume adding previous nodes
-- table.insert(new_groups, 'then')
while i <= #groups do
local previous_group = groups[i]
while i <= #nodes do
local previous_group = nodes[i]
local previous_group_width = strwidth(previous_group.text)
local previous_group_start_position = current_position
local previous_group_end_position = current_position + previous_group_width
Expand Down Expand Up @@ -153,16 +150,15 @@ function m.insert_many(groups, position, others)
return new_groups
end

--- Select from `groups` while fitting within the provided `width`, discarding all indices larger than the last index that fits.
--- @param groups barbar.render.group[]
--- Select from `nodes` while fitting within the provided `width`, discarding all indices larger than the last index that fits.
--- @param width integer
--- @return barbar.render.group[]
function m.slice_right(groups, width)
--- @return barbar.ui.node[] sliced
function Nodes.slice_right(nodes, width)
local accumulated_width = 0

local new_groups = {}

for _, group in ipairs(groups) do
for _, group in ipairs(nodes) do
local text_width = strwidth(group.text)
accumulated_width = accumulated_width + text_width

Expand All @@ -178,16 +174,17 @@ function m.slice_right(groups, width)
return new_groups
end

--- Select from `groups` in reverse while fitting within the provided `width`, discarding all indices less than the last index that fits.
--- @param groups barbar.render.group[]
--- Select from `nodes` in reverse while fitting within the provided `width`, discarding all indices less than the last index that fits.
--- @param nodes barbar.ui.node[]
--- @param width integer
--- @return barbar.render.group[]
function m.slice_left(groups, width)
--- @return barbar.ui.node[] sliced
function Nodes.slice_left(nodes, width)
local accumulated_width = 0

local new_groups = {}

for _, group in ipairs(utils.list_reverse(groups)) do
for i = #nodes, 1, -1 do
local group = nodes[i] --- @type barbar.ui.node (it cannot be `nil`)
local text_width = strwidth(group.text)
accumulated_width = accumulated_width + text_width

Expand All @@ -204,4 +201,4 @@ function m.slice_left(groups, width)
return new_groups
end

return m
return Nodes
Loading

0 comments on commit de928aa

Please sign in to comment.