Skip to content

Commit 8e84f54

Browse files
committed
docs(#3241): Decorator class documentation
1 parent 3e7b376 commit 8e84f54

3 files changed

Lines changed: 95 additions & 50 deletions

File tree

doc/nvim-tree-lua.txt

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,46 +3205,53 @@ winid({opts}) *nvim_tree.api.tree.winid()*
32053205
API: Decorator *nvim-tree-api-decorator*
32063206

32073207
Highlighting and icons for nodes are provided by Decorators, see
3208-
|nvim-tree-icons-highlighting|. You may provide your own in addition to the
3209-
builtin decorators.
3208+
|nvim-tree-icons-highlighting| for an overview. You may provide your own in
3209+
addition to the builtin decorators.
3210+
3211+
Decorators are rendered in |nvim_tree.config.renderer| {decorators} order of
3212+
precedence, with later decorators applying additively over earlier.
32103213

32113214
Decorators may:
32123215
• Add icons
3213-
• Set highlight group for the name or icons
3216+
• Set a highlight group name for the name or icons
32143217
• Override node icon
32153218

32163219
To register your decorator:
32173220
• Create a class that extends |nvim_tree.api.Decorator|
32183221
• Register it by adding the class to |nvim_tree.config.renderer| {decorators}
32193222

3223+
Your decorator will be constructed and executed each time the tree is
3224+
rendered.
3225+
32203226
Your class must:
32213227
|nvim_tree.Class:extend()| the interface |nvim_tree.api.Decorator|
32223228
• Provide a no-arguments constructor |nvim_tree.Class:new()| that sets the
32233229
mandatory fields:
32243230
{enabled}
32253231
• {highlight_range}
32263232
• {icon_placement}
3233+
• Call |nvim_tree.api.Decorator:define_sign()| in your constructor when
3234+
{icon_placement} is `"signcolumn"`
32273235

32283236
Your class may:
32293237
• Implement methods to provide decorations:
32303238
|nvim_tree.api.Decorator:highlight_group()|
32313239
|nvim_tree.api.Decorator:icon_node()|
32323240
|nvim_tree.api.Decorator:icons()|
32333241

3234-
Your class must:
3235-
• Call |nvim_tree.api.Decorator:define_sign()| in your constructor if using
3236-
`"signcolumn"` {icon_placement}
3237-
32383242

32393243
*nvim_tree.api.Decorator*
32403244
Extends: |nvim_tree.Class|
32413245

3242-
Abstract Decorator interface
3246+
Decorator interface
32433247

32443248
Fields: ~
3245-
{enabled} (`boolean`)
3246-
• {highlight_range} (`nvim_tree.api.decorator.highlight_range`)
3247-
• {icon_placement} (`nvim_tree.api.decorator.icon_placement`)
3249+
{enabled} (`boolean`) Enable this decorator.
3250+
• {highlight_range} (`nvim_tree.config.renderer.highlight`) What to
3251+
highlight: |nvim_tree.config.renderer.highlight|
3252+
• {icon_placement} (`"none"|nvim_tree.config.renderer.icons.placement`)
3253+
Where to place the icons:
3254+
|nvim_tree.config.renderer.icons.placement|
32483255
• {icon_node} (`fun(self: nvim_tree.api.Decorator, node: nvim_tree.api.Node): nvim_tree.api.decorator.highlighted_string?`)
32493256
See |nvim_tree.api.Decorator:icon_node()|.
32503257
{icons} (`fun(self: nvim_tree.api.Decorator, node: nvim_tree.api.Node): nvim_tree.api.decorator.highlighted_string[]?`)
@@ -3254,42 +3261,64 @@ Your class must:
32543261
• {define_sign} (`fun(self: nvim_tree.api.Decorator, icon: nvim_tree.api.decorator.highlighted_string?)`)
32553262
See |nvim_tree.api.Decorator:define_sign()|.
32563263

3264+
*nvim_tree.api.decorator.highlighted_string*
3265+
Text or glyphs with optional highlight group names to apply to it.
3266+
3267+
Fields: ~
3268+
{str} (`string`) One or many glyphs/characters.
3269+
{hl} (`string[]`) Highlight group names to apply in order. Empty
3270+
table for no highlighting.
3271+
32573272

32583273
Decorator:define_sign({icon}) *nvim_tree.api.Decorator:define_sign()*
3259-
Defines a sign. This should be called in the constructor.
3274+
Defines a sign for an icon. This is mandatory and necessary only when
3275+
{icon_placement} is `"signcolumn"`
3276+
3277+
This must be called during your constructor for all icons that you will
3278+
return from |nvim_tree.api.Decorator:icons()|
32603279

32613280
Parameters: ~
3262-
{icon} (`nvim_tree.api.decorator.highlighted_string?`)
3281+
{icon} (`nvim_tree.api.decorator.highlighted_string?`) does nothing
3282+
if nil
32633283

32643284
*nvim_tree.api.Decorator:highlight_group()*
32653285
Decorator:highlight_group({node})
3266-
Abstract: optionally implement to provide one highlight group to apply to
3267-
your highlight_range.
3286+
One highlight group that applies additively to the {node} name for
3287+
{highlight_range}.
3288+
3289+
Abstract, optional to implement.
32683290

32693291
Parameters: ~
32703292
{node} (`nvim_tree.api.Node`)
32713293

32723294
Return: ~
3273-
(`string?`) highlight_group
3295+
(`string?`) highlight group name `nil` when no highlighting to apply
3296+
to the node
32743297

32753298
Decorator:icon_node({node}) *nvim_tree.api.Decorator:icon_node()*
3276-
Abstract: optionally implement to set the node's icon
3299+
Icon to override for the node.
3300+
3301+
Abstract, optional to implement.
32773302

32783303
Parameters: ~
32793304
{node} (`nvim_tree.api.Node`)
32803305

32813306
Return: ~
3282-
(`nvim_tree.api.decorator.highlighted_string?`) icon_node
3307+
(`nvim_tree.api.decorator.highlighted_string?`) icon `nil` for no
3308+
override
32833309

32843310
Decorator:icons({node}) *nvim_tree.api.Decorator:icons()*
3285-
Abstract: optionally implement to provide icons and the highlight groups
3286-
for your icon_placement.
3311+
Icons to add to the node as per {icon_placement}
3312+
3313+
Abstract, optional to implement.
32873314

32883315
Parameters: ~
32893316
{node} (`nvim_tree.api.Node`)
32903317

32913318
Return: ~
3292-
(`nvim_tree.api.decorator.highlighted_string[]?`) icons
3319+
(`nvim_tree.api.decorator.highlighted_string[]?`) icons `nil` or empty
3320+
table for no icons. Only the first glyph of {str} is used when
3321+
{icon_placement} is `"signcolumn"`
32933322

32943323

32953324
==============================================================================
Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,109 @@
11
---@meta
22

33
---@brief
4-
---Highlighting and icons for nodes are provided by Decorators, see [nvim-tree-icons-highlighting]. You may provide your own in addition to the builtin decorators.
4+
---Highlighting and icons for nodes are provided by Decorators, see [nvim-tree-icons-highlighting] for an overview. You may provide your own in addition to the builtin decorators.
5+
---
6+
---Decorators are rendered in [nvim_tree.config.renderer] {decorators} order of precedence, with later decorators applying additively over earlier.
57
---
68
---Decorators may:
79
---- Add icons
8-
---- Set highlight group for the name or icons
10+
---- Set a highlight group name for the name or icons
911
---- Override node icon
1012
---
1113
---To register your decorator:
1214
---- Create a class that extends [nvim_tree.api.Decorator]
1315
---- Register it by adding the class to [nvim_tree.config.renderer] {decorators}
1416
---
17+
---Your decorator will be constructed and executed each time the tree is rendered.
18+
---
1519
---Your class must:
16-
---- [nvim_tree.Class:extend()] the interface [nvim_tree.api.Decorator]
20+
---- [nvim_tree.Class:extend()] the interface [nvim_tree.api.Decorator]
1721
---- Provide a no-arguments constructor [nvim_tree.Class:new()] that sets the mandatory fields:
1822
--- - {enabled}
1923
--- - {highlight_range}
2024
--- - {icon_placement}
25+
---- Call [nvim_tree.api.Decorator:define_sign()] in your constructor when {icon_placement} is `"signcolumn"`
2126
---
2227
---Your class may:
2328
---- Implement methods to provide decorations:
2429
--- - [nvim_tree.api.Decorator:highlight_group()]
2530
--- - [nvim_tree.api.Decorator:icon_node()]
2631
--- - [nvim_tree.api.Decorator:icons()]
27-
---
28-
---Your class must:
29-
---- Call [nvim_tree.api.Decorator:define_sign()] in your constructor if using `"signcolumn"` {icon_placement}
3032

3133
local nvim_tree = { api = {} }
3234

3335
local Class = require("nvim-tree.classic")
3436

3537
---
36-
---Highlight group range as per nvim-tree.renderer.highlight_*
37-
---
38-
---@alias nvim_tree.api.decorator.highlight_range nvim_tree.config.renderer.highlight
39-
40-
---Icon position as per renderer.icons.*_placement
41-
---
42-
---@alias nvim_tree.api.decorator.icon_placement "none"|nvim_tree.config.renderer.icons.placement
43-
38+
---TODO #3241 add this to config
4439
---
4540
---Names of builtin decorators or your decorator classes. Builtins are ordered lowest to highest priority.
4641
---
4742
---@alias nvim_tree.api.decorator.types nvim_tree.api.Decorator|"Git"|"Opened"|"Hidden"|"Modified"|"Bookmarks"|"Diagnostics"|"Copied"|"Cut"
4843

44+
45+
---
46+
---Text or glyphs with optional highlight group names to apply to it.
4947
---
50-
---A string for rendering, with optional highlight groups to apply to it
48+
---@class nvim_tree.api.decorator.highlighted_string
5149
---
52-
---@class (exact) nvim_tree.api.decorator.highlighted_string
50+
---One or many glyphs/characters.
5351
---@field str string
52+
---
53+
---Highlight group names to apply in order. Empty table for no highlighting.
5454
---@field hl string[]
5555

56+
5657
---
57-
---Abstract Decorator interface
58+
---Decorator interface
5859
---
5960
---@class nvim_tree.api.Decorator: nvim_tree.Class
61+
---
62+
---Enable this decorator.
6063
---@field enabled boolean
61-
---@field highlight_range nvim_tree.api.decorator.highlight_range
62-
---@field icon_placement nvim_tree.api.decorator.icon_placement
64+
---
65+
---What to highlight: [nvim_tree.config.renderer.highlight]
66+
---@field highlight_range nvim_tree.config.renderer.highlight
67+
---
68+
---Where to place the icons: [nvim_tree.config.renderer.icons.placement]
69+
---@field icon_placement "none"|nvim_tree.config.renderer.icons.placement
70+
---
6371
local Decorator = Class:extend()
6472
nvim_tree.api.Decorator = Decorator
6573

6674
---
67-
---Abstract: optionally implement to set the node's icon
75+
---Icon to override for the node.
76+
---
77+
---Abstract, optional to implement.
6878
---
6979
---@param node nvim_tree.api.Node
70-
---@return nvim_tree.api.decorator.highlighted_string? icon_node
80+
---@return nvim_tree.api.decorator.highlighted_string? icon `nil` for no override
7181
function Decorator:icon_node(node) end
7282

7383
---
74-
---Abstract: optionally implement to provide icons and the highlight groups for your icon_placement.
84+
---Icons to add to the node as per {icon_placement}
85+
---
86+
---Abstract, optional to implement.
7587
---
7688
---@param node nvim_tree.api.Node
77-
---@return nvim_tree.api.decorator.highlighted_string[]? icons
89+
---@return nvim_tree.api.decorator.highlighted_string[]? icons `nil` or empty table for no icons. Only the first glyph of {str} is used when {icon_placement} is `"signcolumn"`
7890
function Decorator:icons(node) end
7991

8092
---
81-
---Abstract: optionally implement to provide one highlight group to apply to your highlight_range.
93+
---One highlight group that applies additively to the {node} name for {highlight_range}.
94+
---
95+
---Abstract, optional to implement.
8296
---
8397
---@param node nvim_tree.api.Node
84-
---@return string? highlight_group
98+
---@return string? highlight group name `nil` when no highlighting to apply to the node
8599
function Decorator:highlight_group(node) end
86100

87101
---
88-
---Defines a sign. This should be called in the constructor.
102+
---Defines a sign for an icon. This is mandatory and necessary only when {icon_placement} is `"signcolumn"`
103+
---
104+
---This must be called during your constructor for all icons that you will return from [nvim_tree.api.Decorator:icons()]
89105
---
90-
---@param icon nvim_tree.api.decorator.highlighted_string?
106+
---@param icon nvim_tree.api.decorator.highlighted_string? does nothing if nil
91107
function Decorator:define_sign(icon) end
92108

93109
return nvim_tree.api.Decorator

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---Abstract Decorator implementation
22
---@class (exact) Decorator: Class
33
---@field protected enabled boolean
4-
---@field protected highlight_range nvim_tree.api.decorator.highlight_range
5-
---@field protected icon_placement nvim_tree.api.decorator.icon_placement
4+
---@field protected highlight_range nvim_tree.config.renderer.highlight
5+
---@field protected icon_placement "none"|nvim_tree.config.renderer.icons.placement
66
local Decorator = require("nvim-tree._meta.api.decorator"):extend()
77

88
---TODO #3241 create an internal decorator class with explorer member and lose the UserDecorator

0 commit comments

Comments
 (0)