* refactor: extract resolution tables into resolve module Problem: ext_map, filename_map, and resolve() are locked inside override.lua as locals, making them inaccessible to any code path that doesn't go through the devicons monkey-patch. Solution: extract all resolution tables and logic into a new nonicons.resolve module. Fix Gemfile/Jenkinsfile case bug (keys were uppercase but resolve() lowercases input before lookup). Add ft_map for vim filetypes that don't match extension or mapping keys. * refactor: wire override.lua to resolve module Problem: override.lua still has its own copies of ext_map, filename_map, and resolve(), duplicating the newly extracted resolve module. Solution: replace local tables and resolve() with imports from nonicons.resolve. Update all *_by_filetype overrides to use resolve_filetype() instead of raw ext_map[ft], gaining ft_map fallback and direct mapping key lookup. * feat: add get_icon and get_icon_by_filetype API Problem: plugins that don't use devicons have no way to get nonicons glyphs for files. The only integration path is the devicons monkey-patch via apply(). Solution: add get_icon(name, ext) and get_icon_by_filetype(ft) to the public API in init.lua. Both use lazy require of the resolve module and return nil on miss so callers decide fallback. Document both functions in vimdoc and update the oil.nvim recipe.
151 lines
4 KiB
Lua
151 lines
4 KiB
Lua
local mapping = require('nonicons.mapping')
|
|
local resolve_mod = require('nonicons.resolve')
|
|
|
|
local function char(name)
|
|
local code = mapping[name]
|
|
if code then
|
|
return vim.fn.nr2char(code)
|
|
end
|
|
end
|
|
|
|
local fallback_icon
|
|
|
|
local function resolve(name, ext)
|
|
local key = resolve_mod.resolve_name(name, ext)
|
|
if key then
|
|
return char(key)
|
|
end
|
|
return fallback_icon
|
|
end
|
|
|
|
local M = {}
|
|
|
|
function M.apply()
|
|
local ok, devicons = pcall(require, 'nvim-web-devicons')
|
|
if not ok then
|
|
return
|
|
end
|
|
|
|
fallback_icon = char('file')
|
|
|
|
local orig_get_icon = devicons.get_icon
|
|
devicons.get_icon = function(name, ext, opts)
|
|
local icon, hl = orig_get_icon(name, ext, opts)
|
|
if icon then
|
|
icon = resolve(name, ext)
|
|
end
|
|
return icon, hl
|
|
end
|
|
|
|
local orig_get_icon_by_filetype = devicons.get_icon_by_filetype
|
|
devicons.get_icon_by_filetype = function(ft, opts)
|
|
local icon, hl = orig_get_icon_by_filetype(ft, opts)
|
|
if icon then
|
|
local key = resolve_mod.resolve_filetype(ft)
|
|
icon = key and char(key) or fallback_icon
|
|
end
|
|
return icon, hl
|
|
end
|
|
|
|
local orig_get_icon_colors = devicons.get_icon_colors
|
|
devicons.get_icon_colors = function(name, ext, opts)
|
|
local icon, color, cterm_color = orig_get_icon_colors(name, ext, opts)
|
|
if icon then
|
|
icon = resolve(name, ext)
|
|
end
|
|
return icon, color, cterm_color
|
|
end
|
|
|
|
local orig_get_icon_color = devicons.get_icon_color
|
|
devicons.get_icon_color = function(name, ext, opts)
|
|
local icon, color = orig_get_icon_color(name, ext, opts)
|
|
if icon then
|
|
icon = resolve(name, ext)
|
|
end
|
|
return icon, color
|
|
end
|
|
|
|
local orig_get_icon_cterm_color = devicons.get_icon_cterm_color
|
|
devicons.get_icon_cterm_color = function(name, ext, opts)
|
|
local icon, cterm_color = orig_get_icon_cterm_color(name, ext, opts)
|
|
if icon then
|
|
icon = resolve(name, ext)
|
|
end
|
|
return icon, cterm_color
|
|
end
|
|
|
|
local orig_get_icon_colors_by_filetype = devicons.get_icon_colors_by_filetype
|
|
devicons.get_icon_colors_by_filetype = function(ft, opts)
|
|
local icon, color, cterm_color = orig_get_icon_colors_by_filetype(ft, opts)
|
|
if icon then
|
|
local key = resolve_mod.resolve_filetype(ft)
|
|
icon = key and char(key) or fallback_icon
|
|
end
|
|
return icon, color, cterm_color
|
|
end
|
|
|
|
local orig_get_icon_color_by_filetype = devicons.get_icon_color_by_filetype
|
|
devicons.get_icon_color_by_filetype = function(ft, opts)
|
|
local icon, color = orig_get_icon_color_by_filetype(ft, opts)
|
|
if icon then
|
|
local key = resolve_mod.resolve_filetype(ft)
|
|
icon = key and char(key) or fallback_icon
|
|
end
|
|
return icon, color
|
|
end
|
|
|
|
local orig_get_icon_cterm_color_by_filetype = devicons.get_icon_cterm_color_by_filetype
|
|
devicons.get_icon_cterm_color_by_filetype = function(ft, opts)
|
|
local icon, cterm_color = orig_get_icon_cterm_color_by_filetype(ft, opts)
|
|
if icon then
|
|
local key = resolve_mod.resolve_filetype(ft)
|
|
icon = key and char(key) or fallback_icon
|
|
end
|
|
return icon, cterm_color
|
|
end
|
|
|
|
local function override_tables()
|
|
local by_ext = devicons.get_icons_by_extension()
|
|
for ext, data in pairs(by_ext) do
|
|
local name = resolve_mod.ext_map[ext]
|
|
if name then
|
|
data.icon = char(name) or fallback_icon
|
|
else
|
|
data.icon = fallback_icon
|
|
end
|
|
end
|
|
|
|
local by_filename = devicons.get_icons_by_filename()
|
|
for fname, data in pairs(by_filename) do
|
|
local name = resolve_mod.filename_map[fname]
|
|
if name then
|
|
data.icon = char(name) or fallback_icon
|
|
else
|
|
data.icon = fallback_icon
|
|
end
|
|
end
|
|
|
|
devicons.set_default_icon(fallback_icon)
|
|
end
|
|
|
|
override_tables()
|
|
|
|
local group = vim.api.nvim_create_augroup('Nonicons', { clear = true })
|
|
|
|
vim.api.nvim_create_autocmd('OptionSet', {
|
|
pattern = 'background',
|
|
group = group,
|
|
callback = function()
|
|
vim.schedule(override_tables)
|
|
end,
|
|
})
|
|
|
|
vim.api.nvim_create_autocmd('ColorScheme', {
|
|
group = group,
|
|
callback = function()
|
|
vim.schedule(override_tables)
|
|
end,
|
|
})
|
|
end
|
|
|
|
return M
|