refactor(commands): move ui/commands config to separate file

This commit is contained in:
Folke Lemaitre 2022-12-23 20:55:49 +01:00
parent c1f39f997d
commit 83270cc5e5
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
6 changed files with 195 additions and 86 deletions

View File

@ -434,11 +434,11 @@ Any operation can be started from the UI, with a sub command or an API function:
| `:Lazy home` | `require("lazy").home()` | Go back to plugin list |
| `:Lazy install [plugins]` | `require("lazy").install(opts?)` | Install missing plugins |
| `:Lazy load {plugins}` | `require("lazy").load(opts)` | Load a plugin that has not been loaded yet. Similar to `:packadd`. Like `:Lazy load foo.nvim` |
| `:Lazy log [plugins]` | `require("lazy").log(opts?)` | Show recent updates for all plugins |
| `:Lazy log [plugins]` | `require("lazy").log(opts?)` | Show recent updates |
| `:Lazy profile` | `require("lazy").profile()` | Show detailed profiling |
| `:Lazy restore [plugins]` | `require("lazy").restore(opts?)` | Updates all plugins to the state in the lockfile |
| `:Lazy sync [plugins]` | `require("lazy").sync(opts?)` | Run install, clean and update |
| `:Lazy update [plugins]` | `require("lazy").update(opts?)` | Update all plugins. This will also update the lockfile |
| `:Lazy update [plugins]` | `require("lazy").update(opts?)` | Update plugins. This will also update the lockfile |
<!-- commands:end -->

View File

