2023-10-08 19:22:01 +01:00
|
|
|
local require = require("lazy.core.util").lazy_require
|
2023-10-09 10:25:42 +01:00
|
|
|
local Config = require("lazy.core.config")
|
2022-11-27 10:02:28 +00:00
|
|
|
local Manage = require("lazy.manage")
|
2022-11-24 21:04:23 +00:00
|
|
|
local Util = require("lazy.util")
|
2023-10-09 10:25:42 +01:00
|
|
|
local View = require("lazy.view")
|
2022-12-23 19:55:49 +00:00
|
|
|
local ViewConfig = require("lazy.view.config")
|
2022-11-20 22:25:28 +00:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
---@param cmd string
|
2022-12-21 21:27:36 +00:00
|
|
|
---@param opts? ManagerOpts
|
|
|
|
function M.cmd(cmd, opts)
|
2022-12-17 14:56:13 +00:00
|
|
|
cmd = cmd == "" and "home" or cmd
|
2022-12-23 19:55:49 +00:00
|
|
|
local command = M.commands[cmd] --[[@as fun(opts)]]
|
2022-11-20 22:25:28 +00:00
|
|
|
if command == nil then
|
|
|
|
Util.error("Invalid lazy command '" .. cmd .. "'")
|
2023-01-01 08:41:43 +00:00
|
|
|
elseif
|
|
|
|
ViewConfig.commands[cmd]
|
|
|
|
and ViewConfig.commands[cmd].plugins_required
|
|
|
|
and not (opts and vim.tbl_count(opts.plugins or {}) > 0)
|
|
|
|
then
|
|
|
|
return Util.error("`Lazy " .. cmd .. "` requires at least one plugin")
|
2022-11-20 22:25:28 +00:00
|
|
|
else
|
2022-12-21 21:27:36 +00:00
|
|
|
command(opts)
|
2022-11-20 22:25:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-18 10:42:27 +00:00
|
|
|
---@class LazyCommands
|
2022-11-20 22:25:28 +00:00
|
|
|
M.commands = {
|
2022-11-23 15:12:02 +00:00
|
|
|
clear = function()
|
2022-11-27 10:02:28 +00:00
|
|
|
Manage.clear()
|
2022-11-23 15:12:02 +00:00
|
|
|
View.show()
|
|
|
|
end,
|
2022-12-30 10:29:17 +00:00
|
|
|
health = function()
|
|
|
|
vim.cmd.checkhealth("lazy")
|
|
|
|
end,
|
2024-06-23 21:00:48 +01:00
|
|
|
---@param opts ManagerOpts
|
2024-06-18 18:40:58 +01:00
|
|
|
pkg = function(opts)
|
2024-06-18 20:54:54 +01:00
|
|
|
local Pkg = require("lazy.pkg")
|
2024-06-18 18:40:58 +01:00
|
|
|
Pkg.update()
|
|
|
|
require("lazy.manage.reloader").reload({
|
|
|
|
{
|
2024-06-18 20:54:54 +01:00
|
|
|
file = "pkg",
|
2024-06-18 18:40:58 +01:00
|
|
|
what = "changed",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
for _, plugin in pairs(opts and opts.plugins or {}) do
|
2024-06-23 21:00:48 +01:00
|
|
|
local spec = Pkg.get(plugin.dir)
|
2024-06-18 18:40:58 +01:00
|
|
|
Util.info(vim.inspect(spec), { lang = "lua", title = plugin.name })
|
|
|
|
end
|
|
|
|
end,
|
2022-12-17 14:56:13 +00:00
|
|
|
home = function()
|
|
|
|
View.show("home")
|
2022-11-20 22:25:28 +00:00
|
|
|
end,
|
2022-12-18 10:42:27 +00:00
|
|
|
show = function()
|
|
|
|
View.show("home")
|
|
|
|
end,
|
2022-11-29 09:30:14 +00:00
|
|
|
help = function()
|
|
|
|
View.show("help")
|
|
|
|
end,
|
2022-12-05 13:46:46 +00:00
|
|
|
debug = function()
|
|
|
|
View.show("debug")
|
|
|
|
end,
|
2022-11-29 11:02:25 +00:00
|
|
|
profile = function()
|
|
|
|
View.show("profile")
|
|
|
|
end,
|
2022-12-21 21:27:36 +00:00
|
|
|
---@param opts ManagerOpts
|
|
|
|
load = function(opts)
|
2023-01-06 06:11:50 +00:00
|
|
|
-- when a command is executed with a bang, wait will be set
|
|
|
|
require("lazy.core.loader").load(opts.plugins, { cmd = "Lazy load" }, { force = opts.wait })
|
2022-12-19 13:19:10 +00:00
|
|
|
end,
|
2023-05-27 14:19:50 +01:00
|
|
|
reload = function(opts)
|
|
|
|
for _, plugin in pairs(opts.plugins) do
|
2024-05-12 08:50:06 +01:00
|
|
|
if type(plugin) == "string" then
|
|
|
|
plugin = Config.plugins[plugin]
|
|
|
|
end
|
2023-05-27 14:19:50 +01:00
|
|
|
Util.warn("Reloading **" .. plugin.name .. "**")
|
|
|
|
require("lazy.core.loader").reload(plugin)
|
|
|
|
end
|
|
|
|
end,
|
2022-12-21 21:27:36 +00:00
|
|
|
log = Manage.log,
|
2023-01-01 08:41:43 +00:00
|
|
|
build = Manage.build,
|
2022-12-21 21:27:36 +00:00
|
|
|
clean = Manage.clean,
|
|
|
|
install = Manage.install,
|
|
|
|
sync = Manage.sync,
|
|
|
|
update = Manage.update,
|
|
|
|
check = Manage.check,
|
|
|
|
restore = Manage.restore,
|
2022-11-20 22:25:28 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 14:20:56 +00:00
|
|
|
function M.complete(cmd, prefix)
|
2024-06-18 18:40:58 +01:00
|
|
|
if not (ViewConfig.commands[cmd] or {}).plugins and cmd ~= "pkg" then
|
2022-12-19 14:20:56 +00:00
|
|
|
return
|
|
|
|
end
|
2022-12-23 19:55:49 +00:00
|
|
|
---@type string[]
|
2022-12-19 13:19:10 +00:00
|
|
|
local plugins = {}
|
2023-05-18 18:51:14 +01:00
|
|
|
if cmd == "load" then
|
|
|
|
plugins[#plugins + 1] = "all"
|
|
|
|
end
|
2022-12-19 13:19:10 +00:00
|
|
|
for name, plugin in pairs(Config.plugins) do
|
2022-12-19 14:20:56 +00:00
|
|
|
if cmd ~= "load" or not plugin._.loaded then
|
2022-12-19 13:19:10 +00:00
|
|
|
plugins[#plugins + 1] = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(plugins)
|
|
|
|
---@param key string
|
|
|
|
return vim.tbl_filter(function(key)
|
2023-01-01 08:40:51 +00:00
|
|
|
return key:find(prefix, 1, true) == 1
|
2022-12-19 13:19:10 +00:00
|
|
|
end, plugins)
|
|
|
|
end
|
|
|
|
|
2022-11-20 22:25:28 +00:00
|
|
|
function M.setup()
|
2022-12-19 13:19:10 +00:00
|
|
|
vim.api.nvim_create_user_command("Lazy", function(cmd)
|
2022-12-21 21:27:36 +00:00
|
|
|
---@type ManagerOpts
|
|
|
|
local opts = { wait = cmd.bang == true }
|
|
|
|
local prefix, args = M.parse(cmd.args)
|
2023-05-18 18:51:14 +01:00
|
|
|
if #args == 1 and args[1] == "all" then
|
|
|
|
args = vim.tbl_keys(Config.plugins)
|
|
|
|
end
|
2022-12-21 21:27:36 +00:00
|
|
|
if #args > 0 then
|
2022-12-23 19:55:49 +00:00
|
|
|
---@param plugin string
|
2022-12-22 16:24:58 +00:00
|
|
|
opts.plugins = vim.tbl_map(function(plugin)
|
|
|
|
return Config.plugins[plugin]
|
|
|
|
end, args)
|
2022-12-21 21:27:36 +00:00
|
|
|
end
|
|
|
|
M.cmd(prefix, opts)
|
2022-11-20 22:25:28 +00:00
|
|
|
end, {
|
2023-01-13 08:05:13 +00:00
|
|
|
bar = true,
|
2022-12-21 21:27:36 +00:00
|
|
|
bang = true,
|
2022-11-20 22:25:28 +00:00
|
|
|
nargs = "?",
|
|
|
|
desc = "Lazy",
|
|
|
|
complete = function(_, line)
|
2022-12-21 21:27:36 +00:00
|
|
|
local prefix, args = M.parse(line)
|
|
|
|
if #args > 0 then
|
|
|
|
return M.complete(prefix, args[#args])
|
2022-11-20 22:25:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param key string
|
|
|
|
return vim.tbl_filter(function(key)
|
2023-05-23 07:43:27 +01:00
|
|
|
return key:find(prefix, 1, true) == 1
|
2022-11-20 22:25:28 +00:00
|
|
|
end, vim.tbl_keys(M.commands))
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-12-21 21:27:36 +00:00
|
|
|
---@return string, string[]
|
|
|
|
function M.parse(args)
|
|
|
|
local parts = vim.split(vim.trim(args), "%s+")
|
2024-10-02 08:52:51 +01:00
|
|
|
if vim.startswith("Lazy", parts[1]) then
|
2022-12-21 21:27:36 +00:00
|
|
|
table.remove(parts, 1)
|
|
|
|
end
|
|
|
|
if args:sub(-1) == " " then
|
|
|
|
parts[#parts + 1] = ""
|
|
|
|
end
|
|
|
|
return table.remove(parts, 1) or "", parts
|
|
|
|
end
|
|
|
|
|
2022-11-20 22:25:28 +00:00
|
|
|
return M
|