Files
nvim/lua/core/commands.lua
T

126 lines
3.3 KiB
Lua

local log = require("log")
vim.api.nvim_create_user_command(
"Q",
"q<bang>",
{ bang = true, desc = "Alias to :q" }
)
vim.api.nvim_create_user_command(
"Qa",
"qa<bang>",
{ bang = true, desc = "Alias to :qa" }
)
local function complete_plugin_names(lead)
return vim.tbl_filter(function(name)
return name:find(lead, 1, true) == 1
end, require("pack").get_names())
end
---@class ow.PluginSubcommand
---@field run fun(args: string[])
---@field min_args? integer
---@field max_args? integer
---@field complete? fun(lead: string): string[]
---@type table<string, ow.PluginSubcommand>
local plugin_cmds = {
watch = {
max_args = 0,
run = function()
require("pack").watch()
end,
},
unwatch = {
max_args = 0,
run = function()
require("pack").unwatch()
end,
},
reload = {
min_args = 1,
max_args = 1,
complete = complete_plugin_names,
run = function(args)
require("pack").reload_plugin(args[1] --[[@as -nil]], true)
end,
},
update = {
complete = complete_plugin_names,
run = function(args)
require("pack").update(#args > 0 and args or nil)
end,
},
["update-offline"] = {
complete = complete_plugin_names,
run = function(args)
require("pack").update(
#args > 0 and args or nil,
{ offline = true }
)
end,
},
restore = {
complete = complete_plugin_names,
run = function(args)
require("pack").update(
#args > 0 and args or nil,
{ target = "lockfile" }
)
end,
},
}
local plugin_cmd_names = vim.tbl_keys(plugin_cmds)
table.sort(plugin_cmd_names)
vim.api.nvim_create_user_command("Plugin", function(opts)
local cmd_name = opts.fargs[1]
local cmd = plugin_cmds[cmd_name]
if not cmd then
log.error(
"Plugin: unknown subcommand %q (expected one of: %s)",
cmd_name or "",
table.concat(plugin_cmd_names, ", ")
)
return
end
local args = vim.list_slice(opts.fargs, 2)
local bound, n = nil, #args
if cmd.min_args and n < cmd.min_args then
bound = { "least", cmd.min_args }
elseif cmd.max_args and n > cmd.max_args then
bound = { "most", cmd.max_args }
end
if bound then
log.error(
"Plugin %s: expected at %s %d argument(s), got %d",
cmd_name,
bound[1],
bound[2],
n
)
return
end
cmd.run(args)
end, {
nargs = "+",
complete = function(arg_lead, cmd_line)
local rest = cmd_line:gsub("^%s*%S+%s*", "", 1)
local words = vim.split(rest, "%s+", { trimempty = false })
if #words <= 1 then
return vim.tbl_filter(function(name)
return name:find(arg_lead, 1, true) == 1
end, plugin_cmd_names)
end
local cmd = plugin_cmds[words[1]]
if not cmd or not cmd.complete then
return {}
end
return cmd.complete(arg_lead)
end,
desc = "Plugin management (subcommands: "
.. table.concat(plugin_cmd_names, ", ")
.. ")",
})