|
| 1 | +local M = {} |
| 2 | + |
| 3 | +---@class MiniPickItem |
| 4 | +---@field entry (string|string[])[] |
| 5 | + |
| 6 | +---Render items to be shown in the `mini.pick` picker menu |
| 7 | +---@param buf_id integer Buffer associated with the `mini.pick` picker |
| 8 | +---@param ns_id integer Namespace associated with the `mini.pick` picker |
| 9 | +---@param items MiniPickItem[] Items to be rendered into the picker menu |
| 10 | +M.show_items = function(buf_id, ns_id, items) |
| 11 | + local lines, highlights = {}, {} |
| 12 | + |
| 13 | + for _, item in ipairs(items) do |
| 14 | + local line, col = {}, 0 |
| 15 | + local hl_spans = {} |
| 16 | + |
| 17 | + for _, part in ipairs(item.entry) do |
| 18 | + if type(part) == "string" then |
| 19 | + part = { part } |
| 20 | + end |
| 21 | + local text, hl = part[1], part[2] |
| 22 | + table.insert(line, text) |
| 23 | + |
| 24 | + local start_col, end_col = col, col + #text |
| 25 | + if hl then |
| 26 | + table.insert(hl_spans, { hl = hl, start_col = start_col, end_col = end_col }) |
| 27 | + end |
| 28 | + |
| 29 | + col = end_col + 1 -- account for space |
| 30 | + table.insert(line, " ") |
| 31 | + end |
| 32 | + |
| 33 | + table.insert(lines, table.concat(line)) |
| 34 | + table.insert(highlights, hl_spans) |
| 35 | + end |
| 36 | + |
| 37 | + -- Set lines |
| 38 | + vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, lines) |
| 39 | + vim.api.nvim_buf_clear_namespace(buf_id, ns_id, 0, -1) |
| 40 | + |
| 41 | + -- Apply highlights on each line chunk |
| 42 | + local opts = { hl_mode = "combine", priority = 200 } |
| 43 | + for row, hl_spans in ipairs(highlights) do |
| 44 | + for _, span in ipairs(hl_spans) do |
| 45 | + opts.hl_group = span.hl |
| 46 | + opts.end_row, opts.end_col = row - 1, span.end_col |
| 47 | + vim.api.nvim_buf_set_extmark(buf_id, ns_id, row - 1, span.start_col, opts) |
| 48 | + end |
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | +return M |
0 commit comments