Skip to content

Commit 45a14f6

Browse files
committed
feat(#2948): extract _meta following nvim pattern
1 parent 1f1ad93 commit 45a14f6

File tree

12 files changed

+130
-103
lines changed

12 files changed

+130
-103
lines changed

lua/nvim-tree/meta.lua renamed to lua/nvim-tree/_meta/api.lua

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
---@meta
2+
error('Cannot require a meta file')
3+
4+
-- TODO describe class
5+
-- TODO describe user decorator
26

37
--
48
-- Nodes
59
--
610

11+
---Base Node, Abstract
712
---@class (exact) nvim_tree.api.Node: Class
813
---@field type "file" | "directory" | "link" uv.fs_stat.result.type
914
---@field absolute_path string
@@ -15,45 +20,41 @@
1520
---@field parent nvim_tree.api.DirectoryNode?
1621
---@field diag_severity lsp.DiagnosticSeverity?
1722

23+
---File
1824
---@class (exact) nvim_tree.api.FileNode: nvim_tree.api.Node
1925
---@field extension string
2026

27+
---Directory
2128
---@class (exact) nvim_tree.api.DirectoryNode: nvim_tree.api.Node
2229
---@field has_children boolean
2330
---@field nodes nvim_tree.api.Node[]
2431
---@field open boolean
2532

33+
---Root Directory
2634
---@class (exact) nvim_tree.api.RootNode: nvim_tree.api.DirectoryNode
2735

36+
---Link mixin
2837
---@class (exact) nvim_tree.api.LinkNode: Class
2938
---@field link_to string
3039
---@field fs_stat_target uv.fs_stat.result
3140

41+
---File Link
3242
---@class (exact) nvim_tree.api.FileLinkNode: nvim_tree.api.FileNode, nvim_tree.api.LinkNode
3343

44+
---DirectoryLink
3445
---@class (exact) nvim_tree.api.DirectoryLinkNode: nvim_tree.api.DirectoryNode, nvim_tree.api.LinkNode
3546

3647
--
37-
-- Decorators
48+
-- Various Types
3849
--
3950

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+
---A string for rendering, with optional highlight groups to apply to it
52+
---@class (exact) nvim_tree.api.HighlightedString
53+
---@field str string
54+
---@field hl string[]
5155

5256
--
53-
-- Types
57+
-- Internal Aliases
5458
--
59+
---@alias HighlightedString nvim_tree.api.HighlightedString
5560

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/_meta/api_decorator.lua

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---@meta
2+
error('Cannot require a meta file')
3+
4+
---Highlight group range as per nvim-tree.renderer.highlight_*
5+
---@alias nvim_tree.api.decorator.HighlightRange "none" | "icon" | "name" | "all"
6+
7+
---Icon position as per renderer.icons.*_placement
8+
---@alias nvim_tree.api.decorator.IconPlacement "none" | "before" | "after" | "signcolumn" | "right_align"
9+
10+
---UserDecorator Constructor Arguments
11+
---@class (exact) nvim_tree.api.decorator.UserDecoratorArgs
12+
---@field enabled boolean
13+
---@field highlight_range nvim_tree.api.decorator.HighlightRange
14+
---@field icon_placement nvim_tree.api.decorator.IconPlacement
15+
16+
17+
--
18+
-- Example UserDecorator
19+
--
20+
21+
local UserDecorator = require("nvim-tree.renderer.decorator.user")
22+
23+
---@class (exact) MyDecorator: UserDecorator
24+
---@field private my_icon nvim_tree.api.HighlightedString
25+
local MyDecorator = UserDecorator:extend()
26+
27+
---Constructor
28+
function MyDecorator:new()
29+
30+
---@type nvim_tree.api.decorator.UserDecoratorArgs
31+
local args = {
32+
enabled = true,
33+
highlight_range = "all",
34+
icon_placement = "signcolumn",
35+
}
36+
37+
-- construct super with args
38+
MyDecorator.super.new(self, args)
39+
40+
-- create your icon once, for convenience
41+
self.my_icon = { str = "I", hl = { "MyIcon" } }
42+
43+
-- Define the icon sign only once
44+
-- Only needed if you are using icon_placement = "signcolumn"
45+
self:define_sign(self.my_icon)
46+
end
47+
48+
---Overridde node icon
49+
---@param node nvim_tree.api.Node
50+
---@return nvim_tree.api.HighlightedString? icon_node
51+
function MyDecorator:icon_node(node)
52+
if node.name == "example" then
53+
return self.my_icon
54+
else
55+
return nil
56+
end
57+
end
58+
59+
---Return one icon for DecoratorIconPlacement
60+
---@param node nvim_tree.api.Node
61+
---@return nvim_tree.api.HighlightedString[]? icons
62+
function MyDecorator:icons(node)
63+
if node.name == "example" then
64+
return { self.my_icon }
65+
else
66+
return nil
67+
end
68+
end
69+
70+
---Exactly one highlight group for DecoratorHighlightRange
71+
---@param node nvim_tree.api.Node
72+
---@return string? highlight_group
73+
function MyDecorator:highlight_group(node)
74+
if node.name == "example" then
75+
return "MyHighlight"
76+
else
77+
return nil
78+
end
79+
end
80+
81+
return MyDecorator
82+
83+
--
84+
-- Internal Aliases
85+
--
86+
---@alias DecoratorHighlightRange nvim_tree.api.decorator.HighlightRange
87+
---@alias DecoratorIconPlacement nvim_tree.api.decorator.IconPlacement
88+

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ end
3333

3434
---Bookmark icon: renderer.icons.show.bookmarks and node is marked
3535
---@param node Node
36-
---@return HighlightedString[]|nil icons
36+
---@return HighlightedString[]? icons
3737
function DecoratorBookmarks:icons(node)
3838
if self.explorer.marks:get(node) then
3939
return { self.icon }
@@ -42,7 +42,7 @@ end
4242

4343
---Bookmark highlight: renderer.highlight_bookmarks and node is marked
4444
---@param node Node
45-
---@return string|nil group
45+
---@return string? highlight_group
4646
function DecoratorBookmarks:highlight_group(node)
4747
if self.highlight_range ~= "none" and self.explorer.marks:get(node) then
4848
return "NvimTreeBookmarkHL"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ end
2424

2525
---Copied highlight: renderer.highlight_clipboard and node is copied
2626
---@param node Node
27-
---@return string|nil group
27+
---@return string? highlight_group
2828
function DecoratorCopied:highlight_group(node)
2929
if self.highlight_range ~= "none" and self.explorer.clipboard:is_copied(node) then
3030
return "NvimTreeCopiedHL"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ end
2424

2525
---Cut highlight: renderer.highlight_clipboard and node is cut
2626
---@param node Node
27-
---@return string|nil group
27+
---@return string? highlight_group
2828
function DecoratorCut:highlight_group(node)
2929
if self.highlight_range ~= "none" and self.explorer.clipboard:is_cut(node) then
3030
return "NvimTreeCutHL"

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ end
7070

7171
---Diagnostic icon: diagnostics.enable, renderer.icons.show.diagnostics and node has status
7272
---@param node Node
73-
---@return HighlightedString[]|nil icons
73+
---@return HighlightedString[]? icons
7474
function DecoratorDiagnostics:icons(node)
7575
if node and self.enabled and self.diag_icons then
7676
local diag_status = diagnostics.get_diag_status(node)
@@ -84,7 +84,7 @@ end
8484

8585
---Diagnostic highlight: diagnostics.enable, renderer.highlight_diagnostics and node has status
8686
---@param node Node
87-
---@return string|nil group
87+
---@return string? highlight_group
8888
function DecoratorDiagnostics:highlight_group(node)
8989
if not node or not self.enabled or self.highlight_range == "none" then
9090
return nil

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local notify = require("nvim-tree.notify")
33
local Decorator = require("nvim-tree.renderer.decorator")
44
local DirectoryNode = require("nvim-tree.node.directory")
55

6-
---@class (exact) GitHighlightedString: HighlightedString
6+
---@class (exact) GitHighlightedString: nvim_tree.api.HighlightedString
77
---@field ord number decreasing priority
88

99
---@alias GitStatusStrings "deleted" | "ignored" | "renamed" | "staged" | "unmerged" | "unstaged" | "untracked"
@@ -147,7 +147,7 @@ end
147147

148148
---Git icons: git.enable, renderer.icons.show.git and node has status
149149
---@param node Node
150-
---@return HighlightedString[]|nil modified icon
150+
---@return HighlightedString[]? icons
151151
function DecoratorGit:icons(node)
152152
if not node or not self.enabled or not self.icons_by_xy then
153153
return nil
@@ -208,7 +208,7 @@ end
208208

209209
---Git highlight: git.enable, renderer.highlight_git and node has status
210210
---@param node Node
211-
---@return string|nil group
211+
---@return string? highlight_group
212212
function DecoratorGit:highlight_group(node)
213213
if not node or not self.enabled or self.highlight_range == "none" then
214214
return nil

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end
3434

3535
---Hidden icon: renderer.icons.show.hidden and node starts with `.` (dotfile).
3636
---@param node Node
37-
---@return HighlightedString[]|nil icons
37+
---@return HighlightedString[]? icons
3838
function DecoratorHidden:icons(node)
3939
if self.enabled and node:is_dotfile() then
4040
return { self.icon }
@@ -43,7 +43,7 @@ end
4343

4444
---Hidden highlight: renderer.highlight_hidden and node starts with `.` (dotfile).
4545
---@param node Node
46-
---@return string|nil group
46+
---@return string? highlight_group
4747
function DecoratorHidden:highlight_group(node)
4848
if not self.enabled or self.highlight_range == "none" or not node:is_dotfile() then
4949
return nil

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ local Class = require("nvim-tree.classic")
77
---@field protected icon_placement DecoratorIconPlacement
88
local Decorator = Class:extend()
99

10+
---@class (exact) DecoratorArgs
11+
---@field enabled boolean
12+
---@field highlight_range DecoratorHighlightRange
13+
---@field icon_placement DecoratorIconPlacement
14+
1015
---@protected
1116
---@param args DecoratorArgs
1217
function Decorator:new(args)
@@ -105,7 +110,7 @@ end
105110
---Maybe highlight group, optionally implemented
106111
---@protected
107112
---@param node Node
108-
---@return string? group
113+
---@return string? highlight_group
109114
function Decorator:highlight_group(node)
110115
self:nop(node)
111116
end

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ end
4040

4141
---Modified icon: modified.enable, renderer.icons.show.modified and node is modified
4242
---@param node Node
43-
---@return HighlightedString[]|nil icons
43+
---@return HighlightedString[]? icons
4444
function DecoratorModified:icons(node)
4545
if self.enabled and buffers.is_modified(node) then
4646
return { self.icon }
@@ -49,7 +49,7 @@ end
4949

5050
---Modified highlight: modified.enable, renderer.highlight_modified and node is modified
5151
---@param node Node
52-
---@return string|nil group
52+
---@return string? highlight_group
5353
function DecoratorModified:highlight_group(node)
5454
if not self.enabled or self.highlight_range == "none" or not buffers.is_modified(node) then
5555
return nil

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ end
2727

2828
---Opened highlight: renderer.highlight_opened_files and node has an open buffer
2929
---@param node Node
30-
---@return string|nil group
30+
---@return string? highlight_group
3131
function DecoratorOpened:highlight_group(node)
3232
if self.highlight_range ~= "none" and buffers.is_opened(node) then
3333
return "NvimTreeOpenedHL"

0 commit comments

Comments
 (0)