2022-11-21 06:25:28 +08:00
|
|
|
local View = require("lazy.view")
|
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")
|
2022-12-19 21:19:10 +08:00
|
|
|
local Config = require("lazy.core.config")
|
2022-11-21 06:25:28 +08:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
---@param cmd string
|
2022-11-29 17:56:17 +08:00
|
|
|
---@param plugins? LazyPlugin[]
|
|
|
|
function M.cmd(cmd, plugins)
|
2022-12-17 22:56:13 +08:00
|
|
|
cmd = cmd == "" and "home" or cmd
|
2022-11-21 06:25:28 +08:00
|
|
|
local command = M.commands[cmd]
|
|
|
|
if command == nil then
|
|
|
|
Util.error("Invalid lazy command '" .. cmd .. "'")
|
|
|
|
else
|
2022-11-29 17:56:17 +08:00
|
|
|
command(plugins)
|
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-29 17:56:17 +08:00
|
|
|
clean = function(plugins)
|
2022-12-01 06:14:16 +08:00
|
|
|
Manage.clean({ clear = true, mode = "clean", plugins = plugins })
|
2022-11-21 06:25:28 +08:00
|
|
|
end,
|
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-11-21 06:25:28 +08:00
|
|
|
install = function()
|
2022-12-01 06:14:16 +08:00
|
|
|
Manage.install({ clear = true, mode = "install" })
|
2022-11-23 04:12:50 +08:00
|
|
|
end,
|
2022-11-29 17:56:17 +08:00
|
|
|
log = function(plugins)
|
2022-12-01 06:14:16 +08:00
|
|
|
Manage.log({ clear = true, mode = "log", plugins = plugins })
|
2022-11-21 06:25:28 +08:00
|
|
|
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-11-21 06:25:28 +08:00
|
|
|
sync = function()
|
2022-12-01 06:14:16 +08:00
|
|
|
Manage.clean({ clear = true, wait = true, mode = "sync" })
|
2022-12-18 18:42:27 +08:00
|
|
|
Manage.update()
|
|
|
|
Manage.install()
|
2022-11-21 06:25:28 +08:00
|
|
|
end,
|
2022-11-29 17:56:17 +08:00
|
|
|
update = function(plugins)
|
2022-12-01 06:14:16 +08:00
|
|
|
Manage.update({ clear = true, mode = "update", plugins = plugins })
|
2022-11-21 06:25:28 +08:00
|
|
|
end,
|
2022-11-29 17:56:17 +08:00
|
|
|
check = function(plugins)
|
2022-12-01 06:14:16 +08:00
|
|
|
Manage.check({ clear = true, mode = "check", plugins = plugins })
|
2022-11-29 15:23:23 +08:00
|
|
|
end,
|
2022-11-29 17:56:17 +08:00
|
|
|
restore = function(plugins)
|
2022-12-01 06:14:16 +08:00
|
|
|
Manage.update({ clear = true, lockfile = true, mode = "restore", plugins = plugins })
|
2022-11-29 07:15:13 +08:00
|
|
|
end,
|
2022-12-19 21:19:10 +08:00
|
|
|
load = function(plugins)
|
|
|
|
require("lazy.core.loader").load(plugins, { cmd = "LazyLoad" })
|
|
|
|
end,
|
2022-11-21 06:25:28 +08:00
|
|
|
}
|
|
|
|
|
2022-12-19 22:20:56 +08:00
|
|
|
function M.complete(cmd, prefix)
|
|
|
|
local with_plugins = false
|
|
|
|
for _, mode in ipairs(View.modes) do
|
|
|
|
if mode.name == cmd and mode.plugin then
|
|
|
|
with_plugins = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not with_plugins then
|
|
|
|
return
|
|
|
|
end
|
2022-12-19 21:19:10 +08:00
|
|
|
local plugins = {}
|
|
|
|
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)
|
|
|
|
return key:find(prefix) == 1
|
|
|
|
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)
|
|
|
|
local args = vim.split(vim.trim(cmd.args or ""), " ")
|
|
|
|
local name = args[1]
|
|
|
|
table.remove(args, 1)
|
|
|
|
M.cmd(name, #args > 0 and args or nil)
|
2022-11-21 06:25:28 +08:00
|
|
|
end, {
|
|
|
|
nargs = "?",
|
|
|
|
desc = "Lazy",
|
|
|
|
complete = function(_, line)
|
2022-12-19 21:19:10 +08:00
|
|
|
---@type string?
|
2022-12-19 22:20:56 +08:00
|
|
|
local cmd, prefix = line:match("^%s*Lazy (%w+) (%w*)")
|
2022-12-19 21:19:10 +08:00
|
|
|
if prefix then
|
2022-12-19 22:20:56 +08:00
|
|
|
return M.complete(cmd, prefix)
|
2022-12-19 21:19:10 +08:00
|
|
|
end
|
|
|
|
|
2022-11-21 06:25:28 +08:00
|
|
|
if line:match("^%s*Lazy %w+ ") then
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
2022-12-19 21:19:10 +08:00
|
|
|
prefix = line:match("^%s*Lazy (%w*)") or ""
|
2022-11-21 06:25:28 +08:00
|
|
|
|
|
|
|
---@param key string
|
|
|
|
return vim.tbl_filter(function(key)
|
|
|
|
return key:find(prefix) == 1
|
|
|
|
end, vim.tbl_keys(M.commands))
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|