feat(#2948): add custom decorators, :help nvim-tree-decorators (#2996)

* feat(#2948): add UserDecorator, proof of concept

* feat(#2948): add UserDecorator, proof of concept

* feat(#2948): add UserDecorator, proof of concept

* feat(#2948): add UserDecorator

* feat(#2948): add UserDecorator

* feat(#2948): add UserDecorator

* feat(#2948): add Decorator node icon override

* feat(#2948): add nvim_tree.api.* node classes

* feat(#2948): extract _meta following nvim pattern

* feat(#2948): extract _meta following nvim pattern

* feat(#2948): add decorator registry and order

* feat(#2948): add decorator registry and order

* feat(#2948): tidy

* feat(#2948): document API

* feat(#2948): document API

* feat(#2948): document API

* feat(#2948): pass api nodes to user decorators

* feat(#2948): document API

* feat(#2948): use renderer.decorators to define order and register

* feat(#2948): tidy decorator args and complete documentation

* feat(#2948): decorator classes specified by prefix rather than suffix

* feat(#2948): improve doc

* feat(#2948): improve doc

* feat(#2948): improve doc

* feat(#2948): additional user decorator safety

* feat(#2948): create nvim_tree.api.decorator.UserDecorator class in API, add :extend

* feat(#2948): improve doc
This commit is contained in:
Alexander Courtis
2024-12-07 16:03:29 +11:00
committed by GitHub
parent ca7c4c33ca
commit 7a4ff1a516
25 changed files with 571 additions and 322 deletions

View File

@@ -3,26 +3,22 @@ local buffers = require("nvim-tree.buffers")
local Decorator = require("nvim-tree.renderer.decorator")
local DirectoryNode = require("nvim-tree.node.directory")
---@class (exact) DecoratorModified: Decorator
---@field icon HighlightedString?
local DecoratorModified = Decorator:extend()
---@class (exact) ModifiedDecorator: Decorator
---@field private explorer Explorer
---@field private icon HighlightedString?
local ModifiedDecorator = Decorator:extend()
---@class DecoratorModified
---@overload fun(explorer: DecoratorArgs): DecoratorModified
---@class ModifiedDecorator
---@overload fun(args: DecoratorArgs): ModifiedDecorator
---@protected
---@param args DecoratorArgs
function DecoratorModified:new(args)
Decorator.new(self, {
explorer = args.explorer,
enabled = true,
hl_pos = args.explorer.opts.renderer.highlight_modified or "none",
icon_placement = args.explorer.opts.renderer.icons.modified_placement or "none",
})
function ModifiedDecorator:new(args)
self.explorer = args.explorer
if not self.enabled then
return
end
self.enabled = true
self.highlight_range = self.explorer.opts.renderer.highlight_modified or "none"
self.icon_placement = self.explorer.opts.renderer.icons.modified_placement or "none"
if self.explorer.opts.renderer.icons.show.modified then
self.icon = {
@@ -35,18 +31,18 @@ end
---Modified icon: modified.enable, renderer.icons.show.modified and node is modified
---@param node Node
---@return HighlightedString[]|nil icons
function DecoratorModified:calculate_icons(node)
if self.enabled and buffers.is_modified(node) then
---@return HighlightedString[]? icons
function ModifiedDecorator:icons(node)
if buffers.is_modified(node) then
return { self.icon }
end
end
---Modified highlight: modified.enable, renderer.highlight_modified and node is modified
---@param node Node
---@return string|nil group
function DecoratorModified:calculate_highlight(node)
if not self.enabled or self.range == "none" or not buffers.is_modified(node) then
---@return string? highlight_group
function ModifiedDecorator:highlight_group(node)
if self.highlight_range == "none" or not buffers.is_modified(node) then
return nil
end
@@ -57,4 +53,4 @@ function DecoratorModified:calculate_highlight(node)
end
end
return DecoratorModified
return ModifiedDecorator