Skip to content

Commit b5f2b3a

Browse files
committed
move file specifics from icons to File
1 parent 612f787 commit b5f2b3a

File tree

6 files changed

+52
-85
lines changed

6 files changed

+52
-85
lines changed

lua/nvim-tree/node/directory.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ function DirectoryNode:highlighted_icon()
218218

219219
-- devicon if enabled and available
220220
if self.explorer.opts.renderer.icons.web_devicons.folder.enable then
221-
str, hl = icons.get_icon(self.name, nil)
221+
str, hl = icons.get_icon(self.name)
222222
if not self.explorer.opts.renderer.icons.web_devicons.folder.color then
223223
hl = nil
224224
end
225225
end
226226

227-
-- icon from opts
227+
-- default icon from opts
228228
if not str then
229229
if #self.nodes ~= 0 or self.has_children then
230230
if self.open then
@@ -241,8 +241,8 @@ function DirectoryNode:highlighted_icon()
241241
end
242242
end
243243

244-
-- hl
245-
if #str > 0 and hl == nil then
244+
-- default hl
245+
if not hl then
246246
if self.open then
247247
hl = "NvimTreeOpenedFolderIcon"
248248
else

lua/nvim-tree/node/file-link.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local icons = require("nvim-tree.renderer.components.icons")
33
local utils = require("nvim-tree.utils")
44

55
local FileNode = require("nvim-tree.node.file")
6+
local Node = require("nvim-tree.node")
67

78
---@class (exact) FileLinkNode: FileNode
89
---@field link_to string absolute path
@@ -43,15 +44,25 @@ end
4344

4445
---@return HighlightedString icon
4546
function FileLinkNode:highlighted_icon()
46-
return { str = icons.i.symlink, hl = { "NvimTreeSymlinkIcon" } }
47+
if not self.explorer.opts.renderer.icons.show.folder then
48+
return Node.highlighted_icon(self)
49+
end
50+
51+
local str, hl
52+
53+
-- default icon from opts
54+
str = self.explorer.opts.renderer.icons.glyphs.symlink
55+
hl = "NvimTreeSymlinkIcon"
56+
57+
return { str = str, hl = { hl } }
4758
end
4859

4960
---@return HighlightedString name
5061
function FileLinkNode:highlighted_name()
5162
local str = self.name
5263
if self.explorer.opts.renderer.symlink_destination then
5364
local link_to = utils.path_relative(self.link_to, self.explorer.absolute_path)
54-
str = string.format("%s%s%s", str, icons.i.symlink_arrow, link_to)
65+
str = string.format("%s%s%s", str, self.explorer.opts.renderer.icons.symlink_arrow, link_to)
5566
end
5667

5768
return { str = str, hl = { "NvimTreeSymlink" } }

lua/nvim-tree/node/file.lua

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,31 @@ end
6868

6969
---@return HighlightedString icon
7070
function FileNode:highlighted_icon()
71-
local icon_str, icon_hl = icons.get_file_icon(self.name, self.extension)
71+
if not self.explorer.opts.renderer.icons.show.file then
72+
return Node.highlighted_icon(self)
73+
end
74+
75+
local str, hl
76+
77+
-- devicon if enabled and available
78+
if self.explorer.opts.renderer.icons.web_devicons.file.enable then
79+
str, hl = icons.get_icon(self.name)
80+
if not self.explorer.opts.renderer.icons.web_devicons.file.color then
81+
hl = nil
82+
end
83+
end
84+
85+
-- default icon from opts
86+
if not str then
87+
str = self.explorer.opts.renderer.icons.glyphs.default
88+
end
89+
90+
-- default hl
91+
if not hl then
92+
hl = "NvimTreeFileIcon"
93+
end
7294

73-
return { str = icon_str, hl = { icon_hl } }
95+
return { str = str, hl = { hl } }
7496
end
7597

7698
---@return HighlightedString name

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

Lines changed: 10 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,90 +9,27 @@
99
---@field get_default_icon fun(): DevIcon
1010

1111
local M = {
12-
i = {},
1312
---@type DevIcons?
1413
devicons = nil,
1514
}
1615

