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