Skip to content

Commit 568d0f1

Browse files
committed
chore: resolve undefined-field
1 parent 3d00128 commit 568d0f1

File tree

9 files changed

+110
-111
lines changed

9 files changed

+110
-111
lines changed

lua/nvim-tree/explorer/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ function Explorer:reload_git()
478478
event_running = true
479479

480480
local projects = git.reload()
481-
self:reload_node_status(projects)
481+
git.reload_node_status(self, projects)
482482
self.renderer:draw()
483483
event_running = false
484484
end

lua/nvim-tree/explorer/watch.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local log = require("nvim-tree.log")
2+
local git = require("nvim-tree.git")
23
local utils = require("nvim-tree.utils")
34
local Watcher = require("nvim-tree.watcher").Watcher
45

@@ -76,7 +77,7 @@ function M.create_watcher(node)
7677
else
7778
log.line("watcher", "node event executing refresh '%s'", node.absolute_path)
7879
end
79-
node:refresh()
80+
git.refresh_dir(node)
8081
end)
8182
end
8283

lua/nvim-tree/git/init.lua

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local git_utils = require("nvim-tree.git.utils")
55
local GitRunner = require("nvim-tree.git.runner")
66
local Watcher = require("nvim-tree.watcher").Watcher
77
local Iterator = require("nvim-tree.iterators.node-iterator")
8-
local DirectoryNode = nil -- circular dependency
8+
local DirectoryNode = require("nvim-tree.node.directory")
99

1010
---@class GitStatus -- xy short-format statuses
1111
---@field file string?
@@ -289,46 +289,67 @@ function M.load_project_status(path)
289289
end
290290
end
291291

292-
---Git file and directory status for an absolute path with optional file fallback
293-
---@param parent_ignored boolean
294-
---@param status table|nil
295-
---@param path string
296-
---@param path_file string? alternative file path when no other file status
297-
---@return GitStatus|nil
298-
function M.git_status_dir(parent_ignored, status, path, path_file)
299-
if parent_ignored then
300-
return { file = "!!" }
301-
end
292+
---@param dir DirectoryNode
293+
---@param project table?
294+
---@param root string?
295+
function M.update_parent_statuses(dir, project, root)
296+
while project and dir do
297+
-- step up to the containing project
298+
if dir.absolute_path == root then
299+
-- stop at the top of the tree
300+
if not dir.parent then
301+
break
302+
end
302303

303-
if status then
304-
return {
305-
file = status.files and (status.files[path] or status.files[path_file]),
306-
dir = status.dirs and {
307-
direct = status.dirs.direct and status.dirs.direct[path],
308-
indirect = status.dirs.indirect and status.dirs.indirect[path],
309-
},
310-
}
304+
root = M.get_toplevel(dir.parent.absolute_path)
305+
306+
-- stop when no more projects
307+
if not root then
308+
break
309+
end
310+
311+
-- update the containing project
312+
project = M.get_project(root)
313+
M.reload_project(root, dir.absolute_path, nil)
314+
end
315+
316+
-- update status
317+
dir:update_git_status(dir.parent and dir.parent:is_git_ignored() or false, project)
318+
319+
-- maybe parent
320+
dir = dir.parent
311321
end
312322
end
313323

314-
---Git file status for an absolute path with optional fallback
315-
---@param parent_ignored boolean
316-
---@param status table|nil
317-
---@param path string
318-
---@param path_fallback string?
319-
---@return GitStatus
320-
function M.git_status_file(parent_ignored, status, path, path_fallback)
321-
if parent_ignored then
322-
return { file = "!!" }
323-
end
324+
---Refresh contents and git status for a single directory
325+
---@param dir DirectoryNode
326+
function M.refresh_dir(dir)
327+
local node = dir:get_parent_of_group() or dir
328+
local toplevel = M.get_toplevel(dir.absolute_path)
324329

