Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork634
Decorators
Alexander Courtis edited this pageJun 21, 2025 ·4 revisions
Please share your custom decorators.
See:help nvim-tree-decorators for documentation and example:help nvim-tree-decorator-example
See_meta/api_decorator.lua fornvim_tree.api.decorator.UserDecorator class documentation.See_meta/api.lua fornvim_tree.api.Node classes documentation.
Highlights files which are in your quickfix list.
---@class (exact)QuickfixDecorator:nvim_tree.api.decorator.UserDecorator---@fieldprivateqf_icon nvim_tree.api.HighlightedStringlocalQuickfixDecorator=require('nvim-tree.api').decorator.UserDecorator:extend()localaugroup=vim.api.nvim_create_augroup('nvim-tree-quickfix-decorator', {clear=true })localautocmds_setup=falselocalfunctionsetup_autocmds()ifautocmds_setupthenreturnendautocmds_setup=truevim.api.nvim_create_autocmd('QuickfixCmdPost', {group=augroup,callback=function()require('nvim-tree.api').tree.reload()end, })vim.api.nvim_create_autocmd('FileType', {pattern='qf',group=augroup,callback=function(evt)vim.api.nvim_create_autocmd('TextChanged', {buffer=evt.buf,group=augroup,callback=function()require('nvim-tree.api').tree.reload()end, })end, })endfunctionQuickfixDecorator:new()self.enabled=trueself.highlight_range='all'self.icon_placement='signcolumn'self.qf_icon= {str='',hl= {'QuickFixLine'} }self:define_sign(self.qf_icon)setup_autocmds()end---Helper function to check if a node is in quickfix list---@paramnodenvim_tree.api.Node---@returnbooleanlocalfunctionis_qf_item(node)ifnode.name=='..'ornode.type=='directory'thenreturnfalseendlocalbufnr=vim.fn.bufnr(node.absolute_path)returnbufnr~=-1andvim.iter(vim.fn.getqflist()):any(function(qf)returnqf.bufnr==bufnrend)end---Return quickfix icons for the node---@paramnodenvim_tree.api.Node---@returnnvim_tree.api.HighlightedString[]?iconsfunctionQuickfixDecorator:icons(node)ifis_qf_item(node)thenreturn {self.qf_icon }endreturnnilend---Return highlight group for the node---@paramnodenvim_tree.api.Node---@returnstring?highlight_groupfunctionQuickfixDecorator:highlight_group(node)ifis_qf_item(node)thenreturn'QuickFixLine'endreturnnilendreturnQuickfixDecorator
A decorator class for nodes named "example", overridind all builtin decoratorsexcept for Cut.
- Highlights node name with
IncSearch - Creates two icons
"1"and"2"placed after the node name, highlighted withDiffAddandDiffText - Replaces the node icon with
"N", highlighted withError
Create a class file~/.config/nvim/lua/my-decorator.lua
Require and register it during setup:
localMyDecorator=require("my-decorator")require("nvim-tree").setup({renderer= {decorators= {"Git","Open","Hidden","Modified","Bookmark","Diagnostics","Copied",MyDecorator,"Cut", }, },})
Contents ofmy-decorator.lua:
---@class (exact)MyDecorator:nvim_tree.api.decorator.UserDecorator---@fieldprivatemy_icon1 nvim_tree.api.HighlightedString---@fieldprivatemy_icon2 nvim_tree.api.HighlightedString---@fieldprivatemy_icon_node nvim_tree.api.HighlightedString---@fieldprivatemy_highlight_group stringlocalMyDecorator=require("nvim-tree.api").decorator.UserDecorator:extend()---Mandatory constructor :new() will be called once per tree render, with no arguments.functionMyDecorator:new()self.enabled=trueself.highlight_range="name"self.icon_placement="after"-- create your icons and highlights once, applied to every nodeself.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 signs only once-- Only needed if you are using icon_placement = "signcolumn"-- self:define_sign(self.my_icon1)-- self:define_sign(self.my_icon2)end---Override node icon---@paramnodenvim_tree.api.Node---@returnnvim_tree.api.HighlightedString?icon_nodefunctionMyDecorator:icon_node(node)ifnode.name=="example"thenreturnself.my_icon_nodeelsereturnnilendend---Return two icons for DecoratorIconPlacement "after"---@paramnodenvim_tree.api.Node---@returnnvim_tree.api.HighlightedString[]?iconsfunctionMyDecorator:icons(node)ifnode.name=="example"thenreturn {self.my_icon1,self.my_icon2, }elsereturnnilendend---Exactly one highlight group for DecoratorHighlightRange "name"---@paramnodenvim_tree.api.Node---@returnstring?highlight_groupfunctionMyDecorator:highlight_group(node)ifnode.name=="example"thenreturnself.my_highlight_groupelsereturnnilendendreturnMyDecorator