@ -75,15 +75,7 @@ end
function M.commands()
local commands = require("lazy.view.commands").commands
---@type table<string,{desc:string, plugin:boolean, opts:boolean}>
local modes = {}
for _, mode in ipairs(require("lazy.view").modes) do
modes[mode.name] = modes[mode.name] or {}
modes[mode.name].plugin = modes[mode.name].plugin or mode.plugin
if not modes[mode.name].desc or not mode.plugin then
modes[mode.name].desc = mode.desc
end
end
local modes = require("lazy.view.config").commands
modes.load.opts = true
local lines = {
{ "Command", "Lua", "Description" },
@ -91,13 +83,13 @@ function M.commands()
}
Util.foreach(modes, function(name, mode)
if commands[name] then
if mode.opts then
if mode.plugins_required then
lines[#lines + 1] = {
("`:Lazy %s {plugins}`"):format(name),
([[`require("lazy").%s(opts)`]]):format(name),
mode.desc,
}
elseif mode.plugin then
elseif mode.plugins then
lines[#lines + 1] = {
("`:Lazy %s [plugins]`"):format(name),
([[`require("lazy").%s(opts?)`]]):format(name),

View File

@ -2,6 +2,7 @@ local View = require("lazy.view")
local Manage = require("lazy.manage")
local Util = require("lazy.util")
local Config = require("lazy.core.config")
local ViewConfig = require("lazy.view.config")
local M = {}
@ -9,7 +10,7 @@ local M = {}
---@param opts? ManagerOpts
function M.cmd(cmd, opts)
cmd = cmd == "" and "home" or cmd
local command = M.commands[cmd]
local command = M.commands[cmd] --[[@as fun(opts)]]
if command == nil then
Util.error("Invalid lazy command '" .. cmd .. "'")
else
@ -55,15 +56,10 @@ M.commands = {
}
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
if not ViewConfig.commands[cmd].plugins then
return
end
---@type string[]
local plugins = {}
for name, plugin in pairs(Config.plugins) do
if cmd ~= "load" or not plugin._.loaded then
@ -83,6 +79,7 @@ function M.setup()
local opts = { wait = cmd.bang == true }
local prefix, args = M.parse(cmd.args)
if #args > 0 then
---@param plugin string
opts.plugins = vim.tbl_map(function(plugin)
return Config.plugins[plugin]
end, args)

138
lua/lazy/view/config.lua Normal file
View File

@ -0,0 +1,138 @@
local M = {}
---@class LazyViewCommand
---@field id number
---@field plugins? boolean
---@field plugins_required? boolean
---@field button? boolean
---@field desc? string
---@field desc_plugin? string
---@field key? string
---@field key_plugin? string
---@field toggle? boolean
function M.get_commands()
---@type (LazyViewCommand|{name:string})[]
local ret = {}
for k, v in pairs(M.commands) do
v.name = k
ret[#ret + 1] = v
end
table.sort(ret, function(a, b)
return a.id < b.id
end)
return ret
end
M.keys = {
hover = "K",
close = "q",
details = "<cr>",
profile_sort = "<C-s>",
profile_filter = "<C-f>",
}
---@type table<string,LazyViewCommand>
M.commands = {
home = {
button = true,
desc = "Go back to plugin list",
id = 1,
key = "H",
},
install = {
button = true,
desc = "Install missing plugins",
desc_plugin = "Install a plugin",
id = 2,
key = "I",
key_plugin = "i",
plugins = true,
},
update = {
button = true,
desc = "Update plugins. This will also update the lockfile",
desc_plugin = "Update a plugin. This will also update the lockfile",
id = 3,
key = "U",
key_plugin = "u",
plugins = true,
},
sync = {
button = true,
desc = "Run install, clean and update",
desc_plugin = "Run install, clean and update",
id = 4,
key = "S",
plugins = true,
},
clean = {
button = true,
desc = "Clean plugins that are no longer needed",
desc_plugin = "Delete a plugin. WARNING: this will delete the plugin even if it should be installed!",
id = 5,
key = "X",
key_plugin = "x",
plugins = true,
},
check = {
button = true,
desc = "Check for updates and show the log (git fetch)",
desc_plugin = "Check for updates and show the log (git fetch)",
id = 6,
key = "C",
key_plugin = "c",
plugins = true,
},
log = {
button = true,
desc = "Show recent updates",
desc_plugin = "Show recent updates",
id = 7,
key = "L",
key_plugin = "gl",
plugins = true,
},
restore = {
button = true,
desc = "Updates all plugins to the state in the lockfile",
desc_plugin = "Restore a plugin to the state in the lockfile",
id = 8,
key = "R",
key_plugin = "r",
plugins = true,
},
profile = {
button = true,
desc = "Show detailed profiling",
id = 9,
key = "P",
toggle = true,
},
debug = {
button = true,
desc = "Show debug information",
id = 10,
key = "D",
toggle = true,
},
help = {
button = true,
desc = "Toggle this help page",
id = 11,
key = "?",
toggle = true,
},
clear = {
desc = "Clear finished tasks",
id = 12,
},
load = {
desc = "Load a plugin that has not been loaded yet. Similar to `:packadd`. Like `:Lazy load foo.nvim`",
id = 13,
plugins = true,
plugins_required = true,
},
}
return M

View File

@ -1,6 +1,7 @@
local Util = require("lazy.util")
local Render = require("lazy.view.render")
local Config = require("lazy.core.config")
local ViewConfig = require("lazy.view.config")
---@class LazyViewState
---@field mode string
@ -21,42 +22,6 @@ local default_state = {
---@field win_opts LazyViewWinOpts
local M = {}
M.modes = {
{ name = "home", key = "H", desc = "Go back to plugin list" },
{ name = "install", key = "I", desc = "Install missing plugins" },
{ name = "update", key = "U", desc = "Update all plugins. This will also update the lockfile" },
{ name = "sync", key = "S", desc = "Run install, clean and update" },
{ name = "clean", key = "X", desc = "Clean plugins that are no longer needed" },
{ name = "check", key = "C", desc = "Check for updates and show the log (git fetch)" },
{ name = "log", key = "L", desc = "Show recent updates for all plugins" },
{ name = "restore", key = "R", desc = "Updates all plugins to the state in the lockfile" },
{ name = "profile", key = "P", desc = "Show detailed profiling", toggle = true },
{ name = "debug", key = "D", desc = "Show debug information", toggle = true },
{ name = "help", key = "?", desc = "Toggle this help page", toggle = true },
{ name = "clear", desc = "Clear finished tasks", hide = true },
{
name = "load",
desc = "Load a plugin that has not been loaded yet. Similar to `:packadd`. Like `:Lazy load foo.nvim`",
hide = true,
plugin = true,
},
{ name = "sync", desc = "Run install, clean and update", hide = true, plugin = true },
{ plugin = true, name = "update", key = "u", desc = "Update this plugin. This will also update the lockfile" },
{
plugin = true,
name = "clean",
key = "x",
desc = "Delete this plugin. WARNING: this will delete the plugin even if it should be installed!",
},
{ plugin = true, name = "check", key = "c", desc = "Check for updates for this plugin and show the log (git fetch)" },
{ plugin = true, name = "install", key = "i", desc = "Install this plugin" },
{ plugin = true, name = "log", key = "gl", desc = "Show recent updates for this plugin" },
{ plugin = true, name = "restore", key = "r", desc = "Restore this plugin to the state in the lockfile" },
}
M.hover = "K"
---@type LazyView
M.view = nil
@ -83,7 +48,7 @@ function M.create(opts)
self.render = Render.new(self)
self.update = Util.throttle(Config.options.ui.throttle, self.update)
self:on_key("q", self.close)
self:on_key(ViewConfig.keys.close, self.close)
self:on({ "BufDelete", "BufLeave", "BufHidden" }, self.close, { once = true })
@ -95,7 +60,7 @@ function M.create(opts)
end)
-- plugin details
self:on_key("<cr>", function()
self:on_key(ViewConfig.keys.details, function()
local plugin = self.render:get_plugin()
if plugin then
self.state.plugin = self.state.plugin ~= plugin.name and plugin.name or nil
@ -103,14 +68,14 @@ function M.create(opts)
end
end)
self:on_key("<C-s>", function()
self:on_key(ViewConfig.keys.profile_sort, function()
if self.state.mode == "profile" then
self.state.profile.sort_time_taken = not self.state.profile.sort_time_taken
self:update()
end
end)
self:on_key("<C-f>", function()
self:on_key(ViewConfig.keys.profile_filter, function()
if self.state.mode == "profile" then
vim.ui.input({
prompt = "Enter time threshold in ms, like 0.5",
@ -160,12 +125,14 @@ end
---@param key string
---@param fn fun(self?)
function M:on_key(key, fn)
---@param desc? string
function M:on_key(key, fn, desc)
vim.keymap.set("n", key, function()
fn(self)
end, {
nowait = true,
buffer = self.buf,
desc = desc,
})
end
@ -285,7 +252,7 @@ function M:setup_hover()
end,
}
self:on_key(M.hover, function()
self:on_key(ViewConfig.keys.hover, function()
local line = vim.api.nvim_get_current_line()
local pos = vim.api.nvim_win_get_cursor(0)
local col = pos[2] + 1
@ -307,23 +274,23 @@ function M:setup_hover()
end
function M:setup_modes()
for _, m in ipairs(M.modes) do
local Commands = require("lazy.view.commands")
for name, m in pairs(ViewConfig.commands) do
if m.key then
self:on_key(m.key, function()
local Commands = require("lazy.view.commands")
if m.plugin then
local plugin = self.render:get_plugin()
if plugin then
Commands.cmd(m.name, { plugins = { plugin } })
end
else
if self.state.mode == m.name and m.toggle then
self.state.mode = "home"
return self:update()
end
Commands.cmd(m.name)
if self.state.mode == name and m.toggle then
return self:update("home")
end
end)
Commands.cmd(name)
end, m.desc)
end
if m.key_plugin then
self:on_key(m.key_plugin, function()
local plugin = self.render:get_plugin()
if plugin then
Commands.cmd(name, { plugins = { plugin } })
end
end, m.desc_plugin)
end
end
end

View File

@ -4,6 +4,7 @@ local Sections = require("lazy.view.sections")
local Handler = require("lazy.core.handler")
local Git = require("lazy.manage.git")
local Plugin = require("lazy.core.plugin")
local ViewConfig = require("lazy.view.config")
local Text = require("lazy.view.text")
@ -97,8 +98,8 @@ end
function M:title()
self:nl():nl()
for _, mode in ipairs(self.view.modes) do
if not mode.hide and not mode.plugin then
for _, mode in ipairs(ViewConfig.get_commands()) do
if mode.button then
local title = " " .. mode.name:sub(1, 1):upper() .. mode.name:sub(2) .. " (" .. mode.key .. ") "
if mode.name == "home" then
if self.view.state.mode == "home" then
@ -144,7 +145,6 @@ end
function M:help()
self:append("Help", "LazyH2"):nl():nl()
self:append("You can press "):append("<CR>", "LazySpecial"):append(" on a plugin to show its details."):nl()
self:append("You can press "):append("<CR>", "LazySpecial"):append(" on a plugin to show its details."):nl()
self:append("Most properties can be hovered with ")
@ -152,14 +152,29 @@ function M:help()
self:append(" to open links, help files, readmes and git commits."):nl():nl()
self:append("Keyboard Shortcuts", "LazyH2"):nl()
for _, mode in ipairs(self.view.modes) do
local title = mode.name:sub(1, 1):upper() .. mode.name:sub(2)
self:append("- ", "LazySpecial", { indent = 2 })
self:append(title, "Title")
for _, mode in ipairs(ViewConfig.get_commands()) do
if mode.key then
self:append(" <" .. mode.key .. ">", "LazyKey")
local title = mode.name:sub(1, 1):upper() .. mode.name:sub(2)
self:append("- ", "LazySpecial", { indent = 2 })
self:append(title, "Title")
if mode.key then
self:append(" <" .. mode.key .. ">", "LazyKey")
end
self:append(" " .. (mode.desc or "")):nl()
end
end
self:nl():append("Keyboard Shortcuts for Plugins", "LazyH2"):nl()
for _, mode in ipairs(ViewConfig.get_commands()) do
if mode.key_plugin then
local title = mode.name:sub(1, 1):upper() .. mode.name:sub(2)
self:append("- ", "LazySpecial", { indent = 2 })
self:append(title, "Title")
if mode.key_plugin then
self:append(" <" .. mode.key_plugin .. ">", "LazyKey")
end
self:append(" " .. (mode.desc_plugin or mode.desc)):nl()
end
self:append(" " .. (mode.desc or "")):nl()
end
end