Skip to content

Commit 4567e0d

Browse files
authored
chore: replace deprecated neovim API (#553)
1 parent 37334ad commit 4567e0d

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

lua/nvim-web-devicons.lua

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,18 @@ local function set_up_highlight(icon_data)
346346
end
347347
end
348348

349-
local nvim_get_hl_by_name = vim.api.nvim_get_hl_by_name
350349
local function highlight_exists(group)
351350
if not group then
352351
return
353352
end
354353

355-
local ok, hl = pcall(nvim_get_hl_by_name, group, true)
356-
return ok and not (hl or {})[true]
354+
if vim.fn.has "nvim-0.9" == 1 then
355+
local hl = vim.api.nvim_get_hl(0, { name = group, link = false })
356+
return not vim.tbl_isempty(hl)
357+
else
358+
local ok, hl = pcall(vim.api.nvim_get_hl_by_name, group, true) ---@diagnostic disable-line: deprecated
359+
return ok and not (hl or {})[true]
360+
end
357361
end
358362

359363
function M.set_up_highlights(allow_override)
@@ -377,15 +381,31 @@ local function get_highlight_foreground(icon_data)
377381
icon_data = default_icon
378382
end
379383

380-
return string.format("#%06x", nvim_get_hl_by_name(get_highlight_name(icon_data), true).foreground)
384+
local higroup = get_highlight_name(icon_data)
385+
386+
local fg
387+
if vim.fn.has "nvim-0.9" == 1 then
388+
fg = vim.api.nvim_get_hl(0, { name = higroup, link = false }).fg
389+
else
390+
fg = vim.api.nvim_get_hl_by_name(higroup, true).foreground ---@diagnostic disable-line: deprecated
391+
end
392+
393+
return string.format("#%06x", fg)
381394
end
382395

383396
local function get_highlight_ctermfg(icon_data)
384397
if not global_opts.color_icons then
385398
icon_data = default_icon
386399
end
387400

388-
return nvim_get_hl_by_name(get_highlight_name(icon_data), false).foreground
401+
local higroup = get_highlight_name(icon_data)
402+
403+
if vim.fn.has "nvim-0.9" == 1 then
404+
--- @diagnostic disable-next-line: undefined-field vim.api.keyset.hl_info specifies cterm, not ctermfg
405+
return vim.api.nvim_get_hl(0, { name = higroup, link = false }).ctermfg
406+
else
407+
return vim.api.nvim_get_hl_by_name(higroup, false).foreground ---@diagnostic disable-line: deprecated
408+
end
389409
end
390410

391411
local loaded = false

0 commit comments

Comments
 (0)