Skip to content

Commit 9193ec9

Browse files
committed
node methods
1 parent 646ce63 commit 9193ec9

File tree

9 files changed

+48
-47
lines changed

9 files changed

+48
-47
lines changed

lua/nvim-tree/explorer/init.lua

+11-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local view = require("nvim-tree.view")
66
local watch = require("nvim-tree.explorer.watch")
77
local explorer_node = require("nvim-tree.explorer.node")
88

9+
local BaseNode = require("nvim-tree.node")
910
local DirectoryNode = require("nvim-tree.node.directory")
1011
local FileNode = require("nvim-tree.node.file")
1112
local LinkNode = require("nvim-tree.node.link")
@@ -25,7 +26,7 @@ local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
2526

2627
local config
2728

28-
---@class Explorer
29+
---@class Explorer: BaseNode
2930
---@field opts table user options
3031
---@field absolute_path string
3132
---@field nodes Node[]
@@ -37,7 +38,7 @@ local config
3738
---@field sorters Sorter
3839
---@field marks Marks
3940
---@field clipboard Clipboard
40-
local Explorer = {}
41+
local Explorer = BaseNode:new()
4142

4243
---@param path string|nil
4344
---@return Explorer|nil
@@ -84,7 +85,7 @@ end
8485

8586
function Explorer:destroy()
8687
local function iterate(node)
87-
explorer_node.node_destroy(node)
88+
node:destroy()
8889
if node.nodes then
8990
for _, child in pairs(node.nodes) do
9091
iterate(child)
@@ -114,7 +115,7 @@ function Explorer:reload(node, git_status)
114115

115116
local remain_childs = {}
116117

117-
local node_ignored = explorer_node.is_git_ignored(node)
118+
local node_ignored = node:is_git_ignored()
118119
---@type table<string, Node>
119120
local nodes_by_path = utils.key_by(node.nodes, "absolute_path")
120121

@@ -150,7 +151,7 @@ function Explorer:reload(node, git_status)
150151

151152
if n.type ~= t then
152153
utils.array_remove(node.nodes, n)
153-
explorer_node.node_destroy(n)
154+
n:destroy()
154155
nodes_by_path[abs] = nil
155156
end
156157
end
@@ -193,14 +194,14 @@ function Explorer:reload(node, git_status)
193194
if remain_childs[n.absolute_path] then
194195
return remain_childs[n.absolute_path]
195196
else
196-
explorer_node.node_destroy(n)
197+
n:destroy()
197198
return false
198199
end
199200
end, node.nodes)
200201
)
201202

202203
local is_root = not node.parent
203-
local child_folder_only = explorer_node.has_one_child_folder(node) and node.nodes[1]
204+
local child_folder_only = node:has_one_child_folder() and node.nodes[1]
204205
if config.renderer.group_empty and not is_root and child_folder_only then
205206
node.group_next = child_folder_only
206207
local ns = self:reload(child_folder_only, git_status)
@@ -329,7 +330,7 @@ function Explorer:update_parent_statuses(node, project, root)
329330
end
330331

331332
-- update status
332-
explorer_node.update_git_status(node, explorer_node.is_git_ignored(node.parent), project)
333+
explorer_node.update_git_status(node, node.parent and node.parent:is_git_ignored(), project)
333334

334335
-- maybe parent
335336
node = node.parent
@@ -343,7 +344,7 @@ end
343344
---@param git_status table
344345
---@param parent Explorer
345346
function Explorer:populate_children(handle, cwd, node, git_status, parent)
346-
local node_ignored = explorer_node.is_git_ignored(node)
347+
local node_ignored = node:is_git_ignored()
347348
local nodes_by_path = utils.bool_record(node.nodes, "absolute_path")
348349

349350
local filter_status = parent.filters:prepare(git_status)
@@ -419,7 +420,7 @@ function Explorer:explore(node, status, parent)
419420
self:populate_children(handle, cwd, node, status, parent)
420421

421422
local is_root = not node.parent
422-
local child_folder_only = explorer_node.has_one_child_folder(node) and node.nodes[1]
423+
local child_folder_only = node:has_one_child_folder() and node.nodes[1]
423424
if config.renderer.group_empty and not is_root and child_folder_only then
424425
local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path
425426
local child_status = git.load_project_status(child_cwd)

lua/nvim-tree/explorer/node.lua

-25
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ local function get_git_status(parent_ignored, status, absolute_path)
3535
return { file = file_status }
3636
end
3737

38-
---@param node Node
39-
---@return boolean
40-
function M.has_one_child_folder(node)
41-
return #node.nodes == 1 and node.nodes[1].nodes and vim.loop.fs_access(node.nodes[1].absolute_path, "R") or false
42-
end
43-
4438
---@param node Node
4539
---@param parent_ignored boolean
4640
---@param status table|nil
@@ -141,25 +135,6 @@ function M.reload_node_status(parent_node, projects)
141135
end
142136
end
143137

