Skip to content

Commit 2fba44f

Browse files
committed
chore: resolve undefined-field
1 parent a16e67f commit 2fba44f

File tree

7 files changed

+101
-74
lines changed

7 files changed

+101
-74
lines changed

lua/nvim-tree/explorer/filters.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ local function custom(self, path)
178178
end
179179

180180
---Prepare arguments for should_filter. This is done prior to should_filter for efficiency reasons.
181-
---@param git_status table|nil optional results of git.load_project_status(...)
181+
---@param git_status table|nil optional results of git.load_project(...)
182182
---@return table
183183
--- git_status: reference
184184
--- bufinfo: empty unless no_buffer set: vim.fn.getbufinfo { buflisted = 1 }

lua/nvim-tree/explorer/init.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ function Explorer:refresh_parent_nodes_for_path(path)
321321
local project = git.get_project(toplevel) or {}
322322

323323
self:reload(node, project)
324-
git.update_parent_statuses(node, project, toplevel)
324+
git.update_parent_projects(node, project, toplevel)
325325
end
326326

327327
log.profile_end(profile)
@@ -331,7 +331,7 @@ end
331331
---@param node DirectoryNode
332332
function Explorer:_load(node)
333333
local cwd = node.link_to or node.absolute_path
334-
local git_status = git.load_project_status(cwd)
334+
local git_status = git.load_project(cwd)
335335
self:explore(node, git_status, self)
336336
end
337337

@@ -423,7 +423,7 @@ function Explorer:explore(node, status, parent)
423423
local single_child = node:single_child_directory()
424424
if config.renderer.group_empty and not is_root and single_child then
425425
local child_cwd = single_child.link_to or single_child.absolute_path
426-
local child_status = git.load_project_status(child_cwd)
426+
local child_status = git.load_project(child_cwd)
427427
node.group_next = single_child
428428
local ns = self:explore(single_child, child_status, parent)
429429
node.nodes = ns or {}
@@ -463,7 +463,7 @@ function Explorer:reload_explorer()
463463
end
464464
event_running = true
465465

466-
local projects = git.reload()
466+
local projects = git.reload_all_projects()
467467
self:refresh_nodes(projects)
468468
if view.is_visible() then
469469
self.renderer:draw()
@@ -477,7 +477,7 @@ function Explorer:reload_git()
477477
end
478478
event_running = true
479479

480-
local projects = git.reload()
480+
local projects = git.reload_all_projects()
481481
git.reload_node_status(self, projects)
482482
self.renderer:draw()
483483
event_running = false

lua/nvim-tree/git/init.lua

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,39 @@ local Watcher = require("nvim-tree.watcher").Watcher
77
local Iterator = require("nvim-tree.iterators.node-iterator")
88
local DirectoryNode = require("nvim-tree.node.directory")
99

10-
---@class (exact) GitStatus -- xy short-format statuses
10+
---Git xy short-format statuses for a single node
11+
---@class (exact) GitStatus
1112
---@field file string?
1213
---@field dir table<"direct" | "indirect", string[]>?
1314

15+
-- Git xy short-format status
16+
---@alias GitPathXY table<string, string>
17+
18+
-- Git xy short-format statuses
19+
---@alias GitPathXYs table<string, string[]>
20+
21+
---@alias GitProjectFiles GitPathXY
22+
---@alias GitProjectDirs table<"direct" | "indirect", GitPathXYs>
23+
24+
---Git state for an entire repo
25+
---@class (exact) GitProject
26+
---@field files GitProjectFiles?
27+
---@field dirs GitProjectDirs?
28+
---@field watcher Watcher?
29+
1430
local M = {
1531
config = {},
1632

17-
-- all projects keyed by toplevel
33+
---all projects keyed by toplevel
34+
---@type table<string, GitProject>
1835
_projects_by_toplevel = {},
1936

20-
-- index of paths inside toplevels, false when not inside a project
37+
---index of paths inside toplevels, false when not inside a project
38+
---@type table<string, string|false>
2139
_toplevels_by_path = {},
2240

2341
-- git dirs by toplevel
42+
---@type table<string, string>
2443
_git_dirs_by_toplevel = {},
2544
}
2645

