Skip to content

Commit

Permalink
perf(render): resolve proper icons on setup
Browse files Browse the repository at this point in the history
This is a >2× performance increase for `render.update`
  • Loading branch information
Iron-E committed Apr 2, 2023
1 parent 3eb21c8 commit 8957f8b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 81 deletions.
15 changes: 15 additions & 0 deletions lua/barbar/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ local buffer = {

get_activity = get_activity,

--- @param activity barbar.buffer.activity.name
--- @param modified boolean
--- @param pinned boolean
--- @return barbar.config.options.icons.buffer
get_icons = function(activity, modified, pinned)
local icons_option = config.options.icons[activity:lower()]
if pinned then
icons_option = icons_option.pinned
elseif modified then
icons_option = icons_option.modified
end

return icons_option
end,

--- @param buffer_number integer
--- @param hide_extensions boolean? if `true`, exclude the extension of the file
--- @return string name
Expand Down
33 changes: 33 additions & 0 deletions lua/barbar/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,39 @@ function config.setup(user_config)
diagnostic_severity_icons.icon = default_diagnostic_severity_icons.icon
end
end

local icons = config.options.icons

--- `config.options.icons` without the recursive structure
--- @type barbar.config.options.icons.buffer
local base_options = {
buffer_index = icons.buffer_index,
buffer_number = icons.buffer_number,
filename = icons.filename,
button = icons.button,
diagnostics = icons.diagnostics,
filetype = icons.filetype,
separator = icons.separator,
}

-- resolve all of the icons for the activities
for _, activity in ipairs {'alternate', 'current', 'inactive', 'visible'} do
local activity_options = tbl_deep_extend('keep', config.options.icons[activity] or {}, base_options)
config.options.icons[activity] = activity_options
config.options.icons[activity].modified = tbl_deep_extend(
'keep',
config.options.icons[activity].modified or {},
config.options.icons.modified or {},
activity_options
)

config.options.icons[activity].pinned = tbl_deep_extend(
'keep',
config.options.icons[activity].pinned or {},
config.options.icons.pinned or {},
activity_options
)
end
end

return config
3 changes: 2 additions & 1 deletion lua/barbar/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local floor = math.floor
local max = math.max
local min = math.min

local buf_get_option = vim.api.nvim_buf_get_option --- @type function
local get_option = vim.api.nvim_get_option --- @type function
local strwidth = vim.api.nvim_strwidth --- @type function
local tabpagenr = vim.fn.tabpagenr --- @type function
Expand Down Expand Up @@ -78,7 +79,7 @@ function Layout.calculate_buffer_width(bufnr, index)
if buffer_data.closing then
width = buffer_data.real_width
else
local icons_option = state.icons(bufnr, buffer_activity)
local icons_option = Buffer.get_icons(buffer_activity, buf_get_option(bufnr, 'modified'), buffer_data.pinned)

width = strwidth(icons_option.separator.left)

Expand Down
7 changes: 3 additions & 4 deletions lua/barbar/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -541,19 +541,18 @@ local function generate_tabline(bufnrs, refocus)
for i, bufnr in ipairs(bufnrs) do
local activity = Buffer.activities[Buffer.get_activity(bufnr)]
local buffer_data = state.get_buffer_data(bufnr)
local modified = buf_get_option(bufnr, 'modified')

local buffer_name = buffer_data.name or '[no name]'
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 ''))

buffer_data.real_position = accumulated_width
buffer_data.real_width = Layout.calculate_width(
buffer_data.pinned and layout.pinned_widths[i] or layout.base_widths[i],
layout.padding_width
)

local icons_option = state.icons(bufnr, activity)
local icons_option = Buffer.get_icons(activity, modified, buffer_data.pinned)

--- Prefix this value to allow an element to be clicked
local clickable = click_enabled and ('%' .. bufnr .. '@barbar#events#main_click_handler@') or ''
Expand Down
76 changes: 0 additions & 76 deletions lua/barbar/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ local command = vim.api.nvim_command
local fnamemodify = vim.fn.fnamemodify
local json_encode = vim.json.encode --- @type function
local json_decode = vim.json.decode --- @type function
local deepcopy = vim.deepcopy
local get_current_buf = vim.api.nvim_get_current_buf --- @type function
local list_bufs = vim.api.nvim_list_bufs --- @type function
local list_extend = vim.list_extend
local list_slice = vim.list_slice
local severity = vim.diagnostic.severity --- @type {[integer]: string, [string]: integer}
local tbl_contains = vim.tbl_contains
local tbl_filter = vim.tbl_filter
local tbl_map = vim.tbl_map
Expand All @@ -29,43 +27,6 @@ local utils = require'barbar.utils'

local CACHE_PATH = vim.fn.stdpath('cache') .. '/barbar.json'

--- Set `higher` to have higher priority than `lower` when resolving the `icons` option.
--- @param higher? barbar.config.options.icons.buffer
--- @param lower barbar.config.options.icons.buffer
--- @return table barbar.options.icons.buffer corresponding to the `tbl` parameter
local function icons_option_prioritize(higher, lower)
if higher and lower then -- set the sub-table fallbacks
do
local lower_diagnostics = utils.tbl_remove_key(lower, 'diagnostics')
if lower_diagnostics then
if higher.diagnostics == nil then
higher.diagnostics = {}
end

for i = 1, #severity do
higher.diagnostics[i] = utils.setfallbacktable(higher.diagnostics[i], lower_diagnostics[i])
end
end
end

do
local lower_filetype = utils.tbl_remove_key(lower, 'filetype')
if lower_filetype then
higher.filetype = utils.setfallbacktable(higher.filetype, lower_filetype)
end
end

do
local lower_separator = utils.tbl_remove_key(lower, 'separator')
if lower_separator then
higher.separator = utils.setfallbacktable(higher.separator, lower_separator)
end
end
end

return utils.setfallbacktable(higher, lower)
end

--------------------------------
-- Section: Application state --
--------------------------------
Expand Down Expand Up @@ -299,43 +260,6 @@ function state.restore_buffers(buffer_data)
end
end

--- The `icons` for a particular activity.
--- @param activity barbar.buffer.activity.name
--- @see barbar.options.icons
--- @return barbar.config.options.icons.buffer
function state.icons(bufnr, activity)
local activity_lower = activity:lower()
local icons = deepcopy(config.options.icons)

--- @type barbar.config.options.icons.state
local activity_icons = utils.tbl_remove_key(icons, activity_lower) or {}

--- @type barbar.config.options.icons.buffer
local buffer_icons = icons_option_prioritize(activity_icons, icons)

--- Prioritize the `modified` or `pinned` states
--- @param option string
local function icons_option_prioritize_state(option)
buffer_icons = icons_option_prioritize(
utils.tbl_remove_key(activity_icons, option),
icons_option_prioritize(
utils.tbl_remove_key(icons, option),
buffer_icons
)
)
end

if buf_get_option(bufnr, 'modified') then
icons_option_prioritize_state'modified'
end

if state.is_pinned(bufnr) then
icons_option_prioritize_state'pinned'
end

return buffer_icons
end

-- Save/load state

--- Save recently_closed list
Expand Down

0 comments on commit 8957f8b

Please sign in to comment.