Skip to content
Alexander Courtis edited this page Dec 2, 2024 · 3 revisions

Please share your custom decorators.

See :help nvim-tree-decorators for documentation and an example.

See _meta/api_decorator.lua for nvim_tree.api.decorator.UserDecorator class documentation. See _meta/api.lua for nvim_tree.api.Node classes documentation.

Quick Fix Needs Proper Name

@b0o

Over to you...

Reference Example

See :help nvim-tree-decorators

Decorates nodes named "example":

  • Highlights name and icon
  • Places an icon in the sign column
  • Replaces the node's icon

Register this decorator class via setup options, taking priority over all but "Copied" and "Cut":

local MyDecorator = require("path.to.my-decorator")

renderer.decorators = decorators = { "Git", "Open", "Hidden", "Modified", "Bookmark", "Diagnostics", MyDecorator, "Copied", "Cut", },

lua/path/to/my-decorator.lua:

---Create your decorator class
---@class (exact) MyDecorator: nvim_tree.api.decorator.UserDecorator
---@field private my_icon nvim_tree.api.HighlightedString
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"

  -- create your icon once, for convenience
  self.my_icon         = { str = "I", hl = { "MyIcon" } }

  -- Define the icon sign only once
  -- Only needed if you are using icon_placement = "signcolumn"
  self:define_sign(self.my_icon)
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
  else
    return nil
  end
end

---Return one icon for DecoratorIconPlacement
---@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 }
  else
    return nil
  end
end

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

return MyDecorator
Clone this wiki locally