@@ -5,26 +5,23 @@ local notify = require "nvim-tree.notify"
55local pad = require " nvim-tree.renderer.components.padding"
66local icons = require " nvim-tree.renderer.components.icons"
77
8- local HL_POSITION = require (" nvim-tree.enum" ).HL_POSITION
9- local ICON_PLACEMENT = require (" nvim-tree.enum" ).ICON_PLACEMENT
10-
118--- @class Builder
12- --- @field decorators Decorator[]
9+ --- @field deco Decorator[]
1310local Builder = {}
1411Builder .__index = Builder
1512
1613local DEFAULT_ROOT_FOLDER_LABEL = " :~:s?$?/..?"
1714
18- function Builder .new (root_cwd , decorators )
15+ function Builder .new (root_cwd , deco )
1916 return setmetatable ({
2017 index = 0 ,
2118 depth = 0 ,
2219 highlights = {},
2320 lines = {},
2421 markers = {},
25- signs = {},
22+ sign_names = {},
2623 root_cwd = root_cwd ,
27- decorators = decorators ,
24+ deco = deco ,
2825 }, Builder )
2926end
3027
@@ -195,95 +192,18 @@ function Builder:_build_file(node)
195192 return icon , { str = node .name , hl = { hl } }
196193end
197194
198- --- @param node table
199- --- @return HighlightedString[] | nil icon
200- function Builder :_get_git_icons (node )
201- local git_icons = self .decorators .git :get_icons (node )
202- if git_icons and # git_icons > 0 and self .decorators .git .icon_placement == ICON_PLACEMENT .signcolumn then
203- table.insert (self .signs , {
204- sign = git_icons [1 ].hl [1 ],
205- lnum = self .index + 1 ,
206- priority = 1 ,
207- })
208- git_icons = nil
209- end
210- return git_icons
211- end
212-
213- --- @param node table
214- --- @return HighlightedString[] | nil icon
215- function Builder :_get_diagnostics_icon (node )
216- local diagnostics_icon = self .decorators .diagnostics :get_icon (node )
217- if diagnostics_icon and self .decorators .diagnostics .icon_placement == ICON_PLACEMENT .signcolumn then
218- table.insert (self .signs , {
219- sign = diagnostics_icon .hl [1 ],
220- lnum = self .index + 1 ,
221- priority = 2 ,
222- })
223- diagnostics_icon = nil
224- end
225- return diagnostics_icon
226- end
227-
228- --- @param node table
229- --- @return HighlightedString | nil icon
230- function Builder :_get_modified_icon (node )
231- local modified_icon = self .decorators .modified :get_icon (node )
232- if modified_icon and self .decorators .modified .icon_placement == ICON_PLACEMENT .signcolumn then
233- table.insert (self .signs , {
234- sign = modified_icon .hl [1 ],
235- lnum = self .index + 1 ,
236- priority = 3 ,
237- })
238- modified_icon = nil
239- end
240- return modified_icon
241- end
242-
243- --- @param node table
244- --- @return HighlightedString[] | nil icon
245- function Builder :_get_bookmark_icon (node )
246- local bookmark_icon = self .decorators .bookmarks :get_icon (node )
247- if bookmark_icon and self .decorators .bookmarks .icon_placement == ICON_PLACEMENT .signcolumn then
248- table.insert (self .signs , {
249- sign = bookmark_icon .hl [1 ],
250- lnum = self .index + 1 ,
251- priority = 4 ,
252- })
253- bookmark_icon = nil
254- end
255- return bookmark_icon
256- end
257-
258- --- Append optional highlighting to icon or name.
259- --- @param node table
260- --- @param decorator Decorator
261- --- @param icon_hl string[] icons to append to
262- --- @param name_hl string[] names to append to
263- function Builder :_append_dec_highlight (node , decorator , icon_hl , name_hl )
264- local hl = decorator :get_highlight (node )
265- if hl then
266- if decorator .hl_pos == HL_POSITION .all or decorator .hl_pos == HL_POSITION .icon then
267- table.insert (icon_hl , hl )
268- end
269- if decorator .hl_pos == HL_POSITION .all or decorator .hl_pos == HL_POSITION .name then
270- table.insert (name_hl , hl )
271- end
272- end
273- end
274-
275195--- @param indent_markers HighlightedString[]
276196--- @param arrows HighlightedString[] | nil
277197--- @param icon HighlightedString
278198--- @param name HighlightedString
279- --- @param git_icons HighlightedString[] | nil
280- --- @param diagnostics_icon HighlightedString | nil
281- --- @param modified_icon HighlightedString | nil
282- --- @param bookmark_icon HighlightedString | nil
199+ --- @param node table
283200--- @return HighlightedString[]
284- function Builder :_format_line (indent_markers , arrows , icon , name , git_icons , diagnostics_icon , modified_icon , bookmark_icon )
201+ function Builder :_format_line (indent_markers , arrows , icon , name , node )
285202 local added_len = 0
286203 local function add_to_end (t1 , t2 )
204+ if not t2 then
205+ return
206+ end
287207 for _ , v in ipairs (t2 ) do
288208 if added_len > 0 then
289209 table.insert (t1 , { str = self .icon_padding })
@@ -301,32 +221,15 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
301221
302222 local line = { indent_markers , arrows }
303223 add_to_end (line , { icon })
304- if git_icons and self .decorators .git .icon_placement == ICON_PLACEMENT .before then
305- add_to_end (line , git_icons )
306- end
307- if modified_icon and self .decorators .modified .icon_placement == ICON_PLACEMENT .before then
308- add_to_end (line , { modified_icon })
309- end
310- if diagnostics_icon and self .decorators .diagnostics .icon_placement == ICON_PLACEMENT .before then
311- add_to_end (line , { diagnostics_icon })
312- end
313- if bookmark_icon and self .decorators .bookmarks .icon_placement == ICON_PLACEMENT .before then
314- add_to_end (line , { bookmark_icon })
224+
225+ for i = # self .deco , 1 , - 1 do
226+ add_to_end (line , self .deco [i ]:icons_before (node ))
315227 end
316228
317229 add_to_end (line , { name })
318230
319- if git_icons and self .decorators .git .icon_placement == ICON_PLACEMENT .after then
320- add_to_end (line , git_icons )
321- end
322- if modified_icon and self .decorators .modified .icon_placement == ICON_PLACEMENT .after then
323- add_to_end (line , { modified_icon })
324- end
325- if diagnostics_icon and self .decorators .diagnostics .icon_placement == ICON_PLACEMENT .after then
326- add_to_end (line , { diagnostics_icon })
327- end
328- if bookmark_icon and self .decorators .bookmarks .icon_placement == ICON_PLACEMENT .after then
329- add_to_end (line , { bookmark_icon })
231+ for i = # self .deco , 1 , - 1 do
232+ add_to_end (line , self .deco [i ]:icons_after (node ))
330233 end
331234
332235 return line
@@ -337,11 +240,15 @@ function Builder:_build_line(node, idx, num_children)
337240 local indent_markers = pad .get_indent_markers (self .depth , idx , num_children , node , self .markers )
338241 local arrows = pad .get_arrows (node )
339242
340- -- adds icons to signcolumn
341- local bookmark_icon = self :_get_bookmark_icon (node )
342- local git_icons = self :_get_git_icons (node )
343- local modified_icon = self :_get_modified_icon (node )
344- local diagnostics_icon = self :_get_diagnostics_icon (node )
243+ -- signs, use the highest priority
244+ local sign_name
245+ for _ , d in ipairs (self .deco ) do
246+ sign_name = d :sign_name (node )
247+ if sign_name then
248+ self .sign_names [self .index ] = sign_name
249+ break
250+ end
251+ end
345252
346253 -- main components
347254 local is_folder = node .nodes ~= nil
@@ -355,16 +262,14 @@ function Builder:_build_line(node, idx, num_children)
355262 icon , name = self :_build_file (node )
356263 end
357264
358- -- extra highighting
359- self :_append_dec_highlight (node , self .decorators .git , icon .hl , name .hl )
360- self :_append_dec_highlight (node , self .decorators .opened , icon .hl , name .hl )
361- self :_append_dec_highlight (node , self .decorators .modified , icon .hl , name .hl )
362- self :_append_dec_highlight (node , self .decorators .bookmarks , icon .hl , name .hl )
363- self :_append_dec_highlight (node , self .decorators .diagnostics , icon .hl , name .hl )
364- self :_append_dec_highlight (node , self .decorators .copied , icon .hl , name .hl )
365- self :_append_dec_highlight (node , self .decorators .cut , icon .hl , name .hl )
265+ -- highighting
266+ for _ , d in ipairs (self .deco ) do
267+ local icon_group , name_group = d :groups_icon_name (node )
268+ table.insert (icon .hl , icon_group )
269+ table.insert (name .hl , name_group )
270+ end
366271
367- local line = self :_format_line (indent_markers , arrows , icon , name , git_icons , diagnostics_icon , modified_icon , bookmark_icon )
272+ local line = self :_format_line (indent_markers , arrows , icon , name , node )
368273 self :_insert_line (self :_unwrap_highlighted_strings (line ))
369274
370275 self .index = self .index + 1
@@ -438,7 +343,7 @@ function Builder:build_header(show_header)
438343end
439344
440345function Builder :unwrap ()
441- return self .lines , self .highlights , self .signs
346+ return self .lines , self .highlights , self .sign_names
442347end
443348
444349return Builder
0 commit comments