Skip to content

Commit c5c48f4

Browse files
committed
more type safety
1 parent e0ab2a2 commit c5c48f4

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

lua/nvim-tree/actions/finders/find-file.lua

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local log = require("nvim-tree.log")
22
local view = require("nvim-tree.view")
33
local utils = require("nvim-tree.utils")
44
local core = require("nvim-tree.core")
5+
6+
local DirectoryNode = require("nvim-tree.node.directory")
57
local Iterator = require("nvim-tree.iterators.node-iterator")
68

79
local M = {}
@@ -43,7 +45,6 @@ function M.fn(path)
4345
return node.absolute_path == path_real or node.link_to == path_real
4446
end)
4547
:applier(function(node)
46-
---@cast node DirectoryNode
4748
local incremented_line = false
4849
if not node.group_next then
4950
line = line + 1
@@ -59,19 +60,27 @@ function M.fn(path)
5960
local link_match = node.link_to and vim.startswith(path_real, node.link_to .. utils.path_separator)
6061

6162
if abs_match or link_match then
62-
if not node.group_next then
63-
node.open = true
64-
end
65-
if #node.nodes == 0 then
66-
core.get_explorer():expand(node)
67-
if node.group_next and incremented_line then
68-
line = line - 1
63+
local dir = node:as(DirectoryNode)
64+
if dir then
65+
if not dir.group_next then
66+
dir.open = true
67+
end
68+
if #dir.nodes == 0 then
69+
core.get_explorer():expand(dir)
70+
if dir.group_next and incremented_line then
71+
line = line - 1
72+
end
6973
end
7074
end
7175
end
7276
end)
7377
:recursor(function(node)
74-
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
78+
node = node and node:as(DirectoryNode)
79+
if node then
80+
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
81+
else
82+
return nil
83+
end
7584
end)
7685
:iterate()
7786

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ local function move(where, what, skip_gitignored)
7272
end
7373
end
7474

75-
---@param node Node
75+
---@param node DirectoryNode
7676
local function expand_node(node)
77-
if node:is(DirectoryNode) and not node.open then
78-
---@cast node DirectoryNode
77+
if not node.open then
7978
-- Expand the node.
8079
-- Should never collapse since we checked open.
8180
node:expand_or_collapse(false)
@@ -100,9 +99,9 @@ local function move_next_recursive(what, skip_gitignored)
10099
if node_init.name ~= ".." then -- root node cannot have a status
101100
valid = status_is_valid(node_init, what, skip_gitignored)
102101
end
103-
if node_init:is(DirectoryNode) and valid and not node_init.open then
104-
---@cast node_init DirectoryNode
105-
node_init:expand_or_collapse(false)
102+
local node_dir = node_init:as(DirectoryNode)
103+
if node_dir and valid and not node_dir.open then
104+
node_dir:expand_or_collapse(false)
106105
end
107106

108107
move("next", what, skip_gitignored)
@@ -119,20 +118,15 @@ local function move_next_recursive(what, skip_gitignored)
119118

120119
-- i is used to limit iterations.
121120
local i = 0
122-
local is_dir = node_cur.nodes ~= nil
123-
while is_dir and i < MAX_DEPTH do
124-
expand_node(node_cur)
121+
local dir_cur = node_cur:as(DirectoryNode)
122+
while dir_cur and i < MAX_DEPTH do
123+
expand_node(dir_cur)
125124

126125
move("next", what, skip_gitignored)
127126

128127
-- Save current node.
129128
node_cur = lib.get_node_at_cursor()
130-
-- Update is_dir.
131-
if node_cur then
132-
is_dir = node_cur.nodes ~= nil
133-
else
134-
is_dir = false
135-
end
129+
dir_cur = node_cur and node_cur:as(DirectoryNode)
136130

137131
i = i + 1
138132
end
@@ -185,8 +179,10 @@ local function move_prev_recursive(what, skip_gitignored)
185179
end
186180

187181
-- 4.2)
188-
local node_dir = node_cur
189-
expand_node(node_dir)
182+
local node_dir = node_cur:as(DirectoryNode)
183+
if node_dir then
184+
expand_node(node_dir)
185+
end
190186

191187
-- 4.3)
192188
if node_init.name == ".." then -- root node

lua/nvim-tree/node/init.lua

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

33
local Class = require("nvim-tree.class")
44

5-
---TODO #2886
6-
---TODO remove all @cast
7-
---TODO remove all references to directory fields:
8-
95
---Abstract Node class.
106
---Uses the abstract factory pattern to instantiate child instances.
117
---@class (exact) Node: Class

0 commit comments

Comments
 (0)