refactor: improve plugin check logic for better clarity and maintainability

This commit is contained in:
2024-11-08 12:34:29 -05:00
parent 362b412d55
commit 15a0028656
+55 -27
View File
@@ -636,52 +636,80 @@ end
--- Options: --- Options:
--- - string: The name of the plugin module to check. --- - string: The name of the plugin module to check.
--- - table: A list of plugin modules to check. --- - table: A list of plugin modules to check.
---@param simple boolean|nil Whether to use a simple check (pcall(require, plugin)). ---@param opts? table Additional options for the function.
--- - load (boolean): Whether to preload the plugin if found.
--- - simple (boolean): Whether to use a simple check (pcall(require, plugin)).
---@return boolean|table condition The installed state of the plugin(s). ---@return boolean|table condition The installed state of the plugin(s).
--- - boolean: The installed state of the plugin. --- - boolean: The installed state of the plugin.
--- - table: A list of installed states of the plugins. --- - table: A list of installed states of the plugins.
--- - boolean: The installed state of the plugin.
--- The return type is determined based on the input type. --- The return type is determined based on the input type.
--- If the input is a table of plugin modules, the return type is a table. --- If the input is a table of plugin modules, the return type is a table.
function M.is_installed(plugins, simple) function M.is_installed(plugins, opts)
opts = opts or {}
local is_single = type(plugins) == "string" local is_single = type(plugins) == "string"
if type(plugins) == "string" then if type(plugins) ~= "table" then
plugins = { plugins } plugins = { plugins }
end end
local load_plug = opts.load or false
local simple = opts.simple or false
local installed_plugins = {} local installed_plugins = {}
for _, plugin in ipairs(plugins) do for _, plugin in ipairs(plugins) do
local installed = false local installed = false
if simple then
installed = pcall(require, plugin) local function check_plugin(name)
else if simple then
local lazy_installed = pcall(require, "lazy") return pcall(require, name)
if lazy_installed then else
installed = require("lazy.core.config").plugins[plugin] ~= nil local lazy_installed = pcall(require, "lazy")
end if lazy_installed and require("lazy.core.config").plugins[name] then
local packer_installed = pcall(require, "packer_plugins") return true
if packer_installed then
---@diagnostic disable-next-line: undefined-field
installed = _G.packer_plugins and _G.packer_plugins[plugin] ~= nil
end
if vim.fn.exists("g:plugs") == 1 then
installed = vim.g.plugs[plugin] ~= nil
end
if not installed then
local has_plug = pcall(require, plugin)
if has_plug then
installed = true
end end
local packer_installed = pcall(require, "packer_plugins")
if
packer_installed
---@diagnostic disable-next-line: undefined-field
and _G.packer_plugins
---@diagnostic disable-next-line: undefined-field
and _G.packer_plugins[name]
then
return true
end
if vim.fn.exists("g:plugs") == 1 and vim.g.plugs[name] then
return true
end
return pcall(require, name)
end end
end end
-- Initial check with the plugin name as-is
installed = check_plugin(plugin)
-- If not installed, try replacing hyphens with underscores
if not installed and plugin:find("-") then
local underscored_name = plugin:gsub("-", "_")
installed = check_plugin(underscored_name)
end
installed_plugins[plugin] = installed installed_plugins[plugin] = installed
end end
if is_single then -- Optionally preload the plugin(s)
return installed_plugins[plugins[1]] if load_plug then
else local lazy_installed = pcall(require, "lazy")
return installed_plugins if lazy_installed then
for _, plugin in ipairs(plugins) do
if installed_plugins[plugin] then
require("lazy").load({ plugins = { plugin } })
end
end
else
M.notify("Unable to preload plugin(s)", "ERROR", "Lazy not found")
end
end end
return is_single and installed_plugins[plugins[1]] or installed_plugins
end end
---@function Function to create a floating terminal window ---@function Function to create a floating terminal window