aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-registry/sources
diff options
context:
space:
mode:
Diffstat (limited to 'lua/mason-registry/sources')
-rw-r--r--lua/mason-registry/sources/file.lua5
-rw-r--r--lua/mason-registry/sources/github.lua5
-rw-r--r--lua/mason-registry/sources/init.lua52
-rw-r--r--lua/mason-registry/sources/lua.lua5
4 files changed, 65 insertions, 2 deletions
diff --git a/lua/mason-registry/sources/file.lua b/lua/mason-registry/sources/file.lua
index 34c855c0..663efaaa 100644
--- a/lua/mason-registry/sources/file.lua
+++ b/lua/mason-registry/sources/file.lua
@@ -189,6 +189,11 @@ function FileRegistrySource:serialize()
}
end
+---@param other FileRegistrySource
+function FileRegistrySource:is_same_location(other)
+ return vim.fn.expand(self.spec.path) == vim.fn.expand(other.spec.path)
+end
+
function FileRegistrySource:__tostring()
return ("FileRegistrySource(path=%s)"):format(self.spec.path)
end
diff --git a/lua/mason-registry/sources/github.lua b/lua/mason-registry/sources/github.lua
index 5cc69023..597e7d84 100644
--- a/lua/mason-registry/sources/github.lua
+++ b/lua/mason-registry/sources/github.lua
@@ -171,6 +171,11 @@ function GitHubRegistrySource:serialize()
}
end
+---@param other GitHubRegistrySource
+function GitHubRegistrySource:is_same_location(other)
+ return self.spec.namespace == other.spec.namespace and self.spec.name == other.spec.name
+end
+
function GitHubRegistrySource:__tostring()
if self.spec.version then
return ("GitHubRegistrySource(repo=%s, version=%s)"):format(self.repo, self.spec.version)
diff --git a/lua/mason-registry/sources/init.lua b/lua/mason-registry/sources/init.lua
index c08a46a5..765d4904 100644
--- a/lua/mason-registry/sources/init.lua
+++ b/lua/mason-registry/sources/init.lua
@@ -1,3 +1,5 @@
+local log = require "mason-core.log"
+
---@class RegistrySource
---@field id string
---@field get_package fun(self: RegistrySource, pkg_name: string): Package?
@@ -7,6 +9,7 @@
---@field is_installed fun(self: RegistrySource): boolean
---@field install fun(self: RegistrySource): Result
---@field serialize fun(self: RegistrySource): InstallReceiptRegistry
+---@field is_same_location fun(self: RegistrySource, other: RegistrySource): boolean
---@alias RegistrySourceType '"github"' | '"lua"' | '"file"'
@@ -69,6 +72,18 @@ function LazySource:get()
return self.instance
end
+---@param other LazySource
+function LazySource:is_same_location(other)
+ if self.type == other.type then
+ return self:get():is_same_location(other:get())
+ end
+ return false
+end
+
+function LazySource:get_full_id()
+ return ("%s:%s"):format(self.type, self.id)
+end
+
function LazySource:__tostring()
return ("LazySource(type=%s, id=%s)"):format(self.type, self.id)
end
@@ -113,16 +128,40 @@ end
---@param registry string
function LazySourceCollection:append(registry)
- table.insert(self.list, parse(registry))
+ self:unique_insert(parse(registry))
return self
end
---@param registry string
function LazySourceCollection:prepend(registry)
- table.insert(self.list, 1, parse(registry))
+ self:unique_insert(parse(registry), 1)
return self
end
+---@param source LazySource
+---@param idx? integer
+function LazySourceCollection:unique_insert(source, idx)
+ idx = idx or #self.list + 1
+ if idx > 1 then
+ for i = 1, math.min(idx, #self.list) do
+ if self.list[i]:is_same_location(source) then
+ log.fmt_warn(
+ "Ignoring duplicate registry entry %s (duplicate of %s)",
+ source:get_full_id(),
+ self.list[i]:get_full_id()
+ )
+ return
+ end
+ end
+ end
+ for i = #self.list, idx, -1 do
+ if self.list[i]:is_same_location(source) then
+ table.remove(self.list, i)
+ end
+ end
+ table.insert(self.list, idx, source)
+end
+
function LazySourceCollection:is_all_installed()
for source in self:iterate { include_uninstalled = true } do
if not source:is_installed() then
@@ -171,6 +210,15 @@ function LazySourceCollection:to_list(opts)
return list
end
+---@param idx integer
+function LazySourceCollection:get(idx)
+ return self.list[idx]
+end
+
+function LazySourceCollection:size()
+ return #self.list
+end
+
function LazySourceCollection:__tostring()
return ("LazySourceCollection(list={%s})"):format(table.concat(vim.tbl_map(tostring, self.list), ", "))
end
diff --git a/lua/mason-registry/sources/lua.lua b/lua/mason-registry/sources/lua.lua
index 8b4aac33..40e728b6 100644
--- a/lua/mason-registry/sources/lua.lua
+++ b/lua/mason-registry/sources/lua.lua
@@ -88,6 +88,11 @@ function LuaRegistrySource:serialize()
}
end
+---@param other LuaRegistrySource
+function LuaRegistrySource:is_same_location(other)
+ return self.id == other.id
+end
+
function LuaRegistrySource:__tostring()
return ("LuaRegistrySource(mod=%s)"):format(self.spec.mod)
end