Skip to content

Commit b144b33

Browse files
feat(#2369): add full renderer.icons.web_devicons options for file and folder (#2375)
* Add `webdev_colors_folder` option * Check if `M.devicons` exists * Refactor `get_folder_icon` * Add configuration options for both files and folders * web_devicons.*.enabled -> enable * silent migration: renderer.icons.webdev_colors -> renderer.icons.web_devicons.file.color * silent migration: renderer.icons.webdev_colors -> renderer.icons.web_devicons.file.color --------- Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent d11d701 commit b144b33

File tree

5 files changed

+87
-17
lines changed

5 files changed

+87
-17
lines changed

doc/nvim-tree-lua.txt

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,16 @@ applying configuration.
362362
},
363363
},
364364
icons = {
365-
webdev_colors = true,
365+
web_devicons = {
366+
file = {
367+
enable = true,
368+
color = true,
369+
},
370+
folder = {
371+
enable = false,
372+
color = true,
373+
},
374+
},
366375
git_placement = "before",
367376
modified_placement = "after",
368377
padding = " ",
@@ -970,9 +979,32 @@ UI rendering setup
970979
*nvim-tree.renderer.icons*
971980
Configuration options for icons.
972981

973-
*nvim-tree.renderer.icons.webdev_colors*
974-
Use the webdev icon colors, otherwise `NvimTreeFileIcon`.
975-
Type: `boolean`, Default: `true`
982+
*nvim-tree.renderer.icons.web_devicons*
983+
Configure optional plugin `"nvim-tree/nvim-web-devicons"`
984+
985+
*nvim-tree.renderer.icons.web_devicons.file*
986+
File icons.
987+
988+
*nvim-tree.renderer.icons.web_devicons.file.enable*
989+
Show icons on files.
990+
Overrides |nvim-tree.renderer.icons.glyphs.default|
991+
Type: `boolean`, Default: `true`
992+
993+
*nvim-tree.renderer.icons.web_devicons.file.color*
994+
Use icon colors for files.
995+
Type: `boolean`, Default: `true`
996+
997+
*nvim-tree.renderer.icons.web_devicons.folder*
998+
Folder icons.
999+
1000+
*nvim-tree.renderer.icons.web_devicons.folder.enable*
1001+
Show icons on folders.
1002+
Overrides |nvim-tree.renderer.icons.glyphs.folder|
1003+
Type: `boolean`, Default: `false`
1004+
1005+
*nvim-tree.renderer.icons.web_devicons.folder.color*
1006+
Use icon colors for folders.
1007+
Type: `boolean`, Default: `true`
9761008

9771009
*nvim-tree.renderer.icons.git_placement*
9781010
Place where the git icons will be rendered.
@@ -999,7 +1031,7 @@ UI rendering setup
9991031
Configuration options for showing icon types.
10001032

10011033
*nvim-tree.renderer.icons.show.file*
1002-
Show an icon before the file name. `nvim-web-devicons` will be used if available.
1034+
Show an icon before the file name.
10031035
Type: `boolean`, Default: `true`
10041036

10051037
*nvim-tree.renderer.icons.show.folder*
@@ -1027,7 +1059,8 @@ UI rendering setup
10271059
to appear in the signcolumn.
10281060

10291061
*nvim-tree.renderer.icons.glyphs.default*
1030-
Glyph for files. Will be overridden by `nvim-web-devicons` if available.
1062+
Glyph for files.
1063+
Overridden by |nvim-tree.renderer.icons.web_devicons| if available.
10311064
Type: `string`, Default: `""`
10321065

10331066
*nvim-tree.renderer.icons.glyphs.symlink*
@@ -1040,6 +1073,7 @@ UI rendering setup
10401073

10411074
*nvim-tree.renderer.icons.glyphs.folder*
10421075
Glyphs for directories.
1076+
Overridden by |nvim-tree.renderer.icons.web_devicons| if available.
10431077
Type: `table`, Default:
10441078
`{`
10451079
`arrow_closed = "",`

lua/nvim-tree.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,16 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
426426
},
427427
},
428428
icons = {
429-
webdev_colors = true,
429+
web_devicons = {
430+
file = {
431+
enable = true,
432+
color = true,
433+
},
434+
folder = {
435+
enable = false,
436+
color = true,
437+
},
438+
},
430439
git_placement = "before",
431440
modified_placement = "after",
432441
padding = " ",

lua/nvim-tree/legacy.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ local function refactored(opts)
3838

3939
-- 2023/07/16
4040
utils.move_missing_val(opts, "git", "ignore", opts, "filters", "git_ignored", true)
41+
42+
-- 2023/08/26
43+
utils.move_missing_val(
44+
opts,
45+
"renderer.icons",
46+
"webdev_colors",
47+
opts,
48+
"renderer.icons.web_devicons.file",
49+
"color",
50+
true
51+
)
4152
end
4253

4354
local function deprecated(opts)

lua/nvim-tree/renderer/builder.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,10 @@ end
130130
---@return HighlightedString icon, HighlightedString name
131131
function Builder:_build_folder(node)
132132
local has_children = #node.nodes ~= 0 or node.has_children
133-
local icon = icons.get_folder_icon(node.open, node.link_to ~= nil, has_children)
133+
local icon, icon_hl = icons.get_folder_icon(node, has_children)
134134
local foldername = get_folder_name(node) .. self.trailing_slash
135135

136-
local icon_hl
137-
if #icon > 0 then
136+
if #icon > 0 and icon_hl == nil then
138137
if node.open then
139138
icon_hl = "NvimTreeOpenedFolderIcon"
140139
else

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ local function empty()
99
return ""
1010
end
1111

12-
local function get_folder_icon(open, is_symlink, has_children)
12+
local function get_folder_icon_default(node, has_children)
13+
local is_symlink = node.links_to ~= nil
1314
local n
14-
if is_symlink and open then
15+
if is_symlink and node.open then
1516
n = M.config.glyphs.folder.symlink_open
1617
elseif is_symlink then
1718
n = M.config.glyphs.folder.symlink
18-
elseif open then
19+
elseif node.open then
1920
if has_children then
2021
n = M.config.glyphs.folder.open
2122
else
@@ -28,7 +29,19 @@ local function get_folder_icon(open, is_symlink, has_children)
2829
n = M.config.glyphs.folder.empty
2930
end
3031
end
31-
return n
32+
return n, nil
33+
end
34+
35+
local function get_folder_icon_webdev(node, has_children)
36+
local icon, hl_group = M.devicons.get_icon(node.name, node.extension)
37+
if not M.config.web_devicons.folder.color then
38+
hl_group = nil
39+
end
40+
if icon ~= nil then
41+
return icon, hl_group
42+
else
43+
return get_folder_icon_default(node, has_children)
44+
end
3245
end
3346

3447
local function get_file_icon_default()
@@ -43,7 +56,7 @@ end
4356

4457
local function get_file_icon_webdev(fname, extension)
4558
local icon, hl_group = M.devicons.get_icon(fname, extension)
46-
if not M.config.webdev_colors then
59+
if not M.config.web_devicons.file.color then
4760
hl_group = "NvimTreeFileIcon"
4861
end
4962
if icon and hl_group ~= "DevIconDefault" then
@@ -58,7 +71,7 @@ end
5871

5972
local function config_file_icon()
6073
if M.config.show.file then
61-
if M.devicons then
74+
if M.devicons and M.config.web_devicons.file.enable then
6275
M.get_file_icon = get_file_icon_webdev
6376
else
6477
M.get_file_icon = get_file_icon_default
@@ -70,7 +83,11 @@ end
7083

7184
local function config_folder_icon()
7285
if M.config.show.folder then
73-
M.get_folder_icon = get_folder_icon
86+
if M.devicons and M.config.web_devicons.folder.enable then
87+
M.get_folder_icon = get_folder_icon_webdev
88+
else
89+
M.get_folder_icon = get_folder_icon_default
90+
end
7491
else
7592
M.get_folder_icon = empty
7693
end

0 commit comments

Comments
 (0)