Skip to content

Commit ad368d9

Browse files
committed
feat(#2948): add UserDecorator
1 parent f2a926d commit ad368d9

File tree

11 files changed

+42
-39
lines changed

11 files changed

+42
-39
lines changed

lua/nvim-tree/renderer/builder.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function Builder:add_highlights(node)
214214
local d, icon, name
215215
for i = #self.decorators, 1, -1 do
216216
d = self.decorators[i]
217-
icon, name = d:groups_icon_name(node)
217+
icon, name = d:highlight_group_icon_name(node)
218218
table.insert(icon_groups, icon)
219219
table.insert(name_groups, name)
220220
end

lua/nvim-tree/renderer/decorator/bookmarks.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end
3434
---Bookmark icon: renderer.icons.show.bookmarks and node is marked
3535
---@param node Node
3636
---@return HighlightedString[]|nil icons
37-
function DecoratorBookmarks:calculate_icons(node)
37+
function DecoratorBookmarks:icons(node)
3838
if self.explorer.marks:get(node) then
3939
return { self.icon }
4040
end
@@ -43,7 +43,7 @@ end
4343
---Bookmark highlight: renderer.highlight_bookmarks and node is marked
4444
---@param node Node
4545
---@return string|nil group
46-
function DecoratorBookmarks:calculate_highlight(node)
46+
function DecoratorBookmarks:highlight_group(node)
4747
if self.highlight_range ~= "none" and self.explorer.marks:get(node) then
4848
return "NvimTreeBookmarkHL"
4949
end

lua/nvim-tree/renderer/decorator/copied.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ end
2525
---Copied highlight: renderer.highlight_clipboard and node is copied
2626
---@param node Node
2727
---@return string|nil group
28-
function DecoratorCopied:calculate_highlight(node)
28+
function DecoratorCopied:highlight_group(node)
2929
if self.highlight_range ~= "none" and self.explorer.clipboard:is_copied(node) then
3030
return "NvimTreeCopiedHL"
3131
end

lua/nvim-tree/renderer/decorator/cut.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ end
2525
---Cut highlight: renderer.highlight_clipboard and node is cut
2626
---@param node Node
2727
---@return string|nil group
28-
function DecoratorCut:calculate_highlight(node)
28+
function DecoratorCut:highlight_group(node)
2929
if self.highlight_range ~= "none" and self.explorer.clipboard:is_cut(node) then
3030
return "NvimTreeCutHL"
3131
end

lua/nvim-tree/renderer/decorator/diagnostics.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ local ICON_KEYS = {
3232

3333
---@class (exact) DecoratorDiagnostics: Decorator
3434
---@field private explorer Explorer
35-
---@field private icons HighlightedString[]?
35+
---@field private diag_icons HighlightedString[]?
3636
local DecoratorDiagnostics = Decorator:extend()
3737

3838
---@class DecoratorDiagnostics
@@ -57,35 +57,35 @@ function DecoratorDiagnostics:new(explorer)
5757
end
5858

5959
if self.explorer.opts.renderer.icons.show.diagnostics then
60-
self.icons = {}
60+
self.diag_icons = {}
6161
for name, sev in pairs(ICON_KEYS) do
62-
self.icons[sev] = {
62+
self.diag_icons[sev] = {
6363
str = self.explorer.opts.diagnostics.icons[name],
6464
hl = { HG_ICON[sev] },
6565
}
66-
self:define_sign(self.icons[sev])
66+
self:define_sign(self.diag_icons[sev])
6767
end
6868
end
6969
end
7070

7171
---Diagnostic icon: diagnostics.enable, renderer.icons.show.diagnostics and node has status
7272
---@param node Node
7373
---@return HighlightedString[]|nil icons
74-
function DecoratorDiagnostics:calculate_icons(node)
75-
if node and self.enabled and self.icons then
74+
function DecoratorDiagnostics:icons(node)
75+
if node and self.enabled and self.diag_icons then
7676
local diag_status = diagnostics.get_diag_status(node)
7777
local diag_value = diag_status and diag_status.value
7878

7979
if diag_value then
80-
return { self.icons[diag_value] }
80+
return { self.diag_icons[diag_value] }
8181
end
8282
end
8383
end
8484

8585
---Diagnostic highlight: diagnostics.enable, renderer.highlight_diagnostics and node has status
8686
---@param node Node
8787
---@return string|nil group
88-
function DecoratorDiagnostics:calculate_highlight(node)
88+
function DecoratorDiagnostics:highlight_group(node)
8989
if not node or not self.enabled or self.highlight_range == "none" then
9090
return nil
9191
end

lua/nvim-tree/renderer/decorator/git.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ end
148148
---Git icons: git.enable, renderer.icons.show.git and node has status
149149
---@param node Node
150150
---@return HighlightedString[]|nil modified icon
151-
function DecoratorGit:calculate_icons(node)
151+
function DecoratorGit:icons(node)
152152
if not node or not self.enabled or not self.icons_by_xy then
153153
return nil
154154
end
@@ -200,7 +200,7 @@ function DecoratorGit:sign_name(node)
200200
return
201201
end
202202

203-
local icons = self:calculate_icons(node)
203+
local icons = self:icons(node)
204204
if icons and #icons > 0 then
205205
return icons[1].hl[1]
206206
end
@@ -209,7 +209,7 @@ end
209209
---Git highlight: git.enable, renderer.highlight_git and node has status
210210
---@param node Node
211211
---@return string|nil group
212-
function DecoratorGit:calculate_highlight(node)
212+
function DecoratorGit:highlight_group(node)
213213
if not node or not self.enabled or self.highlight_range == "none" then
214214
return nil
215215
end

lua/nvim-tree/renderer/decorator/hidden.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ end
3535
---Hidden icon: renderer.icons.show.hidden and node starts with `.` (dotfile).
3636
---@param node Node
3737
---@return HighlightedString[]|nil icons
38-
function DecoratorHidden:calculate_icons(node)
38+
function DecoratorHidden:icons(node)
3939
if self.enabled and node:is_dotfile() then
4040
return { self.icon }
4141
end
@@ -44,7 +44,7 @@ end
4444
---Hidden highlight: renderer.highlight_hidden and node starts with `.` (dotfile).
4545
---@param node Node
4646
---@return string|nil group
47-
function DecoratorHidden:calculate_highlight(node)
47+
function DecoratorHidden:highlight_group(node)
4848
if not self.enabled or self.highlight_range == "none" or not node:is_dotfile() then
4949
return nil
5050
end

lua/nvim-tree/renderer/decorator/init.lua

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ function Decorator:new(args)
1919
end
2020
end
2121

22-
---Maybe highlight groups
22+
---Maybe highlight groups for icon and name
2323
---@param node Node
2424
---@return string? icon highlight group
2525
---@return string? name highlight group
26-
function Decorator:groups_icon_name(node)
26+
function Decorator:highlight_group_icon_name(node)
2727
local icon_hl, name_hl
2828

2929
if self.enabled and self.highlight_range ~= "none" then
30-
local hl = self:calculate_highlight(node)
30+
local hl = self:highlight_group(node)
3131

3232
if self.highlight_range == "all" or self.highlight_range == "icon" then
3333
icon_hl = hl
@@ -48,7 +48,7 @@ function Decorator:sign_name(node)
4848
return
4949
end
5050

51-
local icons = self:calculate_icons(node)
51+
local icons = self:icons(node)
5252
if icons and #icons > 0 then
5353
return icons[1].hl[1]
5454
end
@@ -62,7 +62,7 @@ function Decorator:icons_before(node)
6262
return
6363
end
6464

65-
return self:calculate_icons(node)
65+
return self:icons(node)
6666
end
6767

6868
---Icons when "after"
@@ -73,7 +73,7 @@ function Decorator:icons_after(node)
7373
return
7474
end
7575

76-
return self:calculate_icons(node)
76+
return self:icons(node)
7777
end
7878

7979
---Icons when "right_align"
@@ -84,22 +84,22 @@ function Decorator:icons_right_align(node)
8484
return
8585
end
8686

87-
return self:calculate_icons(node)
87+
return self:icons(node)
8888
end
8989

9090
---Maybe icons, optionally implemented
9191
---@protected
9292
---@param node Node
9393
---@return HighlightedString[]? icons
94-
function Decorator:calculate_icons(node)
94+
function Decorator:icons(node)
9595
self:nop(node)
9696
end
9797

9898
---Maybe highlight group, optionally implemented
9999
---@protected
100100
---@param node Node
101101
---@return string? group
102-
function Decorator:calculate_highlight(node)
102+
function Decorator:highlight_group(node)
103103
self:nop(node)
104104
end
105105

lua/nvim-tree/renderer/decorator/modified.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ end
4141
---Modified icon: modified.enable, renderer.icons.show.modified and node is modified
4242
---@param node Node
4343
---@return HighlightedString[]|nil icons
44-
function DecoratorModified:calculate_icons(node)
44+
function DecoratorModified:icons(node)
4545
if self.enabled and buffers.is_modified(node) then
4646
return { self.icon }
4747
end
@@ -50,7 +50,7 @@ end
5050
---Modified highlight: modified.enable, renderer.highlight_modified and node is modified
5151
---@param node Node
5252
---@return string|nil group
53-
function DecoratorModified:calculate_highlight(node)
53+
function DecoratorModified:highlight_group(node)
5454
if not self.enabled or self.highlight_range == "none" or not buffers.is_modified(node) then
5555
return nil
5656
end

lua/nvim-tree/renderer/decorator/opened.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ end
2828
---Opened highlight: renderer.highlight_opened_files and node has an open buffer
2929
---@param node Node
3030
---@return string|nil group
31-
function DecoratorOpened:calculate_highlight(node)
31+
function DecoratorOpened:highlight_group(node)
3232
if self.highlight_range ~= "none" and buffers.is_opened(node) then
3333
return "NvimTreeOpenedHL"
3434
end

lua/nvim-tree/renderer/decorator/user.lua

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
local Decorator = require("nvim-tree.renderer.decorator")
22

3-
---Abstract user decorator, extend to define your own.
4-
---Icon and highlight are optional.
5-
---Mandatory constructor will be called once per tree render, with no arguments:
6-
--- Must call super passing DecoratorArgs: MyDecorator.super.new(self, args)
7-
--- Must call define_sign, when using "signcolumn"
3+
---Define a Decorator to optionally set:
4+
--- Additional icons
5+
--- Highlight group
6+
--- Node icon
7+
---Mandator constructor MyDecorator:new() will be called once per tree render, with no arguments.
8+
---Must call:
9+
--- super passing DecoratorArgs MyDecorator.super.new(self, args)
10+
--- define_sign when using "signcolumn"
811
---See example at end.
912

1013
---@class (exact) UserDecorator: Decorator
@@ -13,14 +16,14 @@ local UserDecorator = Decorator:extend()
1316
---Override this method to provide icons and the highlight groups to apply to DecoratorIconPlacement
1417
---@param node Node
1518
---@return HighlightedString[]? icons
16-
function UserDecorator:calculate_icons(node)
19+
function UserDecorator:icons(node)
1720
self:nop(node)
1821
end
1922

2023
---Override this method to provide one highlight group to apply to DecoratorRange
2124
---@param node Node
2225
---@return string? group
23-
function UserDecorator:calculate_highlight(node)
26+
function UserDecorator:highlight_group(node)
2427
self:nop(node)
2528
end
2629

@@ -60,7 +63,7 @@ end
6063
---Just one icon for DecoratorIconPlacement
6164
---@param node Node
6265
---@return HighlightedString[]|nil icons
63-
function MyDecorator:calculate_icons(node)
66+
function MyDecorator:icons(node)
6467
if node.name == "example" then
6568
return { self.my_icon }
6669
else
@@ -71,7 +74,7 @@ end
7174
---Exactly one highlight group for DecoratorHighlightRange
7275
---@param node Node
7376
---@return string|nil group
74-
function MyDecorator:calculate_highlight(node)
77+
function MyDecorator:highlight_group(node)
7578
if node.name == "example" then
7679
return "ExampleHighlight"
7780
else

0 commit comments

Comments
 (0)