summaryrefslogtreecommitdiff
path: root/lua/mo/blink.lua
blob: ec68ec4e9173f4fdef4b68195df1908ce72c3275 (plain)
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
---@diagnostic disable: param-type-mismatch
---@diagnostic disable: missing-fields

local M = {}

M.setup = function()
    require("blink.cmp").setup({
        keymap = {
            preset = "default",
            ["<Tab>"] = {},
            ["<S-Tab>"] = {},
        },
        completion = {
            list = { selection = { preselect = false, auto_insert = true } },
        },
        snippets = { preset = "mini_snippets" },
        cmdline = { enabled = false },
        appearance = {
            nerd_font_variant = "normal",
        },
        sources = {
            default = { "lsp", "path", "snippets", "buffer" },
            providers = {
                buffer = {
                    min_keyword_length = function(ctx)
                        return ctx.trigger.initial_kind == "manual" and 0 or 3
                    end,
                    opts = { get_bufnrs = vim.api.nvim_list_bufs },
                },
                snippets = {
                    should_show_items = function(ctx)
                        return ctx.trigger.initial_kind ~= "trigger_character"
                    end,
                },
            },
        },
        fuzzy = { implementation = "prefer_rust" },
        signature = { enabled = true },
    })
end

---@param data string
---@param _opts { hl: string, status: "running"|"success"|"failed", id: (integer|string)?, fast: boolean }
---@return integer|string|nil
local function show_status(data, _opts)
    local opts = vim.tbl_extend(
        "keep",
        _opts,
        { hl = "Normal", status = "running", fast = false }
    )
    local fn = function()
        return vim.api.nvim_echo({ { data, opts.hl } }, false, {
            id = opts.id,
            kind = "progress",
            title = "Building blink-cmp",
            status = opts.status,
        })
    end
    if opts.fast then return fn() end
    vim.schedule(fn)
    return nil
end

M.build_rust = function()
    local blink_pack = vim.pack.get({ "blink.cmp" })[1]
    if blink_pack == nil then return end

    local msg_id = show_status(
        "Starting",
        { hl = "Comment", status = "running", fast = true }
    )

    local uv = vim.uv
    local stdout = uv.new_pipe()
    local stderr = uv.new_pipe()

    local on_exit = function(code, signal)
        show_status(
            ("Finished build (code=%s, signal=%s)"):format(code, signal),
            {
                hl = "Comment",
                status = code == 0 and "success" or "failed",
                id = msg_id,
                fast = false,
            }
        )
    end

    local on_stdout = function(err, data)
        assert(not err, err)
        if data then
            show_status(data, {
                hl = "Normal",
                status = "running",
                id = msg_id,
                fast = false,
            })
        end
    end

    local on_stderr = function(err, data)
        assert(not err, err)
        if data then
            show_status(data, {
                hl = "Comment",
                status = "running",
                id = msg_id,
                fast = false,
            })
        end
    end

    uv.spawn("cargo", {
        args = { "build", "--release" },
        stdio = { nil, stdout, stderr },
        cwd = blink_pack.path,
    }, on_exit)

    uv.read_start(stdout, on_stdout)
    uv.read_start(stderr, on_stderr)
end

return M