Skip to content

Commit ebe5cca

Browse files
committed
move mostly unchanged DirectoryNode methods back to BaseNode
1 parent d4242af commit ebe5cca

File tree

2 files changed

+59
-95
lines changed

2 files changed

+59
-95
lines changed

lua/nvim-tree/node/directory.lua

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ local BaseNode = require("nvim-tree.node")
99
---@field nodes Node[]
1010
---@field open boolean
1111
---@field hidden_stats table? -- Each field of this table is a key for source and value for count
12-
local DirectoryNode = BaseNode.dn
13-
-- local DirectoryNode = BaseNode:new()
12+
local DirectoryNode = BaseNode:new()
1413

1514
---Static factory method
1615
---@param explorer Explorer
@@ -67,6 +66,55 @@ function DirectoryNode:update_git_status(parent_ignored, status)
6766
self.git_status = git.git_status_dir(parent_ignored, status, self.absolute_path)
6867
end
6968

69+
---@return GitStatus|nil
70+
function DirectoryNode:get_git_status()
71+
if not self.git_status or not self.explorer.opts.git.show_on_dirs then
72+
return nil
73+
end
74+
75+
local status = {}
76+
if not self:last_group_node().open or self.explorer.opts.git.show_on_open_dirs then
77+
-- dir is closed or we should show on open_dirs
78+
if self.git_status.file ~= nil then
79+
table.insert(status, self.git_status.file)
80+
end
81+
if self.git_status.dir ~= nil then
82+
if self.git_status.dir.direct ~= nil then
83+
for _, s in pairs(self.git_status.dir.direct) do
84+
table.insert(status, s)
85+
end
86+
end
87+
if self.git_status.dir.indirect ~= nil then
88+
for _, s in pairs(self.git_status.dir.indirect) do
89+
table.insert(status, s)
90+
end
91+
end
92+
end
93+
else
94+
-- dir is open and we shouldn't show on open_dirs
95+
if self.git_status.file ~= nil then
96+
table.insert(status, self.git_status.file)
97+
end
98+
if self.git_status.dir ~= nil and self.git_status.dir.direct ~= nil then
99+
local deleted = {
100+
[" D"] = true,
101+
["D "] = true,
102+
["RD"] = true,
103+
["DD"] = true,
104+
}
105+
for _, s in pairs(self.git_status.dir.direct) do
106+
if deleted[s] then
107+
table.insert(status, s)
108+
end
109+
end
110+
end
111+
end
112+
if #status == 0 then
113+
return nil
114+
else
115+
return status
116+
end
117+
end
70118

71119
---Create a sanitized partial copy of a node, populating children recursively.
72120
---@return DirectoryNode cloned

lua/nvim-tree/node/init.lua

Lines changed: 9 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ function BaseNode:new(o)
3939
return o
4040
end
4141

42-
-- TODO temporary hack to allow DirectoryNode methods in this file, for easier reviewing
43-
---@class DirectoryNode
44-
local DirectoryNode = BaseNode:new()
45-
BaseNode.dn = DirectoryNode ---@diagnostic disable-line: inject-field
46-
4742
function BaseNode:destroy()
4843
if self.watcher then
4944
self.watcher:destroy()
@@ -69,7 +64,7 @@ function BaseNode:is(T)
6964
end
7065

7166
---@return boolean
72-
function DirectoryNode:has_one_child_folder()
67+
function BaseNode:has_one_child_folder()
7368
return #self.nodes == 1 and self.nodes[1].nodes and vim.loop.fs_access(self.nodes[1].absolute_path, "R") or false
7469
end
7570

@@ -83,58 +78,8 @@ end
8378
function BaseNode:get_git_status()
8479
end
8580

