Skip to content

Commit b2f7b9a

Browse files
committed
typechecked optargs constructors for GitRunner
1 parent eab49e3 commit b2f7b9a

File tree

2 files changed

+43
-38
lines changed

2 files changed

+43
-38
lines changed

lua/nvim-tree/git/init.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ function M.reload_project(toplevel, path, callback)
128128
return
129129
end
130130

131-
---@type GitRunnerOpts
132-
local runner_opts = {
131+
---@type GitRunnerArgs
132+
local args = {
133133
toplevel = toplevel,
134134
path = path,
135135
list_untracked = git_utils.should_show_untracked(toplevel),
@@ -139,14 +139,14 @@ function M.reload_project(toplevel, path, callback)
139139

140140
if callback then
141141
---@param path_xy GitPathXY
142-
runner_opts.callback = function(path_xy)
142+
args.callback = function(path_xy)
143143
reload_git_project(toplevel, path, project, path_xy)
144144
callback()
145145
end
146-
GitRunner:run(runner_opts)
146+
GitRunner:run(args)
147147
else
148148
-- TODO #1974 use callback once async/await is available
149-
reload_git_project(toplevel, path, project, GitRunner:run(runner_opts))
149+
reload_git_project(toplevel, path, project, GitRunner:run(args))
150150
end
151151
end
152152

lua/nvim-tree/git/runner.lua

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,35 @@ local log = require("nvim-tree.log")
22
local utils = require("nvim-tree.utils")
33
local notify = require("nvim-tree.notify")
44

5-
local Class = require("nvim-tree.class")
5+
local Class = require("nvim-tree.classic")
66

7-
---@class (exact) GitRunnerOpts
7+
---@class (exact) GitRunner: Class
8+
---@field private args GitRunnerArgs
9+
---@field private path_xy GitPathXY
10+
---@field private rc integer? -- -1 indicates timeout
11+
local GitRunner = Class:extend()
12+
13+
---@class GitRunner
14+
---@overload fun(args: GitRunnerArgs): GitRunner
15+
16+
---@class (exact) GitRunnerArgs
817
---@field toplevel string absolute path
918
---@field path string? absolute path
1019
---@field list_untracked boolean
1120
---@field list_ignored boolean
1221
---@field timeout integer
1322
---@field callback fun(path_xy: GitPathXY)?
1423

15-
---@class (exact) GitRunner: Class
16-
---@field private opts GitRunnerOpts
17-
---@field private path_xy GitPathXY
18-
---@field private rc integer? -- -1 indicates timeout
19-
local GitRunner = Class:new()
20-
2124
local timeouts = 0
2225
local MAX_TIMEOUTS = 5
2326

27+
---@private
28+
---@param args GitRunnerArgs
29+
function GitRunner:new(args)
30+
self.args = args
31+
self.path_xy = {}
32+
end
33+
2434
---@private
2535
---@param status string
2636
---@param path string|nil
@@ -34,7 +44,7 @@ function GitRunner:parse_status_output(status, path)
3444
path = path:gsub("/", "\\")
3545
end
3646
if #status > 0 and #path > 0 then
37-
self.path_xy[utils.path_remove_trailing(utils.path_join({ self.opts.toplevel, path }))] = status
47+
self.path_xy[utils.path_remove_trailing(utils.path_join({ self.args.toplevel, path }))] = status
3848
end
3949
end
4050

@@ -81,11 +91,11 @@ end
8191
---@param stderr_handle uv.uv_pipe_t
8292
---@return uv.spawn.options
8393
function GitRunner:get_spawn_options(stdout_handle, stderr_handle)
84-
local untracked = self.opts.list_untracked and "-u" or nil
85-
local ignored = (self.opts.list_untracked and self.opts.list_ignored) and "--ignored=matching" or "--ignored=no"
94+
local untracked = self.args.list_untracked and "-u" or nil
95+
local ignored = (self.args.list_untracked and self.args.list_ignored) and "--ignored=matching" or "--ignored=no"
8696
return {
87-
args = { "--no-optional-locks", "status", "--porcelain=v1", "-z", ignored, untracked, self.opts.path },
88-
cwd = self.opts.toplevel,
97+
args = { "--no-optional-locks", "status", "--porcelain=v1", "-z", ignored, untracked, self.args.path },
98+
cwd = self.args.toplevel,
8999
stdio = { nil, stdout_handle, stderr_handle },
90100
}
91101
end
@@ -139,7 +149,7 @@ function GitRunner:run_git_job(callback)
139149
end
140150

141151
local spawn_options = self:get_spawn_options(stdout, stderr)
142-
log.line("git", "running job with timeout %dms", self.opts.timeout)
152+
log.line("git", "running job with timeout %dms", self.args.timeout)
143153
log.line("git", "git %s", table.concat(utils.array_remove_nils(spawn_options.args), " "))
144154

145155
handle, pid = vim.loop.spawn(
@@ -151,7 +161,7 @@ function GitRunner:run_git_job(callback)
151161
)
152162

153163
timer:start(
154-
self.opts.timeout,
164+
self.args.timeout,
155165
0,
156166
vim.schedule_wrap(function()
157167
on_finish(-1)
@@ -191,35 +201,35 @@ end
191201
---@private
192202
function GitRunner:finalise()
193203
if self.rc == -1 then
194-
log.line("git", "job timed out %s %s", self.opts.toplevel, self.opts.path)
204+
log.line("git", "job timed out %s %s", self.args.toplevel, self.args.path)
195205
timeouts = timeouts + 1
196206
if timeouts == MAX_TIMEOUTS then
197207
notify.warn(string.format("%d git jobs have timed out after git.timeout %dms, disabling git integration.", timeouts,
198-
self.opts.timeout))
208+
self.args.timeout))
199209
require("nvim-tree.git").disable_git_integration()
200210
end
201211
elseif self.rc ~= 0 then
202-
log.line("git", "job fail rc %d %s %s", self.rc, self.opts.toplevel, self.opts.path)
212+
log.line("git", "job fail rc %d %s %s", self.rc, self.args.toplevel, self.args.path)
203213
else
204-
log.line("git", "job success %s %s", self.opts.toplevel, self.opts.path)
214+
log.line("git", "job success %s %s", self.args.toplevel, self.args.path)
205215
end
206216
end
207217

208218
---Return nil when callback present
209219
---@private
210220
---@return GitPathXY?
211221
function GitRunner:execute()
212-
local async = self.opts.callback ~= nil
213-
local profile = log.profile_start("git %s job %s %s", async and "async" or "sync", self.opts.toplevel, self.opts.path)
222+
local async = self.args.callback ~= nil
223+
local profile = log.profile_start("git %s job %s %s", async and "async" or "sync", self.args.toplevel, self.args.path)
214224

215-
if async and self.opts.callback then
225+
if async and self.args.callback then
216226
-- async, always call back
217227
self:run_git_job(function()
218228
log.profile_end(profile)
219229

220230
self:finalise()
221231

222-
self.opts.callback(self.path_xy)
232+
self.args.callback(self.path_xy)
223233
end)
224234
else
225235
-- sync, maybe call back
@@ -230,8 +240,8 @@ function GitRunner:execute()
230240

231241
self:finalise()
232242

233-
if self.opts.callback then
234-
self.opts.callback(self.path_xy)
243+
if self.args.callback then
244+
self.args.callback(self.path_xy)
235245
else
236246
return self.path_xy
237247
end
@@ -240,15 +250,10 @@ end
240250

241251
---Static method to run a git process, which will be killed if it takes more than timeout
242252
---Return nil when callback present
243-
---@param opts GitRunnerOpts
253+
---@param args GitRunnerArgs
244254
---@return GitPathXY?
245-
function GitRunner:run(opts)
246-
---@type GitRunner
247-
local runner = {
248-
opts = opts,
249-
path_xy = {},
250-
}
251-
runner = GitRunner:new(runner)
255+
function GitRunner:run(args)
256+
local runner = GitRunner(args)
252257

253258
return runner:execute()
254259
end

0 commit comments

Comments
 (0)