|
| 1 | +---@meta |
| 2 | +error('Cannot require a meta file') |
| 3 | + |
| 4 | +---Highlight group range as per nvim-tree.renderer.highlight_* |
| 5 | +---@alias nvim_tree.api.decorator.HighlightRange "none" | "icon" | "name" | "all" |
| 6 | + |
| 7 | +---Icon position as per renderer.icons.*_placement |
| 8 | +---@alias nvim_tree.api.decorator.IconPlacement "none" | "before" | "after" | "signcolumn" | "right_align" |
| 9 | + |
| 10 | +---UserDecorator Constructor Arguments |
| 11 | +---@class (exact) nvim_tree.api.decorator.UserDecoratorArgs |
| 12 | +---@field enabled boolean |
| 13 | +---@field highlight_range nvim_tree.api.decorator.HighlightRange |
| 14 | +---@field icon_placement nvim_tree.api.decorator.IconPlacement |
| 15 | + |
| 16 | + |
| 17 | +-- |
| 18 | +-- Example UserDecorator |
| 19 | +-- |
| 20 | + |
| 21 | +local UserDecorator = require("nvim-tree.renderer.decorator.user") |
| 22 | + |
| 23 | +---@class (exact) MyDecorator: UserDecorator |
| 24 | +---@field private my_icon nvim_tree.api.HighlightedString |
| 25 | +local MyDecorator = UserDecorator:extend() |
| 26 | + |
| 27 | +---Constructor |
| 28 | +function MyDecorator:new() |
| 29 | + |
| 30 | + ---@type nvim_tree.api.decorator.UserDecoratorArgs |
| 31 | + local args = { |
| 32 | + enabled = true, |
| 33 | + highlight_range = "all", |
| 34 | + icon_placement = "signcolumn", |
| 35 | + } |
| 36 | + |
| 37 | + -- construct super with args |
| 38 | + MyDecorator.super.new(self, args) |
| 39 | + |
| 40 | + -- create your icon once, for convenience |
| 41 | + self.my_icon = { str = "I", hl = { "MyIcon" } } |
| 42 | + |
| 43 | + -- Define the icon sign only once |
| 44 | + -- Only needed if you are using icon_placement = "signcolumn" |
| 45 | + self:define_sign(self.my_icon) |
| 46 | +end |
| 47 | + |
| 48 | +---Overridde node icon |
| 49 | +---@param node nvim_tree.api.Node |
| 50 | +---@return nvim_tree.api.HighlightedString? icon_node |
| 51 | +function MyDecorator:icon_node(node) |
| 52 | + if node.name == "example" then |
| 53 | + return self.my_icon |
| 54 | + else |
| 55 | + return nil |
| 56 | + end |
| 57 | +end |
| 58 | + |
| 59 | +---Return one icon for DecoratorIconPlacement |
| 60 | +---@param node nvim_tree.api.Node |
| 61 | +---@return nvim_tree.api.HighlightedString[]? icons |
| 62 | +function MyDecorator:icons(node) |
| 63 | + if node.name == "example" then |
| 64 | + return { self.my_icon } |
| 65 | + else |
| 66 | + return nil |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +---Exactly one highlight group for DecoratorHighlightRange |
| 71 | +---@param node nvim_tree.api.Node |
| 72 | +---@return string? highlight_group |
| 73 | +function MyDecorator:highlight_group(node) |
| 74 | + if node.name == "example" then |
| 75 | + return "MyHighlight" |
| 76 | + else |
| 77 | + return nil |
| 78 | + end |
| 79 | +end |
| 80 | + |
| 81 | +return MyDecorator |
| 82 | + |
| 83 | +-- |
| 84 | +-- Internal Aliases |
| 85 | +-- |
| 86 | +---@alias DecoratorHighlightRange nvim_tree.api.decorator.HighlightRange |
| 87 | +---@alias DecoratorIconPlacement nvim_tree.api.decorator.IconPlacement |
| 88 | + |
0 commit comments