17-
local function config_symlinks()
18-
M.i.symlink = #M.config.glyphs.symlink > 0 and M.config.glyphs.symlink or ""
19-
M.i.symlink_arrow = M.config.symlink_arrow
20-
end
21-
22-
---@return string icon
23-
---@return string? hl_group
24-
local function empty()
25-
return "", nil
26-
end
27-
28-
---@return string icon
29-
---@return string? hl_group
30-
local function get_file_icon_default()
31-
local hl_group = "NvimTreeFileIcon"
32-
local icon = M.config.glyphs.default
33-
if #icon > 0 then
34-
return icon, hl_group
35-
else
36-
return "", nil
37-
end
38-
end
39-
40-
---@param name string
41-
---@param ext string
42-
---@return string icon
43-
---@return string? hl_group
44-
local function get_file_icon_webdev(name, ext)
45-
local icon, hl_group = M.devicons.get_icon(name, ext)
46-
if not M.config.web_devicons.file.color then
47-
hl_group = "NvimTreeFileIcon"
48-
end
49-
if icon and hl_group ~= "DevIconDefault" then
50-
return icon, hl_group
51-
elseif string.match(ext, "%.(.*)") then
52-
-- If there are more extensions to the file, try to grab the icon for them recursively
53-
return get_file_icon_webdev(name, string.match(ext, "%.(.*)"))
54-
else
55-
local devicons_default = M.devicons.get_default_icon()
56-
if devicons_default and type(devicons_default.icon) == "string" and type(devicons_default.name) == "string" then
57-
return devicons_default.icon, "DevIcon" .. devicons_default.name
58-
else
59-
return get_file_icon_default()
60-
end
61-
end
62-
end
63-
64-
local function config_file_icon()
65-
if M.config.show.file then
66-
if M.devicons and M.config.web_devicons.file.enable then
67-
M.get_file_icon = get_file_icon_webdev
68-
else
69-
M.get_file_icon = get_file_icon_default
70-
end
71-
else
72-
M.get_file_icon = empty
73-
end
74-
end
75-
76-
---Wrapper around nvim-web-devicons, nil if not present
16+
---Wrapper around nvim-web-devicons, nils if not present
7717
---@param name string
78-
---@param ext string?
7918
---@return string? icon
8019
---@return string? hl_group
81-
function M.get_icon(name, ext)
20+
function M.get_icon(name)
8221
if M.devicons then
83-
return M.devicons.get_icon(name, ext)
22+
return M.devicons.get_icon(name, nil)
23+
else
24+
return nil, nil
8425
end
8526
end
8627

87-
function M.reset_config()
88-
config_symlinks()
89-
config_file_icon()
90-
end
91-
92-
function M.setup(opts)
93-
M.config = opts.renderer.icons
94-
95-
M.devicons = pcall(require, "nvim-web-devicons") and require("nvim-web-devicons") or nil
28+
function M.setup()
29+
local devicons_ok, devicons = pcall(require, "nvim-web-devicons")
30+
if devicons_ok then
31+
M.devicons = devicons
32+
end
9633
end
9734

9835
return M

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ M.padding = require("nvim-tree.renderer.components.padding")
88
function M.setup(opts)
99
M.diagnostics.setup(opts)
1010
M.full_name.setup(opts)
11-
M.icons.setup(opts)
11+
M.icons.setup()
1212
M.padding.setup(opts)
1313
end
1414

lua/nvim-tree/renderer/init.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ local log = require("nvim-tree.log")
22
local view = require("nvim-tree.view")
33
local events = require("nvim-tree.events")
44

5-
local icon_component = require("nvim-tree.renderer.components.icons")
6-
75
local Builder = require("nvim-tree.renderer.builder")
86

97
local SIGN_GROUP = "NvimTreeRendererSigns"
@@ -107,7 +105,6 @@ function Renderer:draw()
107105
local profile = log.profile_start("draw")
108106

109107
local cursor = vim.api.nvim_win_get_cursor(view.get_winnr() or 0)
110-
icon_component.reset_config()
111108

112109
local builder = Builder:new(self.opts, self.explorer):build()
113110

0 commit comments

Comments
 (0)