aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/ui/init.lua
blob: b40c0c47c5bac90ca887c5c4aa3345a9431d7f41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
local Data = require "nvim-lsp-installer.data"
local M = {}

---@alias NodeType
---| '"NODE"'
---| '"CASCADING_STYLE"'
---| '"VIRTUAL_TEXT"'
---| '"HL_TEXT"'
---| '"KEYBIND_HANDLER"'

---@alias INode Node | HlTextNode | CascadingStyleNode | VirtualTextNode | KeybindHandlerNode

---@param children INode[]
function M.Node(children)
    ---@class Node
    local node = {
        type = "NODE",
        children = children,
    }
    return node
end

---@param lines_with_span_tuples string[][]|string[]
function M.HlTextNode(lines_with_span_tuples)
    if type(lines_with_span_tuples[1]) == "string" then
        -- this enables a convenience API for just rendering a single line (with just a single span)
        lines_with_span_tuples = { { lines_with_span_tuples } }
    end
    ---@class HlTextNode
    local node = {
        type = "HL_TEXT",
        lines = lines_with_span_tuples,
    }
    return node
end

---@param lines string[]
function M.Text(lines)
    return M.HlTextNode(Data.list_map(function(line)
        return { { line, "" } }
    end, lines))
end

---@alias CascadingStyle
---| '"INDENT"'
---| '"CENTERED"'

---@param styles CascadingStyle[]
---@param children INode[]
function M.CascadingStyleNode(styles, children)
    ---@class CascadingStyleNode
    local node = {
        type = "CASCADING_STYLE",
        styles = styles,
        children = children,
    }
    return node
end

---@param virt_text string[][] @List of (text, highlight) tuples.
function M.VirtualTextNode(virt_text)
    ---@class VirtualTextNode
    local node = {
        type = "VIRTUAL_TEXT",
        virt_text = virt_text,
    }
    return node
end

---@param condition boolean
---@param node INode | fun(): INode
function M.When(condition, node)
    if condition then
        if type(node) == "function" then
            return node()
        else
            return node
        end
    end
    return M.Node {}
end

---@param key string @The keymap to register to. Example: "<CR>".
---@param effect string @The effect to call when keymap is triggered by the user.
---@param payload any @The payload to pass to the effect handler when triggered.
---@param is_global boolean @Whether to register the keybind to apply on all lines in the buffer.
function M.Keybind(key, effect, payload, is_global)
    ---@class KeybindHandlerNode
    local node = {
        type = "KEYBIND_HANDLER",
        key = key,
        effect = effect,
        payload = payload,
        is_global = is_global or false,
    }
    return node
end

function M.EmptyLine()
    return M.Text { "" }
end

---@param rows string[][][] @A list of rows to include in the table. Each row consists of an array of (text, highlight) tuples (aka spans).
function M.Table(rows)
    local col_maxwidth = {}
    for i = 1, #rows do
        local row = rows[i]
        for j = 1, #row do
            local col = row[j]
            local content = col[1]
            col_maxwidth[j] = math.max(#content, col_maxwidth[j] or 0)
        end
    end

    for i = 1, #rows do
        local row = rows[i]
        for j = 1, #row do
            local col = row[j]
            local content = col[1]
            col[1] = content .. string.rep(" ", (col_maxwidth[j] - #content) + 1) -- +1 for default minimum padding
        end
    end

    return M.HlTextNode(rows)
end

return M