@@ -36,33 +55,33 @@ local WATCHED_FILES = {
3655

3756
---@param toplevel string|nil
3857
---@param path string|nil
39-
---@param project table
40-
---@param statuses GitXYByPath?
41-
local function reload_git_statuses(toplevel, path, project, statuses)
58+
---@param project GitProject
59+
---@param project_files GitProjectFiles?
60+
local function reload_git_project(toplevel, path, project, project_files)
4261
if path then
4362
for p in pairs(project.files) do
4463
if p:find(path, 1, true) == 1 then
4564
project.files[p] = nil
4665
end
4766
end
48-
project.files = vim.tbl_deep_extend("force", project.files, statuses)
67+
project.files = vim.tbl_deep_extend("force", project.files, project_files)
4968
else
50-
project.files = statuses
69+
project.files = project_files or {}
5170
end
5271

53-
project.dirs = git_utils.file_status_to_dir_status(project.files, toplevel)
72+
project.dirs = git_utils.project_files_to_project_dirs(project.files, toplevel)
5473
end
5574

5675
--- Is this path in a known ignored directory?
5776
---@param path string
58-
---@param project table git status
77+
---@param project GitProject
5978
---@return boolean
6079
local function path_ignored_in_project(path, project)
6180
if not path or not project then
6281
return false
6382
end
6483

65-
if project and project.files then
84+
if project.files then
6685
for file, status in pairs(project.files) do
6786
if status == "!!" and vim.startswith(path, file) then
6887
return true
@@ -72,9 +91,8 @@ local function path_ignored_in_project(path, project)
7291
return false
7392
end
7493

75-
--- Reload all projects
76-
---@return table projects maybe empty
77-
function M.reload()
94+
---@return GitProject[] maybe empty
95+
function M.reload_all_projects()
7896
if not M.config.git.enable then
7997
return {}
8098
end
@@ -87,11 +105,12 @@ function M.reload()
87105
end
88106

89107
--- Reload one project. Does nothing when no project or path is ignored
90-
---@param toplevel string|nil
91-
---@param path string|nil optional path to update only
92-
---@param callback function|nil
108+
---@param toplevel string?
109+
---@param path string? optional path to update only
110+
---@param callback function?
93111
function M.reload_project(toplevel, path, callback)
94-
local project = M._projects_by_toplevel[toplevel]
112+
local project = M._projects_by_toplevel[toplevel] --[[@as GitProject]]
113+
95114
if not toplevel or not project or not M.config.git.enable then
96115
if callback then
97116
callback()
@@ -116,21 +135,21 @@ function M.reload_project(toplevel, path, callback)
116135
}
117136

118137
if callback then
119-
---@param statuses GitXYByPath
138+
---@param statuses GitPathXY
120139
runner_opts.callback = function(statuses)
121-
reload_git_statuses(toplevel, path, project, statuses)
140+
reload_git_project(toplevel, path, project, statuses)
122141
callback()
123142
end
124143
GitRunner:run(runner_opts)
125144
else
126145
-- TODO #1974 use callback once async/await is available
127-
reload_git_statuses(toplevel, path, project, GitRunner:run(runner_opts))
146+
reload_git_project(toplevel, path, project, GitRunner:run(runner_opts))
128147
end
129148
end
130149

131150
--- Retrieve a known project
132-
---@param toplevel string|nil
133-
---@return table|nil project
151+
---@param toplevel string?
152+
---@return GitProject? project
134153
function M.get_project(toplevel)
135154
return M._projects_by_toplevel[toplevel]
136155
end
@@ -151,11 +170,10 @@ function M.get_toplevel(path)
151170
return nil
152171
end
153172

154-
if M._toplevels_by_path[path] then
155-
return M._toplevels_by_path[path]
156-
end
157-
158-
if M._toplevels_by_path[path] == false then
173+
local tl = M._toplevels_by_path[path]
174+
if tl then
175+
return tl
176+
elseif tl == false then
159177
return nil
160178
end
161179

@@ -194,8 +212,15 @@ function M.get_toplevel(path)
194212
end
195213

196214
M._toplevels_by_path[path] = toplevel
215+
197216
M._git_dirs_by_toplevel[toplevel] = git_dir
198-
return M._toplevels_by_path[path]
217+
218+
toplevel = M._toplevels_by_path[path]
219+
if toplevel == false then
220+
return nil
221+
else
222+
return toplevel
223+
end
199224
end
200225

