Skip to content

Commit d128bc4

Browse files
alex-courtisolivertzeng
authored andcommitted
fix(nvim-tree#419): harden HiTest, add missing override_by_operating_system (nvim-tree#423)
* fix(nvim-tree#419): HiTest shows overrides, add missing override_by_operating_system * fix(nvim-tree#419): HiTest harden
1 parent 9c57c4c commit d128bc4

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ require'nvim-web-devicons'.setup {
9393
name = "Log"
9494
}
9595
};
96+
-- same as `override` but specifically for operating system
97+
-- takes effect when `strict` is true
98+
override_by_operating_system = {
99+
["apple"] = {
100+
icon = "",
101+
color = "#A2AAAD",
102+
cterm_color = "248",
103+
name = "Apple",
104+
},
105+
};
96106
}
97107
```
98108

lua/nvim-web-devicons.lua

+15-3
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,23 @@ function M.setup(opts)
343343

344344
local user_filename_icons = user_icons.override_by_filename
345345
local user_file_ext_icons = user_icons.override_by_extension
346+
local user_operating_system_icons = user_icons.override_by_operating_system
346347

347-
icons =
348-
vim.tbl_extend("force", icons, user_icons.override or {}, user_filename_icons or {}, user_file_ext_icons or {})
348+
icons = vim.tbl_extend(
349+
"force",
350+
icons,
351+
user_icons.override or {},
352+
user_filename_icons or {},
353+
user_file_ext_icons or {},
354+
user_operating_system_icons or {}
355+
)
349356
global_opts.override = vim.tbl_extend(
350357
"force",
351358
global_opts.override,
352359
user_icons.override or {},
353360
user_filename_icons or {},
354-
user_file_ext_icons or {}
361+
user_file_ext_icons or {},
362+
user_operating_system_icons or {}
355363
)
356364

357365
if user_filename_icons then
@@ -360,6 +368,9 @@ function M.setup(opts)
360368
if user_file_ext_icons then
361369
icons_by_file_extension = vim.tbl_extend("force", icons_by_file_extension, user_file_ext_icons)
362370
end
371+
if user_operating_system_icons then
372+
icons_by_operating_system = vim.tbl_extend("force", icons_by_operating_system, user_operating_system_icons)
373+
end
363374

364375
icons[1] = default_icon
365376

@@ -375,6 +386,7 @@ function M.setup(opts)
375386
vim.api.nvim_create_user_command("NvimWebDeviconsHiTest", function()
376387
require "nvim-web-devicons.hi-test"(
377388
default_icon,
389+
global_opts.override,
378390
icons_by_filename,
379391
icons_by_file_extension,
380392
icons_by_operating_system

lua/nvim-web-devicons/hi-test.lua

+20-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22
---Display all icons and their group highlighted, followed by the concrete definition
33
--
44
---@class IconDisplay for :NvimTreeHiTest
5-
---@field tag any filename, os or extension, only strings accepted
5+
---@field tag string filename, os or extension
66
---@field name string name without prefix
77
---@field icon string icon itself
88
---@field group string|nil :hi group name
99
---@field def string|nil :hi concrete definition
1010
local IconDisplay = {}
1111

1212
---@param o IconDisplay
13-
---@return IconDisplay
13+
---@return IconDisplay|nil
1414
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+
1519
setmetatable(o, self)
1620
self.__index = self
1721

1822
o.group = "DevIcon" .. o.name
19-
o.tag = type(o.tag) == "string" and o.tag or ""
23+
o.tag = o.tag or ""
2024

2125
-- concrete definition
2226
local ok, res = pcall(vim.api.nvim_cmd, { cmd = "highlight", args = { o.group } }, { output = true })
@@ -61,16 +65,20 @@ end
6165
---@param header string
6266
---@return number l incremented
6367
local function render_icons(bufnr, l, icons, header)
64-
local displays = {}
6568
local max_tag_len = 0
6669
local max_name_len = 0
6770

71+
local displays = {}
72+
---@cast displays IconDisplay[]
73+
6874
-- build all icon displays
6975
for tag, icon in pairs(icons) do
7076
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
7482
end
7583

7684
-- sort by name
@@ -92,16 +100,20 @@ end
92100
---Icon, name, <tag>, concrete highlight definition
93101
---tag and header follows param
94102
---@param default_icon table no tag "Default"
103+
---@param global_override table[] all global overrides "Overrides"
95104
---@param icons_by_filename table[] filename "By File Name"
96105
---@param icons_by_file_extension table[] extension "By File Extension"
97106
---@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)
99108
-- create a buffer
100109
local bufnr = vim.api.nvim_create_buf(false, true)
101110

102111
-- render and highlight each section
103112
local l = 0
104113
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
105117
l = render_icons(bufnr, l, icons_by_filename, "By File Name")
106118
l = render_icons(bufnr, l, icons_by_file_extension, "By File Extension")
107119
render_icons(bufnr, l, icons_by_operating_system, "By Operating System")

0 commit comments

Comments
 (0)