Skip to content

Commit 0d6c0dd

Browse files
committed
refactor(renderer): remove set_folder_hl function from icons
This greatly simplifies the folder highlighting logic. It wasn't clear before and the code was probably doing more than it should. The logic with highlight_opened_file wasn't working at all, so i just removed it. This should allow us to greatly extend the order in which the components are rendered.
1 parent d8fe48a commit 0d6c0dd

File tree

2 files changed

+15
-42
lines changed

2 files changed

+15
-42
lines changed

lua/nvim-tree/renderer/builder.lua

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local icons = require "nvim-tree.renderer.components.icons"
66

77
-- TODO(refactor): the builder abstraction is not perfect yet. We shouldn't leak data in components.
88
-- Components should return only and icon / highlight group pair at most.
9-
-- The code was mostly moved from renderer/init.lua and rearranged, so it's still under construction.
9+
-- Only missing git refactoring
1010

1111
local Builder = {}
1212
Builder.__index = Builder
@@ -84,39 +84,28 @@ function Builder:_build_folder(node, padding, git_hl)
8484
local has_children = #node.nodes ~= 0 or node.has_children
8585
local icon = icons.get_folder_icon(node.open, node.link_to ~= nil, has_children)
8686
local git_icon = git.get_icons(node, self.index, offset, #icon, self.highlights) or ""
87+
local line = padding .. icon .. git_icon .. name .. self.trailing_slash
8788

88-
local folder_hl = "NvimTreeFolderName"
89+
self:_insert_line(line)
90+
91+
if #icon > 0 then
92+
self:_insert_highlight("NvimTreeFolderIcon", offset, offset + #icon)
93+
end
94+
95+
local foldername_hl = "NvimTreeFolderName"
8996
if self.special_map[node.absolute_path] then
90-
folder_hl = "NvimTreeSpecialFolderName"
97+
foldername_hl = "NvimTreeSpecialFolderName"
9198
elseif node.open then
92-
folder_hl = "NvimTreeOpenedFolderName"
99+
foldername_hl = "NvimTreeOpenedFolderName"
93100
elseif not has_children then
94-
folder_hl = "NvimTreeEmptyFolderName"
101+
foldername_hl = "NvimTreeEmptyFolderName"
95102
end
96103

97-
icons.set_folder_hl(
98-
self.index,
99-
offset,
100-
#icon + #git_icon,
101-
#name,
102-
"NvimTreeFolderIcon",
103-
folder_hl,
104-
self.highlights,
105-
self.open_file_highlight
106-
)
104+
self:_insert_highlight(foldername_hl, offset + #icon + #git_icon, #line)
105+
107106
if git_hl then
108-
icons.set_folder_hl(
109-
self.index,
110-
offset,
111-
#icon + #git_icon,
112-
#name,
113-
git_hl,
114-
git_hl,
115-
self.highlights,
116-
self.open_file_highlight
117-
)
107+
self:_insert_highlight(git_hl, offset + #icon + #git_icon, #line)
118108
end
119-
self:_insert_line(padding .. icon .. git_icon .. name .. self.trailing_slash)
120109
end
121110

122111
-- TODO: missing git icon for symlinks

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ local function empty()
1111
return ""
1212
end
1313

14-
local function get_trailing_length()
15-
return vim.g.nvim_tree_add_trailing and 1 or 0
16-
end
17-
18-
local function set_folder_hl_default(line, depth, git_icon_len, _, hl_group, _, hl)
19-
table.insert(hl, { hl_group, line, depth + git_icon_len, -1 })
20-
end
21-
2214
local function get_folder_icon(open, is_symlink, has_children)
2315
local n
2416
if is_symlink and open then
@@ -41,12 +33,6 @@ local function get_folder_icon(open, is_symlink, has_children)
4133
return n .. M.padding
4234
end
4335

44-
local function set_folder_hl(line, depth, icon_len, name_len, hl_icongroup, hl_fnamegroup, hl, should_hl_opened_files)
45-
local hl_icon = should_hl_opened_files and hl_icongroup or "NvimTreeFolderIcon"
46-
table.insert(hl, { hl_icon, line, depth, depth + icon_len })
47-
table.insert(hl, { hl_fnamegroup, line, depth + icon_len, depth + icon_len + name_len + get_trailing_length() })
48-
end
49-
5036
local function get_file_icon_default()
5137
local hl_group = "NvimTreeFileIcon"
5238
local icon = M.icons.default
@@ -95,10 +81,8 @@ end
9581
local function config_folder_icon()
9682
if M.configs.show_folder_icon then
9783
M.get_folder_icon = get_folder_icon
98-
M.set_folder_hl = set_folder_hl
9984
else
10085
M.get_folder_icon = empty
101-
M.set_folder_hl = set_folder_hl_default
10286
end
10387
end
10488

0 commit comments

Comments
 (0)