325-
if not status or not status.files then
326-
return {}
327-
end
330+
M.reload_project(toplevel, dir.absolute_path, function()
331+
local project = M.get_project(toplevel) or {}
328332

329-
return {
330-
file = status.files[path] or status.files[path_fallback]
331-
}
333+
dir.explorer:reload(node, project)
334+
335+
M.update_parent_statuses(dir, project, toplevel)
336+
337+
dir.explorer.renderer:draw()
338+
end)
339+
end
340+
341+
---@param n DirectoryNode
342+
---@param projects table
343+
function M.reload_node_status(n, projects)
344+
local toplevel = M.get_toplevel(n.absolute_path)
345+
local status = projects[toplevel] or {}
346+
for _, node in ipairs(n.nodes) do
347+
node:update_git_status(n:is_git_ignored(), status)
348+
local dir = node:as(DirectoryNode)
349+
if dir and #dir.nodes > 0 then
350+
dir:reload_node_status(projects)
351+
end
352+
end
332353
end
333354

334355
function M.purge_state()
@@ -355,7 +376,6 @@ end
355376
function M.setup(opts)
356377
M.config.git = opts.git
357378
M.config.filesystem_watchers = opts.filesystem_watchers
358-
DirectoryNode = require("nvim-tree.node.directory")
359379
end
360380

361381
return M

lua/nvim-tree/git/utils.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,48 @@ function M.file_status_to_dir_status(status, cwd)
127127
return r
128128
end
129129

130+
---Git file status for an absolute path with optional fallback
131+
---@param parent_ignored boolean
132+
---@param status table|nil
133+
---@param path string
134+
---@param path_fallback string?
135+
---@return GitStatus
136+
function M.git_status_file(parent_ignored, status, path, path_fallback)
137+
if parent_ignored then
138+
return { file = "!!" }
139+
end
140+
141+
if not status or not status.files then
142+
return {}
143+
end
144+
145+
return {
146+
file = status.files[path] or status.files[path_fallback]
147+
}
148+
end
149+
150+
---Git file and directory status for an absolute path with optional file fallback
151+
---@param parent_ignored boolean
152+
---@param status table|nil
153+
---@param path string
154+
---@param path_file string? alternative file path when no other file status
155+
---@return GitStatus|nil
156+
function M.git_status_dir(parent_ignored, status, path, path_file)
157+
if parent_ignored then
158+
return { file = "!!" }
159+
end
160+
161+
if status then
162+
return {
163+
file = status.files and (status.files[path] or status.files[path_file]),
164+
dir = status.dirs and {
165+
direct = status.dirs.direct and status.dirs.direct[path],
166+
indirect = status.dirs.indirect and status.dirs.indirect[path],
167+
},
168+
}
169+
end
170+
end
171+
130172
function M.setup(opts)
131173
if opts.git.cygwin_support then
132174
M.use_cygpath = vim.fn.executable("cygpath") == 1

lua/nvim-tree/node/directory-link.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local git = require("nvim-tree.git")
1+
local git_utils = require("nvim-tree.git.utils")
22

33
local DirectoryNode = require("nvim-tree.node.directory")
44

@@ -40,7 +40,7 @@ end
4040
-----@param parent_ignored boolean
4141
-----@param status table|nil
4242
function DirectoryLinkNode:update_git_status(parent_ignored, status)
43-
self.git_status = git.git_status_dir(parent_ignored, status, self.link_to, self.absolute_path)
43+
self.git_status = git_utils.git_status_dir(parent_ignored, status, self.link_to, self.absolute_path)
4444
end
4545

4646
---Create a sanitized partial copy of a node, populating children recursively.

lua/nvim-tree/node/directory.lua

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
local git = require("nvim-tree.git")
2-
local watch = require("nvim-tree.explorer.watch")
1+
local git_utils = require("nvim-tree.git.utils")
32

43
local Node = require("nvim-tree.node")
54

@@ -46,7 +45,7 @@ function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat)
4645
}
4746
o = self:new(o) --[[@as DirectoryNode]]
4847

49-
o.watcher = watch.create_watcher(o)
48+
o.watcher = require("nvim-tree.explorer.watch").create_watcher(o)
5049

5150
return o
5251
end
@@ -70,7 +69,7 @@ end
7069
---@param parent_ignored boolean
7170
---@param status table|nil
7271
function DirectoryNode:update_git_status(parent_ignored, status)
73-
self.git_status = git.git_status_dir(parent_ignored, status, self.absolute_path, nil)
72+
self.git_status = git_utils.git_status_dir(parent_ignored, status, self.absolute_path, nil)
7473
end
7574

