-
Notifications
You must be signed in to change notification settings - Fork 192
/
nvim-web-devicons.lua
650 lines (573 loc) · 15.9 KB
/
nvim-web-devicons.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
local M = {}
-- NOTE: When adding new icons, remember to add an entry to the `filetypes` table, if applicable.
local icons, icons_by_filename, icons_by_file_extension, icons_by_operating_system
local icons_by_desktop_environment, icons_by_window_manager
local default_icon = {
icon = "",
color = "#6d8086",
cterm_color = "66",
name = "Default",
}
function M.get_icons()
return icons
end
function M.get_icons_by_filename()
return icons_by_filename
end
function M.get_icons_by_extension()
return icons_by_file_extension
end
function M.get_icons_by_operating_system()
return icons_by_operating_system
end
function M.get_icons_by_desktop_environment()
return icons_by_desktop_environment
end
function M.get_icons_by_window_manager()
return icons_by_window_manager
end
local global_opts = {
override = {},
strict = false,
default = false,
color_icons = true,
variant = nil,
}
---Change all keys in a table to lowercase
---Remove entry when lowercase entry already exists
---@param t table
local function lowercase_keys(t)
if not t then
return
end
for k, v in pairs(t) do
if type(k) == "string" then
local lower_k = k:lower()
if lower_k ~= k then
if not t[lower_k] then
t[lower_k] = v
end
t[k] = nil
end
end
end
end
-- Set the current icons tables, depending on variant option, then &background
local function refresh_icons()
local theme
if global_opts.variant == "light" then
theme = require "nvim-web-devicons.icons-light"
elseif global_opts.variant == "dark" then
theme = require "nvim-web-devicons.icons-default"
else
if vim.o.background == "light" then
theme = require "nvim-web-devicons.icons-light"
else
theme = require "nvim-web-devicons.icons-default"
end
end
icons_by_filename = theme.icons_by_filename
icons_by_file_extension = theme.icons_by_file_extension
icons_by_operating_system = theme.icons_by_operating_system
icons_by_desktop_environment = theme.icons_by_desktop_environment
icons_by_window_manager = theme.icons_by_window_manager
-- filename matches are case insensitive
lowercase_keys(icons_by_filename)
icons = vim.tbl_extend(
"keep",
{},
icons_by_filename,
icons_by_file_extension,
icons_by_operating_system,
icons_by_desktop_environment,
icons_by_window_manager
)
icons = vim.tbl_extend("force", icons, global_opts.override)
icons[1] = default_icon
end
-- Map of filetypes -> icon names
local filetypes = {
["apl"] = "apl",
["asm"] = "asm",
["astro"] = "astro",
["avif"] = "avif",
["bash"] = "bash",
["bib"] = "bib",
["bicep"] = "bicep",
["bicepparam"] = "bicepparam",
["bqn"] = "bqn",
["bzl"] = "bzl",
["brewfile"] = "brewfile",
["blueprint"] = "blp",
["checkhealth"] = "checkhealth",
["commit"] = "commit_editmsg",
["copying"] = "copying",
["gemfile"] = "gemfile$",
["lesser"] = "copying.lesser",
["vagrantfile"] = "vagrantfile$",
["awk"] = "awk",
["bmp"] = "bmp",
["c"] = "c",
["cfg"] = "cfg",
["clojure"] = "clj",
["cmake"] = "cmake",
["cobol"] = "cobol",
["coffee"] = "coffee",
["conf"] = "conf",
["cp"] = "cp",
["cpp"] = "cpp",
["cr"] = "cr",
["cs"] = "cs",
["csh"] = "csh",
["cson"] = "cson",
["css"] = "css",
["csv"] = "csv",
["cuda"] = "cu",
["d"] = "d",
["dart"] = "dart",
["desktop"] = "desktop",
["diff"] = "diff",
["doc"] = "doc",
["docx"] = "docx",
["dockerfile"] = "dockerfile",
["dosbatch"] = "bat",
["dosini"] = "ini",
["dot"] = "dot",
["drools"] = "drl",
["dropbox"] = "dropbox",
["dump"] = "dump",
["eex"] = "eex",
["ejs"] = "ejs",
["elixir"] = "ex",
["elf"] = "elf",
["elm"] = "elm",
["epuppet"] = "epp",
["erlang"] = "erl",
["eruby"] = "erb",
["fennel"] = "fnl",
["fish"] = "fish",
["forth"] = "fs",
["fortran"] = "f90",
["fsharp"] = "f#",
["fsi"] = "fsi",
["fsscript"] = "fsscript",
["fsx"] = "fsx",
["gd"] = "gd",
["gif"] = "gif",
["git"] = "git",
["gitconfig"] = ".gitconfig",
["gitcommit"] = "commit_editmsg",
["gitignore"] = ".gitignore",
["gitattributes"] = ".gitattributes",
["glb"] = "glb",
["go"] = "go",
["godot"] = "godot",
["graphql"] = "graphql",
["groovy"] = "groovy",
["gql"] = "gql",
["gruntfile"] = "gruntfile",
["gtkrc"] = "gtkrc",
["gulpfile"] = "gulpfile",
["haml"] = "haml",
["haxe"] = "hx",
["haskell"] = "hs",
["hbs"] = "hbs",
["heex"] = "heex",
["hex"] = "hex",
["html"] = "html",
["ico"] = "ico",
["idlang"] = "pro",
["ino"] = "ino",
["import"] = "import",
["ipynb"] = "ipynb",
["java"] = "java",
["javascript"] = "js",
["javascript.jsx"] = "jsx",
["javascriptreact"] = "jsx",
["jpeg"] = "jpeg",
["jpg"] = "jpg",
["json"] = "json",
["jsonc"] = "jsonc",
["json5"] = "json5",
["julia"] = "jl",
["kotlin"] = "kt",
["leex"] = "leex",
["less"] = "less",
["liquid"] = "liquid",
["lhaskell"] = "lhs",
["license"] = "license",
["unlicense"] = "unlicense",
["log"] = "log",
["lock"] = "lock",
["lprolog"] = "sig",
["lua"] = "lua",
["luau"] = "luau",
["make"] = "makefile",
["markdown"] = "markdown",
["material"] = "material",
["mdx"] = "mdx",
["mint"] = "mint",
["mojo"] = "mojo",
["motoko"] = "mo",
["mustache"] = "mustache",
["nim"] = "nim",
["nix"] = "nix",
["nu"] = "nu",
["node"] = "node_modules",
["obj"] = "obj",
["ocaml"] = "ml",
["odin"] = "odin",
["openscad"] = "scad",
["opus"] = "opus",
["otf"] = "otf",
["pck"] = "pck",
["pdf"] = "pdf",
["perl"] = "pl",
["php"] = "php",
["plaintex"] = "tex",
["png"] = "png",
["po"] = "po",
["postscr"] = "ai",
["ppt"] = "ppt",
["prisma"] = "prisma",
["procfile"] = "procfile",
["prolog"] = "pro",
["ps1"] = "ps1",
["psd1"] = "psd1",
["psm1"] = "psm1",
["psb"] = "psb",
["psd"] = "psd",
["puppet"] = "pp",
["pyc"] = "pyc",
["pyd"] = "pyd",
["pyo"] = "pyo",
["python"] = "py",
["qml"] = "qml",
["query"] = "query",
["r"] = "r",
["res"] = "rescript",
["resi"] = "rescript",
["rlib"] = "rlib",
["rmd"] = "rmd",
["rproj"] = "rproj",
["ruby"] = "rb",
["rust"] = "rs",
["sass"] = "sass",
["sbt"] = "sbt",
["scala"] = "scala",
["scheme"] = "scm",
["scss"] = "scss",
["sh"] = "sh",
["slim"] = "slim",
["sln"] = "sln",
["sml"] = "sml",
["solidity"] = "sol",
["sql"] = "sql",
["sqlite"] = "sqlite",
["sqlite3"] = "sqlite3",
["srt"] = "srt",
["ssa"] = "ssa",
["stp"] = "stp",
["styl"] = "styl",
["sublime"] = "sublime",
["suo"] = "suo",
["svelte"] = "svelte",
["svg"] = "svg",
["swift"] = "swift",
["systemverilog"] = "sv",
["tads"] = "t",
["tcl"] = "tcl",
["tf"] = "tf",
["templ"] = "templ",
["terminal"] = "terminal",
["tex"] = "tex",
["tmux"] = "tmux",
["toml"] = "toml",
["tres"] = "tres",
["tscn"] = "tscn",
["twig"] = "twig",
["txt"] = "txt",
["typescript"] = "ts",
["typescriptreact"] = "tsx",
["vala"] = "vala",
["verilog"] = "v",
["vhdl"] = "vhd",
["vim"] = "vim",
["vue"] = "vue",
["wasm"] = "wasm",
["webm"] = "webm",
["webp"] = "webp",
["webpack"] = "webpack",
["xcplayground"] = "xcplayground",
["xls"] = "xls",
["xlsx"] = "xlsx",
["xml"] = "xml",
["yaml"] = "yaml",
["zig"] = "zig",
["zsh"] = "zsh",
}
local function get_highlight_name(data)
if not global_opts.color_icons then
data = default_icon
end
return data.name and "DevIcon" .. data.name
end
local nvim_set_hl = vim.api.nvim_set_hl
local function set_up_highlight(icon_data)
if not global_opts.color_icons then
icon_data = default_icon
end
local hl_group = get_highlight_name(icon_data)
if hl_group and (icon_data.color or icon_data.cterm_color) then
nvim_set_hl(0, get_highlight_name(icon_data), {
fg = icon_data.color,
ctermfg = tonumber(icon_data.cterm_color),
})
end
end
local nvim_get_hl_by_name = vim.api.nvim_get_hl_by_name
local function highlight_exists(group)
if not group then
return
end
local ok, hl = pcall(nvim_get_hl_by_name, group, true)
return ok and not (hl or {})[true]
end
function M.set_up_highlights(allow_override)
if not global_opts.color_icons then
set_up_highlight(default_icon)
return
end
for _, icon_data in pairs(icons) do
local has_color = icon_data.color or icon_data.cterm_color
local name_valid = icon_data.name
local defined_before = highlight_exists(get_highlight_name(icon_data))
if has_color and name_valid and (allow_override or not defined_before) then
set_up_highlight(icon_data)
end
end
end
local function get_highlight_foreground(icon_data)
if not global_opts.color_icons then
icon_data = default_icon
end
return string.format("#%06x", nvim_get_hl_by_name(get_highlight_name(icon_data), true).foreground)
end
local function get_highlight_ctermfg(icon_data)
if not global_opts.color_icons then
icon_data = default_icon
end
return nvim_get_hl_by_name(get_highlight_name(icon_data), false).foreground
end
local loaded = false
function M.has_loaded()
return loaded
end
local if_nil = vim.F.if_nil
function M.setup(opts)
if loaded then
return
end
loaded = true
local user_icons = opts or {}
if user_icons.default then
global_opts.default = true
end
if user_icons.strict then
global_opts.strict = true
end
global_opts.color_icons = if_nil(user_icons.color_icons, global_opts.color_icons)
if user_icons.variant == "light" or user_icons.variant == "dark" then
global_opts.variant = user_icons.variant
-- Reload the icons after setting variant option
refresh_icons()
end
if user_icons.override and user_icons.override.default_icon then
default_icon = user_icons.override.default_icon
end
local user_filename_icons = user_icons.override_by_filename
local user_file_ext_icons = user_icons.override_by_extension
local user_operating_system_icons = user_icons.override_by_operating_system
local user_desktop_environment_icons = user_icons.override_by_desktop_environment
local user_window_manager_icons = user_icons.override_by_window_manager
-- filename matches are case insensitive
lowercase_keys(icons_by_filename)
lowercase_keys(user_icons.override)
lowercase_keys(user_icons.override_by_filename)
icons = vim.tbl_extend(
"force",
icons,
user_icons.override or {},
user_filename_icons or {},
user_file_ext_icons or {},
user_operating_system_icons or {},
user_desktop_environment_icons or {},
user_window_manager_icons or {}
)
global_opts.override = vim.tbl_extend(
"force",
global_opts.override,
user_icons.override or {},
user_filename_icons or {},
user_file_ext_icons or {},
user_operating_system_icons or {},
user_desktop_environment_icons or {},
user_window_manager_icons or {}
)
if user_filename_icons then
icons_by_filename = vim.tbl_extend("force", icons_by_filename, user_filename_icons)
end
if user_file_ext_icons then
icons_by_file_extension = vim.tbl_extend("force", icons_by_file_extension, user_file_ext_icons)
end
if user_operating_system_icons then
icons_by_operating_system = vim.tbl_extend("force", icons_by_operating_system, user_operating_system_icons)
end
if user_desktop_environment_icons then
icons_by_desktop_environment = vim.tbl_extend("force", icons_by_desktop_environment, user_desktop_environment_icons)
end
if user_window_manager_icons then
icons_by_window_manager = vim.tbl_extend("force", icons_by_window_manager, user_window_manager_icons)
end
icons[1] = default_icon
M.set_up_highlights()
vim.api.nvim_create_autocmd("ColorScheme", {
desc = "Re-apply icon colors after changing colorschemes",
group = vim.api.nvim_create_augroup("NvimWebDevicons", { clear = true }),
callback = M.set_up_highlights,
})
-- highlight test command
vim.api.nvim_create_user_command("NvimWebDeviconsHiTest", function()
require "nvim-web-devicons.hi-test"(
default_icon,
global_opts.override,
icons_by_filename,
icons_by_file_extension,
icons_by_operating_system,
icons_by_desktop_environment,
icons_by_window_manager
)
end, {
desc = "nvim-web-devicons: highlight test",
})
end
function M.get_default_icon()
return default_icon
end
-- recursively iterate over each segment separated by '.' to parse extension with multiple dots in filename
local function iterate_multi_dotted_extension(name, icon_table)
if name == nil then
return nil
end
local compound_ext = name:match "%.(.*)"
local icon = icon_table[compound_ext]
if icon then
return icon
end
return iterate_multi_dotted_extension(compound_ext, icon_table)
end
local function get_icon_by_extension(name, ext, opts)
local is_strict = if_nil(opts and opts.strict, global_opts.strict)
local icon_table = is_strict and icons_by_file_extension or icons
if ext ~= nil then
return icon_table[ext]
end
return iterate_multi_dotted_extension(name, icon_table)
end
local function get_icon_data(name, ext, opts)
if type(name) == "string" then
name = name:lower()
end
if not loaded then
M.setup()
end
local has_default = if_nil(opts and opts.default, global_opts.default)
local is_strict = if_nil(opts and opts.strict, global_opts.strict)
local icon_data
if is_strict then
icon_data = icons_by_filename[name] or get_icon_by_extension(name, ext, opts) or (has_default and default_icon)
else
icon_data = icons[name] or get_icon_by_extension(name, ext, opts) or (has_default and default_icon)
end
return icon_data
end
function M.get_icon(name, ext, opts)
local icon_data = get_icon_data(name, ext, opts)
if icon_data then
return icon_data.icon, get_highlight_name(icon_data)
end
end
function M.get_icon_name_by_filetype(ft)
return filetypes[ft]
end
function M.get_icon_by_filetype(ft, opts)
local name = M.get_icon_name_by_filetype(ft)
opts = opts or {}
opts.strict = false
return M.get_icon(name or "", nil, opts)
end
function M.get_icon_colors(name, ext, opts)
local icon_data = get_icon_data(name, ext, opts)
if icon_data then
local color = icon_data.color
local cterm_color = icon_data.cterm_color
if icon_data.name and highlight_exists(get_highlight_name(icon_data)) then
color = get_highlight_foreground(icon_data) or color
cterm_color = get_highlight_ctermfg(icon_data) or cterm_color
end
return icon_data.icon, color, cterm_color
end
end
function M.get_icon_colors_by_filetype(ft, opts)
local name = M.get_icon_name_by_filetype(ft)
return M.get_icon_colors(name or "", nil, opts)
end
function M.get_icon_color(name, ext, opts)
local data = { M.get_icon_colors(name, ext, opts) }
return data[1], data[2]
end
function M.get_icon_color_by_filetype(ft, opts)
local name = M.get_icon_name_by_filetype(ft)
opts = opts or {}
opts.strict = false
return M.get_icon_color(name or "", nil, opts)
end
function M.get_icon_cterm_color(name, ext, opts)
local data = { M.get_icon_colors(name, ext, opts) }
return data[1], data[3]
end
function M.get_icon_cterm_color_by_filetype(ft, opts)
local name = M.get_icon_name_by_filetype(ft)
return M.get_icon_cterm_color(name or "", nil, opts)
end
function M.set_icon(user_icons)
icons = vim.tbl_extend("force", icons, user_icons or {})
global_opts.override = vim.tbl_extend("force", global_opts.override, user_icons or {})
if not global_opts.color_icons then
return
end
for _, icon_data in pairs(user_icons) do
set_up_highlight(icon_data)
end
end
function M.set_icon_by_filetype(user_filetypes)
filetypes = vim.tbl_extend("force", filetypes, user_filetypes or {})
end
function M.set_default_icon(icon, color, cterm_color)
default_icon.icon = icon
default_icon.color = color
default_icon.cterm_color = cterm_color
set_up_highlight(default_icon)
end
-- Load the icons already, the loaded tables depend on the 'background' setting.
refresh_icons()
function M.refresh()
refresh_icons()
M.set_up_highlights(true)
end
-- Change icon set on background change
vim.api.nvim_create_autocmd("OptionSet", {
pattern = "background",
callback = M.refresh,
})
return M