Skip to content

Commit b0b4955

Browse files
authored
docs: polish example decorator (#3160)
1 parent 8eb5e0b commit b0b4955

File tree

1 file changed

+60
-30
lines changed

1 file changed

+60
-30
lines changed

doc/nvim-tree-lua.txt

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,77 +2929,107 @@ Decorators may:
29292929
- Set highlight group for the name or icons
29302930
- Override node icon
29312931

2932-
Specify decorators and their precedence via |nvim-tree.renderer.decorators|
2933-
e.g. defaults with a user decorator class being overridden only by Cut: >lua
2934-
{
2935-
"Git",
2936-
"Open",
2937-
"Hidden",
2938-
"Modified",
2939-
"Bookmark",
2940-
"Diagnostics",
2941-
"Copied",
2942-
MyDecorator,
2943-
"Cut",
2944-
}
2932+
Create a `nvim_tree.api.decorator.UserDecorator` class and register it with
2933+
precedence via |nvim-tree.renderer.decorators|
2934+
2935+
See |nvim-tree-decorator-example|
2936+
2937+
See `nvim-tree/_meta/api_decorator.lua` for full class documentation.
29452938

2946-
See `nvim-tree/_meta/api_decorator.lua` for full
2947-
`nvim_tree.api.decorator.UserDecorator` class documentation.
2948-
<
29492939
==============================================================================
29502940
11.1. DECORATOR EXAMPLE *nvim-tree-decorator-example*
2941+
2942+
A decorator class for nodes named "example", overridind all builtin decorators
2943+
except for Cut.
2944+
2945+
- Highlights node name with `IncSearch`
2946+
- Creates two icons `"1"` and `"2"` placed after the node name, highlighted with
2947+
`DiffAdd` and `DiffText`
2948+
- Replaces the node icon with `"N"`, highlighted with `Error `
2949+
2950+
Create a class file `~/.config/nvim/lua/my-decorator.lua`
2951+
2952+
Require and register it during |nvim-tree-setup|:
2953+
>lua
2954+
local MyDecorator = require("my-decorator")
2955+
2956+
require("nvim-tree").setup({
2957+
renderer = {
2958+
decorators = {
2959+
"Git",
2960+
"Open",
2961+
"Hidden",
2962+
"Modified",
2963+
"Bookmark",
2964+
"Diagnostics",
2965+
"Copied",
2966+
MyDecorator,
2967+
"Cut",
2968+
},
2969+
},
2970+
})
2971+
<
2972+
Contents of `my-decorator.lua`:
29512973
>lua
2952-
---Create your decorator class
29532974
---@class (exact) MyDecorator: nvim_tree.api.decorator.UserDecorator
2954-
---@field private my_icon nvim_tree.api.HighlightedString
2975+
---@field private my_icon1 nvim_tree.api.HighlightedString
2976+
---@field private my_icon2 nvim_tree.api.HighlightedString
2977+
---@field private my_icon_node nvim_tree.api.HighlightedString
2978+
---@field private my_highlight_group string
29552979
local MyDecorator = require("nvim-tree.api").decorator.UserDecorator:extend()
29562980

29572981
---Mandatory constructor :new() will be called once per tree render, with no arguments.
29582982
function MyDecorator:new()
2959-
self.enabled = true
2960-
self.highlight_range = "all"
2961-
self.icon_placement = "signcolumn"
2983+
self.enabled = true
2984+
self.highlight_range = "name"
2985+
self.icon_placement = "after"
29622986

2963-
-- create your icon once, for convenience
2964-
self.my_icon = { str = "I", hl = { "MyIcon" } }
2987+
-- create your icons and highlights once, applied to every node
2988+
self.my_icon1 = { str = "1", hl = { "DiffAdd" } }
2989+
self.my_icon2 = { str = "2", hl = { "DiffText" } }
2990+
self.my_icon_node = { str = "N", hl = { "Error" } }
2991+
self.my_highlight_group = "IncSearch"
29652992

2966-
-- Define the icon sign only once
2993+
-- Define the icon signs only once
29672994
-- Only needed if you are using icon_placement = "signcolumn"
2968-
self:define_sign(self.my_icon)
2995+
-- self:define_sign(self.my_icon1)
2996+
-- self:define_sign(self.my_icon2)
29692997
end
29702998

29712999
---Override node icon
29723000
---@param node nvim_tree.api.Node
29733001
---@return nvim_tree.api.HighlightedString? icon_node
29743002
function MyDecorator:icon_node(node)
29753003
if node.name == "example" then
2976-
return self.my_icon
3004+
return self.my_icon_node
29773005
else
29783006
return nil
29793007
end
29803008
end
29813009

2982-
---Return one icon for DecoratorIconPlacement
3010+
---Return two icons for DecoratorIconPlacement "after"
29833011
---@param node nvim_tree.api.Node
29843012
---@return nvim_tree.api.HighlightedString[]? icons
29853013
function MyDecorator:icons(node)
29863014
if node.name == "example" then
2987-
return { self.my_icon }
3015+
return { self.my_icon1, self.my_icon2, }
29883016
else
29893017
return nil
29903018
end
29913019
end
29923020

2993-
---Exactly one highlight group for DecoratorHighlightRange
3021+
---Exactly one highlight group for DecoratorHighlightRange "name"
29943022
---@param node nvim_tree.api.Node
29953023
---@return string? highlight_group
29963024
function MyDecorator:highlight_group(node)
29973025
if node.name == "example" then
2998-
return "MyHighlight"
3026+
return self.my_highlight_group
29993027
else
30003028
return nil
30013029
end
30023030
end
3031+
3032+
return MyDecorator
30033033
<
30043034
==============================================================================
30053035
12. OS SPECIFIC RESTRICTIONS *nvim-tree-os-specific*

0 commit comments

Comments
 (0)