Skip to content

Commit c32ee3e

Browse files
committed
move add BaseNode:as and more doc
1 parent 8f4ab9a commit c32ee3e

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

lua/nvim-tree/actions/moves/item.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ local function expand_node(node)
7878
---@cast node DirectoryNode
7979
-- Expand the node.
8080
-- Should never collapse since we checked open.
81-
node:expand_or_collapse()
81+
node:expand_or_collapse(false)
8282
end
8383
end
8484

@@ -102,7 +102,7 @@ local function move_next_recursive(what, skip_gitignored)
102102
end
103103
if node_init:is(DirectoryNode) and valid and not node_init.open then
104104
---@cast node_init DirectoryNode
105-
node_init:expand_or_collapse()
105+
node_init:expand_or_collapse(false)
106106
end
107107

108108
move("next", what, skip_gitignored)

lua/nvim-tree/node/directory.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ function DirectoryNode:last_group_node()
128128
return node
129129
end
130130

131+
---@param toggle_group boolean
131132
function DirectoryNode:expand_or_collapse(toggle_group)
132133
toggle_group = toggle_group or false
133134
if self.has_children then

lua/nvim-tree/node/init.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,29 @@ function BaseNode:destroy()
4242
end
4343
end
4444

45-
---From plenary
46-
---Checks if the object is an instance
45+
---Object is an instance of class
4746
---This will start with the lowest class and loop over all the superclasses.
48-
---@param self BaseNode
49-
---@param T BaseNode
47+
---@param class table
5048
---@return boolean
51-
function BaseNode:is(T)
49+
function BaseNode:is(class)
5250
local mt = getmetatable(self)
5351
while mt do
54-
if mt == T then
52+
if mt == class then
5553
return true
5654
end
5755
mt = getmetatable(mt)
5856
end
5957
return false
6058
end
6159

60+
---Return object if it is an instance of class, otherwise nil
61+
---@generic T
62+
---@param class T
63+
---@return `T`|nil
64+
function BaseNode:as(class)
65+
return self:is(class) and self or nil
66+
end
67+
6268
---@return boolean
6369
function BaseNode:has_one_child_folder()
6470
return #self.nodes == 1 and self.nodes[1].nodes and vim.loop.fs_access(self.nodes[1].absolute_path, "R") or false
@@ -202,7 +208,6 @@ function BaseNode:group_empty_folders()
202208
local is_root = not self.parent
203209
local child_folder_only = self:has_one_child_folder() and self.nodes[1]
204210
if self.explorer.opts.renderer.group_empty and not is_root and child_folder_only then
205-
---@cast self DirectoryNode -- TODO #2886 move this to the class
206211
self.group_next = child_folder_only
207212
local ns = child_folder_only:group_empty_folders()
208213
self.nodes = ns or {}

0 commit comments

Comments
 (0)