Skip to content

Commit 0a0e987

Browse files
committed
feat: support icons_by_file_prefix
1 parent cff25ce commit 0a0e987

File tree

4 files changed

+71
-45
lines changed

4 files changed

+71
-45
lines changed

lua/nvim-web-devicons.lua

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local 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

66
function 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)
2324
end
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
360365
end
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
363386
local 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)
375398
end
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+
377411
local 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)
386420
end
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)
420454
end
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)
450484
end
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]
455489
end
456490

@@ -461,8 +495,8 @@ function M.get_icon_color_by_filetype(ft, opts)
461495
return M.get_icon_color(name or "", nil, opts)
462496
end
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]
467501
end
468502

lua/nvim-web-devicons/icons-default.lua

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,6 @@ local icons_by_filename = {
227227
cterm_color = "68",
228228
name = "Dockerfile",
229229
},
230-
["dockerfile"] = {
231-
icon = "󰡨",
232-
color = "#458ee6",
233-
cterm_color = "68",
234-
name = "Dockerfile",
235-
},
236230
["favicon.ico"] = {
237231
icon = "",
238232
color = "#cbcb41",
@@ -367,13 +361,10 @@ local icons_by_filename = {
367361
},
368362
}
369363

364+
local icons_by_file_prefix = {
365+
}
366+
370367
local icons_by_file_extension = {
371-
["Dockerfile"] = {
372-
icon = "󰡨",
373-
color = "#458ee6",
374-
cterm_color = "68",
375-
name = "Dockerfile",
376-
},
377368
["R"] = {
378369
icon = "󰟔",
379370
color = "#2266ba",
@@ -2139,6 +2130,7 @@ local icons_by_operating_system = {
21392130

21402131
return {
21412132
icons_by_filename = icons_by_filename,
2133+
icons_by_file_prefix = icons_by_file_prefix,
21422134
icons_by_file_extension = icons_by_file_extension,
21432135
icons_by_operating_system = icons_by_operating_system,
21442136
}

lua/nvim-web-devicons/icons-light.lua

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,6 @@ local icons_by_filename = {
227227
cterm_color = "25",
228228
name = "Dockerfile",
229229
},
230-
["dockerfile"] = {
231-
icon = "󰡨",
232-
color = "#2e5f99",
233-
cterm_color = "25",
234-
name = "Dockerfile",
235-
},
236230
["favicon.ico"] = {
237231
icon = "",
238232
color = "#666620",
@@ -367,13 +361,10 @@ local icons_by_filename = {
367361
},
368362
}
369363

364+
local icons_by_file_prefix = {
365+
}
366+
370367
local icons_by_file_extension = {
371-
["Dockerfile"] = {
372-
icon = "󰡨",
373-
color = "#2e5f99",
374-
cterm_color = "25",
375-
name = "Dockerfile",
376-
},
377368
["R"] = {
378369
icon = "󰟔",
379370
color = "#1a4c8c",
@@ -2139,6 +2130,7 @@ local icons_by_operating_system = {
21392130

21402131
return {
21412132
icons_by_filename = icons_by_filename,
2133+
icons_by_file_prefix = icons_by_file_prefix,
21422134
icons_by_file_extension = icons_by_file_extension,
21432135
icons_by_operating_system = icons_by_operating_system,
21442136
}

scripts/generate_colors.lua

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,35 @@ end
136136
local lines = generate_lines()
137137

138138
-- second table
139-
if fn.search("^local icons_by_file_extension", "c") == 0 then
140-
error_exit("Table 'icons_by_file_extension' not found in lua/nvim-web-devicons/icons-default.lua", 1)
139+
if fn.search("^local icons_by_file_prefix", "c") == 0 then
140+
error_exit("Table 'icons_by_file_prefix' not found in lua/nvim-web-devicons/icons-default.lua", 1)
141141
end
142142
local lines2 = generate_lines()
143143

144144
-- third table
145+
if fn.search("^local icons_by_file_extension", "c") == 0 then
146+
error_exit("Table 'icons_by_file_extension' not found in lua/nvim-web-devicons/icons-default.lua", 1)
147+
end
148+
local lines3 = generate_lines()
149+
150+
-- fourth table
145151
if fn.search("^local icons_by_operating_system", "c") == 0 then
146152
error_exit("Table 'icons_by_operating_system' not found in lua/nvim-web-devicons/icons-default.lua", 1)
147153
end
148-
local lines3 = generate_lines()
154+
local lines4 = generate_lines()
149155

150-
table.insert(lines3, "return {")
151-
table.insert(lines3, " icons_by_filename = icons_by_filename,")
152-
table.insert(lines3, " icons_by_file_extension = icons_by_file_extension,")
153-
table.insert(lines3, " icons_by_operating_system = icons_by_operating_system,")
154-
table.insert(lines3, "}")
156+
table.insert(lines4, "return {")
157+
table.insert(lines4, " icons_by_filename = icons_by_filename,")
158+
table.insert(lines4, " icons_by_file_prefix = icons_by_file_prefix,")
159+
table.insert(lines4, " icons_by_file_extension = icons_by_file_extension,")
160+
table.insert(lines4, " icons_by_operating_system = icons_by_operating_system,")
161+
table.insert(lines4, "}")
155162

156163
-- write both tables to file
157164
fn.writefile(lines, "lua/nvim-web-devicons/icons-light.lua")
158165
fn.writefile(lines2, "lua/nvim-web-devicons/icons-light.lua", "a")
159166
fn.writefile(lines3, "lua/nvim-web-devicons/icons-light.lua", "a")
167+
fn.writefile(lines4, "lua/nvim-web-devicons/icons-light.lua", "a")
160168

161169
print "Finished creating new file!"
162170

0 commit comments

Comments
 (0)