From ae0b87181db0ac10b60cfb35c8f4691234444a9d Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 20 Nov 2022 23:25:28 +0100 Subject: [PATCH] feat: lazy commands --- lua/lazy/view/commands.lua | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 lua/lazy/view/commands.lua diff --git a/lua/lazy/view/commands.lua b/lua/lazy/view/commands.lua new file mode 100644 index 0000000..67b6ba9 --- /dev/null +++ b/lua/lazy/view/commands.lua @@ -0,0 +1,68 @@ +local View = require("lazy.view") +local Manager = require("lazy.manager") + +local M = {} + +---@param cmd string +function M.cmd(cmd) + cmd = cmd == "" and "show" or cmd + local command = M.commands[cmd] + if command == nil then + Util.error("Invalid lazy command '" .. cmd .. "'") + else + command() + end +end + +M.commands = { + clean = function() + Manager.clean({ clear = true, show = true }) + end, + install = function() + Manager.install({ clear = true, show = true }) + end, + show = function() + View.show() + end, + sync = function() + Manager.update({ clear = true, show = true }) + Manager.install({ show = true }) + Manager.clean({ show = true }) + end, + update = function() + Manager.update({ clear = true, show = true }) + end, +} + +function M.setup() + vim.api.nvim_create_user_command("Lazy", function(args) + M.cmd(vim.trim(args.args or "")) + end, { + nargs = "?", + desc = "Lazy", + complete = function(_, line) + if line:match("^%s*Lazy %w+ ") then + return {} + end + + local prefix = line:match("^%s*Lazy (%w*)") or "" + + ---@param key string + return vim.tbl_filter(function(key) + return key:find(prefix) == 1 + end, vim.tbl_keys(M.commands)) + end, + }) + + for name in pairs(M.commands) do + local cmd = "Lazy" .. name:sub(1, 1):upper() .. name:sub(2) + + vim.api.nvim_create_user_command(cmd, function() + M.cmd(name) + end, { + desc = "Lazy " .. name, + }) + end +end + +return M