11local M = {}
22
33-- When adding new icons, remember to add an entry to the `filetypes` table, if applicable.
4- local icons , icons_by_filename , icons_by_file_extension , icons_by_operating_system
4+ local icons , icons_by_filename , icons_by_file_prefix , icons_by_file_extension , icons_by_operating_system
55
66function M .get_icons ()
77 return icons
@@ -17,9 +17,10 @@ local function refresh_icons()
1717 end
1818
1919 icons_by_filename = theme .icons_by_filename
20+ icons_by_file_prefix = theme .icons_by_file_prefix
2021 icons_by_file_extension = theme .icons_by_file_extension
2122 icons_by_operating_system = theme .icons_by_operating_system
22- icons = vim .tbl_extend (" keep" , {}, icons_by_filename , icons_by_file_extension , icons_by_operating_system )
23+ icons = vim .tbl_extend (" keep" , {}, icons_by_filename , icons_by_file_prefix , icons_by_file_extension , icons_by_operating_system )
2324end
2425
2526-- Map of filetypes -> icon names
@@ -332,14 +333,18 @@ function M.setup(opts)
332333 end
333334
334335 local user_filename_icons = user_icons .override_by_filename
336+ local user_file_pre_icons = user_icons .override_by_prefix
335337 local user_file_ext_icons = user_icons .override_by_extension
336338
337339 icons =
338- vim .tbl_extend (" force" , icons , user_icons .override or {}, user_filename_icons or {}, user_file_ext_icons or {})
340+ vim .tbl_extend (" force" , icons , user_icons .override or {}, user_filename_icons or {}, user_file_pre_icons or {}, user_file_ext_icons or {})
339341
340342 if user_filename_icons then
341343 icons_by_filename = vim .tbl_extend (" force" , icons_by_filename , user_filename_icons )
342344 end
345+ if user_file_pre_icons then
346+ icons_by_file_prefix = vim .tbl_extend (" force" , icons_by_file_prefix , user_file_pre_icons )
347+ end
343348 if user_file_ext_icons then
344349 icons_by_file_extension = vim .tbl_extend (" force" , icons_by_file_extension , user_file_ext_icons )
345350 end
@@ -359,6 +364,24 @@ function M.get_default_icon()
359364 return default_icon
360365end
361366
367+ -- recursively iterate over each segment separated by '.' to parse prefix with multiple dots in filename
368+ local function iterate_multi_dotted_prefix (name , icon_table )
369+ if name == nil then
370+ return nil
371+ end
372+
373+ local compound_pre , rest = name :match " (.?[^.]*)%.(.*)"
374+ local icon = icon_table [compound_pre ]
375+ if icon then
376+ return icon
377+ end
378+ if not rest then
379+ return nil
380+ end
381+
382+ return iterate_multi_dotted_prefix (rest , icon_table )
383+ end
384+
362385-- recursively iterate over each segment separated by '.' to parse extension with multiple dots in filename
363386local function iterate_multi_dotted_extension (name , icon_table )
364387 if name == nil then
@@ -374,6 +397,17 @@ local function iterate_multi_dotted_extension(name, icon_table)
374397 return iterate_multi_dotted_extension (compound_ext , icon_table )
375398end
376399
400+ local function get_icon_by_prefix (name , prefix , opts )
401+ local is_strict = if_nil (opts and opts .strict , global_opts .strict )
402+ local icon_table = is_strict and icons_by_file_prefix or icons
403+
404+ if prefix ~= nil then
405+ return icon_table [prefix ]
406+ end
407+
408+ return iterate_multi_dotted_prefix (name , icon_table )
409+ end
410+
377411local function get_icon_by_extension (name , ext , opts )
378412 local is_strict = if_nil (opts and opts .strict , global_opts .strict )
379413 local icon_table = is_strict and icons_by_file_extension or icons
@@ -385,7 +419,7 @@ local function get_icon_by_extension(name, ext, opts)
385419 return iterate_multi_dotted_extension (name , icon_table )
386420end
387421
388- function M .get_icon (name , ext , opts )
422+ function M .get_icon (name , pre_or_ext , opts )
389423 if type (name ) == " string" then
390424 name = name :lower ()
391425 end
@@ -398,9 +432,9 @@ function M.get_icon(name, ext, opts)
398432 local is_strict = if_nil (opts and opts .strict , global_opts .strict )
399433 local icon_data
400434 if is_strict then
401- icon_data = icons_by_filename [name ] or get_icon_by_extension (name , ext , opts ) or (has_default and default_icon )
435+ icon_data = icons_by_filename [name ] or get_icon_by_prefix ( name , pre_or_ext , opts ) or get_icon_by_extension (name , pre_or_ext , opts ) or (has_default and default_icon )
402436 else
403- icon_data = icons [name ] or get_icon_by_extension (name , ext , opts ) or (has_default and default_icon )
437+ icon_data = icons [name ] or get_icon_by_prefix ( name , pre_or_ext , opts ) or get_icon_by_extension (name , pre_or_ext , opts ) or (has_default and default_icon )
404438 end
405439
406440 if icon_data then
@@ -419,7 +453,7 @@ function M.get_icon_by_filetype(ft, opts)
419453 return M .get_icon (name or " " , nil , opts )
420454end
421455
422- function M .get_icon_colors (name , ext , opts )
456+ function M .get_icon_colors (name , pre_or_ext , opts )
423457 if not loaded then
424458 M .setup ()
425459 end
@@ -428,9 +462,9 @@ function M.get_icon_colors(name, ext, opts)
428462 local is_strict = if_nil (opts and opts .strict , global_opts .strict )
429463 local icon_data
430464 if is_strict then
431- icon_data = icons_by_filename [name ] or get_icon_by_extension (name , ext , opts ) or (has_default and default_icon )
465+ icon_data = icons_by_filename [name ] or get_icon_by_prefix ( name , pre_or_ext , opts ) or get_icon_by_extension (name , pre_or_ext , opts ) or (has_default and default_icon )
432466 else
433- icon_data = icons [name ] or get_icon_by_extension (name , ext , opts ) or (has_default and default_icon )
467+ icon_data = icons [name ] or get_icon_by_prefix ( name , pre_or_ext , opts ) or get_icon_by_extension (name , pre_or_ext , opts ) or (has_default and default_icon )
434468 end
435469
436470 if icon_data then
@@ -449,8 +483,8 @@ function M.get_icon_colors_by_filetype(ft, opts)
449483 return M .get_icon_colors (name or " " , nil , opts )
450484end
451485
452- function M .get_icon_color (name , ext , opts )
453- local data = { M .get_icon_colors (name , ext , opts ) }
486+ function M .get_icon_color (name , pre_or_ext , opts )
487+ local data = { M .get_icon_colors (name , pre_or_ext , opts ) }
454488 return data [1 ], data [2 ]
455489end
456490
@@ -461,8 +495,8 @@ function M.get_icon_color_by_filetype(ft, opts)
461495 return M .get_icon_color (name or " " , nil , opts )
462496end
463497
464- function M .get_icon_cterm_color (name , ext , opts )
465- local data = { M .get_icon_colors (name , ext , opts ) }
498+ function M .get_icon_cterm_color (name , pre_or_ext , opts )
499+ local data = { M .get_icon_colors (name , pre_or_ext , opts ) }
466500 return data [1 ], data [3 ]
467501end
468502
0 commit comments