typechecked optargs constructors for GitRunner

This commit is contained in:
Alexander Courtis
2024-11-06 16:53:01 +11:00
parent eab49e3d6d
commit b2f7b9a876
2 changed files with 43 additions and 38 deletions

View File

@@ -128,8 +128,8 @@ function M.reload_project(toplevel, path, callback)
return return
end end
---@type GitRunnerOpts ---@type GitRunnerArgs
local runner_opts = { local args = {
toplevel = toplevel, toplevel = toplevel,
path = path, path = path,
list_untracked = git_utils.should_show_untracked(toplevel), list_untracked = git_utils.should_show_untracked(toplevel),
@@ -139,14 +139,14 @@ function M.reload_project(toplevel, path, callback)
if callback then if callback then
---@param path_xy GitPathXY ---@param path_xy GitPathXY
runner_opts.callback = function(path_xy) args.callback = function(path_xy)
reload_git_project(toplevel, path, project, path_xy) reload_git_project(toplevel, path, project, path_xy)
callback() callback()
end end
GitRunner:run(runner_opts) GitRunner:run(args)
else else
-- TODO #1974 use callback once async/await is available -- TODO #1974 use callback once async/await is available
reload_git_project(toplevel, path, project, GitRunner:run(runner_opts)) reload_git_project(toplevel, path, project, GitRunner:run(args))
end end
end end

View File

@@ -2,9 +2,18 @@ local log = require("nvim-tree.log")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local notify = require("nvim-tree.notify") local notify = require("nvim-tree.notify")
local Class = require("nvim-tree.class") local Class = require("nvim-tree.classic")
---@class (exact) GitRunnerOpts ---@class (exact) GitRunner: Class
---@field private args GitRunnerArgs
---@field private path_xy GitPathXY
---@field private rc integer? -- -1 indicates timeout
local GitRunner = Class:extend()
---@class GitRunner
---@overload fun(args: GitRunnerArgs): GitRunner
---@class (exact) GitRunnerArgs
---@field toplevel string absolute path ---@field toplevel string absolute path
---@field path string? absolute path ---@field path string? absolute path
---@field list_untracked boolean ---@field list_untracked boolean
@@ -12,15 +21,16 @@ local Class = require("nvim-tree.class")
---@field timeout integer ---@field timeout integer
---@field callback fun(path_xy: GitPathXY)? ---@field callback fun(path_xy: GitPathXY)?
---@class (exact) GitRunner: Class
---@field private opts GitRunnerOpts
---@field private path_xy GitPathXY
---@field private rc integer? -- -1 indicates timeout
local GitRunner = Class:new()
local timeouts = 0 local timeouts = 0
local MAX_TIMEOUTS = 5 local MAX_TIMEOUTS = 5
---@private
---@param args GitRunnerArgs
function GitRunner:new(args)
self.args = args
self.path_xy = {}
end
---@private ---@private
---@param status string ---@param status string
---@param path string|nil ---@param path string|nil
@@ -34,7 +44,7 @@ function GitRunner:parse_status_output(status, path)
path = path:gsub("/", "\\") path = path:gsub("/", "\\")
end end
if #status > 0 and #path > 0 then if #status > 0 and #path > 0 then
self.path_xy[utils.path_remove_trailing(utils.path_join({ self.opts.toplevel, path }))] = status self.path_xy[utils.path_remove_trailing(utils.path_join({ self.args.toplevel, path }))] = status
end end
end end
@@ -81,11 +91,11 @@ end
---@param stderr_handle uv.uv_pipe_t ---@param stderr_handle uv.uv_pipe_t
---@return uv.spawn.options ---@return uv.spawn.options
function GitRunner:get_spawn_options(stdout_handle, stderr_handle) function GitRunner:get_spawn_options(stdout_handle, stderr_handle)
local untracked = self.opts.list_untracked and "-u" or nil local untracked = self.args.list_untracked and "-u" or nil
local ignored = (self.opts.list_untracked and self.opts.list_ignored) and "--ignored=matching" or "--ignored=no" local ignored = (self.args.list_untracked and self.args.list_ignored) and "--ignored=matching" or "--ignored=no"
return { return {
args = { "--no-optional-locks", "status", "--porcelain=v1", "-z", ignored, untracked, self.opts.path }, args = { "--no-optional-locks", "status", "--porcelain=v1", "-z", ignored, untracked, self.args.path },
cwd = self.opts.toplevel, cwd = self.args.toplevel,
stdio = { nil, stdout_handle, stderr_handle }, stdio = { nil, stdout_handle, stderr_handle },
} }
end end
@@ -139,7 +149,7 @@ function GitRunner:run_git_job(callback)
end end
local spawn_options = self:get_spawn_options(stdout, stderr) local spawn_options = self:get_spawn_options(stdout, stderr)
log.line("git", "running job with timeout %dms", self.opts.timeout) log.line("git", "running job with timeout %dms", self.args.timeout)
log.line("git", "git %s", table.concat(utils.array_remove_nils(spawn_options.args), " ")) log.line("git", "git %s", table.concat(utils.array_remove_nils(spawn_options.args), " "))
handle, pid = vim.loop.spawn( handle, pid = vim.loop.spawn(
@@ -151,7 +161,7 @@ function GitRunner:run_git_job(callback)
) )
timer:start( timer:start(
self.opts.timeout, self.args.timeout,
0, 0,
vim.schedule_wrap(function() vim.schedule_wrap(function()
on_finish(-1) on_finish(-1)
@@ -191,17 +201,17 @@ end
---@private ---@private
function GitRunner:finalise() function GitRunner:finalise()
if self.rc == -1 then if self.rc == -1 then
log.line("git", "job timed out %s %s", self.opts.toplevel, self.opts.path) log.line("git", "job timed out %s %s", self.args.toplevel, self.args.path)
timeouts = timeouts + 1 timeouts = timeouts + 1
if timeouts == MAX_TIMEOUTS then if timeouts == MAX_TIMEOUTS then
notify.warn(string.format("%d git jobs have timed out after git.timeout %dms, disabling git integration.", timeouts, notify.warn(string.format("%d git jobs have timed out after git.timeout %dms, disabling git integration.", timeouts,
self.opts.timeout)) self.args.timeout))
require("nvim-tree.git").disable_git_integration() require("nvim-tree.git").disable_git_integration()
end end
elseif self.rc ~= 0 then elseif self.rc ~= 0 then
log.line("git", "job fail rc %d %s %s", self.rc, self.opts.toplevel, self.opts.path) log.line("git", "job fail rc %d %s %s", self.rc, self.args.toplevel, self.args.path)
else else
log.line("git", "job success %s %s", self.opts.toplevel, self.opts.path) log.line("git", "job success %s %s", self.args.toplevel, self.args.path)
end end
end end
@@ -209,17 +219,17 @@ end
---@private ---@private
---@return GitPathXY? ---@return GitPathXY?
function GitRunner:execute() function GitRunner:execute()
local async = self.opts.callback ~= nil local async = self.args.callback ~= nil
local profile = log.profile_start("git %s job %s %s", async and "async" or "sync", self.opts.toplevel, self.opts.path) local profile = log.profile_start("git %s job %s %s", async and "async" or "sync", self.args.toplevel, self.args.path)
if async and self.opts.callback then if async and self.args.callback then
-- async, always call back -- async, always call back
self:run_git_job(function() self:run_git_job(function()
log.profile_end(profile) log.profile_end(profile)
self:finalise() self:finalise()
self.opts.callback(self.path_xy) self.args.callback(self.path_xy)
end) end)
else else
-- sync, maybe call back -- sync, maybe call back
@@ -230,8 +240,8 @@ function GitRunner:execute()
self:finalise() self:finalise()
if self.opts.callback then if self.args.callback then
self.opts.callback(self.path_xy) self.args.callback(self.path_xy)
else else
return self.path_xy return self.path_xy
end end
@@ -240,15 +250,10 @@ end
---Static method to run a git process, which will be killed if it takes more than timeout ---Static method to run a git process, which will be killed if it takes more than timeout
---Return nil when callback present ---Return nil when callback present
---@param opts GitRunnerOpts ---@param args GitRunnerArgs
---@return GitPathXY? ---@return GitPathXY?
function GitRunner:run(opts) function GitRunner:run(args)
---@type GitRunner local runner = GitRunner(args)
local runner = {
opts = opts,
path_xy = {},
}
runner = GitRunner:new(runner)
return runner:execute() return runner:execute()
end end