Skip to content

Commit

Permalink
feat(menu): allow showing virtual text below entries (#92)
Browse files Browse the repository at this point in the history
* feat(menu): allow showing virtual text below entries

* docs: document virt text property

* refactor(extmarks): use PascalCase for namespace id
  • Loading branch information
willothy authored Sep 29, 2023
1 parent 5b957d5 commit 3daffc1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,7 @@ multiple `dropbar_menu_entry_t` instances while a
| `separator` | [`dropbar_symbol_t`](#dropbar_symbol_t) | separator to use in the entry |
| `padding` | `{left: integer, right: integer}` | padding to use between the menu entry and the menu border |
| `components` | [`dropbar_symbol_t[]`](#dropbar_symbol_t) | components<sub>[`dropbar_symbol_t[]`](#dropbar_symbol_t)</sub> in the entry |
| `virt_text` | `string[][]?` | list of virtual text chunks to display below the entry |
| `menu` | [`dropbar_menu_t?`](#dropbar_menu_t) | the menu the entry belongs to |
| `idx` | `integer?` | the index of the entry in the menu |
Expand Down
18 changes: 13 additions & 5 deletions doc/dropbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1832,36 +1832,44 @@ contain multiple `dropbar_symbol_t` instances.

`dropbar_menu_entry_t` has the following fields:

dropbar_menu_t.separator *dropbar_menu_t.separator*
dropbar_menu_entry_t.separator *dropbar_menu_t.separator*

Separator to use in the entry

Type ~
`dropbar_symbol_t`

dropbar_menu_t.padding *dropbar_menu_t.padding*
dropbar_menu_entry_t.padding *dropbar_menu_t.padding*

Padding to use between the menu entry and the menu border

Type ~
{ left: integer, right: integer }

dropbar_menu_t.components *dropbar_menu_t.components*
dropbar_menu_entry_t.components *dropbar_menu_t.components*

Symbols got from sources |dropbar-developers-classes-dropbar_source_t|
in the entry

Type ~
`dropbar_symbol_t`[]

dropbar_menu_t.menu *dropbar_menu_t.menu*
dropbar_menu_entry_t.virt_text *dropbar_menu_t.virt_text*

Symbols got from sources |dropbar-developers-classes-dropbar_source_t|
in the entry

Type ~
`string`[][]?

dropbar_menu_entry_t.menu *dropbar_menu_t.menu*

The menu the entry belongs to

Type ~
`dropbar_menu_t`?

dropbar_menu_t.idx *dropbar_menu_t.idx*
dropbar_menu_entry_t.idx *dropbar_menu_t.idx*

The index of the entry in the menu

Expand Down
25 changes: 24 additions & 1 deletion lua/dropbar/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ _G.dropbar.menus = {}
---@field separator dropbar_symbol_t
---@field padding {left: integer, right: integer}
---@field components dropbar_symbol_t[]
---@field virt_text string[][]?
---@field menu dropbar_menu_t? the menu the entry belongs to
---@field idx integer? the index of the entry in the menu
local dropbar_menu_entry_t = {}
Expand All @@ -26,6 +27,7 @@ dropbar_menu_entry_t.__index = dropbar_menu_entry_t
---@field separator dropbar_symbol_t?
---@field padding {left: integer, right: integer}?
---@field components dropbar_symbol_t[]?
---@field virt_text string[][]?
---@field menu dropbar_menu_t? the menu the entry belongs to
---@field idx integer? the index of the entry in the menu

Expand Down Expand Up @@ -398,8 +400,9 @@ end
---highlights to the buffer
function dropbar_menu_t:fill_buf()
local lines = {} ---@type string[]
local extmarks = {} ---@type table<integer, string[][]>
local hl_info = {} ---@type dropbar_menu_hl_info_t[][]
for _, entry in ipairs(self.entries) do
for i, entry in ipairs(self.entries) do
local line, entry_hl_info = entry:cat()
-- Pad lines with spaces to the width of the window
-- This is to make sure hl-DropBarMenuCurrentContext colors
Expand All @@ -413,9 +416,29 @@ function dropbar_menu_t:fill_buf()
)
)
table.insert(hl_info, entry_hl_info)
if entry.virt_text then
table.insert(extmarks, i, entry.virt_text)
end
end
vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, lines)
self:add_hl(hl_info)

local extmark_ns = vim.api.nvim_create_namespace('DropBarExtmarks')
vim.api.nvim_buf_clear_namespace(self.buf, extmark_ns, 0, -1)

for i, virt_text in pairs(extmarks) do
if type(i) == 'number' then
vim.api.nvim_buf_set_extmark(
self.buf,
extmark_ns,
i - 1, -- 0-indexed
0,
{
virt_lines = { virt_text },
}
)
end
end
end

---Make a buffer for the menu and set buffer-local keymaps
Expand Down

0 comments on commit 3daffc1

Please sign in to comment.