perf(git): minor performance cleanups
This commit is contained in:
+28
-11
@@ -18,16 +18,8 @@ local SPLIT_HANDLERS = {
|
||||
---@type string[]?
|
||||
local cached_cmds
|
||||
|
||||
---@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()
|
||||
---@param result vim.SystemCompleted
|
||||
local function populate_cached_cmds(result)
|
||||
cached_cmds = {}
|
||||
if result.code == 0 then
|
||||
for line in (result.stdout or ""):gmatch("[^\r\n]+") do
|
||||
@@ -37,7 +29,31 @@ local function git_cmds()
|
||||
end
|
||||
table.sort(cached_cmds)
|
||||
end
|
||||
return cached_cmds
|
||||
end
|
||||
|
||||
---Prime `cached_cmds` asynchronously so the first `:G <Tab>` doesn't block.
|
||||
local function prefetch_cmds()
|
||||
vim.system(
|
||||
{ "git", "--list-cmds=main,others,alias" },
|
||||
{ text = true },
|
||||
function(result)
|
||||
vim.schedule(function()
|
||||
populate_cached_cmds(result)
|
||||
end)
|
||||
end
|
||||
)
|
||||
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 {}
|
||||
end
|
||||
|
||||
---@param content string
|
||||
@@ -204,6 +220,7 @@ local function complete(arg_lead, cmd_line, _)
|
||||
end
|
||||
|
||||
function M.setup()
|
||||
prefetch_cmds()
|
||||
vim.api.nvim_create_user_command("G", function(opts)
|
||||
M.run(opts.fargs)
|
||||
end, {
|
||||
|
||||
Reference in New Issue
Block a user