Skip to content

Commit

Permalink
feat(bar/highlights): add current context highlighting to winbar
Browse files Browse the repository at this point in the history
  • Loading branch information
bekaboo committed Jul 2, 2023
1 parent a0faad2 commit 36125e5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,8 @@ should be self-explanatory:

| Highlight group | Attributes |
|----------------------------------|-----------------------------------------|
| DropBarCurrentContext | `{ link = 'Visual' }` |
| DropBarIconCurrentContext | `{ link = 'Visual' }` |
| DropBarIconKindArray | `{ link = 'Array' }` |
| DropBarIconKindBoolean | `{ link = 'Boolean' }` |
| DropBarIconKindBreakStatement | `{ link = 'Error' }` |
Expand Down
2 changes: 2 additions & 0 deletions doc/dropbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,8 @@ Highlight groups ~

Highlight group Attributes

DropBarCurrentContext `{ link = 'Visual' }`
DropBarIconCurrentContext `{ link = 'Visual' }`
DropBarIconKindArray `{ link = 'Array' }`
DropBarIconKindBoolean `{ link = 'Boolean' }`
DropBarIconKindBreakStatement `{ link = 'Error' }`
Expand Down
27 changes: 27 additions & 0 deletions lua/dropbar/bar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ function dropbar_symbol_t:new(opts)
-- is shown inside a menu
if this.entry and this.entry.menu then
this.entry.menu:update_current_context_hl(this.entry.idx)
elseif this.bar then
this.bar:update_current_context_hl(this.bar_idx)
end

-- Determine menu configs
Expand Down Expand Up @@ -624,6 +626,31 @@ function dropbar_t:get_component_at(col, look_ahead)
return nil, nil
end

---Highlight the symbol at bar_idx as current context
---@param bar_idx integer? see dropbar_symbol_t.bar_idx
---@return nil
function dropbar_t:update_current_context_hl(bar_idx)
local symbol = self.components[bar_idx]
if not symbol then
return
end
local hl_currentcontext_icon = '_DropBarIconCurrentContext'
local hl_currentcontext_name = '_DropBarCurrentContext'
vim.api.nvim_set_hl(
0,
hl_currentcontext_icon,
utils.hl_merge('DropBarIconCurrentContext', symbol.icon_hl or 'WinBar')
)
vim.api.nvim_set_hl(
0,
hl_currentcontext_name,
utils.hl_merge('DropBarCurrentContext', symbol.name_hl or 'WinBar')
)
symbol:swap_field('icon_hl', hl_currentcontext_icon)
symbol:swap_field('name_hl', hl_currentcontext_name)
self:redraw()
end

---Get the string representation of the dropbar
---@return string
function dropbar_t:__tostring()
Expand Down
2 changes: 2 additions & 0 deletions lua/dropbar/hlgroups.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-- stylua: ignore start
local hlgroups = {
DropBarCurrentContext = { link = 'Visual' },
DropBarIconCurrentContext = { link = 'Visual' },
DropBarIconKindArray = { link = 'Array' },
DropBarIconKindBoolean = { link = 'Boolean' },
DropBarIconKindBreakStatement = { link = 'Error' },
Expand Down
15 changes: 15 additions & 0 deletions lua/dropbar/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,22 @@ local function hl_line_single(buf, hlgroup, linenr)
})
end

---Merge highlight attributes, use values from the right most hl group
---if there are conflicts
---@vararg string highlight group names
---@return table merged highlight attributes
local function hl_merge(...)
local hl_attr = vim.tbl_map(function(hl_name)
return vim.api.nvim_get_hl(0, {
name = hl_name,
link = false,
})
end, { ... })
return vim.tbl_extend('force', unpack(hl_attr))
end

return {
hl_range_single = hl_range_single,
hl_line_single = hl_line_single,
hl_merge = hl_merge,
}

0 comments on commit 36125e5

Please sign in to comment.