86-
---@return GitStatus|nil
87-
function DirectoryNode:get_git_status()
88-
if not self.git_status or not self.explorer.opts.git.show_on_dirs then
89-
return nil
90-
end
91-
92-
local status = {}
93-
if not self:last_group_node().open or self.explorer.opts.git.show_on_open_dirs then
94-
-- dir is closed or we should show on open_dirs
95-
if self.git_status.file ~= nil then
96-
table.insert(status, self.git_status.file)
97-
end
98-
if self.git_status.dir ~= nil then
99-
if self.git_status.dir.direct ~= nil then
100-
for _, s in pairs(self.git_status.dir.direct) do
101-
table.insert(status, s)
102-
end
103-
end
104-
if self.git_status.dir.indirect ~= nil then
105-
for _, s in pairs(self.git_status.dir.indirect) do
106-
table.insert(status, s)
107-
end
108-
end
109-
end
110-
else
111-
-- dir is open and we shouldn't show on open_dirs
112-
if self.git_status.file ~= nil then
113-
table.insert(status, self.git_status.file)
114-
end
115-
if self.git_status.dir ~= nil and self.git_status.dir.direct ~= nil then
116-
local deleted = {
117-
[" D"] = true,
118-
["D "] = true,
119-
["RD"] = true,
120-
["DD"] = true,
121-
}
122-
for _, s in pairs(self.git_status.dir.direct) do
123-
if deleted[s] then
124-
table.insert(status, s)
125-
end
126-
end
127-
end
128-
end
129-
if #status == 0 then
130-
return nil
131-
else
132-
return status
133-
end
134-
end
135-
13681
---@param projects table
137-
function DirectoryNode:reload_node_status(projects)
82+
function BaseNode:reload_node_status(projects)
13883
local toplevel = git.get_toplevel(self.absolute_path)
13984
local status = projects[toplevel] or {}
14085
for _, node in ipairs(self.nodes) do
@@ -165,7 +110,7 @@ end
165110

166111
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
167112
---@return Node
168-
function DirectoryNode:last_group_node()
113+
function BaseNode:last_group_node()
169114
local node = self --[[@as BaseNode]]
170115

171116
while node.group_next do
@@ -245,7 +190,7 @@ function BaseNode:get_all_nodes_in_group()
245190
end
246191

247192
-- Toggle group empty folders
248-
function DirectoryNode:toggle_group_folders()
193+
function BaseNode:toggle_group_folders()
249194
local is_grouped = self.group_next ~= nil
250195

251196
if is_grouped then
@@ -258,10 +203,11 @@ end
258203
---Group empty folders
259204
-- Recursively group nodes
260205
---@return Node[]
261-
function DirectoryNode:group_empty_folders()
206+
function BaseNode:group_empty_folders()
262207
local is_root = not self.parent
263208
local child_folder_only = self:has_one_child_folder() and self.nodes[1]
264209
if self.explorer.opts.renderer.group_empty and not is_root and child_folder_only then
210+
---@cast self DirectoryNode -- TODO move this to the class
265211
self.group_next = child_folder_only
266212
local ns = child_folder_only:group_empty_folders()
267213
self.nodes = ns or {}
@@ -272,12 +218,12 @@ end
272218

273219
---Ungroup empty folders
274220
-- If a node is grouped, ungroup it: put node.group_next to the node.nodes and set node.group_next to nil
275-
function DirectoryNode:ungroup_empty_folders()
276-
local cur = self --[[@as DirectoryNode]]
221+
function BaseNode:ungroup_empty_folders()
222+
local cur = self
277223
while cur and cur.group_next do
278224
cur.nodes = { cur.group_next }
279225
cur.group_next = nil
280-
cur = cur.nodes[1] --[[@as DirectoryNode]]
226+
cur = cur.nodes[1]
281227
end
282228
end
283229

@@ -337,34 +283,4 @@ function BaseNode:clone()
337283
return clone
338284
end
339285

340-
--
341-
-- TODO temporary hack to allow DirectoryNode methods in this file, for easier reviewing
342-
--
343-
344-
---@return boolean
345-
function BaseNode:has_one_child_folder()
346-
return false
347-
end
348-
349-
---@param _ table projects
350-
function BaseNode:reload_node_status(_)
351-
end
352-
353-
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
354-
---@return Node
355-
function BaseNode:last_group_node()
356-
return self
357-
end
358-
359-
---Group empty folders
360-
-- Recursively group nodes
361-
---@return Node[]
362-
function BaseNode:group_empty_folders()
363-
return {}
364-
end
365-
366-
---Ungroup empty folders
367-
function BaseNode:ungroup_empty_folders()
368-
end
369-
370286
return BaseNode

0 commit comments

Comments
 (0)