@@ -60,6 +60,13 @@ function Builder:configure_git_icons_padding(padding)
60
60
return self
61
61
end
62
62
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
+
63
70
function Builder :_insert_highlight (group , start , end_ )
64
71
table.insert (self .highlights , { group , self .index , start , end_ or - 1 })
65
72
end
@@ -93,16 +100,17 @@ function Builder:_unwrap_git_data(git_icons_and_hl_groups, offset)
93
100
return icon
94
101
end
95
102
96
- function Builder :_build_folder (node , padding , git_hl )
103
+ function Builder :_build_folder (node , padding , git_hl , git_icons_tbl )
97
104
local offset = string.len (padding )
98
105
99
106
local name = get_folder_name (node )
100
107
local has_children = # node .nodes ~= 0 or node .has_children
101
108
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
105
109
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 )
106
114
self :_insert_line (line )
107
115
108
116
if # icon > 0 then
@@ -118,76 +126,93 @@ function Builder:_build_folder(node, padding, git_hl)
118
126
foldername_hl = " NvimTreeEmptyFolderName"
119
127
end
120
128
121
- self :_insert_highlight (foldername_hl , offset + # icon + # git_icon , # line )
129
+ self :_insert_highlight (foldername_hl , fname_starts_at , fname_starts_at + # foldername )
122
130
123
131
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 )
125
133
end
126
134
end
127
135
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
+
130
149
local icon = icons .i .symlink
131
150
local arrow = icons .i .symlink_arrow
151
+ local symlink_formatted = node .name .. arrow .. node .link_to
132
152
133
153
local link_highlight = git_highlight or " NvimTreeSymlink"
134
154
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 ))
137
160
self :_insert_line (line )
138
161
end
139
162
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 )
151
167
end
168
+ return icon , false
152
169
end
153
170
154
- function Builder :_highlight_opened_files (node , offset , icon , git_icons )
171
+ function Builder :_highlight_opened_files (node , offset , icon_length , git_icons_length )
155
172
local from = offset
156
173
local to = offset
157
174
158
175
if self .open_file_highlight == " icon" then
159
- to = from + # icon
176
+ to = from + icon_length
160
177
elseif self .open_file_highlight == " name" then
161
- from = offset + # icon + # git_icons
178
+ from = offset + icon_length + git_icons_length
162
179
to = from + # node .name
163
180
elseif self .open_file_highlight == " all" then
164
- to = - 1
181
+ to = from + icon_length + git_icons_length + # node . name
165
182
end
166
183
167
184
self :_insert_highlight (" NvimTreeOpenedFile" , from , to )
168
185
end
169
186
170
- function Builder :_build_file (node , padding , git_highlight )
187
+ function Builder :_build_file (node , padding , git_highlight , git_icons_tbl )
171
188
local offset = string.len (padding )
172
189
173
- local icon , git_icons = self :_build_file_icons (node , offset )
190
+ local icon = self :_build_file_icon (node , offset )
174
191
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 )
177
194
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 )
180
205
elseif self .picture_map [node .extension ] then
181
- self :_insert_highlight (" NvimTreeImageFile" , col_start )
206
+ self :_insert_highlight (" NvimTreeImageFile" , col_start , col_end )
182
207
end
183
208
184
209
local should_highlight_opened_files = self .open_file_highlight and vim .fn .bufloaded (node .absolute_path ) > 0
185
210
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 )
187
212
end
188
213
189
214
if git_highlight then
190
- self :_insert_highlight (git_highlight , col_start )
215
+ self :_insert_highlight (git_highlight , col_start , col_end )
191
216
end
192
217
end
193
218
@@ -199,16 +224,17 @@ function Builder:_build_line(tree, node, idx)
199
224
end
200
225
201
226
local git_highlight = git .get_highlight (node )
227
+ local git_icons_tbl = git .get_icons (node )
202
228
203
229
local is_folder = node .nodes ~= nil
204
230
local is_symlink = node .link_to ~= nil
205
231
206
232
if is_folder then
207
- self :_build_folder (node , padding , git_highlight )
233
+ self :_build_folder (node , padding , git_highlight , git_icons_tbl )
208
234
elseif is_symlink then
209
- self :_build_symlink (node , padding , git_highlight )
235
+ self :_build_symlink (node , padding , git_highlight , git_icons_tbl )
210
236
else
211
- self :_build_file (node , padding , git_highlight )
237
+ self :_build_file (node , padding , git_highlight , git_icons_tbl )
212
238
end
213
239
self .index = self .index + 1
214
240
0 commit comments