Skip to content

Commit 1f1ad93

Browse files
committed
feat(#2948): add nvim_tree.api.* node classes
1 parent dd6b015 commit 1f1ad93

File tree

10 files changed

+89
-40
lines changed

10 files changed

+89
-40
lines changed

lua/nvim-tree/explorer/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ function Explorer:place_cursor_on_node()
530530
end
531531

532532
---Api.tree.get_nodes
533-
---@return Node
533+
---@return nvim_tree.api.Node
534534
function Explorer:get_nodes()
535535
return self:clone()
536536
end

lua/nvim-tree/meta.lua

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---@meta
2+
3+
--
4+
-- Nodes
5+
--
6+
7+
---@class (exact) nvim_tree.api.Node: Class
8+
---@field type "file" | "directory" | "link" uv.fs_stat.result.type
9+
---@field absolute_path string
10+
---@field executable boolean
11+
---@field fs_stat uv.fs_stat.result?
12+
---@field git_status GitNodeStatus?
13+
---@field hidden boolean
14+
---@field name string
15+
---@field parent nvim_tree.api.DirectoryNode?
16+
---@field diag_severity lsp.DiagnosticSeverity?
17+
18+
---@class (exact) nvim_tree.api.FileNode: nvim_tree.api.Node
19+
---@field extension string
20+
21+
---@class (exact) nvim_tree.api.DirectoryNode: nvim_tree.api.Node
22+
---@field has_children boolean
23+
---@field nodes nvim_tree.api.Node[]
24+
---@field open boolean
25+
26+
---@class (exact) nvim_tree.api.RootNode: nvim_tree.api.DirectoryNode
27+
28+
---@class (exact) nvim_tree.api.LinkNode: Class
29+
---@field link_to string
30+
---@field fs_stat_target uv.fs_stat.result
31+
32+
---@class (exact) nvim_tree.api.FileLinkNode: nvim_tree.api.FileNode, nvim_tree.api.LinkNode
33+
34+
---@class (exact) nvim_tree.api.DirectoryLinkNode: nvim_tree.api.DirectoryNode, nvim_tree.api.LinkNode
35+
36+
--
37+
-- Decorators
38+
--
39+
40+
---Highlight group range as per nvim-tree.renderer.highlight_*
41+
---@alias DecoratorHighlightRange "none" | "icon" | "name" | "all"
42+
43+
---Icon position as per renderer.icons.*_placement
44+
---@alias DecoratorIconPlacement "none" | "before" | "after" | "signcolumn" | "right_align"
45+
46+
---Decorator Constructor Arguments
47+
---@class (exact) DecoratorArgs
48+
---@field enabled boolean
49+
---@field highlight_range DecoratorHighlightRange
50+
---@field icon_placement DecoratorIconPlacement
51+
52+
--
53+
-- Types
54+
--
55+
56+
---A string for rendering, with optional highlight groups to apply to it
57+
---@class (exact) HighlightedString
58+
---@field str string
59+
---@field hl string[]

lua/nvim-tree/node/directory-link.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ function DirectoryLinkNode:highlighted_name()
7373
end
7474

7575
---Create a sanitized partial copy of a node, populating children recursively.
76-
---@return DirectoryLinkNode cloned
76+
---@return nvim_tree.api.DirectoryLinkNode cloned
7777
function DirectoryLinkNode:clone()
78-
local clone = DirectoryNode.clone(self) --[[@as DirectoryLinkNode]]
78+
local clone = DirectoryNode.clone(self) --[[@as nvim_tree.api.DirectoryLinkNode]]
7979

8080
clone.link_to = self.link_to
8181
clone.fs_stat_target = self.fs_stat_target

lua/nvim-tree/node/directory.lua

+6-5
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,19 @@ function DirectoryNode:highlighted_name()
271271
end
272272

273273
---Create a sanitized partial copy of a node, populating children recursively.
274-
---@return DirectoryNode cloned
274+
---@return nvim_tree.api.DirectoryNode cloned
275275
function DirectoryNode:clone()
276-
local clone = Node.clone(self) --[[@as DirectoryNode]]
276+
local clone = Node.clone(self) --[[@as nvim_tree.api.DirectoryNode]]
277277

278278
clone.has_children = self.has_children
279-
clone.group_next = nil
280279
clone.nodes = {}
281280
clone.open = self.open
282-
clone.hidden_stats = nil
283281

282+
local clone_child
284283
for _, child in ipairs(self.nodes) do
285-
table.insert(clone.nodes, child:clone())
284+
clone_child = child:clone()
285+
clone_child.parent = clone
286+
table.insert(clone.nodes, clone_child)
286287
end
287288

288289
return clone

lua/nvim-tree/node/file-link.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ function FileLinkNode:highlighted_name()
5858
end
5959

6060
---Create a sanitized partial copy of a node
61-
---@return FileLinkNode cloned
61+
---@return nvim_tree.api.FileLinkNode cloned
6262
function FileLinkNode:clone()
63-
local clone = FileNode.clone(self) --[[@as FileLinkNode]]
63+
local clone = FileNode.clone(self) --[[@as nvim_tree.api.FileLinkNode]]
6464

6565
clone.link_to = self.link_to
6666
clone.fs_stat_target = self.fs_stat_target

lua/nvim-tree/node/file.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ function FileNode:highlighted_name()
9494
end
9595

9696
---Create a sanitized partial copy of a node
97-
---@return FileNode cloned
97+
---@return nvim_tree.api.FileNode cloned
9898
function FileNode:clone()
99-
local clone = Node.clone(self) --[[@as FileNode]]
99+
local clone = Node.clone(self) --[[@as nvim_tree.api.FileNode]]
100100

101101
clone.extension = self.extension
102102

lua/nvim-tree/node/init.lua

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local Class = require("nvim-tree.classic")
22

33
---Abstract Node class.
44
---@class (exact) Node: Class
5+
---@field uid_node number vim.loop.hrtime() at construction time
56
---@field type "file" | "directory" | "link" uv.fs_stat.result.type
67
---@field explorer Explorer
78
---@field absolute_path string
@@ -11,6 +12,7 @@ local Class = require("nvim-tree.classic")
1112
---@field hidden boolean
1213
---@field name string
1314
---@field parent DirectoryNode?
15+
---TODO split this into diag_severity and diag_severity_cache_version
1416
---@field diag_status DiagStatus?
1517
---@field private is_dot boolean cached is_dotfile
1618
local Node = Class:extend()
@@ -25,6 +27,7 @@ local Node = Class:extend()
2527
---@protected
2628
---@param args NodeArgs
2729
function Node:new(args)
30+
self.uid_node = vim.loop.hrtime()
2831
self.explorer = args.explorer
2932
self.absolute_path = args.absolute_path
3033
self.executable = false
@@ -112,30 +115,26 @@ end
112115

113116
---Highlighted name for the node
114117
---Empty for base Node
115-
---@return HighlightedString icon
118+
---@return HighlightedString name
116119
function Node:highlighted_name()
117120
return self:highlighted_name_empty()
118121
end
119122

120123
---Create a sanitized partial copy of a node, populating children recursively.
121-
---@return Node cloned
124+
---@return nvim_tree.api.Node cloned
122125
function Node:clone()
123-
---@type Explorer
124-
local explorer_placeholder = nil
125-
126-
---@type Node
126+
---@type nvim_tree.api.Node
127127
local clone = {
128+
uid_node = self.uid_node,
128129
type = self.type,
129-
explorer = explorer_placeholder,
130130
absolute_path = self.absolute_path,
131131
executable = self.executable,
132132
fs_stat = self.fs_stat,
133133
git_status = self.git_status,
134134
hidden = self.hidden,
135135
name = self.name,
136136
parent = nil,
137-
diag_status = nil,
138-
is_dot = self.is_dot,
137+
diag_severity = self.diag_status and self.diag_status.value or nil,
139138
}
140139

141140
return clone

lua/nvim-tree/node/link.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local Class = require("nvim-tree.classic")
22

33
---@class (exact) LinkNode: Class
44
---@field link_to string
5-
---@field protected fs_stat_target uv.fs_stat.result
5+
---@field fs_stat_target uv.fs_stat.result
66
local LinkNode = Class:extend()
77

88
---@class (exact) LinkNodeArgs: NodeArgs

lua/nvim-tree/node/root.lua

+8
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ function RootNode:destroy()
2222
DirectoryNode.destroy(self)
2323
end
2424

25+
---Create a sanitized partial copy of a node, populating children recursively.
26+
---@return nvim_tree.api.RootNode cloned
27+
function RootNode:clone()
28+
local clone = DirectoryNode.clone(self) --[[@as nvim_tree.api.RootNode]]
29+
30+
return clone
31+
end
32+
2533
return RootNode

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

-18
This file was deleted.

0 commit comments

Comments
 (0)