Skip to content

Commit

Permalink
feat(#2948): add UserDecorator
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Nov 22, 2024
1 parent f2a926d commit ad368d9
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 39 deletions.
2 changes: 1 addition & 1 deletion lua/nvim-tree/renderer/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/renderer/decorator/bookmarks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/renderer/decorator/copied.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/renderer/decorator/cut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lua/nvim-tree/renderer/decorator/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,35 +57,35 @@ 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

---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

---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
Expand Down
6 changes: 3 additions & 3 deletions lua/nvim-tree/renderer/decorator/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/renderer/decorator/hidden.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions lua/nvim-tree/renderer/decorator/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -62,7 +62,7 @@ function Decorator:icons_before(node)
return
end

return self:calculate_icons(node)
return self:icons(node)
end

---Icons when "after"
Expand All @@ -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"
Expand All @@ -84,22 +84,22 @@ 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

---Maybe highlight group, optionally implemented
---@protected
---@param node Node
---@return string? group
function Decorator:calculate_highlight(node)
function Decorator:highlight_group(node)
self:nop(node)
end

Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/renderer/decorator/modified.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/renderer/decorator/opened.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions lua/nvim-tree/renderer/decorator/user.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ad368d9

Please sign in to comment.