refactor(git): centralize registrations, defer submodule loads

This commit is contained in:
2026-04-30 16:23:18 +02:00
parent bf9cc73626
commit 8e6b13a7a3
7 changed files with 131 additions and 151 deletions
+11 -34
View File
@@ -18,11 +18,17 @@ local SPLIT_HANDLERS = {
---@type string[]?
local cached_cmds
---@param result vim.SystemCompleted
local function populate_cached_cmds(result)
---@return string[]
local function git_cmds()
if cached_cmds then
return cached_cmds
end
local result = vim
.system({ "git", "--list-cmds=main,others,alias" }, { text = true })
:wait()
if result.code ~= 0 then
util.error("git --list-cmds failed: %s", vim.trim(result.stderr or ""))
return
return {}
end
cached_cmds = {}
for line in (result.stdout or ""):gmatch("[^\r\n]+") do
@@ -31,26 +37,7 @@ local function populate_cached_cmds(result)
end
end
table.sort(cached_cmds)
end
local function prefetch_cmds()
vim.system(
{ "git", "--list-cmds=main,others,alias" },
{ text = true },
vim.schedule_wrap(populate_cached_cmds)
)
end
---@return string[]
local function git_cmds()
if cached_cmds then
return cached_cmds
end
populate_cached_cmds(
vim.system({ "git", "--list-cmds=main,others,alias" }, { text = true })
:wait()
)
return cached_cmds or {}
return cached_cmds
end
---@param args string[]
@@ -233,7 +220,7 @@ end
---@param arg_lead string
---@param cmd_line string
---@return string[]
local function complete(arg_lead, cmd_line, _)
function M.complete(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
@@ -248,14 +235,4 @@ local function complete(arg_lead, cmd_line, _)
return matches
end
function M.setup()
prefetch_cmds()
vim.api.nvim_create_user_command("G", function(opts)
M.run(opts.fargs)
end, {
nargs = "*",
complete = complete,
})
end
return M