feat(#2948): add UserDecorator

This commit is contained in:
Alexander Courtis 2024-11-22 14:00:56 +11:00
parent f2a926d608
commit ad368d9187
11 changed files with 42 additions and 39 deletions

View File

@ -214,7 +214,7 @@ function Builder:add_highlights(node)
local d, icon, name
for i = #self.decorators, 1, -1 do
d = self.decorators[i]
icon, name = d:groups_icon_name(node)
icon, name = d:highlight_group_icon_name(node)
table.insert(icon_groups, icon)
table.insert(name_groups, name)
end

View File

@ -34,7 +34,7 @@ end
---Bookmark icon: renderer.icons.show.bookmarks and node is marked
---@param node Node
---@return HighlightedString[]|nil icons
function DecoratorBookmarks:calculate_icons(node)
function DecoratorBookmarks:icons(node)
if self.explorer.marks:get(node) then
return { self.icon }
end
@ -43,7 +43,7 @@ end
---Bookmark highlight: renderer.highlight_bookmarks and node is marked
---@param node Node
---@return string|nil group
function DecoratorBookmarks:calculate_highlight(node)
function DecoratorBookmarks:highlight_group(node)
if self.highlight_range ~= "none" and self.explorer.marks:get(node) then
return "NvimTreeBookmarkHL"
end

View File

@ -25,7 +25,7 @@ end
---Copied highlight: renderer.highlight_clipboard and node is copied
---@param node Node
---@return string|nil group
function DecoratorCopied:calculate_highlight(node)
function DecoratorCopied:highlight_group(node)
if self.highlight_range ~= "none" and self.explorer.clipboard:is_copied(node) then
return "NvimTreeCopiedHL"
end

View File

@ -25,7 +25,7 @@ end
---Cut highlight: renderer.highlight_clipboard and node is cut
---@param node Node
---@return string|nil group
function DecoratorCut:calculate_highlight(node)
function DecoratorCut:highlight_group(node)
if self.highlight_range ~= "none" and self.explorer.clipboard:is_cut(node) then
return "NvimTreeCutHL"
end

View File

@ -32,7 +32,7 @@ local ICON_KEYS = {
---@class (exact) DecoratorDiagnostics: Decorator
---@field private explorer Explorer
---@field private icons HighlightedString[]?
---@field private diag_icons HighlightedString[]?
local DecoratorDiagnostics = Decorator:extend()
---@class DecoratorDiagnostics
@ -57,13 +57,13 @@ function DecoratorDiagnostics:new(explorer)
end
if self.explorer.opts.renderer.icons.show.diagnostics then
self.icons = {}
self.diag_icons = {}
for name, sev in pairs(ICON_KEYS) do
self.icons[sev] = {
self.diag_icons[sev] = {
str = self.explorer.opts.diagnostics.icons[name],
hl = { HG_ICON[sev] },
}
self:define_sign(self.icons[sev])
self:define_sign(self.diag_icons[sev])
end
end
end
@ -71,13 +71,13 @@ end
---Diagnostic icon: diagnostics.enable, renderer.icons.show.diagnostics and node has status
---@param node Node
---@return HighlightedString[]|nil icons
function DecoratorDiagnostics:calculate_icons(node)
if node and self.enabled and self.icons then
function DecoratorDiagnostics:icons(node)
if node and self.enabled and self.diag_icons then
local diag_status = diagnostics.get_diag_status(node)
local diag_value = diag_status and diag_status.value
if diag_value then
return { self.icons[diag_value] }
return { self.diag_icons[diag_value] }
end
end
end
@ -85,7 +85,7 @@ end
---Diagnostic highlight: diagnostics.enable, renderer.highlight_diagnostics and node has status
---@param node Node
---@return string|nil group
function DecoratorDiagnostics:calculate_highlight(node)
function DecoratorDiagnostics:highlight_group(node)
if not node or not self.enabled or self.highlight_range == "none" then
return nil
end

View File

@ -148,7 +148,7 @@ end
---Git icons: git.enable, renderer.icons.show.git and node has status
---@param node Node
---@return HighlightedString[]|nil modified icon
function DecoratorGit:calculate_icons(node)
function DecoratorGit:icons(node)
if not node or not self.enabled or not self.icons_by_xy then
return nil
end
@ -200,7 +200,7 @@ function DecoratorGit:sign_name(node)
return
end
local icons = self:calculate_icons(node)
local icons = self:icons(node)
if icons and #icons > 0 then
return icons[1].hl[1]
end
@ -209,7 +209,7 @@ end
---Git highlight: git.enable, renderer.highlight_git and node has status
---@param node Node
---@return string|nil group
function DecoratorGit:calculate_highlight(node)
function DecoratorGit:highlight_group(node)
if not node or not self.enabled or self.highlight_range == "none" then
return nil
end

View File

@ -35,7 +35,7 @@ end
---Hidden icon: renderer.icons.show.hidden and node starts with `.` (dotfile).
---@param node Node
---@return HighlightedString[]|nil icons
function DecoratorHidden:calculate_icons(node)
function DecoratorHidden:icons(node)
if self.enabled and node:is_dotfile() then
return { self.icon }
end
@ -44,7 +44,7 @@ end
---Hidden highlight: renderer.highlight_hidden and node starts with `.` (dotfile).
---@param node Node
---@return string|nil group
function DecoratorHidden:calculate_highlight(node)
function DecoratorHidden:highlight_group(node)
if not self.enabled or self.highlight_range == "none" or not node:is_dotfile() then
return nil
end

View File

@ -19,15 +19,15 @@ function Decorator:new(args)
end
end
---Maybe highlight groups
---Maybe highlight groups for icon and name
---@param node Node
---@return string? icon highlight group
---@return string? name highlight group
function Decorator:groups_icon_name(node)
function Decorator:highlight_group_icon_name(node)
local icon_hl, name_hl
if self.enabled and self.highlight_range ~= "none" then
local hl = self:calculate_highlight(node)
local hl = self:highlight_group(node)
if self.highlight_range == "all" or self.highlight_range == "icon" then
icon_hl = hl
@ -48,7 +48,7 @@ function Decorator:sign_name(node)
return
end
local icons = self:calculate_icons(node)
local icons = self:icons(node)
if icons and #icons > 0 then
return icons[1].hl[1]
end
@ -62,7 +62,7 @@ function Decorator:icons_before(node)
return
end
return self:calculate_icons(node)
return self:icons(node)
end
---Icons when "after"
@ -73,7 +73,7 @@ function Decorator:icons_after(node)
return
end
return self:calculate_icons(node)
return self:icons(node)
end
---Icons when "right_align"
@ -84,14 +84,14 @@ function Decorator:icons_right_align(node)
return
end
return self:calculate_icons(node)
return self:icons(node)
end
---Maybe icons, optionally implemented
---@protected
---@param node Node
---@return HighlightedString[]? icons
function Decorator:calculate_icons(node)
function Decorator:icons(node)
self:nop(node)
end
@ -99,7 +99,7 @@ end
---@protected
---@param node Node
---@return string? group
function Decorator:calculate_highlight(node)
function Decorator:highlight_group(node)
self:nop(node)
end

View File

@ -41,7 +41,7 @@ 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)
function DecoratorModified:icons(node)
if self.enabled and buffers.is_modified(node) then
return { self.icon }
end
@ -50,7 +50,7 @@ end
---Modified highlight: modified.enable, renderer.highlight_modified and node is modified
---@param node Node
---@return string|nil group
function DecoratorModified:calculate_highlight(node)
function DecoratorModified:highlight_group(node)
if not self.enabled or self.highlight_range == "none" or not buffers.is_modified(node) then
return nil
end

View File

@ -28,7 +28,7 @@ end
---Opened highlight: renderer.highlight_opened_files and node has an open buffer
---@param node Node
---@return string|nil group
function DecoratorOpened:calculate_highlight(node)
function DecoratorOpened:highlight_group(node)
if self.highlight_range ~= "none" and buffers.is_opened(node) then
return "NvimTreeOpenedHL"
end

View File

@ -1,10 +1,13 @@
local Decorator = require("nvim-tree.renderer.decorator")
---Abstract user decorator, extend to define your own.
---Icon and highlight are optional.
---Mandatory 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"
---Define a Decorator to optionally set:
--- Additional icons
--- Highlight group
--- Node icon
---Mandator constructor MyDecorator:new() will be called once per tree render, with no arguments.
---Must call:
--- super passing DecoratorArgs MyDecorator.super.new(self, args)
--- define_sign when using "signcolumn"
---See example at end.
---@class (exact) UserDecorator: Decorator
@ -13,14 +16,14 @@ local UserDecorator = Decorator:extend()
---Override this method to provide icons and the highlight groups to apply to DecoratorIconPlacement
---@param node Node
---@return HighlightedString[]? icons
function UserDecorator:calculate_icons(node)
function UserDecorator: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)
function UserDecorator:highlight_group(node)
self:nop(node)
end
@ -60,7 +63,7 @@ end
---Just one icon for DecoratorIconPlacement
---@param node Node
---@return HighlightedString[]|nil icons
function MyDecorator:calculate_icons(node)
function MyDecorator:icons(node)
if node.name == "example" then
return { self.my_icon }
else
@ -71,7 +74,7 @@ end
---Exactly one highlight group for DecoratorHighlightRange
---@param node Node
---@return string|nil group
function MyDecorator:calculate_highlight(node)
function MyDecorator:highlight_group(node)
if node.name == "example" then
return "ExampleHighlight"
else