@@ -5,26 +5,23 @@ local notify = require "nvim-tree.notify"
5
5
local pad = require " nvim-tree.renderer.components.padding"
6
6
local icons = require " nvim-tree.renderer.components.icons"
7
7
8
- local HL_POSITION = require (" nvim-tree.enum" ).HL_POSITION
9
- local ICON_PLACEMENT = require (" nvim-tree.enum" ).ICON_PLACEMENT
10
-
11
8
--- @class Builder
12
- --- @field decorators Decorator[]
9
+ --- @field deco Decorator[]
13
10
local Builder = {}
14
11
Builder .__index = Builder
15
12
16
13
local DEFAULT_ROOT_FOLDER_LABEL = " :~:s?$?/..?"
17
14
18
- function Builder .new (root_cwd , decorators )
15
+ function Builder .new (root_cwd , deco )
19
16
return setmetatable ({
20
17
index = 0 ,
21
18
depth = 0 ,
22
19
highlights = {},
23
20
lines = {},
24
21
markers = {},
25
- signs = {},
22
+ sign_names = {},
26
23
root_cwd = root_cwd ,
27
- decorators = decorators ,
24
+ deco = deco ,
28
25
}, Builder )
29
26
end
30
27
@@ -195,95 +192,18 @@ function Builder:_build_file(node)
195
192
return icon , { str = node .name , hl = { hl } }
196
193
end
197
194
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
-
275
195
--- @param indent_markers HighlightedString[]
276
196
--- @param arrows HighlightedString[] | nil
277
197
--- @param icon HighlightedString
278
198
--- @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
283
200
--- @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 )
285
202
local added_len = 0
286
203
local function add_to_end (t1 , t2 )
204
+ if not t2 then
205
+ return
206
+ end
287
207
for _ , v in ipairs (t2 ) do
288
208
if added_len > 0 then
289
209
table.insert (t1 , { str = self .icon_padding })
@@ -301,32 +221,15 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
301
221
302
222
local line = { indent_markers , arrows }
303
223
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 ))
315
227
end
316
228
317
229
add_to_end (line , { name })
318
230
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 ))
330
233
end
331
234
332
235
return line
@@ -337,11 +240,15 @@ function Builder:_build_line(node, idx, num_children)
337
240
local indent_markers = pad .get_indent_markers (self .depth , idx , num_children , node , self .markers )
338
241
local arrows = pad .get_arrows (node )
339
242
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
345
252
346
253
-- main components
347
254
local is_folder = node .nodes ~= nil
@@ -355,16 +262,14 @@ function Builder:_build_line(node, idx, num_children)
355
262
icon , name = self :_build_file (node )
356
263
end
357
264
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
366
271
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 )
368
273
self :_insert_line (self :_unwrap_highlighted_strings (line ))
369
274
370
275
self .index = self .index + 1
@@ -438,7 +343,7 @@ function Builder:build_header(show_header)
438
343
end
439
344
440
345
function Builder :unwrap ()
441
- return self .lines , self .highlights , self .signs
346
+ return self .lines , self .highlights , self .sign_names
442
347
end
443
348
444
349
return Builder
0 commit comments