Skip to content

Commit 0acbc79

Browse files
committed
tidy hl icon name
1 parent 3b68b94 commit 0acbc79

File tree

8 files changed

+51
-35
lines changed

8 files changed

+51
-35
lines changed

lua/nvim-tree.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,6 @@ function M.setup(conf)
736736
require("nvim-tree.buffers").setup(opts)
737737
require("nvim-tree.help").setup(opts)
738738
require("nvim-tree.watcher").setup(opts)
739-
if M.config.renderer.icons.show.file and pcall(require, "nvim-web-devicons") then
740-
require("nvim-web-devicons").setup()
741-
end
742739

743740
setup_autocommands(opts)
744741

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
local git_utils = require("nvim-tree.git.utils")
2-
local icons = require("nvim-tree.renderer.components.icons")
32
local utils = require("nvim-tree.utils")
43

54
local DirectoryNode = require("nvim-tree.node.directory")
6-
local Node = require("nvim-tree.node")
75

86
---@class (exact) DirectoryLinkNode: DirectoryNode
97
---@field link_to string absolute path
@@ -49,7 +47,7 @@ end
4947
---@return HighlightedString name
5048
function DirectoryLinkNode:highlighted_icon()
5149
if not self.explorer.opts.renderer.icons.show.folder then
52-
return Node.highlighted_icon(self)
50+
return self:highlighted_icon_empty()
5351
end
5452

5553
local str, hl
@@ -72,7 +70,7 @@ function DirectoryLinkNode:highlighted_name()
7270

7371
if self.explorer.opts.renderer.symlink_destination then
7472
local link_to = utils.path_relative(self.link_to, self.explorer.absolute_path)
75-
name.str = string.format("%s%s%s", name.str, icons.i.symlink_arrow, link_to)
73+
name.str = string.format("%s%s%s", name.str, self.explorer.opts.renderer.icons.symlink_arrow, link_to)
7674
name.hl = { "NvimTreeSymlinkFolderName" }
7775
end
7876

lua/nvim-tree/node/directory.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ end
211211
---@return HighlightedString icon
212212
function DirectoryNode:highlighted_icon()
213213
if not self.explorer.opts.renderer.icons.show.folder then
214-
return Node.highlighted_icon(self)
214+
return self:highlighted_icon_empty()
215215
end
216216

217217
local str, hl

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
local git_utils = require("nvim-tree.git.utils")
2-
local icons = require("nvim-tree.renderer.components.icons")
32
local utils = require("nvim-tree.utils")
43

54
local FileNode = require("nvim-tree.node.file")
6-
local Node = require("nvim-tree.node")
75

86
---@class (exact) FileLinkNode: FileNode
97
---@field link_to string absolute path
@@ -44,8 +42,8 @@ end
4442

4543
---@return HighlightedString icon
4644
function FileLinkNode:highlighted_icon()
47-
if not self.explorer.opts.renderer.icons.show.folder then
48-
return Node.highlighted_icon(self)
45+
if not self.explorer.opts.renderer.icons.show.file then
46+
return self:highlighted_icon_empty()
4947
end
5048

5149
local str, hl
@@ -68,21 +66,6 @@ function FileLinkNode:highlighted_name()
6866
return { str = str, hl = { "NvimTreeSymlink" } }
6967
end
7068

71-
---Icon and name for the file link
72-
---@return HighlightedString icon
73-
---@return HighlightedString name
74-
function FileLinkNode:icon_name()
75-
local icon_str = icons.i.symlink
76-
77-
local name_str = self.name
78-
if self.explorer.opts.renderer.symlink_destination then
79-
local link_to = utils.path_relative(self.link_to, self.explorer.absolute_path)
80-
name_str = string.format("%s%s%s", name_str, icons.i.symlink_arrow, link_to)
81-
end
82-
83-
return { str = icon_str, hl = { "NvimTreeSymlinkIcon" } }, { str = name_str, hl = { "NvimTreeSymlink" } }
84-
end
85-
8669
---Create a sanitized partial copy of a node
8770
---@return FileLinkNode cloned
8871
function FileLinkNode:clone()

lua/nvim-tree/node/file.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,21 @@ end
6969
---@return HighlightedString icon
7070
function FileNode:highlighted_icon()
7171
if not self.explorer.opts.renderer.icons.show.file then
72-
return Node.highlighted_icon(self)
72+
return self:highlighted_icon_empty()
7373
end
7474

7575
local str, hl
7676

7777
-- devicon if enabled and available
7878
if self.explorer.opts.renderer.icons.web_devicons.file.enable then
7979
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
8087
if not self.explorer.opts.renderer.icons.web_devicons.file.color then
8188
hl = nil
8289
end

lua/nvim-tree/node/init.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,32 @@ function Node:get_parent_of_group()
6868
end
6969
end
7070

71+
---Empty highlighted icon
72+
---@protected
73+
---@return HighlightedString icon
74+
function Node:highlighted_icon_empty()
75+
return { str = "", hl = {} }
76+
end
77+
7178
---Highlighted icon for the node
7279
---Empty for base Node
7380
---@return HighlightedString icon
7481
function Node:highlighted_icon()
82+
return self:highlighted_icon_empty()
83+
end
84+
85+
---Empty highlighted name
86+
---@protected
87+
---@return HighlightedString name
88+
function Node:highlighted_name_empty()
7589
return { str = "", hl = {} }
7690
end
7791

7892
---Highlighted name for the node
7993
---Empty for base Node
8094
---@return HighlightedString icon
8195
function Node:highlighted_name()
82-
return { str = "", hl = {} }
96+
return self:highlighted_name_empty()
8397
end
8498

8599
---Create a sanitized partial copy of a node, populating children recursively.

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ local M = {
1313
devicons = nil,
1414
}
1515

16-
---Wrapper around nvim-web-devicons, nils if not present
16+
---Wrapper around nvim-web-devicons, nils if devicons not available
1717
---@param name string
1818
---@return string? icon
1919
---@return string? hl_group
@@ -25,10 +25,27 @@ function M.get_icon(name)
2525
end
2626
end
2727

28-
function M.setup()
29-
local devicons_ok, devicons = pcall(require, "nvim-web-devicons")
30-
if devicons_ok then
31-
M.devicons = devicons
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+
38+
---Attempt to use nvim-web-devicons if present and enabled for file or folder
39+
---@param opts table
40+
function M.setup(opts)
41+
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
45+
46+
-- does nothing if already called i.e. don't clobber previous user setup
47+
M.devicons.setup()
48+
end
3249
end
3350
end
3451

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()
11+
M.icons.setup(opts)
1212
M.padding.setup(opts)
1313
end
1414

0 commit comments

Comments
 (0)