aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/ui/init.lua
blob: 4f8d69356f58bcb0296c80ee77a24f547fc8d85c (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
local Data = require "nvim-lsp-installer.data"
local M = {}

M.NodeType = Data.enum {
    "NODE",
    "CASCADING_STYLE",
    "VIRTUAL_TEXT",
    "HL_TEXT",
    "KEYBIND_HANDLER",
}

function M.Node(children)
    return {
        type = M.NodeType.NODE,
        children = children,
    }
end

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
    return {
        type = M.NodeType.HL_TEXT,
        lines = lines_with_span_tuples,
    }
end

function M.Text(lines)
    return M.HlTextNode(Data.list_map(function(line)
        return { { line, "" } }
    end, lines))
end

M.CascadingStyle = Data.enum {
    "INDENT",
    "CENTERED",
}

function M.CascadingStyleNode(styles, children)
    return {
        type = M.NodeType.CASCADING_STYLE,
        styles = styles,
        children = children,
    }
end

function M.VirtualTextNode(virt_text)
    return {
        type = M.NodeType.VIRTUAL_TEXT,
        virt_text = virt_text,
    }
end

function M.When(condition, a)
    if condition then
        if type(a) == "function" then
            return a()
        else
            return a
        end
    end
    return M.Node {}
end

function M.Keybind(key, effect, payload, is_global)
    return {
        type = M.NodeType.KEYBIND_HANDLER,
        key = key,
        effect = effect,
        payload = payload,
        is_global = is_global or false,
    }
end

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

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