REFACTOR: Updated from older config
This commit is contained in:
@@ -1,31 +1,19 @@
|
||||
local registry = require "mason-registry"
|
||||
|
||||
local M = {}
|
||||
|
||||
local function resolve_package(mason_package_name)
|
||||
local Optional = require "mason-core.optional"
|
||||
|
||||
local ok, pkg = pcall(registry.get_package, mason_package_name)
|
||||
if ok then
|
||||
return Optional.of_nilable(pkg)
|
||||
end
|
||||
end
|
||||
|
||||
---@param pkg Package The mason package to install.
|
||||
---@param version? string Pin the installation to a specific version.
|
||||
local function install_package(pkg, version)
|
||||
local linter_name = pkg.name
|
||||
vim.notify(("[mason-conform] installing %s"):format(pkg.name))
|
||||
|
||||
vim.notify(("[mason-conform] installing %s"):format(linter_name))
|
||||
|
||||
return pkg:install({ version = version }):once(
|
||||
pkg:install({ version = version }):once(
|
||||
"closed",
|
||||
vim.schedule_wrap(function()
|
||||
if pkg:is_installed() then
|
||||
vim.notify(("[mason-conform] %s was successfully installed"):format(linter_name))
|
||||
vim.notify(("[mason-conform] %s was successfully installed"):format(pkg.name))
|
||||
else
|
||||
vim.notify(
|
||||
("[mason-conform] failed to install %s. Installation logs are available in :Mason and :MasonLog")
|
||||
:format(
|
||||
linter_name
|
||||
("[mason-conform] failed to install %s. Installation logs are available in :Mason and :MasonLog"):format(
|
||||
pkg.name
|
||||
),
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
@@ -34,32 +22,30 @@ local function install_package(pkg, version)
|
||||
)
|
||||
end
|
||||
|
||||
---Install the mason package if it exists in the registry and is not already
|
||||
---installed or installing.
|
||||
---@param mason_package_name string Mason package identifier, optionally `name@version`.
|
||||
function M.try_install(mason_package_name)
|
||||
local Package = require "mason-core.package"
|
||||
local Package = require("mason-core.package")
|
||||
local registry = require("mason-registry")
|
||||
local package_name, version = Package.Parse(mason_package_name)
|
||||
|
||||
resolve_package(package_name)
|
||||
:if_present(
|
||||
function(pkg)
|
||||
if not pkg:is_installed() then
|
||||
if require("mason.version").MAJOR_VERSION == 2 then
|
||||
if pkg:is_installing() then
|
||||
return
|
||||
end
|
||||
end
|
||||
install_package(pkg, version)
|
||||
end
|
||||
end
|
||||
local ok, pkg = pcall(registry.get_package, package_name)
|
||||
if not ok or pkg == nil then
|
||||
vim.notify(
|
||||
("[mason-conform] Formatter %q is not available in the mason registry. Make sure it is a valid formatter name, or add it to 'ignore_install' to silence this warning."):format(
|
||||
package_name
|
||||
),
|
||||
vim.log.levels.WARN
|
||||
)
|
||||
:if_not_present(function()
|
||||
vim.notify(
|
||||
("[mason-conform] Formatter %q is not a valid entry in ensure_installed. Make sure to only provide valid formatter names.")
|
||||
:format(
|
||||
package_name
|
||||
),
|
||||
vim.log.levels.WARN
|
||||
)
|
||||
end)
|
||||
return
|
||||
end
|
||||
|
||||
if pkg:is_installed() or pkg:is_installing() then
|
||||
return
|
||||
end
|
||||
|
||||
install_package(pkg, version)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user