Skip to content

Commit a1600e5

Browse files
authored
feat(renderer): allow placing git icons after filename (#1203)
This feature allows placing git icons after the filename.
1 parent ec888d0 commit a1600e5

File tree

6 files changed

+74
-48
lines changed

6 files changed

+74
-48
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
154154
},
155155
icons = {
156156
webdev_colors = true,
157-
},
157+
git_placement = "before",
158+
}
158159
},
159160
hijack_directories = {
160161
enable = true,

doc/nvim-tree-lua.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ Values may be functions. Warning: this may result in unexpected behaviour.
123123
},
124124
icons = {
125125
webdev_colors = true,
126-
},
126+
git_placement = "before",
127+
}
127128
},
128129
hijack_directories = {
129130
enable = true,
@@ -444,6 +445,11 @@ Here is a list of the options available in the setup call:
444445
type: `boolean`
445446
default: `true`
446447

448+
- |renderer.icons.git_placement|: place where the git icons will be rendered.
449+
After or before the `filename` (after the file/folders icons).
450+
type: `after` or `before`
451+
default: `before`
452+
447453
*nvim-tree.filters*
448454
|filters|: filtering options
449455

lua/nvim-tree.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
351351
},
352352
icons = {
353353
webdev_colors = true,
354+
git_placement = "before",
354355
},
355356
},
356357
hijack_directories = {

lua/nvim-tree/renderer/builder.lua

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ function Builder:configure_git_icons_padding(padding)
6060
return self
6161
end
6262

63+
function Builder:configure_git_icons_placement(where)
64+
where = where or "before"
65+
self.is_git_before = where == "before"
66+
self.is_git_after = not self.is_git_before
67+
return self
68+
end
69+
6370
function Builder:_insert_highlight(group, start, end_)
6471
table.insert(self.highlights, { group, self.index, start, end_ or -1 })
6572
end
@@ -93,16 +100,17 @@ function Builder:_unwrap_git_data(git_icons_and_hl_groups, offset)
93100
return icon
94101
end
95102

96-
function Builder:_build_folder(node, padding, git_hl)
103+
function Builder:_build_folder(node, padding, git_hl, git_icons_tbl)
97104
local offset = string.len(padding)
98105

99106
local name = get_folder_name(node)
100107
local has_children = #node.nodes ~= 0 or node.has_children
101108
local icon = icons.get_folder_icon(node.open, node.link_to ~= nil, has_children)
102-
local git_icon = self:_unwrap_git_data(git.get_icons(node), offset + #icon)
103-
104-
local line = padding .. icon .. git_icon .. name .. self.trailing_slash
105109

110+
local foldername = name .. self.trailing_slash
111+
local git_icons = self:_unwrap_git_data(git_icons_tbl, offset + #icon + (self.is_git_after and #foldername + 1 or 0))
112+
local fname_starts_at = offset + #icon + (self.is_git_before and #git_icons or 0)
113+
local line = self:_format_line(padding .. icon, foldername, git_icons)
106114
self:_insert_line(line)
107115

108116
if #icon > 0 then
@@ -118,76 +126,93 @@ function Builder:_build_folder(node, padding, git_hl)
118126
foldername_hl = "NvimTreeEmptyFolderName"
119127
end
120128

121-
self:_insert_highlight(foldername_hl, offset + #icon + #git_icon, #line)
129+
self:_insert_highlight(foldername_hl, fname_starts_at, fname_starts_at + #foldername)
122130

123131
if git_hl then
124-
self:_insert_highlight(git_hl, offset + #icon + #git_icon, #line)
132+
self:_insert_highlight(git_hl, fname_starts_at, fname_starts_at + #foldername)
125133
end
126134
end
127135

128-
-- TODO: missing git icon for symlinks
129-
function Builder:_build_symlink(node, padding, git_highlight)
136+
function Builder:_format_line(before, after, git_icons)
137+
return string.format(
138+
"%s%s%s %s",
139+
before,
140+
self.is_git_after and "" or git_icons,
141+
after,
142+
self.is_git_after and git_icons or ""
143+
)
144+
end
145+
146+
function Builder:_build_symlink(node, padding, git_highlight, git_icons_tbl)
147+
local offset = string.len(padding)
148+
130149
local icon = icons.i.symlink
131150
local arrow = icons.i.symlink_arrow
151+
local symlink_formatted = node.name .. arrow .. node.link_to
132152

133153
local link_highlight = git_highlight or "NvimTreeSymlink"
134154

135-
local line = padding .. icon .. node.name .. arrow .. node.link_to
136-
self:_insert_highlight(link_highlight, string.len(padding), string.len(line))
155+
local git_icons_starts_at = offset + #icon + (self.is_git_after and #symlink_formatted + 1 or 0)
156+
local git_icons = self:_unwrap_git_data(git_icons_tbl, git_icons_starts_at)
157+
local line = self:_format_line(padding .. icon, symlink_formatted, git_icons)
158+
159+
self:_insert_highlight(link_highlight, offset + (self.is_git_after and 0 or #git_icons), string.len(line))
137160
self:_insert_line(line)
138161
end
139162

140-
function Builder:_build_file_icons(node, offset)
141-
if self.special_map[node.absolute_path] or self.special_map[node.name] then
142-
local git_icons = self:_unwrap_git_data(git.get_icons(node), offset + #icons.i.special)
143-
self:_insert_highlight("NvimTreeSpecialFile", offset + #git_icons)
144-
return icons.i.special, git_icons
145-
else
146-
local icon, hl_group = icons.get_file_icon(node.name, node.extension)
147-
if hl_group then
148-
self:_insert_highlight(hl_group, offset, offset + #icon)
149-
end
150-
return icon, self:_unwrap_git_data(git.get_icons(node), offset + #icon)
163+
function Builder:_build_file_icon(node, offset)
164+
local icon, hl_group = icons.get_file_icon(node.name, node.extension)
165+
if hl_group then
166+
self:_insert_highlight(hl_group, offset, offset + #icon)
151167
end
168+
return icon, false
152169
end
153170

154-
function Builder:_highlight_opened_files(node, offset, icon, git_icons)
171+
function Builder:_highlight_opened_files(node, offset, icon_length, git_icons_length)
155172
local from = offset
156173
local to = offset
157174

158175
if self.open_file_highlight == "icon" then
159-
to = from + #icon
176+
to = from + icon_length
160177
elseif self.open_file_highlight == "name" then
161-
from = offset + #icon + #git_icons
178+
from = offset + icon_length + git_icons_length
162179
to = from + #node.name
163180
elseif self.open_file_highlight == "all" then
164-
to = -1
181+
to = from + icon_length + git_icons_length + #node.name
165182
end
166183

167184
self:_insert_highlight("NvimTreeOpenedFile", from, to)
168185
end
169186

170-
function Builder:_build_file(node, padding, git_highlight)
187+
function Builder:_build_file(node, padding, git_highlight, git_icons_tbl)
171188
local offset = string.len(padding)
172189

173-
local icon, git_icons = self:_build_file_icons(node, offset)
190+
local icon = self:_build_file_icon(node, offset)
174191

175-
self:_insert_line(padding .. icon .. git_icons .. node.name)
176-
local col_start = offset + #icon + #git_icons
192+
local git_icons_starts_at = offset + #icon + (self.is_git_after and #node.name + 1 or 0)
193+
local git_icons = self:_unwrap_git_data(git_icons_tbl, git_icons_starts_at)
177194

178-
if node.executable then
179-
self:_insert_highlight("NvimTreeExecFile", col_start)
195+
self:_insert_line(self:_format_line(padding .. icon, node.name, git_icons))
196+
197+
local git_icons_length = self.is_git_after and 0 or #git_icons
198+
local col_start = offset + #icon + git_icons_length
199+
local col_end = col_start + #node.name
200+
201+
if self.special_map[node.absolute_path] or self.special_map[node.name] then
202+
self:_insert_highlight("NvimTreeSpecialFile", col_start, col_end)
203+
elseif node.executable then
204+
self:_insert_highlight("NvimTreeExecFile", col_start, col_end)
180205
elseif self.picture_map[node.extension] then
181-
self:_insert_highlight("NvimTreeImageFile", col_start)
206+
self:_insert_highlight("NvimTreeImageFile", col_start, col_end)
182207
end
183208

184209
local should_highlight_opened_files = self.open_file_highlight and vim.fn.bufloaded(node.absolute_path) > 0
185210
if should_highlight_opened_files then
186-
self:_highlight_opened_files(node, offset, icon, git_icons)
211+
self:_highlight_opened_files(node, offset, #icon, git_icons_length)
187212
end
188213

189214
if git_highlight then
190-
self:_insert_highlight(git_highlight, col_start)
215+
self:_insert_highlight(git_highlight, col_start, col_end)
191216
end
192217
end
193218

@@ -199,16 +224,17 @@ function Builder:_build_line(tree, node, idx)
199224
end
200225

201226
local git_highlight = git.get_highlight(node)
227+
local git_icons_tbl = git.get_icons(node)
202228

203229
local is_folder = node.nodes ~= nil
204230
local is_symlink = node.link_to ~= nil
205231

206232
if is_folder then
207-
self:_build_folder(node, padding, git_highlight)
233+
self:_build_folder(node, padding, git_highlight, git_icons_tbl)
208234
elseif is_symlink then
209-
self:_build_symlink(node, padding, git_highlight)
235+
self:_build_symlink(node, padding, git_highlight, git_icons_tbl)
210236
else
211-
self:_build_file(node, padding, git_highlight)
237+
self:_build_file(node, padding, git_highlight, git_icons_tbl)
212238
end
213239
self.index = self.index + 1
214240

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,6 @@ local function config_file_icon()
7070
end
7171
end
7272

73-
local function config_special_icon()
74-
if M.configs.show_file_icon then
75-
M.i.special = #M.icons.default > 0 and M.icons.default .. M.padding or ""
76-
else
77-
M.i.special = ""
78-
end
79-
end
80-
8173
local function config_folder_icon()
8274
if M.configs.show_folder_icon then
8375
M.get_folder_icon = get_folder_icon
@@ -94,7 +86,6 @@ function M.reset_config(webdev_colors)
9486
M.webdev_colors = webdev_colors
9587

9688
config_symlinks()
97-
config_special_icon()
9889
config_file_icon()
9990
config_folder_icon()
10091
end

lua/nvim-tree/renderer/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ function M.draw()
8282
:configure_picture_map(picture_map)
8383
:configure_opened_file_highlighting(vim.g.nvim_tree_highlight_opened_files)
8484
:configure_git_icons_padding(vim.g.nvim_tree_icon_padding)
85+
:configure_git_icons_placement(M.config.icons.git_placement)
8586
:build_header(view.is_root_folder_visible(core.get_cwd()))
8687
:build(core.get_explorer())
8788
:unwrap()

0 commit comments

Comments
 (0)