aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-registry/sources/init.lua
blob: e4abe06263db7827acbf056340c8bc3846aa235e (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
local M = {}

---@param registry_id string
---@return fun(): RegistrySource # Thunk to instantiate provider.
local function parse(registry_id)
    local type, id = registry_id:match "^(.+):(.+)$"
    if type == "lua" then
        return function()
            local LuaRegistrySource = require "mason-registry.sources.lua"
            return LuaRegistrySource.new {
                id = registry_id,
                mod = id,
            }
        end
    elseif type ~= nil then
        error(("Unknown registry type %q: %q."):format(type, registry_id), 0)
    end
    error(("Malformed registry id: %q."):format(registry_id), 0)
end

---@type ((fun(): RegistrySource) | RegistrySource)[]
local registries = {}

---@param registry_ids string[]
function M.set_registries(registry_ids)
    for _, registry in ipairs(registry_ids) do
        local ok, err = pcall(function()
            table.insert(registries, parse(registry))
        end)
        if not ok then
            local log = require "mason-core.log"
            local notify = require "mason-core.notify"
            log.fmt_error("Failed to parse registry %q: %s", registry, err)
            notify(err, vim.log.levels.ERROR)
        end
    end
end

---@param opts? { include_uninstalled?: boolean }
function M.iter(opts)
    opts = opts or {}
    local i = 1
    return function()
        while i <= #registries do
            local registry = registries[i]
            if type(registry) == "function" then
                -- unwrap thunk
                registry = registry()
                registries[i] = registry
            end
            i = i + 1
            if opts.include_uninstalled or registry:is_installed() then
                return registry
            end
        end
    end
end

return M