2023-10-09 02:22:01 +08:00
|
|
|
local require = require("lazy.core.util").lazy_require
|
2023-10-09 17:25:42 +08:00
|
|
|
local Config = require("lazy.core.config")
|
2022-11-27 18:02:28 +08:00
|
|
|
local Manage = require("lazy.manage")
|
2022-11-25 05:04:23 +08:00
|
|
|
local Util = require("lazy.util")
|
2023-10-09 17:25:42 +08:00
|
|
|
local View = require("lazy.view")
|
2022-12-24 03:55:49 +08:00
|
|
|
local ViewConfig = require("lazy.view.config")
|
2022-11-21 06:25:28 +08:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
---@param cmd string
|
2022-12-22 05:27:36 +08:00
|
|
|
---@param opts? ManagerOpts
|
|
|
|
function M.cmd(cmd, opts)
|
2022-12-17 22:56:13 +08:00
|
|
|
cmd = cmd == "" and "home" or cmd
|
2022-12-24 03:55:49 +08:00
|
|
|
local command = M.commands[cmd] --[[@as fun(opts)]]
|
2022-11-21 06:25:28 +08:00
|
|
|
if command == nil then
|
|
|
|
Util.error("Invalid lazy command '" .. cmd .. "'")
|
2023-01-01 16:41:43 +08: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-21 06:25:28 +08:00
|
|
|
else
|
2022-12-22 05:27:36 +08:00
|
|
|
command(opts)
|
2022-11-21 06:25:28 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-18 18:42:27 +08:00
|
|
|
---@class LazyCommands
|
2022-11-21 06:25:28 +08:00
|
|
|
M.commands = {
|
2022-11-23 23:12:02 +08:00
|
|
|
clear = function()
|
2022-11-27 18:02:28 +08:00
|
|
|
Manage.clear()
|
2022-11-23 23:12:02 +08:00
|
|
|
View.show()
|
|
|
|
end,
|
2022-12-30 18:29:17 +08:00
|
|
|
health = function()
|
|
|
|
vim.cmd.checkhealth("lazy")
|
|
|
|
end,
|
2022-12-17 22:56:13 +08:00
|
|
|
home = function()
|
|
|
|
View.show("home")
|
2022-11-21 06:25:28 +08:00
|
|
|
end,
|
2022-12-18 18:42:27 +08:00
|
|
|
show = function()
|
|
|
|
View.show("home")
|
|
|
|
end,
|
2022-11-29 17:30:14 +08:00
|
|
|
help = function()
|
|
|
|
View.show("help")
|
|
|
|
end,
|
2022-12-05 21:46:46 +08:00
|
|
|
debug = function()
|
|
|
|
View.show("debug")
|
|
|
|
end,
|
2022-11-29 19:02:25 +08:00
|
|
|
profile = function()
|
|
|
|
View.show("profile")
|
|
|
|
end,
|
2022-12-22 05:27:36 +08:00
|
|
|
---@param opts ManagerOpts
|
|
|
|
load = function(opts)
|
2023-01-06 14:11:50 +08: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 21:19:10 +08:00
|
|
|
end,
|
2023-05-27 21:19:50 +08:00
|
|
|
reload = function(opts)
|
|
|
|
for _, plugin in pairs(opts.plugins) do
|
|
|
|
Util.warn("Reloading **" .. plugin.name .. "**")
|
|
|
|
require("lazy.core.loader").reload(plugin)
|
|
|
|
end
|
|
|
|
end,
|
2022-12-22 05:27:36 +08:00
|
|
|
log = Manage.log,
|
2023-01-01 16:41:43 +08:00
|
|
|
build = Manage.build,
|
2022-12-22 05:27:36 +08:00
|
|
|
clean = Manage.clean,
|
|
|
|
install = Manage.install,
|
|
|
|
sync = Manage.sync,
|
|
|
|
update = Manage.update,
|
|
|
|
check = Manage.check,
|
|
|
|
restore = Manage.restore,
|
2022-11-21 06:25:28 +08:00
|
|
|
}
|
|
|
|
|
2022-12-19 22:20:56 +08:00
|
|
|
function M.complete(cmd, prefix)
|
2022-12-29 00:39:31 +08:00
|
|
|
if not (ViewConfig.commands[cmd] or {}).plugins then
|
2022-12-19 22:20:56 +08:00
|
|
|
return
|
|
|
|
end
|
2022-12-24 03:55:49 +08:00
|
|
|
---@type string[]
|
2022-12-19 21:19:10 +08:00
|
|
|
local plugins = {}
|
2023-05-19 01:51:14 +08:00
|
|
|
if cmd == "load" then
|
|
|
|
plugins[#plugins + 1] = "all"
|
|
|
|
end
|
2022-12-19 21:19:10 +08:00
|
|
|
for name, plugin in pairs(Config.plugins) do
|
2022-12-19 22:20:56 +08:00
|
|
|
if cmd ~= "load" or not plugin._.loaded then
|
2022-12-19 21:19:10 +08:00
|
|
|
plugins[#plugins + 1] = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(plugins)
|
|
|
|
---@param key string
|
|
|
|
return vim.tbl_filter(function(key)
|
2023-01-01 16:40:51 +08:00
|
|
|
return key:find(prefix, 1, true) == 1
|
2022-12-19 21:19:10 +08:00
|
|
|
end, plugins)
|
|
|
|
end
|
|
|
|
|
2022-11-21 06:25:28 +08:00
|
|
|
function M.setup()
|
2022-12-19 21:19:10 +08:00
|
|
|
vim.api.nvim_create_user_command("Lazy", function(cmd)
|
2022-12-22 05:27:36 +08:00
|
|
|
---@type ManagerOpts
|
|
|
|
local opts = { wait = cmd.bang == true }
|
|
|
|
local prefix, args = M.parse(cmd.args)
|
2023-05-19 01:51:14 +08:00
|
|
|
if #args == 1 and args[1] == "all" then
|
|
|
|
args = vim.tbl_keys(Config.plugins)
|
|
|
|
end
|
2022-12-22 05:27:36 +08:00
|
|
|
if #args > 0 then
|
2022-12-24 03:55:49 +08:00
|
|
|
---@param plugin string
|
2022-12-23 00:24:58 +08:00
|
|
|
opts.plugins = vim.tbl_map(function(plugin)
|
|
|
|
return Config.plugins[plugin]
|
|
|
|
end, args)
|
2022-12-22 05:27:36 +08:00
|
|
|
end
|
|
|
|
M.cmd(prefix, opts)
|
2022-11-21 06:25:28 +08:00
|
|
|
end, {
|
2023-01-13 16:05:13 +08:00
|
|
|
bar = true,
|
2022-12-22 05:27:36 +08:00
|
|
|
bang = true,
|
2022-11-21 06:25:28 +08:00
|
|
|
nargs = "?",
|
|
|
|
desc = "Lazy",
|
|
|
|
complete = function(_, line)
|
2022-12-22 05:27:36 +08:00
|
|
|
local prefix, args = M.parse(line)
|
|
|
|
if #args > 0 then
|
|
|
|
return M.complete(prefix, args[#args])
|
2022-11-21 06:25:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param key string
|
|
|
|
return vim.tbl_filter(function(key)
|
2023-05-23 14:43:27 +08:00
|
|
|
return key:find(prefix, 1, true) == 1
|
2022-11-21 06:25:28 +08:00
|
|
|
end, vim.tbl_keys(M.commands))
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-12-22 05:27:36 +08:00
|
|
|
---@return string, string[]
|
|
|
|
function M.parse(args)
|
|
|
|
local parts = vim.split(vim.trim(args), "%s+")
|
|
|
|
if parts[1]:find("Lazy") then
|
|
|
|
table.remove(parts, 1)
|
|
|
|
end
|
|
|
|
if args:sub(-1) == " " then
|
|
|
|
parts[#parts + 1] = ""
|
|
|
|
end
|
|
|
|
return table.remove(parts, 1) or "", parts
|
|
|
|
end
|
|
|
|
|
2022-11-21 06:25:28 +08:00
|
|
|
return M
|