Skip to content

Commit dd6b015

Browse files
committed
feat(#2948): add Decorator node icon override
1 parent ad368d9 commit dd6b015

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

lua/nvim-tree/renderer/builder.lua

+29-27
Original file line numberDiff line numberDiff line change
@@ -197,43 +197,50 @@ function Builder:create_combined_group(groups)
197197
return combined_name
198198
end
199199

200-
---Calculate highlight group for icon and name. A combined highlight group will be created
201-
---when there is more than one highlight.
200+
---Calculate decorated icon and name for a node.
201+
---A combined highlight group will be created when there is more than one highlight.
202202
---A highlight group is always calculated and upserted for the case of highlights changing.
203203
---@private
204204
---@param node Node
205-
---@return string|nil icon_hl_group
206-
---@return string|nil name_hl_group
207-
function Builder:add_highlights(node)
208-
-- result
209-
local icon_hl_group, name_hl_group
205+
---@return HighlightedString icon
206+
---@return HighlightedString name
207+
function Builder:icon_name_decorated(node)
210208

211-
-- calculate all groups
209+
-- base case
210+
local icon = node:highlighted_icon()
211+
local name = node:highlighted_name()
212+
213+
-- calculate node icon and all decorated highlight groups
212214
local icon_groups = {}
213215
local name_groups = {}
214-
local d, icon, name
216+
local decorator, hl_icon, hl_name
215217
for i = #self.decorators, 1, -1 do
216-
d = self.decorators[i]
217-
icon, name = d:highlight_group_icon_name(node)
218-
table.insert(icon_groups, icon)
219-
table.insert(name_groups, name)
218+
decorator = self.decorators[i]
219+
220+
-- maybe overridde icon
221+
icon = decorator:icon_node(node) or icon
222+
223+
hl_icon, hl_name = decorator:highlight_group_icon_name(node)
224+
225+
table.insert(icon_groups, hl_icon)
226+
table.insert(name_groups, hl_name)
220227
end
221228

222-
-- one or many icon groups
229+
-- add one or many icon groups
223230
if #icon_groups > 1 then
224-
icon_hl_group = self:create_combined_group(icon_groups)
231+
table.insert(icon.hl, self:create_combined_group(icon_groups))
225232
else
226-
icon_hl_group = icon_groups[1]
233+
table.insert(icon.hl, icon_groups[1])
227234
end
228235

229-
-- one or many name groups
236+
-- add one or many name groups
230237
if #name_groups > 1 then
231-
name_hl_group = self:create_combined_group(name_groups)
238+
table.insert(name.hl, self:create_combined_group(name_groups))
232239
else
233-
name_hl_group = name_groups[1]
240+
table.insert(name.hl, name_groups[1])
234241
end
235242

236-
return icon_hl_group, name_hl_group
243+
return icon, name
237244
end
238245

239246
---Insert node line into self.lines, calling Builder:build_lines for each directory
@@ -246,13 +253,8 @@ function Builder:build_line(node, idx, num_children)
246253
local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers)
247254
local arrows = pad.get_arrows(node)
248255

249-
-- main components
250-
local icon, name = node:highlighted_icon(), node:highlighted_name()
251-
252-
-- highighting
253-
local icon_hl_group, name_hl_group = self:add_highlights(node)
254-
table.insert(icon.hl, icon_hl_group)
255-
table.insert(name.hl, name_hl_group)
256+
-- decorated node icon and name
257+
local icon, name = self:icon_name_decorated(node)
256258

257259
local line = self:format_line(indent_markers, arrows, icon, name, node)
258260
table.insert(self.lines, self:unwrap_highlighted_strings(line))

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

+7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ function Decorator:icons_right_align(node)
8787
return self:icons(node)
8888
end
8989

90+
---Maybe icon override, optionally implemented
91+
---@param node Node
92+
---@return HighlightedString? icon_node
93+
function Decorator:icon_node(node)
94+
return self:nop(node)
95+
end
96+
9097
---Maybe icons, optionally implemented
9198
---@protected
9299
---@param node Node

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

+18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ local Decorator = require("nvim-tree.renderer.decorator")
1313
---@class (exact) UserDecorator: Decorator
1414
local UserDecorator = Decorator:extend()
1515

16+
---Override this method to set the node's icon
17+
---@param node Node
18+
---@return HighlightedString? icon_node
19+
function UserDecorator:icon_node(node)
20+
return self:nop(node)
21+
end
22+
1623
---Override this method to provide icons and the highlight groups to apply to DecoratorIconPlacement
1724
---@param node Node
1825
---@return HighlightedString[]? icons
@@ -60,6 +67,17 @@ function MyDecorator:new()
6067
self:define_sign(self.my_icon)
6168
end
6269
70+
---Overridden node icon
71+
---@param node Node
72+
---@return HighlightedString? icon_node
73+
function MyDecorator:icon_node(node)
74+
if node.name == "example" then
75+
return self.my_icon
76+
else
77+
return nil
78+
end
79+
end
80+
6381
---Just one icon for DecoratorIconPlacement
6482
---@param node Node
6583
---@return HighlightedString[]|nil icons

0 commit comments

Comments
 (0)