144-
---@param node Node
145-
---@return boolean
146-
function M.is_git_ignored(node)
147-
return node and node.git_status ~= nil and node.git_status.file == "!!"
148-
end
149-
150-
---@param node Node
151-
---@return boolean
152-
function M.is_dotfile(node)
153-
if node == nil then
154-
return false
155-
end
156-
if node.is_dot or (node.name and (node.name:sub(1, 1) == ".")) or M.is_dotfile(node.parent) then
157-
node.is_dot = true
158-
return true
159-
end
160-
return false
161-
end
162-
163138
---@param node Node
164139
function M.node_destroy(node)
165140
if not node then

lua/nvim-tree/git/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ local function reload_tree_at(toplevel)
208208
Iterator.builder(root_node.nodes)
209209
:hidden()
210210
:applier(function(node)
211-
local parent_ignored = explorer_node.is_git_ignored(node.parent)
211+
local parent_ignored = node.parent and node.parent:is_git_ignored()
212212
explorer_node.update_git_status(node, parent_ignored, git_status)
213213
end)
214214
:recursor(function(node)

lua/nvim-tree/lib.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local core = require("nvim-tree.core")
33
local utils = require("nvim-tree.utils")
44
local events = require("nvim-tree.events")
55
local notify = require("nvim-tree.notify")
6-
local explorer_node = require("nvim-tree.explorer.node")
76

87
---@class LibOpenOpts
98
---@field path string|nil path
@@ -101,7 +100,7 @@ end
101100
---@return Node[]
102101
function M.group_empty_folders(node)
103102
local is_root = not node.parent
104-
local child_folder_only = explorer_node.has_one_child_folder(node) and node.nodes[1]
103+
local child_folder_only = node:has_one_child_folder() and node.nodes[1]
105104
if M.group_empty and not is_root and child_folder_only then
106105
node.group_next = child_folder_only
107106
local ns = M.group_empty_folders(child_folder_only)

lua/nvim-tree/node/directory.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local watch = require("nvim-tree.explorer.watch")
22

3-
local BaseNode = require("nvim-tree.node.init")
3+
local BaseNode = require("nvim-tree.node")
44

55
---@class (exact) DirectoryNode: BaseNode
66
---@field has_children boolean

lua/nvim-tree/node/file.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local utils = require("nvim-tree.utils")
22

3-
local BaseNode = require("nvim-tree.node.init")
3+
local BaseNode = require("nvim-tree.node")
44

55
---@class (exact) FileNode: BaseNode
66
---@field extension string

lua/nvim-tree/node/init.lua

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
---@class ParentNode
2-
---@field name string
3-
41
---@class (exact) BaseNode
52
---@field type NODE_TYPE
63
---@field explorer Explorer
@@ -28,4 +25,34 @@ function BaseNode:new(o)
2825
return o
2926
end
3027

28+
function BaseNode:destroy()
29+
if self.watcher then
30+
self.watcher:destroy()
31+
self.watcher = nil
32+
end
33+
end
34+
35+
---@return boolean
36+
function BaseNode:has_one_child_folder()
37+
return #self.nodes == 1 and self.nodes[1].nodes and vim.loop.fs_access(self.nodes[1].absolute_path, "R") or false
38+
end
39+
40+
---@return boolean
41+
function BaseNode:is_git_ignored()
42+
return self.git_status ~= nil and self.git_status.file == "!!"
43+
end
44+
45+
---@return boolean
46+
function BaseNode:is_dotfile()
47+
if
48+
self.is_dot --
49+
or (self.name and (self.name:sub(1, 1) == ".")) --
50+
or (self.parent and self.parent:is_dotfile())
51+
then
52+
self.is_dot = true
53+
return true
54+
end
55+
return false
56+
end
57+
3158
return BaseNode

lua/nvim-tree/node/link.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local watch = require("nvim-tree.explorer.watch")
22

3-
local BaseNode = require("nvim-tree.node.init")
3+
local BaseNode = require("nvim-tree.node")
44

55
---@class (exact) LinkNode: BaseNode
66
---@field link_to string absolute path

lua/nvim-tree/renderer/decorator/hidden.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
22
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
3-
local explorer_node = require("nvim-tree.explorer.node")
43
local Decorator = require("nvim-tree.renderer.decorator")
54

65
---@class (exact) DecoratorHidden: Decorator
@@ -34,7 +33,7 @@ end
3433
---@param node Node
3534
---@return HighlightedString[]|nil icons
3635
function DecoratorHidden:calculate_icons(node)
37-
if self.enabled and explorer_node.is_dotfile(node) then
36+
if self.enabled and node:is_dotfile() then
3837
return { self.icon }
3938
end
4039
end
@@ -43,7 +42,7 @@ end
4342
---@param node Node
4443
---@return string|nil group
4544
function DecoratorHidden:calculate_highlight(node)
46-
if not self.enabled or self.hl_pos == HL_POSITION.none or (not explorer_node.is_dotfile(node)) then
45+
if not self.enabled or self.hl_pos == HL_POSITION.none or not node:is_dotfile() then
4746
return nil
4847
end
4948

0 commit comments

Comments
 (0)