Skip to content

Commit

Permalink
fix(highlights): winbar highlights changed after hovering/clicking if…
Browse files Browse the repository at this point in the history
… not defined
  • Loading branch information
bekaboo committed Aug 6, 2023
1 parent 88d71c6 commit 4785774
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
12 changes: 8 additions & 4 deletions lua/dropbar/bar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -662,15 +662,16 @@ function dropbar_t:update_current_context_hl(bar_idx)
end
local hl_currentcontext_icon = '_DropBarIconCurrentContext'
local hl_currentcontext_name = '_DropBarCurrentContext'
symbol:restore()
vim.api.nvim_set_hl(
0,
hl_currentcontext_icon,
utils.hl.merge(symbol.icon_hl or 'WinBar', 'DropBarCurrentContext')
utils.hl.merge('WinBarNC', symbol.icon_hl, 'DropBarCurrentContext')
)
vim.api.nvim_set_hl(
0,
hl_currentcontext_name,
utils.hl.merge(symbol.name_hl or 'WinBar', 'DropBarCurrentContext')
utils.hl.merge('WinBarNC', symbol.name_hl, 'DropBarCurrentContext')
)
symbol:swap_field('icon_hl', hl_currentcontext_icon)
symbol:swap_field('name_hl', hl_currentcontext_name)
Expand All @@ -681,6 +682,7 @@ end
---@param col integer? displaywidth-indexed, 0-indexed mouse position, nil to clear the hover highlights
---@return nil
function dropbar_t:update_hover_hl(col)
print('update_hover_hl')
if not col then
if self.symbol_on_hover then
self.symbol_on_hover:restore()
Expand All @@ -695,15 +697,17 @@ function dropbar_t:update_hover_hl(col)
end
local hl_hover_icon = '_DropBarIconHover'
local hl_hover_name = '_DropBarHover'
local hl_winbar = vim.api.nvim_get_current_win() == self.win and 'WinBar'
or 'WinbarNC'
vim.api.nvim_set_hl(
0,
hl_hover_icon,
utils.hl.merge(symbol.icon_hl or 'WinBar', 'DropBarHover')
utils.hl.merge(hl_winbar, symbol.icon_hl, 'DropBarHover')
)
vim.api.nvim_set_hl(
0,
hl_hover_name,
utils.hl.merge(symbol.name_hl or 'WinBar', 'DropBarHover')
utils.hl.merge(hl_winbar, symbol.name_hl, 'DropBarHover')
)
symbol:swap_field('icon_hl', hl_hover_icon)
symbol:swap_field('name_hl', hl_hover_name)
Expand Down
2 changes: 1 addition & 1 deletion lua/dropbar/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ function dropbar_menu_t:add_hl(hl_info)
end
for linenr, hl_line_info in ipairs(hl_info) do
for _, hl_symbol_info in ipairs(hl_line_info) do
vim.api.nvim_buf_add_highlight(
utils.hl.buf_add_hl(
self.buf,
hl_symbol_info.ns or -1,
hl_symbol_info.hlgroup,
Expand Down
65 changes: 55 additions & 10 deletions lua/dropbar/utils/hl.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
local M = {}

---Wrapper of nvim_get_hl(), but does not create a cleared highlight group
---if it doesn't exist
---NOTE: vim.api.nvim_get_hl() has a side effect, it will create a cleared
---highlight group if it doesn't exist, see
---https://github.com/neovim/neovim/issues/24583
---This affects regions highlighted by non-existing highlight groups in a
---winbar, which should falls back to the default 'WinBar' or 'WinBarNC'
---highlight groups but instead falls back to 'Normal' highlight group
---because of this side effect
---So we need to check if the highlight group exists before calling
---vim.api.nvim_get_hl()
---@param ns_id integer
---@param opts table{ name: string?, id: integer?, link: boolean? }
---@return table highlight attributes
function M.get_hl(ns_id, opts)
if not opts.name then
return vim.api.nvim_get_hl(ns_id, opts)
end
return vim.fn.hlexists(opts.name) == 1 and vim.api.nvim_get_hl(ns_id, opts)
or {}
end

---Wrapper of nvim_buf_add_highlight(), but does not create a cleared
---highlight group if it doesn't exist
---@param buffer integer buffer handle, or 0 for current buffer
---@param ns_id integer namespace to use or -1 for ungrouped highlight
---@param hl_group string name of the highlight group to use
---@param line integer line to highlight (zero-indexed)
---@param col_start integer start of (byte-indexed) column range to highlight
---@param col_end integer end of (byte-indexed) column range to highlight, or -1 to highlight to end of line
---@return nil
function M.buf_add_hl(buffer, ns_id, hl_group, line, col_start, col_end)
if vim.fn.hlexists(hl_group) == 0 then
return
end
vim.api.nvim_buf_add_highlight(
buffer,
ns_id,
hl_group,
line,
col_start,
col_end
)
end

---Highlight text in buffer, clear previous highlight if any exists
---@param buf integer
---@param hlgroup string
Expand All @@ -13,14 +58,7 @@ function M.range_single(buf, hlgroup, range)
or 0
local end_col = linenr == range['end'].line and range['end'].character
or -1
vim.api.nvim_buf_add_highlight(
buf,
ns,
hlgroup,
linenr,
start_col,
end_col
)
M.buf_add_hl(buf, ns, hlgroup, linenr, start_col, end_col)
end
end
end
Expand All @@ -47,12 +85,19 @@ end
---@vararg string highlight group names
---@return table merged highlight attributes
function M.merge(...)
-- Eliminate nil values in vararg
local hl_names = {}
for _, hl_name in pairs({ ... }) do
if hl_name then
table.insert(hl_names, hl_name)
end
end
local hl_attr = vim.tbl_map(function(hl_name)
return vim.api.nvim_get_hl(0, {
return M.get_hl(0, {
name = hl_name,
link = false,
})
end, { ... })
end, hl_names)
return vim.tbl_extend('force', unpack(hl_attr))
end

Expand Down

0 comments on commit 4785774

Please sign in to comment.