Skip to content

Commit

Permalink
feat: highlight buffer button by activity (#543)
Browse files Browse the repository at this point in the history
* docs: new highlight groups

* feat(highlight): groups for buttons by activity and state

* fix(render): incorrect type annotations

* ref(render): use new highlight groups
  • Loading branch information
Iron-E authored Mar 28, 2024
1 parent 3c48b5e commit bca01b7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Highlight groups are created in this way: `Buffer<STATUS><PART>`.
| `<PART>` | Meaning |
|:---------------|:-------------------------------------------------------------------------------------|
| `ADDED` | Git status added. |
| `Btn` | The button that shows when a buffer is unpinned and unmodified. |
| `CHANGED` | Git status changed. |
| `DELETED` | Git status deleted. |
| `ERROR` | Diagnostic errors. |
Expand All @@ -424,7 +425,10 @@ Highlight groups are created in this way: `Buffer<STATUS><PART>`.
| `Index` | The buffer's position in the tabline. |
| `INFO` | Diagnostic info. |
| `Mod` | When the buffer is modified. |
| `ModBtn` | The button that shows when a buffer is modified. |
| `Number` | The `:h bufnr()`. |
| `Pin` | When the buffer is pinned. |
| `PinBtn` | The button that shows when a buffer is pinned. |
| `Sign` | The separator between buffers. |
| `SignRight` | The separator between buffers. |
| `Target` | The letter in buffer-pick mode. |
Expand Down
6 changes: 5 additions & 1 deletion doc/barbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ Highlight groups are created in this way: `Buffer<STATUS><PART>`.
`Visible` |active-buffer|s which are not alternate or current.

`<PART>` Meaning
------ ----------------------------------------------------------------
--------- ----------------------------------------------------------------
`ADDED` Git status added.
`Btn` The button that shows when a buffer is unpinned and unmodified.
`CHANGED` Git status changed.
`DELETED` Git status deleted.
`ERROR` Diagnostic errors.
Expand All @@ -105,7 +106,10 @@ Highlight groups are created in this way: `Buffer<STATUS><PART>`.
`Index` The buffer's position in the tabline.
`INFO` Diagnostic info.
`Mod` When the buffer is modified.
`ModBtn` The button that shows when a buffer is modified.
`Number` The |bufnr()|.
`Pin` When the buffer is pinned.
`PinBtn` The button that shows when a buffer is pinned.
`Sign` The separator between buffers.
`SignRight` The separator between buffers.
`Target` The letter in buffer-pick mode.
Expand Down
13 changes: 8 additions & 5 deletions lua/barbar/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ local GIT_STATUSES = {'added', 'changed', 'deleted'}
--- @type {[barbar.config.options.icons.preset]: fun(default_icons: barbar.config.options.icons, user_icons?: table)}
local ICON_PRESETS = {
default = function(default_icons, user_icons)
default_icons.inactive = { separator = { left = '', right = '' } }
default_icons.inactive = { separator = { left = '', right = '' } } --- @diagnostic disable-line: missing-fields
default_icons.separator = { left = '', right = '' }
default_icons.separator_at_end = true

Expand All @@ -114,18 +114,18 @@ local ICON_PRESETS = {
pinned_icons.button == false or
(pinned_icons.button and strwidth(pinned_icons.button) < 1)
then
default_icons.pinned.separator = { right = ' ' }
default_icons.pinned.separator = { right = ' ' } --- @diagnostic disable-line: missing-fields
end
end,

powerline = function(default_icons)
default_icons.inactive = { separator = { left = '', right = '' } }
default_icons.inactive = { separator = { left = '', right = '' } } --- @diagnostic disable-line: missing-fields
default_icons.separator = { left = '', right = '' }
default_icons.separator_at_end = false
end,

slanted = function(default_icons)
default_icons.inactive = { separator = { left = '', right = '' } }
default_icons.inactive = { separator = { left = '', right = '' } } --- @diagnostic disable-line: missing-fields
default_icons.separator = { left = '', right = '' }
default_icons.separator_at_end = false
end,
Expand Down Expand Up @@ -220,7 +220,10 @@ local DEPRECATED_OPTIONS = {

--- @class barbar.Config
--- @field options barbar.config.options
local config = { git_statuses = GIT_STATUSES, options = {} }
local config = {
git_statuses = GIT_STATUSES,
options = {}, --- @diagnostic disable-line: missing-fields
}

--- @param options? table
function config.setup(options)
Expand Down
35 changes: 34 additions & 1 deletion lua/barbar/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local icons = require('barbar.icons')
-- Setup the highlight groups used by the plugin.
hl.set_default_link('BufferAlternate', 'BufferDefaultAlternate')
hl.set_default_link('BufferAlternateADDED', 'BufferDefaultAlternateADDED')
hl.set_default_link('BufferAlternateBtn', 'BufferDefaultAlternateBtn')
hl.set_default_link('BufferAlternateCHANGED', 'BufferDefaultAlternateCHANGED')
hl.set_default_link('BufferAlternateDELETED', 'BufferDefaultAlternateDELETED')
hl.set_default_link('BufferAlternateERROR', 'BufferDefaultAlternateERROR')
Expand All @@ -15,14 +16,18 @@ hl.set_default_link('BufferAlternateIcon', 'BufferDefaultAlternateIcon')
hl.set_default_link('BufferAlternateIndex', 'BufferDefaultAlternateIndex')
hl.set_default_link('BufferAlternateINFO', 'BufferDefaultAlternateINFO')
hl.set_default_link('BufferAlternateMod', 'BufferDefaultAlternateMod')
hl.set_default_link('BufferAlternateModBtn', 'BufferDefaultAlternateModBtn')
hl.set_default_link('BufferAlternateNumber', 'BufferDefaultAlternateNumber')
hl.set_default_link('BufferAlternatePin', 'BufferDefaultAlternatePin')
hl.set_default_link('BufferAlternatePinBtn', 'BufferDefaultAlternatePinBtn')
hl.set_default_link('BufferAlternateSign', 'BufferDefaultAlternateSign')
hl.set_default_link('BufferAlternateSignRight', 'BufferDefaultAlternateSignRight')
hl.set_default_link('BufferAlternateTarget', 'BufferDefaultAlternateTarget')
hl.set_default_link('BufferAlternateWARN', 'BufferDefaultAlternateWARN')

hl.set_default_link('BufferCurrent', 'BufferDefaultCurrent')
hl.set_default_link('BufferCurrentADDED', 'BufferDefaultCurrentADDED')
hl.set_default_link('BufferCurrentBtn', 'BufferDefaultCurrentBtn')
hl.set_default_link('BufferCurrentCHANGED', 'BufferDefaultCurrentCHANGED')
hl.set_default_link('BufferCurrentDELETED', 'BufferDefaultCurrentDELETED')
hl.set_default_link('BufferCurrentERROR', 'BufferDefaultCurrentERROR')
Expand All @@ -31,14 +36,18 @@ hl.set_default_link('BufferCurrentIcon', 'BufferDefaultCurrentIcon')
hl.set_default_link('BufferCurrentIndex', 'BufferDefaultCurrentIndex')
hl.set_default_link('BufferCurrentINFO', 'BufferDefaultCurrentINFO')
hl.set_default_link('BufferCurrentMod', 'BufferDefaultCurrentMod')
hl.set_default_link('BufferCurrentModBtn', 'BufferDefaultCurrentModBtn')
hl.set_default_link('BufferCurrentNumber', 'BufferDefaultCurrentNumber')
hl.set_default_link('BufferCurrentPin', 'BufferDefaultCurrentPin')
hl.set_default_link('BufferCurrentPinBtn', 'BufferDefaultCurrentPinBtn')
hl.set_default_link('BufferCurrentSign', 'BufferDefaultCurrentSign')
hl.set_default_link('BufferCurrentSignRight', 'BufferDefaultCurrentSignRight')
hl.set_default_link('BufferCurrentTarget', 'BufferDefaultCurrentTarget')
hl.set_default_link('BufferCurrentWARN', 'BufferDefaultCurrentWARN')

hl.set_default_link('BufferInactive', 'BufferDefaultInactive')
hl.set_default_link('BufferInactiveADDED', 'BufferDefaultInactiveADDED')
hl.set_default_link('BufferInactiveBtn', 'BufferDefaultInactiveBtn')
hl.set_default_link('BufferInactiveCHANGED', 'BufferDefaultInactiveCHANGED')
hl.set_default_link('BufferInactiveDELETED', 'BufferDefaultInactiveDELETED')
hl.set_default_link('BufferInactiveERROR', 'BufferDefaultInactiveERROR')
Expand All @@ -47,7 +56,10 @@ hl.set_default_link('BufferInactiveIcon', 'BufferDefaultInactiveIcon')
hl.set_default_link('BufferInactiveIndex', 'BufferDefaultInactiveIndex')
hl.set_default_link('BufferInactiveINFO', 'BufferDefaultInactiveINFO')
hl.set_default_link('BufferInactiveMod', 'BufferDefaultInactiveMod')
hl.set_default_link('BufferInactiveModBtn', 'BufferDefaultInactiveModBtn')
hl.set_default_link('BufferInactiveNumber', 'BufferDefaultInactiveNumber')
hl.set_default_link('BufferInactivePin', 'BufferDefaultInactivePin')
hl.set_default_link('BufferInactivePinBtn', 'BufferDefaultInactivePinBtn')
hl.set_default_link('BufferInactiveSign', 'BufferDefaultInactiveSign')
hl.set_default_link('BufferInactiveSignRight', 'BufferDefaultInactiveSignRight')
hl.set_default_link('BufferInactiveTarget', 'BufferDefaultInactiveTarget')
Expand All @@ -62,6 +74,7 @@ hl.set_default_link('BufferTabpagesSep', 'BufferDefaultTabpagesSep')

hl.set_default_link('BufferVisible', 'BufferDefaultVisible')
hl.set_default_link('BufferVisibleADDED', 'BufferDefaultVisibleADDED')
hl.set_default_link('BufferVisibleBtn', 'BufferDefaultVisibleBtn')
hl.set_default_link('BufferVisibleCHANGED', 'BufferDefaultVisibleCHANGED')
hl.set_default_link('BufferVisibleDELETED', 'BufferDefaultVisibleDELETED')
hl.set_default_link('BufferVisibleERROR', 'BufferDefaultVisibleERROR')
Expand All @@ -70,25 +83,45 @@ hl.set_default_link('BufferVisibleIcon', 'BufferDefaultVisibleIcon')
hl.set_default_link('BufferVisibleIndex', 'BufferDefaultVisibleIndex')
hl.set_default_link('BufferVisibleINFO', 'BufferDefaultVisibleINFO')
hl.set_default_link('BufferVisibleMod', 'BufferDefaultVisibleMod')
hl.set_default_link('BufferVisibleModBtn', 'BufferDefaultVisibleModBtn')
hl.set_default_link('BufferVisibleNumber', 'BufferDefaultVisibleNumber')
hl.set_default_link('BufferVisiblePin', 'BufferDefaultVisiblePin')
hl.set_default_link('BufferVisiblePinBtn', 'BufferDefaultVisiblePinBtn')
hl.set_default_link('BufferVisibleSign', 'BufferDefaultVisibleSign')
hl.set_default_link('BufferVisibleSignRight', 'BufferDefaultVisibleSignRight')
hl.set_default_link('BufferVisibleTarget', 'BufferDefaultVisibleTarget')
hl.set_default_link('BufferVisibleWARN', 'BufferDefaultVisibleWARN')

-- NOTE: these should move to `setup_defaults` if the definition stops being a link

hl.set_default_link('BufferDefaultAlternateBtn', 'BufferAlternate')
hl.set_default_link('BufferDefaultAlternateIcon', 'BufferAlternate')
hl.set_default_link('BufferDefaultAlternateModBtn', 'BufferAlternateMod')
hl.set_default_link('BufferDefaultAlternateNumber', 'BufferAlternateIndex')
hl.set_default_link('BufferDefaultAlternatePin', 'BufferAlternate')
hl.set_default_link('BufferDefaultAlternatePinBtn', 'BufferAlternatePin')
hl.set_default_link('BufferDefaultAlternateSignRight', 'BufferAlternateSign')
hl.set_default_link('BufferDefaultCurrentBtn', 'BufferCurrent')
hl.set_default_link('BufferDefaultCurrentIcon', 'BufferCurrent')
hl.set_default_link('BufferDefaultCurrentModBtn', 'BufferCurrentMod')
hl.set_default_link('BufferDefaultCurrentNumber', 'BufferCurrentIndex')
hl.set_default_link('BufferDefaultCurrentPin', 'BufferCurrent')
hl.set_default_link('BufferDefaultCurrentPinBtn', 'BufferCurrentPin')
hl.set_default_link('BufferDefaultCurrentSignRight', 'BufferCurrentSign')
hl.set_default_link('BufferDefaultInactiveBtn', 'BufferInactive')
hl.set_default_link('BufferDefaultInactiveIcon', 'BufferInactive')
hl.set_default_link('BufferDefaultInactiveModBtn', 'BufferInactiveMod')
hl.set_default_link('BufferDefaultInactiveNumber', 'BufferInactiveIndex')
hl.set_default_link('BufferDefaultInactivePin', 'BufferInactive')
hl.set_default_link('BufferDefaultInactivePinBtn', 'BufferInactivePin')
hl.set_default_link('BufferDefaultInactiveSignRight', 'BufferInactiveSign')
hl.set_default_link('BufferDefaultOffset', 'BufferTabpageFill')
hl.set_default_link('BufferDefaultVisibleBtn', 'BufferVisible')
hl.set_default_link('BufferDefaultVisibleIcon', 'BufferVisible')
hl.set_default_link('BufferDefaultVisibleModBtn', 'BufferVisibleMod')
hl.set_default_link('BufferDefaultVisibleNumber', 'BufferVisibleIndex')
hl.set_default_link('BufferDefaultVisiblePin', 'BufferVisible')
hl.set_default_link('BufferDefaultVisiblePinBtn', 'BufferVisiblePin')
hl.set_default_link('BufferDefaultVisibleSignRight', 'BufferVisibleSign')

--- @class barbar.Highlight
Expand All @@ -99,7 +132,7 @@ local highlight = {}
function highlight.setup()
local preset = config.options.icons.preset

local fg_target = {gui = 'red', cterm = 'red'} --- @type barbar.utils.hl.color
local fg_target = {cterm = 'red', gui = 'red'} --- @type barbar.utils.hl.color

local fg_added = hl.fg_or_default({'GitSignsAdd'}, '#59ff5a', 82)
local fg_changed = hl.fg_or_default({'GitSignsChange'}, '#599eff', 75)
Expand Down
1 change: 1 addition & 0 deletions lua/barbar/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ local WARN = severity.WARN
--- @field buffers integer[] the open buffers, in visual order.
--- @field data_by_bufnr {[integer]: barbar.state.data} the buffer data indexed on buffer number
--- @field is_picking_buffer boolean whether the user is currently in jump-mode
--- @field last_current_buffer? integer the previously-open buffer before rendering starts
--- @field offset barbar.state.offset
--- @field recently_closed string[] the list of recently closed paths
local state = {
Expand Down
30 changes: 18 additions & 12 deletions lua/barbar/ui/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,14 @@ local function get_bufferline_containers(data, bufnrs, refocus)
end
end

--- the start of all rendered highlight names
local hl_prefix = 'Buffer' .. activity_name

--- the suffix of some (eventually all) rendered highlight names
local hl_suffix = (modified and 'Mod') or (pinned and 'Pin') or ''

local buffer_name = buffer_data.name or '[no name]'
local buffer_hl = wrap_hl('Buffer' .. activity_name .. (modified and 'Mod' or ''))
local buffer_hl = wrap_hl(hl_prefix .. hl_suffix)

local icons_option = buffer.get_icons(activity_name, modified, pinned)

Expand All @@ -442,21 +448,21 @@ local function get_bufferline_containers(data, bufnrs, refocus)
--- @type barbar.ui.node
local buffer_index = { hl = '', text = '' }
if icons_option.buffer_index then
buffer_index.hl = wrap_hl('Buffer' .. activity_name .. 'Index')
buffer_index.hl = wrap_hl(hl_prefix .. 'Index')
buffer_index.text = style_number(i, icons_option.buffer_index) .. ' '
end

--- The buffer number
--- @type barbar.ui.node
local buffer_number = { hl = '', text = '' }
if icons_option.buffer_number then
buffer_number.hl = wrap_hl('Buffer' .. activity_name .. 'Number')
buffer_number.hl = wrap_hl(hl_prefix .. 'Number')
buffer_number.text = style_number(bufnr, icons_option.buffer_number) .. ' '
end

--- The close icon
--- @type barbar.ui.container
local button = {hl = buffer_hl, text = ''}
--- @type barbar.ui.node
local button = {hl = wrap_hl(hl_prefix .. hl_suffix .. 'Btn'), text = ''}

local button_icon = icons_option.button
if button_icon and #button_icon > 0 then
Expand All @@ -483,7 +489,7 @@ local function get_bufferline_containers(data, bufnrs, refocus)
name.text = strcharpart(name.text, 1)
end

jump_letter.hl = wrap_hl('Buffer' .. activity_name .. 'Target')
jump_letter.hl = wrap_hl(hl_prefix .. 'Target')
if letter then
jump_letter.text = letter
if icons_option.filetype.enabled and #name.text > 0 then
Expand All @@ -499,14 +505,14 @@ local function get_bufferline_containers(data, bufnrs, refocus)
or iconHl

icon.hl = icons_option.filetype.custom_colors and
wrap_hl('Buffer' .. activity_name .. 'Icon') or
wrap_hl(hl_prefix .. 'Icon') or
(hlName and wrap_hl(hlName) or buffer_hl)
icon.text = #name.text > 0 and iconChar .. ' ' or iconChar
end

--- @type barbar.ui.node
local left_separator = {
hl = clickable .. wrap_hl('Buffer' .. activity_name .. 'Sign'),
hl = clickable .. wrap_hl(hl_prefix .. 'Sign'),
text = icons_option.separator.left,
}

Expand All @@ -523,21 +529,21 @@ local function get_bufferline_containers(data, bufnrs, refocus)

state.for_each_counted_enabled_diagnostic(bufnr, icons_option.diagnostics, function(count, idx, option)
table_insert(container.nodes, {
hl = wrap_hl('Buffer' .. activity_name .. severity[idx]),
hl = wrap_hl(hl_prefix .. severity[idx]),
text = ' ' .. option.icon .. count,
})
end)

state.for_each_counted_enabled_git_status(bufnr, icons_option.gitsigns, function(count, idx, option)
table_insert(container.nodes, {
hl = wrap_hl('Buffer' .. activity_name .. idx:upper()),
hl = wrap_hl(hl_prefix .. idx:upper()),
text = ' ' .. option.icon .. count,
})
end)

--- @type barbar.ui.node
local right_separator = {
hl = clickable .. wrap_hl('Buffer' .. activity_name .. 'SignRight'),
hl = clickable .. wrap_hl(hl_prefix .. 'SignRight'),
text = icons_option.separator.right,
}

Expand Down Expand Up @@ -617,7 +623,7 @@ local function generate_tabline(bufnrs, refocus)

-- Buffer tabs
do
--- @type barbar.ui.container
--- @type barbar.ui.node[]
local content = { { hl = HL.FILL, text = (' '):rep(data.buffers.width) } }

do
Expand Down

0 comments on commit bca01b7

Please sign in to comment.