|
2 | 2 | ---Display all icons and their group highlighted, followed by the concrete definition
|
3 | 3 | --
|
4 | 4 | ---@class IconDisplay for :NvimTreeHiTest
|
5 |
| ----@field tag any filename, os or extension, only strings accepted |
| 5 | +---@field tag string filename, os or extension |
6 | 6 | ---@field name string name without prefix
|
7 | 7 | ---@field icon string icon itself
|
8 | 8 | ---@field group string|nil :hi group name
|
9 | 9 | ---@field def string|nil :hi concrete definition
|
10 | 10 | local IconDisplay = {}
|
11 | 11 |
|
12 | 12 | ---@param o IconDisplay
|
13 |
| ----@return IconDisplay |
| 13 | +---@return IconDisplay|nil |
14 | 14 | function IconDisplay:new(o)
|
| 15 | + if type(o.tag) ~= "string" or type(o.name) ~= "string" or type(o.icon) ~= "string" then |
| 16 | + return nil |
| 17 | + end |
| 18 | + |
15 | 19 | setmetatable(o, self)
|
16 | 20 | self.__index = self
|
17 | 21 |
|
18 | 22 | o.group = "DevIcon" .. o.name
|
19 |
| - o.tag = type(o.tag) == "string" and o.tag or "" |
| 23 | + o.tag = o.tag or "" |
20 | 24 |
|
21 | 25 | -- concrete definition
|
22 | 26 | local ok, res = pcall(vim.api.nvim_cmd, { cmd = "highlight", args = { o.group } }, { output = true })
|
|
61 | 65 | ---@param header string
|
62 | 66 | ---@return number l incremented
|
63 | 67 | local function render_icons(bufnr, l, icons, header)
|
64 |
| - local displays = {} |
65 | 68 | local max_tag_len = 0
|
66 | 69 | local max_name_len = 0
|
67 | 70 |
|
| 71 | + local displays = {} |
| 72 | + ---@cast displays IconDisplay[] |
| 73 | + |
68 | 74 | -- build all icon displays
|
69 | 75 | for tag, icon in pairs(icons) do
|
70 | 76 | local display = IconDisplay:new { tag = tag, name = icon.name, icon = icon.icon }
|
71 |
| - table.insert(displays, display) |
72 |
| - max_tag_len = math.max(max_tag_len, #display.tag) |
73 |
| - max_name_len = math.max(max_name_len, #display.name) |
| 77 | + if display then |
| 78 | + table.insert(displays, display) |
| 79 | + max_tag_len = math.max(max_tag_len, #display.tag) |
| 80 | + max_name_len = math.max(max_name_len, #display.name) |
| 81 | + end |
74 | 82 | end
|
75 | 83 |
|
76 | 84 | -- sort by name
|
|
92 | 100 | ---Icon, name, <tag>, concrete highlight definition
|
93 | 101 | ---tag and header follows param
|
94 | 102 | ---@param default_icon table no tag "Default"
|
| 103 | +---@param global_override table[] all global overrides "Overrides" |
95 | 104 | ---@param icons_by_filename table[] filename "By File Name"
|
96 | 105 | ---@param icons_by_file_extension table[] extension "By File Extension"
|
97 | 106 | ---@param icons_by_operating_system table[] os "By Operating System"
|
98 |
| -return function(default_icon, icons_by_filename, icons_by_file_extension, icons_by_operating_system) |
| 107 | +return function(default_icon, global_override, icons_by_filename, icons_by_file_extension, icons_by_operating_system) |
99 | 108 | -- create a buffer
|
100 | 109 | local bufnr = vim.api.nvim_create_buf(false, true)
|
101 | 110 |
|
102 | 111 | -- render and highlight each section
|
103 | 112 | local l = 0
|
104 | 113 | l = render_icons(bufnr, l, { default_icon }, "Default")
|
| 114 | + if global_override and next(global_override) then |
| 115 | + l = render_icons(bufnr, l, global_override, "Overrides") |
| 116 | + end |
105 | 117 | l = render_icons(bufnr, l, icons_by_filename, "By File Name")
|
106 | 118 | l = render_icons(bufnr, l, icons_by_file_extension, "By File Extension")
|
107 | 119 | render_icons(bufnr, l, icons_by_operating_system, "By Operating System")
|
|
0 commit comments