Skip to content

Commit 6818306

Browse files
committed
file devicon uses library to fall back
1 parent 0acbc79 commit 6818306

File tree

2 files changed

+17
-41
lines changed

2 files changed

+17
-41
lines changed

lua/nvim-tree/node/file.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,9 @@ function FileNode:highlighted_icon()
7474

7575
local str, hl
7676

77-
-- devicon if enabled and available
77+
-- devicon if enabled and available, fallback to default
7878
if self.explorer.opts.renderer.icons.web_devicons.file.enable then
79-
str, hl = icons.get_icon(self.name)
80-
if not str then
81-
local default_icon = icons.get_default_icon()
82-
if default_icon then
83-
str = default_icon.icon
84-
hl = "DevIcon" .. default_icon.name
85-
end
86-
end
79+
str, hl = icons.get_icon(self.name, nil, { default = true })
8780
if not self.explorer.opts.renderer.icons.web_devicons.file.color then
8881
hl = nil
8982
end

lua/nvim-tree/renderer/components/icons.lua

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,33 @@
1-
---@class DevIcon
2-
---@field icon string
3-
---@field color string
4-
---@field cterm_color string
5-
---@field name string
1+
---@alias devicons_get_icon fun(name: string, ext: string?, opts: table?): string?, string?
2+
---@alias devicons_setup fun(opts: table?)
63

7-
---@class DevIcons
8-
---@field get_icon fun(name: string, ext: string?): string?, string?
9-
---@field get_default_icon fun(): DevIcon
4+
---@class DevIcons?
5+
---@field setup devicons_setup
6+
---@field get_icon devicons_get_icon
7+
local devicons
108

11-
local M = {
12-
---@type DevIcons?
13-
devicons = nil,
14-
}
9+
local M = {}
1510

1611
---Wrapper around nvim-web-devicons, nils if devicons not available
17-
---@param name string
18-
---@return string? icon
19-
---@return string? hl_group
20-
function M.get_icon(name)
21-
if M.devicons then
22-
return M.devicons.get_icon(name, nil)
12+
---@type devicons_get_icon
13+
function M.get_icon(name, ext, opts)
14+
if devicons then
15+
return devicons.get_icon(name, ext, opts)
2316
else
2417
return nil, nil
2518
end
2619
end
2720

28-
---Wrapper around nvim-web-devicons, nil if devicons not available
29-
---@return DevIcon?
30-
function M.get_default_icon()
31-
if M.devicons then
32-
return M.devicons.get_default_icon()
33-
else
34-
return nil
35-
end
36-
end
37-
3821
---Attempt to use nvim-web-devicons if present and enabled for file or folder
3922
---@param opts table
4023
function M.setup(opts)
4124
if opts.renderer.icons.show.file or opts.renderer.icons.show.folder then
42-
local devicons_ok, devicons = pcall(require, "nvim-web-devicons")
43-
if devicons_ok then
44-
M.devicons = devicons
25+
local ok, di = pcall(require, "nvim-web-devicons")
26+
if ok then
27+
devicons = di
4528

4629
-- does nothing if already called i.e. don't clobber previous user setup
47-
M.devicons.setup()
30+
devicons.setup()
4831
end
4932
end
5033
end

0 commit comments

Comments
 (0)