Skip to content

docs: polish example decorator #3160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 60 additions & 30 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2929,77 +2929,107 @@ Decorators may:
- Set highlight group for the name or icons
- Override node icon

Specify decorators and their precedence via |nvim-tree.renderer.decorators|
e.g. defaults with a user decorator class being overridden only by Cut: >lua
{
"Git",
"Open",
"Hidden",
"Modified",
"Bookmark",
"Diagnostics",
"Copied",
MyDecorator,
"Cut",
}
Create a `nvim_tree.api.decorator.UserDecorator` class and register it with
precedence via |nvim-tree.renderer.decorators|

See |nvim-tree-decorator-example|

See `nvim-tree/_meta/api_decorator.lua` for full class documentation.

See `nvim-tree/_meta/api_decorator.lua` for full
`nvim_tree.api.decorator.UserDecorator` class documentation.
<
==============================================================================
11.1. DECORATOR EXAMPLE *nvim-tree-decorator-example*

A decorator class for nodes named "example", overridind all builtin decorators
except for Cut.

- Highlights node name with `IncSearch`
- Creates two icons `"1"` and `"2"` placed after the node name, highlighted with
`DiffAdd` and `DiffText`
- Replaces the node icon with `"N"`, highlighted with `Error `

Create a class file `~/.config/nvim/lua/my-decorator.lua`

Require and register it during |nvim-tree-setup|:
>lua
local MyDecorator = require("my-decorator")

require("nvim-tree").setup({
renderer = {
decorators = {
"Git",
"Open",
"Hidden",
"Modified",
"Bookmark",
"Diagnostics",
"Copied",
MyDecorator,
"Cut",
},
},
})
<
Contents of `my-decorator.lua`:
>lua
---Create your decorator class
---@class (exact) MyDecorator: nvim_tree.api.decorator.UserDecorator
---@field private my_icon nvim_tree.api.HighlightedString
---@field private my_icon1 nvim_tree.api.HighlightedString
---@field private my_icon2 nvim_tree.api.HighlightedString
---@field private my_icon_node nvim_tree.api.HighlightedString
---@field private my_highlight_group string
local MyDecorator = require("nvim-tree.api").decorator.UserDecorator:extend()

---Mandatory constructor :new() will be called once per tree render, with no arguments.
function MyDecorator:new()
self.enabled = true
self.highlight_range = "all"
self.icon_placement = "signcolumn"
self.enabled = true
self.highlight_range = "name"
self.icon_placement = "after"

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

-- Define the icon sign only once
-- Define the icon signs only once
-- Only needed if you are using icon_placement = "signcolumn"
self:define_sign(self.my_icon)
-- self:define_sign(self.my_icon1)
-- self:define_sign(self.my_icon2)
end

---Override node icon
---@param node nvim_tree.api.Node
---@return nvim_tree.api.HighlightedString? icon_node
function MyDecorator:icon_node(node)
if node.name == "example" then
return self.my_icon
return self.my_icon_node
else
return nil
end
end

---Return one icon for DecoratorIconPlacement
---Return two icons for DecoratorIconPlacement "after"
---@param node nvim_tree.api.Node
---@return nvim_tree.api.HighlightedString[]? icons
function MyDecorator:icons(node)
if node.name == "example" then
return { self.my_icon }
return { self.my_icon1, self.my_icon2, }
else
return nil
end
end

---Exactly one highlight group for DecoratorHighlightRange
---Exactly one highlight group for DecoratorHighlightRange "name"
---@param node nvim_tree.api.Node
---@return string? highlight_group
function MyDecorator:highlight_group(node)
if node.name == "example" then
return "MyHighlight"
return self.my_highlight_group
else
return nil
end
end

return MyDecorator
<
==============================================================================
12. OS SPECIFIC RESTRICTIONS *nvim-tree-os-specific*
Expand Down
Loading