diff --git a/README.md b/README.md
index e8459d62..17777ce6 100644
--- a/README.md
+++ b/README.md
@@ -1371,6 +1371,8 @@ Declared and defined in [`lua/dropbar/menu.lua`](https://github.com/Bekaboo/drop
| `dropbar_menu_t:hl_line_single(line: integer?, hlgroup: string?)` | add highlight to a single line in the menu buffer; `hlgroups` defaults to `'DropBarMenuCurrentContext'`
*all highlights with the same hlgroup added by this functions before will be cleared |
| `dropbar_menu_t:hl_range_single(line: integer?, range: { start: integer, end: integer }?, hlgroup: string?)` | add highlight to a single range in a single line in the menu buffer; `hlgroups` defaults to `'DropBarMenuHoverSymbol'`
*all highlights with the same hlgroup added by this functions before will be cleared |
| `dropbar_menu_t:update_hover_hl(pos: integer[])` | update the hover highlights (`DropBarMenuHover*`) assuming the cursor/mouse is hovering at `pos` in the menu |
+| `dropbar_menu_t:update_current_context_hl(linenr: integer?)` | update the current context highlight (`hl-DropBarMenuCurrentContext`) assuming the cursor is at line `linenr` in the menu |
+| `dropbar_menu_t:add_hl(hl_info: dropbar_menu_hl_info_t[][])` | add highlights to the menu buffer |
| `dropbar_menu_t:make_buf()` | create the menu buffer from the entries[`dropbar_menu_entry_t`](#dropbar_menu_entry_t) |
| `dropbar_menu_t:override(opts: dropbar_menu_t?)` | override menu options |
| `dropbar_menu_t:open(opts: dropbar_menu_t?)` | open the menu with options `opts` |
diff --git a/doc/dropbar.txt b/doc/dropbar.txt
index 0c40e24e..e85fdf30 100644
--- a/doc/dropbar.txt
+++ b/doc/dropbar.txt
@@ -1539,6 +1539,23 @@ dropbar_menu_t:udpate_hover_hl([{pos}]) *dropbar_menu_t:udpate_hover_hl()*
Parameters ~
• {pos} (integer[]?): { start, end }
+ *dropbar_menu_t:update_current_context_hl*
+dropbar_menu_t:update_current_context_hl([{linenr}])
+
+ Update the current context highlight hl-DropBarMenuCurrentContext
+ assuming the cursor is at line {linenr} in the menu
+
+ Parameters ~
+ • {linenr} (integer?): 1-indexed line number
+
+dropbar_menu_t:add_hl({hl_info}) *dropbar_menu_t:add_hl()*
+
+ Add highlight to the menu buffer according to the highlight info
+ |dropbar-developers-classes-dropbar_menu_hl_info_t|
+
+ Parameters ~
+ • {hl_info} (`dropbar_menu_hl_info_t[][]`): highlight info
+
dropbar_menu_t:make_buf() *dropbar_menu_t:make_buf()*
Create the menu buffer from the entries
diff --git a/lua/dropbar/bar.lua b/lua/dropbar/bar.lua
index f691d588..a31fe9b5 100644
--- a/lua/dropbar/bar.lua
+++ b/lua/dropbar/bar.lua
@@ -76,6 +76,12 @@ function dropbar_symbol_t:new(opts)
on_click = opts
---@param this dropbar_symbol_t
and function(this, _, _, _, _)
+ -- Update current context highlights if the symbol
+ -- is shown inside a menu
+ if this.entry and this.entry.menu then
+ this.entry.menu:update_current_context_hl(this.entry.idx)
+ end
+
-- Determine menu configs
local prev_win = nil ---@type integer?
local entries_source = nil ---@type dropbar_symbol_t[]?
diff --git a/lua/dropbar/menu.lua b/lua/dropbar/menu.lua
index f6c26a8a..1724a425 100644
--- a/lua/dropbar/menu.lua
+++ b/lua/dropbar/menu.lua
@@ -327,6 +327,35 @@ function dropbar_menu_t:update_hover_hl(pos)
end
end
+---Update highlights for current context according to pos
+---@param linenr integer? 1-indexed line number
+function dropbar_menu_t:update_current_context_hl(linenr)
+ if self.buf then
+ utils.hl_line_single(self.buf, 'DropBarMenuCurrentContext', linenr)
+ end
+end
+
+---Add highlights to the menu buffer
+---@param hl_info dropbar_menu_hl_info_t[][]
+---@return nil
+function dropbar_menu_t:add_hl(hl_info)
+ if not self.buf then
+ return
+ 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(
+ self.buf,
+ hl_symbol_info.ns or -1,
+ hl_symbol_info.hlgroup,
+ linenr - 1, -- 0-indexed
+ hl_symbol_info.start,
+ hl_symbol_info['end']
+ )
+ end
+ end
+end
+
---Make a buffer for the menu and set buffer-local keymaps
---Must be called after the popup window is created
---Side effect: change self.buf, self.hl_info
@@ -337,7 +366,7 @@ function dropbar_menu_t:make_buf()
end
self.buf = vim.api.nvim_create_buf(false, true)
local lines = {} ---@type string[]
- local hl_info = {} ---@type {start: integer, end: integer, hlgroup: string, ns: integer?}[][]
+ local hl_info = {} ---@type dropbar_menu_hl_info_t[][]
for _, entry in ipairs(self.entries) do
local line, entry_hl_info = entry:cat()
-- Pad lines with spaces to the width of the window
@@ -354,20 +383,9 @@ function dropbar_menu_t:make_buf()
table.insert(hl_info, entry_hl_info)
end
vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, lines)
- for entry_idx, entry_hl_info in ipairs(hl_info) do
- for _, hl in ipairs(entry_hl_info) do
- vim.api.nvim_buf_add_highlight(
- self.buf,
- hl.ns or -1,
- hl.hlgroup,
- entry_idx - 1, -- 0-indexed
- hl.start,
- hl['end']
- )
- end
- if self.cursor and entry_idx == self.cursor[1] then
- utils.hl_line_single(self.buf, 'DropBarMenuCurrentContext', entry_idx)
- end
+ self:add_hl(hl_info)
+ if self.cursor then
+ self:update_current_context_hl(self.cursor[1])
end
vim.bo[self.buf].ma = false
vim.bo[self.buf].ft = 'dropbar_menu'