nvim-tree.lua/lua/nvim-tree/renderer/decorator/user.lua
2024-11-22 12:18:47 +11:00

83 lines
2.0 KiB
Lua

local Decorator = require("nvim-tree.renderer.decorator")
---Abstract user decorator, extend to define your own.
---Icon and highlight are optional.
---Constructor will be called once per tree render, with no arguments:
--- Must call super passing DecoratorArgs: MyDecorator.super.new(self, args)
--- Must call define_sign, when using "signcolumn"
---See example at end.
---@class (exact) UserDecorator: Decorator
local UserDecorator = Decorator:extend()
---Override this method to provide icons and the highlight groups to apply to them
---@param node Node
---@return HighlightedString[]? icons
function UserDecorator:calculate_icons(node)
self:nop(node)
end
---Override this method to provide one highlight group to apply to DecoratorRange
---@param node Node
---@return string? group
function UserDecorator:calculate_highlight(node)
self:nop(node)
end
return UserDecorator
---
---Example user decorator
--[[
local UserDecorator = require("nvim-tree.renderer.decorator.user")
---@class (exact) MyDecorator: UserDecorator
---@field private my_icon HighlightedString
local MyDecorator = UserDecorator:extend()
---Constructor
function MyDecorator:new()
---@type DecoratorArgs
local args = {
enabled = true,
highlight_range = "all",
icon_placement = "signcolumn",
}
MyDecorator.super.new(self, args)
-- 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
---Just one icon for DecoratorIconPlacement
---@param node Node
---@return HighlightedString[]|nil icons
function MyDecorator:calculate_icons(node)
if node.name == "example" then
return { self.my_icon }
else
return nil
end
end
---Exactly one highlight group for DecoratorHighlightRange
---@param node Node
---@return string|nil group
function MyDecorator:calculate_highlight(node)
if node.name == "example" then
return "ExampleHighlight"
else
return nil
end
end
--]]