201226
local function reload_tree_at(toplevel)
@@ -230,8 +255,8 @@ end
230255
--- Load the project status for a path. Does nothing when no toplevel for path.
231256
--- Only fetches project status when unknown, otherwise returns existing.
232257
---@param path string absolute
233-
---@return table project maybe empty
234-
function M.load_project_status(path)
258+
---@return GitProject maybe empty
259+
function M.load_project(path)
235260
if not M.config.git.enable then
236261
return {}
237262
end
@@ -242,12 +267,12 @@ function M.load_project_status(path)
242267
return {}
243268
end
244269

245-
local status = M._projects_by_toplevel[toplevel]
246-
if status then
247-
return status
270+
local project = M._projects_by_toplevel[toplevel]
271+
if project then
272+
return project
248273
end
249274

250-
local statuses = GitRunner:run({
275+
local path_xys = GitRunner:run({
251276
toplevel = toplevel,
252277
list_untracked = git_utils.should_show_untracked(toplevel),
253278
list_ignored = true,
@@ -275,10 +300,10 @@ function M.load_project_status(path)
275300
})
276301
end
277302

278-
if statuses then
303+
if path_xys then
279304
M._projects_by_toplevel[toplevel] = {
280-
files = statuses,
281-
dirs = git_utils.file_status_to_dir_status(statuses, toplevel),
305+
files = path_xys,
306+
dirs = git_utils.project_files_to_project_dirs(path_xys, toplevel),
282307
watcher = watcher,
283308
}
284309
return M._projects_by_toplevel[toplevel]
@@ -289,9 +314,9 @@ function M.load_project_status(path)
289314
end
290315

291316
---@param dir DirectoryNode
292-
---@param project table?
317+
---@param project GitProject?
293318
---@param root string?
294-
function M.update_parent_statuses(dir, project, root)
319+
function M.update_parent_projects(dir, project, root)
295320
while project and dir do
296321
-- step up to the containing project
297322
if dir.absolute_path == root then
@@ -331,14 +356,14 @@ function M.refresh_dir(dir)
331356

332357
dir.explorer:reload(node, project)
333358

334-
M.update_parent_statuses(dir, project, toplevel)
359+
M.update_parent_projects(dir, project, toplevel)
335360

336361
dir.explorer.renderer:draw()
337362
end)
338363
end
339364

340365
---@param dir DirectoryNode?
341-
---@param projects table
366+
---@param projects GitProject[]
342367
function M.reload_node_status(dir, projects)
343368
dir = dir and dir:as(DirectoryNode)
344369
if not dir or #dir.nodes == 0 then

lua/nvim-tree/git/runner.lua

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@ local notify = require("nvim-tree.notify")
44

55
local Class = require("nvim-tree.class")
66

7-
---@alias GitXYByPath table<string, string> -- short-format statuses
8-
97
---@class (exact) GitRunnerOpts
108
---@field toplevel string absolute path
119
---@field path string? absolute path
1210
---@field list_untracked boolean
1311
---@field list_ignored boolean
1412
---@field timeout integer
15-
---@field callback fun(statuses: GitXYByPath)?
13+
---@field callback fun(statuses: GitPathXY)?
1614

1715
---@class (exact) GitRunner: Class
1816
---@field private opts GitRunnerOpts
19-
---@field private statuses GitXYByPath
17+
---@field private statuses GitPathXY
2018
---@field private rc integer? -- -1 indicates timeout
2119
local GitRunner = Class:new()
2220

@@ -207,7 +205,8 @@ function GitRunner:finalise()
207205
end
208206
end
209207

210-
---@return GitXYByPath? statuses nil if callback present
208+
---@private
209+
---@return GitPathXY? statuses nil if callback present
211210
function GitRunner:execute()
212211
local async = self.opts.callback ~= nil
213212
local profile = log.profile_start("git %s job %s %s", async and "async" or "sync", self.opts.toplevel, self.opts.path)
@@ -240,7 +239,7 @@ end
240239

241240
---Static method to run a git process, which will be killed if it takes more than timeout
242241
---@param opts GitRunnerOpts
243-
---@return GitXYByPath? statuses nil if callback present
242+
---@return GitPathXY? statuses nil if callback present
244243
function GitRunner:run(opts)
245244
---@type GitRunner
246245
local runner = {

0 commit comments

Comments
 (0)