Skip to content

Commit

Permalink
fix(highlights): update current context highlights correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bekaboo committed Jun 9, 2023
1 parent 990cae9 commit 7367616
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'`<br> *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'`<br> *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<sub>[`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` |
Expand Down
17 changes: 17 additions & 0 deletions doc/dropbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lua/dropbar/bar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[]?
Expand Down
48 changes: 33 additions & 15 deletions lua/dropbar/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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'
Expand Down

0 comments on commit 7367616

Please sign in to comment.