Skip to content

Commit 4bf4a85

Browse files
committed
feat(#2948): tidy decorator args and complete documentation
1 parent cb351ae commit 4bf4a85

File tree

15 files changed

+172
-267
lines changed

15 files changed

+172
-267
lines changed

doc/nvim-tree-lua.txt

Lines changed: 100 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ CONTENTS *nvim-tree*
5252
8.2 Highlight: Overhaul |nvim-tree-highlight-overhaul|
5353
9. Events |nvim-tree-events|
5454
10. Prompts |nvim-tree-prompts|
55-
11. OS Specific Restrictions |nvim-tree-os-specific|
56-
12. Netrw |nvim-tree-netrw|
57-
13. Legacy |nvim-tree-legacy|
58-
13.1 Legacy: Opts |nvim-tree-legacy-opts|
59-
13.2 Legacy: Highlight |nvim-tree-legacy-highlight|
60-
14. Index |nvim-tree-index|
61-
14.1 Index: Opts |nvim-tree-index-opts|
62-
14.2 Index: API |nvim-tree-index-api|
55+
11. Decorators |nvim-tree-decorators|
56+
12. OS Specific Restrictions |nvim-tree-os-specific|
57+
13. Netrw |nvim-tree-netrw|
58+
14. Legacy |nvim-tree-legacy|
59+
14.1 Legacy: Opts |nvim-tree-legacy-opts|
60+
14.2 Legacy: Highlight |nvim-tree-legacy-highlight|
61+
15. Index |nvim-tree-index|
62+
15.1 Index: Opts |nvim-tree-index-opts|
63+
15.2 Index: API |nvim-tree-index-api|
6364

6465
==============================================================================
6566
1. INTRODUCTION *nvim-tree-introduction*
@@ -843,9 +844,6 @@ Use nvim-tree in a floating window.
843844
==============================================================================
844845
5.3 OPTS: RENDERER *nvim-tree-opts-renderer*
845846

846-
Highlight precedence, additive, change via |nvim-tree.renderer.decorators|
847-
git < opened < modified < bookmarked < diagnostics < copied < cut
848-
849847
*nvim-tree.renderer.add_trailing*
850848
Appends a trailing slash to folder names.
851849
Type: `boolean`, Default: `false`
@@ -1013,9 +1011,6 @@ Configuration options for tree indent markers.
10131011
*nvim-tree.renderer.icons*
10141012
Configuration options for icons.
10151013

1016-
Icon order and sign column precedence, change via |nvim-tree.renderer.decorators|
1017-
git < hidden < modified < bookmarked < diagnostics
1018-
10191014
`renderer.icons.*_placement` options may be:
10201015
- `"before"` : before file/folder, after the file/folders icons
10211016
- `"after"` : after file/folder
@@ -2772,7 +2767,90 @@ configurations for different types of prompts.
27722767
send all bookmarked to trash during |nvim-tree-api.marks.bulk.trash()|
27732768

27742769
==============================================================================
2775-
11. OS SPECIFIC RESTRICTIONS *nvim-tree-os-specific*
2770+
11. DECORATORS *nvim-tree-decorators*
2771+
2772+
Highlighting and icons for nodes are is provided by Decorators. You may provide
2773+
your own in addition to the builtin decorators.
2774+
2775+
Decorators may:
2776+
- Add icons
2777+
- Set highlight group for the name or icons
2778+
- Override node icon
2779+
2780+
Create your decorator class via `api.decorator.DecoratorUser:extend()` and add it
2781+
to |nvim-tree.renderer.decorators|
2782+
2783+
e.g. default decorators with an user decorator being overridden only by Cut: >lua
2784+
{
2785+
"Git",
2786+
"Opened",
2787+
"Hidden",
2788+
"Modified",
2789+
"Bookmarks",
2790+
"Diagnostics",
2791+
"Copied",
2792+
MyDecorator,
2793+
"Cut",
2794+
}
2795+
<
2796+
See `api_decorator.lua` for decorator class definition and full documentation.
2797+
2798+
Example decorator: >lua
2799+
2800+
---Create your decorator class
2801+
---@class (exact) MyDecorator: nvim_tree.api.decorator.DecoratorUser
2802+
---@field private my_icon nvim_tree.api.HighlightedString
2803+
local MyDecorator = require("nvim-tree.api").decorator.DecoratorUser:extend()
2804+
2805+
---Mandatory constructor :new() will be called once per tree render, with no arguments.
2806+
function MyDecorator:new()
2807+
self.enabled = true
2808+
self.highlight_range = "all"
2809+
self.icon_placement = "signcolumn"
2810+
2811+
-- create your icon once, for convenience
2812+
self.my_icon = { str = "I", hl = { "MyIcon" } }
2813+
2814+
-- Define the icon sign only once
2815+
-- Only needed if you are using icon_placement = "signcolumn"
2816+
self:define_sign(self.my_icon)
2817+
end
2818+
2819+
---Override node icon
2820+
---@param node nvim_tree.api.Node
2821+
---@return nvim_tree.api.HighlightedString? icon_node
2822+
function MyDecorator:icon_node(node)
2823+
if node.name == "example" then
2824+
return self.my_icon
2825+
else
2826+
return nil
2827+
end
2828+
end
2829+
2830+
---Return one icon for DecoratorIconPlacement
2831+
---@param node nvim_tree.api.Node
2832+
---@return nvim_tree.api.HighlightedString[]? icons
2833+
function MyDecorator:icons(node)
2834+
if node.name == "example" then
2835+
return { self.my_icon }
2836+
else
2837+
return nil
2838+
end
2839+
end
2840+
2841+
---Exactly one highlight group for DecoratorHighlightRange
2842+
---@param node nvim_tree.api.Node
2843+
---@return string? highlight_group
2844+
function MyDecorator:highlight_group(node)
2845+
if node.name == "example" then
2846+
return "MyHighlight"
2847+
else
2848+
return nil
2849+
end
2850+
end
2851+
<
2852+
==============================================================================
2853+
12. OS SPECIFIC RESTRICTIONS *nvim-tree-os-specific*
27762854

27772855
Windows WSL and PowerShell
27782856
- Trash is synchronized
@@ -2784,7 +2862,7 @@ Windows WSL and PowerShell
27842862
issues or disable this feature.
27852863

27862864
==============================================================================
2787-
12. NETRW *nvim-tree-netrw*
2865+
13. NETRW *nvim-tree-netrw*
27882866

27892867
|netrw| is a standard neovim plugin that is enabled by default. It provides,
27902868
amongst other functionality, a file/directory browser.
@@ -2805,14 +2883,14 @@ keep using |netrw| without its browser features please ensure:
28052883
|nvim-tree.hijack_netrw| ` = true`
28062884

28072885
==============================================================================
2808-
13. LEGACY *nvim-tree-legacy*
2886+
14. LEGACY *nvim-tree-legacy*
28092887

28102888
Breaking refactors have been made however the legacy versions will be silently
28112889
migrated and used.
28122890
There are no plans to remove this migration.
28132891

28142892
==============================================================================
2815-
13.1 LEGACY: OPTS *nvim-tree-legacy-opts*
2893+
14.1 LEGACY: OPTS *nvim-tree-legacy-opts*
28162894

28172895
Legacy options are translated to the current, making type and value changes as
28182896
needed.
@@ -2830,7 +2908,7 @@ needed.
28302908
`renderer.icons.webdev_colors` |nvim-tree.renderer.icons.web_devicons.file.color|
28312909

28322910
==============================================================================
2833-
13.2 LEGACY: HIGHLIGHT *nvim-tree-legacy-highlight*
2911+
14.2 LEGACY: HIGHLIGHT *nvim-tree-legacy-highlight*
28342912

28352913
Legacy highlight group are still obeyed when they are defined and the current
28362914
highlight group is not, hard linking as follows: >
@@ -2879,10 +2957,10 @@ highlight group is not, hard linking as follows: >
28792957
NvimTreeLspDiagnosticsHintFolderText NvimTreeDiagnosticHintFolderHL
28802958
<
28812959
==============================================================================
2882-
14 INDEX *nvim-tree-index*
2960+
15 INDEX *nvim-tree-index*
28832961

28842962
==============================================================================
2885-
14.1 INDEX: OPTS *nvim-tree-index-opts*
2963+
15.1 INDEX: OPTS *nvim-tree-index-opts*
28862964

28872965
|nvim-tree.actions.change_dir|
28882966
|nvim-tree.actions.change_dir.enable|
@@ -3051,7 +3129,7 @@ highlight group is not, hard linking as follows: >
30513129
|nvim-tree.view.width.padding|
30523130

30533131
==============================================================================
3054-
14.2 INDEX: API *nvim-tree-index-api*
3132+
15.2 INDEX: API *nvim-tree-index-api*
30553133

30563134
|nvim-tree-api.commands.get()|
30573135
|nvim-tree-api.config.mappings.default_on_attach()|

lua/nvim-tree/_meta/aliases.lua

Lines changed: 0 additions & 19 deletions
This file was deleted.

lua/nvim-tree/_meta/api_decorator.lua

Lines changed: 7 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
---@meta
22
error("Cannot require a meta file")
33

4-
local nvim_tree = { api = { decorator = { DecoratorUser = {} } } }
5-
6-
---Custom decorator
7-
---It may:
8-
--- Add icons
9-
--- Set highlight group for the name or icons
10-
--- Override node icon
11-
---Register it via :help nvim-tree.renderer.decorators
12-
---Create class via require("nvim-tree.api").decorator.DecoratorUser:extend()
13-
---Mandatory constructor :new() will be called once per tree render, with no arguments.
14-
---Constructor must call:
15-
--- :init
16-
--- :define_sign when using "signcolumn" range
4+
local nvim_tree = {}
175

186
---Highlight group range as per nvim-tree.renderer.highlight_*
197
---@alias nvim_tree.api.decorator.HighlightRange "none" | "icon" | "name" | "all"
@@ -24,27 +12,28 @@ local nvim_tree = { api = { decorator = { DecoratorUser = {} } } }
2412
---Names of builtin decorators or your decorator classes. Builtins are ordered lowest to highest priority.
2513
---@alias nvim_tree.api.decorator.Name "Git" | "Opened" | "Hidden" | "Modified" | "Bookmarks" | "Diagnostics" | "Copied" | "Cut" | nvim_tree.api.decorator.DecoratorUser
2614

27-
---Your decorator will extend this class via require("nvim-tree.api").decorator.DecoratorUser:extend()
15+
---Custom decorator, see :help nvim-tree-decorators
2816
---
2917
---@class (exact) nvim_tree.api.decorator.DecoratorUser
3018
---@field protected enabled boolean
3119
---@field protected highlight_range nvim_tree.api.decorator.HighlightRange
3220
---@field protected icon_placement nvim_tree.api.decorator.IconPlacement
3321

34-
---Abstract: no-args constructor must be implemented.
22+
---Abstract: no-args constructor must be implemented and will be called once per tree render.
23+
---Must set all fields.
3524
---
3625
function nvim_tree.api.decorator.DecoratorUser:new() end
3726

3827
---Abstract: optionally implement to set the node's icon
3928
---
4029
---@param node nvim_tree.api.Node
41-
---@return HighlightedString? icon_node
30+
---@return nvim_tree.api.HighlightedString? icon_node
4231
function nvim_tree.api.decorator.DecoratorUser:icon_node(node) end
4332

4433
---Abstract: optionally implement to provide icons and the highlight groups for your icon_placement.
4534
---
4635
---@param node nvim_tree.api.Node
47-
---@return HighlightedString[]? icons
36+
---@return nvim_tree.api.HighlightedString[]? icons
4837
function nvim_tree.api.decorator.DecoratorUser:icons(node) end
4938

5039
---Abstract: optionally implement to provide one highlight group to apply to your highlight_range.
@@ -53,81 +42,8 @@ function nvim_tree.api.decorator.DecoratorUser:icons(node) end
5342
---@return string? highlight_group
5443
function nvim_tree.api.decorator.DecoratorUser:highlight_group(node) end
5544

56-
---Must be called from your constructor.
57-
---
58-
---@class (exact) nvim_tree.api.decorator.InitArgs
59-
---@field enabled boolean
60-
---@field highlight_range nvim_tree.api.decorator.HighlightRange
61-
---@field icon_placement nvim_tree.api.decorator.IconPlacement
62-
---
63-
---@protected
64-
---@param args nvim_tree.api.decorator.InitArgs
65-
function nvim_tree.api.decorator.DecoratorUser:init(args) end
66-
6745
---Define a sign. This should be called in the constructor.
6846
---
6947
---@protected
70-
---@param icon HighlightedString?
48+
---@param icon nvim_tree.api.HighlightedString?
7149
function nvim_tree.api.decorator.DecoratorUser:define_sign(icon) end
72-
73-
74-
--
75-
-- Example Decorator
76-
--
77-
78-
---@class (exact) MyDecorator: nvim_tree.api.decorator.DecoratorUser
79-
---@field private my_icon nvim_tree.api.HighlightedString
80-
local MyDecorator = require("nvim-tree.api").decorator.DecoratorUser:extend()
81-
82-
---Mandatory constructor :new() will be called once per tree render, with no arguments.
83-
function MyDecorator:new()
84-
---@type nvim_tree.api.decorator.InitArgs
85-
local args = {
86-
enabled = true,
87-
highlight_range = "all",
88-
icon_placement = "signcolumn",
89-
}
90-
91-
-- init
92-
self:init(args)
93-
94-
-- create your icon once, for convenience
95-
self.my_icon = { str = "I", hl = { "MyIcon" } }
96-
97-
-- Define the icon sign only once
98-
-- Only needed if you are using icon_placement = "signcolumn"
99-
self:define_sign(self.my_icon)
100-
end
101-
102-
---Override node icon
103-
---@param node nvim_tree.api.Node
104-
---@return nvim_tree.api.HighlightedString? icon_node
105-
function MyDecorator:icon_node(node)
106-
if node.name == "example" then
107-
return self.my_icon
108-
else
109-
return nil
110-
end
111-
end
112-
113-
---Return one icon for DecoratorIconPlacement
114-
---@param node nvim_tree.api.Node
115-
---@return nvim_tree.api.HighlightedString[]? icons
116-
function MyDecorator:icons(node)
117-
if node.name == "example" then
118-
return { self.my_icon }
119-
else
120-
return nil
121-
end
122-
end
123-
124-
---Exactly one highlight group for DecoratorHighlightRange
125-
---@param node nvim_tree.api.Node
126-
---@return string? highlight_group
127-
function MyDecorator:highlight_group(node)
128-
if node.name == "example" then
129-
return "MyHighlight"
130-
else
131-
return nil
132-
end
133-
end

lua/nvim-tree/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ Api.commands.get = wrap(function()
313313
return require("nvim-tree.commands").get()
314314
end)
315315

316-
---User provided decorator. Extend this class via :extend()
316+
---Create a decorator class by calling :extend()
317317
---@type nvim_tree.api.decorator.DecoratorUser
318318
Api.decorator.DecoratorUser = DecoratorUser --[[@as nvim_tree.api.decorator.DecoratorUser]]
319319

lua/nvim-tree/renderer/builder.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ local DecoratorUser = require("nvim-tree.renderer.decorator.user")
1818

1919
local pad = require("nvim-tree.renderer.components.padding")
2020

21+
---@alias HighlightedString nvim_tree.api.HighlightedString
22+
2123
-- Builtin Decorators
2224
---@type table<nvim_tree.api.decorator.Name, Decorator>
2325
local BUILTIN_DECORATORS = {

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@ local DecoratorBookmarks = Decorator:extend()
1111
---@protected
1212
---@param args DecoratorArgs
1313
function DecoratorBookmarks:new(args)
14-
self.explorer = args.explorer
14+
self.explorer = args.explorer
1515

16-
---@type AbstractDecoratorArgs
17-
local a = {
18-
enabled = true,
19-
highlight_range = self.explorer.opts.renderer.highlight_bookmarks or "none",
20-
icon_placement = self.explorer.opts.renderer.icons.bookmarks_placement or "none",
21-
}
22-
23-
DecoratorBookmarks.super.new(self, a)
16+
self.enabled = true
17+
self.highlight_range = self.explorer.opts.renderer.highlight_bookmarks or "none"
18+
self.icon_placement = self.explorer.opts.renderer.icons.bookmarks_placement or "none"
2419

2520
if self.explorer.opts.renderer.icons.show.bookmarks then
2621
self.icon = {

0 commit comments

Comments
 (0)