7675
---@return string[]? xy short-format statuses
@@ -123,35 +122,6 @@ function DirectoryNode:get_git_status()
123122
end
124123
end
125124

126-
---Refresh contents and git status for a single node
127-
function DirectoryNode:refresh()
128-
local node = self:get_parent_of_group() or self
129-
local toplevel = git.get_toplevel(self.absolute_path)
130-
131-
git.reload_project(toplevel, self.absolute_path, function()
132-
local project = git.get_project(toplevel) or {}
133-
134-
self.explorer:reload(node, project)
135-
136-
node:update_parent_statuses(project, toplevel)
137-
138-
self.explorer.renderer:draw()
139-
end)
140-
end
141-
142-
---@param projects table
143-
function DirectoryNode:reload_node_status(projects)
144-
local toplevel = git.get_toplevel(self.absolute_path)
145-
local status = projects[toplevel] or {}
146-
for _, node in ipairs(self.nodes) do
147-
node:update_git_status(self:is_git_ignored(), status)
148-
local dir = node:as(DirectoryNode)
149-
if dir and #dir.nodes > 0 then
150-
dir:reload_node_status(projects)
151-
end
152-
end
153-
end
154-
155125
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
156126
---@return DirectoryNode
157127
function DirectoryNode:last_group_node()

lua/nvim-tree/node/file-link.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local git = require("nvim-tree.git")
1+
local git_utils = require("nvim-tree.git.utils")
22

33
local FileNode = require("nvim-tree.node.file")
44

@@ -36,7 +36,7 @@ end
3636
-----@param parent_ignored boolean
3737
-----@param status table|nil
3838
function FileLinkNode:update_git_status(parent_ignored, status)
39-
self.git_status = git.git_status_file(parent_ignored, status, self.link_to, self.absolute_path)
39+
self.git_status = git_utils.git_status_file(parent_ignored, status, self.link_to, self.absolute_path)
4040
end
4141

4242
---Create a sanitized partial copy of a node

lua/nvim-tree/node/file.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local git = require("nvim-tree.git")
1+
local git_utils = require("nvim-tree.git.utils")
22
local utils = require("nvim-tree.utils")
33

44
local Node = require("nvim-tree.node")
@@ -44,7 +44,7 @@ end
4444
---@param parent_ignored boolean
4545
---@param status table|nil
4646
function FileNode:update_git_status(parent_ignored, status)
47-
self.git_status = git.git_status_file(parent_ignored, status, self.absolute_path, nil)
47+
self.git_status = git_utils.git_status_file(parent_ignored, status, self.absolute_path, nil)
4848
end
4949

5050
---@return string[]? xy short-format statuses

lua/nvim-tree/node/init.lua

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local git = require("nvim-tree.git")
2-
31
local Class = require("nvim-tree.class")
42

53
---Abstract Node class.
@@ -52,38 +50,6 @@ function Node:is_dotfile()
5250
return false
5351
end
5452

55-
---@param project table?
56-
---@param root string?
57-
function Node:update_parent_statuses(project, root)
58-
local node = self
59-
while project and node do
60-
-- step up to the containing project
61-
if node.absolute_path == root then
62-
-- stop at the top of the tree
63-
if not node.parent then
64-
break
65-
end
66-
67-
root = git.get_toplevel(node.parent.absolute_path)
68-
69-
-- stop when no more projects
70-
if not root then
71-
break
72-
end
73-
74-
-- update the containing project
75-
project = git.get_project(root)
76-
git.reload_project(root, node.absolute_path, nil)
77-
end
78-
79-
-- update status
80-
node:update_git_status(node.parent and node.parent:is_git_ignored() or false, project)
81-
82-
-- maybe parent
83-
node = node.parent
84-
end
85-
end
86-
8753
---Get the highest parent of grouped nodes, nil when not grouped
8854
---@return DirectoryNode?
8955
function Node:get_parent_of_group()

0 commit comments

Comments
 (0)