Skip to content

Commit ce2cf71

Browse files
committed
refactor(renderer): refactor git handling
Concat and apply git highlight inside the builder. This allows to not leak private data from builder to the git component. This will also now allow us to customize git icon placement.
1 parent 0d6c0dd commit ce2cf71

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

lua/nvim-tree/renderer/builder.lua

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ local git = require "nvim-tree.renderer.components.git"
44
local pad = require "nvim-tree.renderer.components.padding"
55
local icons = require "nvim-tree.renderer.components.icons"
66

7-
-- TODO(refactor): the builder abstraction is not perfect yet. We shouldn't leak data in components.
8-
-- Components should return only and icon / highlight group pair at most.
9-
-- Only missing git refactoring
10-
117
local Builder = {}
128
Builder.__index = Builder
139

@@ -59,6 +55,11 @@ function Builder:configure_opened_file_highlighting(level)
5955
return self
6056
end
6157

58+
function Builder:configure_git_icons_padding(padding)
59+
self.git_icon_padding = padding or " "
60+
return self
61+
end
62+
6263
function Builder:_insert_highlight(group, start, end_)
6364
table.insert(self.highlights, { group, self.index, start, end_ or -1 })
6465
end
@@ -77,13 +78,29 @@ local function get_folder_name(node)
7778
return name
7879
end
7980

81+
function Builder:_unwrap_git_data(git_icons_and_hl_groups, offset)
82+
if not git_icons_and_hl_groups then
83+
return ""
84+
end
85+
86+
local icon = ""
87+
for _, v in ipairs(git_icons_and_hl_groups) do
88+
if #v.icon > 0 then
89+
self:_insert_highlight(v.hl, offset + #icon, offset + #icon + #v.icon)
90+
icon = icon .. v.icon .. self.git_icon_padding
91+
end
92+
end
93+
return icon
94+
end
95+
8096
function Builder:_build_folder(node, padding, git_hl)
8197
local offset = string.len(padding)
8298

8399
local name = get_folder_name(node)
84100
local has_children = #node.nodes ~= 0 or node.has_children
85101
local icon = icons.get_folder_icon(node.open, node.link_to ~= nil, has_children)
86-
local git_icon = git.get_icons(node, self.index, offset, #icon, self.highlights) or ""
102+
local git_icon = self:_unwrap_git_data(git.get_icons(node), offset + #icon)
103+
87104
local line = padding .. icon .. git_icon .. name .. self.trailing_slash
88105

89106
self:_insert_line(line)
@@ -122,15 +139,15 @@ end
122139

123140
function Builder:_build_file_icons(node, offset)
124141
if self.special_map[node.absolute_path] or self.special_map[node.name] then
125-
local git_icons = git.get_icons(node, self.index, offset, 0, self.highlights)
142+
local git_icons = self:_unwrap_git_data(git.get_icons(node), offset + #icons.i.special)
126143
self:_insert_highlight("NvimTreeSpecialFile", offset + #git_icons)
127144
return icons.i.special, git_icons
128145
else
129146
local icon, hl_group = icons.get_file_icon(node.name, node.extension)
130147
if hl_group then
131148
self:_insert_highlight(hl_group, offset, offset + #icon)
132149
end
133-
return icon, git.get_icons(node, self.index, offset, #icon, self.highlights)
150+
return icon, self:_unwrap_git_data(git.get_icons(node), offset + #icon)
134151
end
135152
end
136153

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ local function build_icons_table()
6565
}
6666
end
6767

68-
local function empty()
69-
return ""
70-
end
7168
local function nil_() end
7269

7370
local function warn_status(git_status)
@@ -78,28 +75,21 @@ local function warn_status(git_status)
7875
)
7976
end
8077

81-
local function get_icons_(node, line, depth, icon_len, hl)
78+
local function get_icons_(node)
8279
local git_status = node.git_status
8380
if not git_status then
84-
return ""
81+
return nil
8582
end
8683

87-
local icon = ""
8884
local icons = M.git_icons[git_status]
8985
if not icons then
9086
if vim.g.nvim_tree_git_hl ~= 1 then
9187
warn_status(git_status)
9288
end
93-
return ""
94-
end
95-
for _, v in ipairs(icons) do
96-
if #v.icon > 0 then
97-
table.insert(hl, { v.hl, line, depth + icon_len + #icon, depth + icon_len + #icon + #v.icon })
98-
icon = icon .. v.icon .. M.icon_padding
99-
end
89+
return nil
10090
end
10191

102-
return icon
92+
return icons
10393
end
10494

10595
local git_hl = {
@@ -143,26 +133,20 @@ local function get_highlight_(node)
143133
return git_hl[git_status]
144134
end
145135

146-
function M.get_icons()
147-
return empty()
148-
end
136+
M.get_icons = nil_
137+
M.get_highlight = nil_
149138

150-
function M.get_highlight()
151-
return nil_()
152-
end
153-
154-
M.icon_padding = vim.g.nvim_tree_icon_padding or " "
155139
M.icon_state = _icons.get_config()
156140
M.git_icons = build_icons_table()
157141

158142
function M.reload()
159143
M.icon_state = _icons.get_config()
160-
M.icon_padding = vim.g.nvim_tree_icon_padding or " "
161144
M.git_icons = build_icons_table()
145+
162146
if M.icon_state.show_git_icon then
163147
M.get_icons = get_icons_
164148
else
165-
M.get_icons = empty
149+
M.get_icons = nil_
166150
end
167151
if vim.g.nvim_tree_git_hl == 1 then
168152
M.get_highlight = get_highlight_

lua/nvim-tree/renderer/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function M.draw()
7979
:configure_special_map(get_special_files_map())
8080
:configure_picture_map(picture_map)
8181
:configure_opened_file_highlighting(vim.g.nvim_tree_highlight_opened_files)
82+
:configure_git_icons_padding(vim.g.nvim_tree_icon_padding)
8283
:build_header(view.is_root_folder_visible(core.get_cwd()))
8384
:build(core.get_explorer())
8485
:unwrap()

0 commit comments

Comments
 (0)