Skip to content

Commit cb041e0

Browse files
authored
refactor(#2886): multi instance: node class refactoring: DirectoryNode:expand_or_collapse (#2957)
move expand_or_collapse to DirectoryNode
1 parent cc61f74 commit cb041e0

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

Diff for: lua/nvim-tree/actions/moves/item.lua

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ local core = require("nvim-tree.core")
44
local lib = require("nvim-tree.lib")
55
local diagnostics = require("nvim-tree.diagnostics")
66

7+
local DirectoryNode = require("nvim-tree.node.directory")
8+
79
local M = {}
810
local MAX_DEPTH = 100
911

@@ -70,8 +72,10 @@ local function move(where, what, skip_gitignored)
7072
end
7173
end
7274

75+
---@param node Node
7376
local function expand_node(node)
74-
if not node.open then
77+
if node:is(DirectoryNode) and not node.open then
78+
---@cast node DirectoryNode
7579
-- Expand the node.
7680
-- Should never collapse since we checked open.
7781
node:expand_or_collapse()
@@ -96,7 +100,8 @@ local function move_next_recursive(what, skip_gitignored)
96100
if node_init.name ~= ".." then -- root node cannot have a status
97101
valid = status_is_valid(node_init, what, skip_gitignored)
98102
end
99-
if node_init.nodes ~= nil and valid and not node_init.open then
103+
if node_init:is(DirectoryNode) and valid and not node_init.open then
104+
---@cast node_init DirectoryNode
100105
node_init:expand_or_collapse()
101106
end
102107

Diff for: lua/nvim-tree/api.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ local help = require("nvim-tree.help")
99
local keymap = require("nvim-tree.keymap")
1010
local notify = require("nvim-tree.notify")
1111

12+
local DirectoryNode = require("nvim-tree.node.directory")
13+
1214
local Api = {
1315
tree = {},
1416
node = {
@@ -213,7 +215,8 @@ local function open_or_expand_or_dir_up(mode, toggle_group)
213215
return function(node)
214216
if node.name == ".." then
215217
actions.root.change_dir.fn("..")
216-
elseif node.nodes then
218+
elseif node:is(DirectoryNode) then
219+
---@cast node DirectoryNode
217220
node:expand_or_collapse(toggle_group)
218221
elseif not toggle_group then
219222
edit(mode, node)

Diff for: lua/nvim-tree/node/directory.lua

+29
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,35 @@ function DirectoryNode:get_git_status()
116116
end
117117
end
118118

119+
function DirectoryNode:expand_or_collapse(toggle_group)
120+
toggle_group = toggle_group or false
121+
if self.has_children then
122+
self.has_children = false
123+
end
124+
125+
if #self.nodes == 0 then
126+
self.explorer:expand(self)
127+
end
128+
129+
local head_node = self:get_parent_of_group()
130+
if toggle_group then
131+
head_node:toggle_group_folders()
132+
end
133+
134+
local open = self:last_group_node().open
135+
local next_open
136+
if toggle_group then
137+
next_open = open
138+
else
139+
next_open = not open
140+
end
141+
for _, n in ipairs(head_node:get_all_nodes_in_group()) do
142+
n.open = next_open
143+
end
144+
145+
self.explorer.renderer:draw()
146+
end
147+
119148
---Create a sanitized partial copy of a node, populating children recursively.
120149
---@return DirectoryNode cloned
121150
function DirectoryNode:clone()

Diff for: lua/nvim-tree/node/init.lua

-30
Original file line numberDiff line numberDiff line change
@@ -225,36 +225,6 @@ function BaseNode:ungroup_empty_folders()
225225
end
226226
end
227227

228-
function BaseNode:expand_or_collapse(toggle_group)
229-
toggle_group = toggle_group or false
230-
if self.has_children then
231-
---@cast self DirectoryNode -- TODO move this to the class
232-
self.has_children = false
233-
end
234-
235-
if #self.nodes == 0 then
236-
self.explorer:expand(self)
237-
end
238-
239-
local head_node = self:get_parent_of_group()
240-
if toggle_group then
241-
head_node:toggle_group_folders()
242-
end
243-
244-
local open = self:last_group_node().open
245-
local next_open
246-
if toggle_group then
247-
next_open = open
248-
else
249-
next_open = not open
250-
end
251-
for _, n in ipairs(head_node:get_all_nodes_in_group()) do
252-
n.open = next_open
253-
end
254-
255-
self.explorer.renderer:draw()
256-
end
257-
258228
---Create a sanitized partial copy of a node, populating children recursively.
259229
---@return BaseNode cloned
260230
function BaseNode:clone()

0 commit comments

Comments
 (0)