chore: migrate to classic (#2991)

* add classic, migrating nodes classes

* add mixins to classic

* typechecked optargs constructors for nodes

* typechecked optargs constructors for watcher and event

* luacheck

* typechecked optargs constructors for GitRunner

* typechecked optargs constructors for Sorter

* typechecked optargs constructors for decorators, WIP

* typechecked optargs constructors for decorators, WIP

* typechecked optargs constructors for decorators

* remove class

* replace enums with named maps

* Renderer and Builder use classic, tidy opts

* LiveFilter uses classic, tidy opts

* Filter uses classic, tidy opts

* add FilterTypes named map

* move toggles into filters

* Marks uses classic, tidy opts

* Sorter uses classic, tidy opts

* Clipboard uses classic, tidy opts

* use supers for node methods

* HighlightDisplay uses classic

* protected :new

* Watcher tidy

* Revert "use supers for node methods"

This reverts commit 9fc7a866ec.

* Watcher tidy

* format

* format

* Filters private methods

* format

* Sorter type safety

* Sorter type safety

* Sorter type safety

* Sorter type safety

* Sorter type safety

* Sorter type safety

* tidy Runner

* tidy hi-test name
This commit is contained in:
Alexander Courtis
2024-11-09 14:14:04 +11:00
committed by GitHub
parent 610a1c189b
commit 3fc8de198c
42 changed files with 995 additions and 1115 deletions

View File

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