aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-registry/sources
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2023-02-20 22:19:39 +0100
committerGitHub <noreply@github.com>2023-02-20 22:19:39 +0100
commitb8a6632a0f2d263199d5d480ca85477fe0f414ab (patch)
tree57e1ee0f3cef078ec144ecdf1b2fa861acf47755 /lua/mason-registry/sources
parentchore: autogenerate (#1015) (diff)
downloadmason-b8a6632a0f2d263199d5d480ca85477fe0f414ab.tar
mason-b8a6632a0f2d263199d5d480ca85477fe0f414ab.tar.gz
mason-b8a6632a0f2d263199d5d480ca85477fe0f414ab.tar.bz2
mason-b8a6632a0f2d263199d5d480ca85477fe0f414ab.tar.lz
mason-b8a6632a0f2d263199d5d480ca85477fe0f414ab.tar.xz
mason-b8a6632a0f2d263199d5d480ca85477fe0f414ab.tar.zst
mason-b8a6632a0f2d263199d5d480ca85477fe0f414ab.zip
feat: configurable registries (#1016)
Diffstat (limited to 'lua/mason-registry/sources')
-rw-r--r--lua/mason-registry/sources/init.lua57
-rw-r--r--lua/mason-registry/sources/lua.lua43
2 files changed, 100 insertions, 0 deletions
diff --git a/lua/mason-registry/sources/init.lua b/lua/mason-registry/sources/init.lua
new file mode 100644
index 00000000..39b7fad5
--- /dev/null
+++ b/lua/mason-registry/sources/init.lua
@@ -0,0 +1,57 @@
+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
+
+function M.iter()
+ 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 registry:is_installed() then
+ return registry
+ end
+ end
+ end
+end
+
+return M
diff --git a/lua/mason-registry/sources/lua.lua b/lua/mason-registry/sources/lua.lua
new file mode 100644
index 00000000..94b4e4f8
--- /dev/null
+++ b/lua/mason-registry/sources/lua.lua
@@ -0,0 +1,43 @@
+---@class LuaRegistrySourceSpec
+---@field id string
+---@field mod string
+
+---@class LuaRegistrySource : RegistrySource
+---@field private spec LuaRegistrySourceSpec
+local LuaRegistrySource = {}
+LuaRegistrySource.__index = LuaRegistrySource
+
+---@param spec LuaRegistrySourceSpec
+function LuaRegistrySource.new(spec)
+ return setmetatable({
+ spec = spec,
+ }, LuaRegistrySource)
+end
+
+function LuaRegistrySource:get_package(pkg_name)
+ local index = require(self.spec.mod)
+ if index[pkg_name] then
+ return require(index[pkg_name])
+ end
+end
+
+function LuaRegistrySource:get_all_package_names()
+ local index = require(self.spec.mod)
+ return vim.tbl_keys(index)
+end
+
+function LuaRegistrySource:is_installed()
+ local ok = pcall(require, self.spec.mod)
+ return ok
+end
+
+function LuaRegistrySource:install()
+ local Result = require "mason-core.result"
+ return Result.success()
+end
+
+function LuaRegistrySource:__tostring()
+ return ("LuaRegistry(mod=%s)"):format(self.spec.mod)
+end
+
+return LuaRegistrySource