2022-12-05 21:45:50 +08:00
|
|
|
local Loader = require("lazy.core.loader")
|
2023-10-09 17:25:42 +08:00
|
|
|
local Util = require("lazy.core.util")
|
2022-12-05 21:45:50 +08:00
|
|
|
|
|
|
|
---@class LazyCmdHandler:LazyHandler
|
|
|
|
local M = {}
|
|
|
|
|
2022-12-16 16:13:08 +08:00
|
|
|
function M:_load(cmd)
|
2022-12-05 21:45:50 +08:00
|
|
|
vim.api.nvim_del_user_command(cmd)
|
|
|
|
Util.track({ cmd = cmd })
|
2022-12-16 16:13:08 +08:00
|
|
|
Loader.load(self.active[cmd], { cmd = cmd })
|
2022-12-05 21:45:50 +08:00
|
|
|
Util.track()
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param cmd string
|
2022-12-16 16:13:08 +08:00
|
|
|
function M:_add(cmd)
|
2022-12-05 21:45:50 +08:00
|
|
|
vim.api.nvim_create_user_command(cmd, function(event)
|
2023-02-08 03:56:54 +08:00
|
|
|
local command = {
|
|
|
|
cmd = cmd,
|
|
|
|
bang = event.bang or nil,
|
|
|
|
mods = event.smods,
|
|
|
|
args = event.fargs,
|
2023-02-10 01:02:40 +08:00
|
|
|
count = event.count >= 0 and event.range == 0 and event.count or nil,
|
2023-02-08 03:56:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if event.range == 1 then
|
|
|
|
command.range = { event.line1 }
|
|
|
|
elseif event.range == 2 then
|
|
|
|
command.range = { event.line1, event.line2 }
|
|
|
|
end
|
|
|
|
|
2023-04-18 16:17:22 +08:00
|
|
|
---@type string
|
|
|
|
local plugins = "`" .. table.concat(vim.tbl_values(self.active[cmd]), ", ") .. "`"
|
|
|
|
|
2022-12-16 16:13:08 +08:00
|
|
|
self:_load(cmd)
|
2023-03-17 06:54:09 +08:00
|
|
|
|
2023-10-14 23:30:24 +08:00
|
|
|
local info = vim.api.nvim_get_commands({})[cmd] or vim.api.nvim_buf_get_commands(0, {})[cmd]
|
2023-04-18 16:17:22 +08:00
|
|
|
if not info then
|
|
|
|
return Util.error("Command `" .. cmd .. "` not found after loading " .. plugins)
|
|
|
|
end
|
|
|
|
|
2023-03-17 06:54:09 +08:00
|
|
|
command.nargs = info.nargs
|
|
|
|
if event.args and event.args ~= "" and info.nargs and info.nargs:find("[1?]") then
|
|
|
|
command.args = { event.args }
|
|
|
|
end
|
2023-02-08 03:56:54 +08:00
|
|
|
vim.cmd(command)
|
2022-12-05 21:45:50 +08:00
|
|
|
end, {
|
|
|
|
bang = true,
|
2022-12-22 17:36:56 +08:00
|
|
|
range = true,
|
2022-12-05 21:45:50 +08:00
|
|
|
nargs = "*",
|
2022-12-13 16:48:17 +08:00
|
|
|
complete = function(_, line)
|
2022-12-16 16:13:08 +08:00
|
|
|
self:_load(cmd)
|
2022-12-13 16:48:17 +08:00
|
|
|
-- NOTE: return the newly loaded command completion
|
|
|
|
return vim.fn.getcompletion(line, "cmdline")
|
2022-12-05 21:45:50 +08:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param value string
|
2022-12-30 00:32:54 +08:00
|
|
|
function M:_del(value)
|
2022-12-05 21:45:50 +08:00
|
|
|
pcall(vim.api.nvim_del_user_command, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|