Skip to content

Commit 2ad6125

Browse files
committed
icon placement before & after
1 parent 00d3739 commit 2ad6125

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

lua/nvim-tree/modified.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
local M = {}
22

33
---@type table<string, boolean> record of which file is modified
4-
M.record = {}
4+
M._record = {}
55

66
---refresh M.record
77
function M.reload()
8-
M.record = {}
8+
M._record = {}
99
local bufs = vim.fn.getbufinfo { bufmodified = true, buflisted = true }
1010
for _, buf in pairs(bufs) do
1111
local path = buf.name
12-
M.record[path] = true
12+
M._record[path] = true
1313
while path ~= vim.fn.getcwd() and path ~= "/" do
1414
path = vim.fn.fnamemodify(path, ":h")
15-
M.record[path] = true
15+
M._record[path] = true
1616
end
1717
end
1818
end
@@ -21,7 +21,7 @@ end
2121
---@return boolean
2222
function M.is_modified(node)
2323
return M.config.enable
24-
and M.record[node.absolute_path]
24+
and M._record[node.absolute_path]
2525
and (not node.nodes or M.config.show_on_dirs)
2626
and (not node.open or M.config.show_on_open_dirs)
2727
end

lua/nvim-tree/renderer/builder.lua

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,39 @@ function Builder:_unwrap_git_data(git_icons_and_hl_groups, offset)
116116
return icon
117117
end
118118

119+
---return modified icon if node is modified, otherwise return empty string
120+
---@param node table
121+
---@return string modified icon
122+
function Builder:_get_modified_icon(node)
123+
if not modified.is_modified(node) or self.modified.placement == "signcolumn" then
124+
return ""
125+
end
126+
127+
return self.modified.placement == "before" and self.modified.icon .. " " or " " .. self.modified.icon
128+
end
129+
119130
function Builder:_build_folder(node, padding, git_hl, git_icons_tbl)
120131
local offset = string.len(padding)
121132

122-
local name = get_folder_name(node)
123-
if modified.is_modified(node) then
124-
name = name .. self.modified.icon
125-
end
126133
local has_children = #node.nodes ~= 0 or node.has_children
127134
local icon = icons.get_folder_icon(node.open, node.link_to ~= nil, has_children)
128135

129-
local foldername = name .. self.trailing_slash
136+
local foldername = get_folder_name(node) .. self.trailing_slash
130137
if node.link_to and self.symlink_destination then
131138
local arrow = icons.i.symlink_arrow
132139
local link_to = utils.path_relative(node.link_to, core.get_cwd())
133140
foldername = foldername .. arrow .. link_to
134141
end
135142

136143
local git_icons = self:_unwrap_git_data(git_icons_tbl, offset + #icon + (self.is_git_after and #foldername + 1 or 0))
137-
local fname_starts_at = offset + #icon + (self.is_git_after and 0 or #git_icons)
138-
local line = self:_format_line(padding .. icon, foldername, git_icons)
144+
local modified_icon = self:_get_modified_icon(node)
145+
146+
-- TODO: this is duplicated logic with _build_file & _build_symlink
147+
local fname_starts_at = offset
148+
+ #icon
149+
+ (self.is_git_after and 0 or #git_icons)
150+
+ (self.modified.placement ~= "before" and 0 or #modified_icon)
151+
local line = self:_format_line(padding .. icon, foldername, git_icons, modified_icon)
139152
self:_insert_line(line)
140153

141154
if #icon > 0 then
@@ -162,13 +175,24 @@ function Builder:_build_folder(node, padding, git_hl, git_icons_tbl)
162175
end
163176
end
164177

165-
function Builder:_format_line(before, after, git_icons)
178+
---format line
179+
---@param before string
180+
---@param after string
181+
---@param git_icons string|nil
182+
---@param modified_icon string
183+
---@return string
184+
function Builder:_format_line(before, after, git_icons, modified_icon)
166185
git_icons = self.is_git_after and git_icons and " " .. git_icons or git_icons
186+
187+
local is_modified_after = self.modified.placement ~= "before"
188+
167189
return string.format(
168-
"%s%s%s%s",
190+
"%s%s%s%s%s%s",
169191
before,
170192
self.is_git_after and "" or git_icons,
193+
is_modified_after and "" or modified_icon,
171194
after,
195+
is_modified_after and modified_icon or "",
172196
self.is_git_after and git_icons or ""
173197
)
174198
end
@@ -188,9 +212,14 @@ function Builder:_build_symlink(node, padding, git_highlight, git_icons_tbl)
188212

189213
local git_icons_starts_at = offset + #icon + (self.is_git_after and #symlink_formatted + 1 or 0)
190214
local git_icons = self:_unwrap_git_data(git_icons_tbl, git_icons_starts_at)
191-
local line = self:_format_line(padding .. icon, symlink_formatted, git_icons)
215+
local modified_icon = self:_get_modified_icon(node)
216+
local line = self:_format_line(padding .. icon, symlink_formatted, git_icons, modified_icon)
192217

193-
self:_insert_highlight(link_highlight, offset + (self.is_git_after and 0 or #git_icons), string.len(line))
218+
self:_insert_highlight(
219+
link_highlight,
220+
offset + (self.is_git_after and 0 or #git_icons) + (self.modified.placement ~= "before" and 0 or #modified_icon),
221+
string.len(line)
222+
)
194223
self:_insert_line(line)
195224
end
196225

@@ -226,14 +255,12 @@ function Builder:_build_file(node, padding, git_highlight, git_icons_tbl, unload
226255
local git_icons_starts_at = offset + #icon + (self.is_git_after and #node.name + 1 or 0)
227256
local git_icons = self:_unwrap_git_data(git_icons_tbl, git_icons_starts_at)
228257

229-
local name = node.name
230-
if modified.is_modified(node) then
231-
name = name .. self.modified.icon
232-
end
233-
self:_insert_line(self:_format_line(padding .. icon, name, git_icons))
258+
local modified_icon = self:_get_modified_icon(node)
259+
self:_insert_line(self:_format_line(padding .. icon, node.name, git_icons, modified_icon))
234260

235261
local git_icons_length = self.is_git_after and 0 or #git_icons
236-
local col_start = offset + #icon + git_icons_length
262+
local modified_icon_length = self.modified.placement ~= "before" and 0 or #modified_icon
263+
local col_start = offset + #icon + git_icons_length + modified_icon_length
237264
local col_end = col_start + #node.name
238265

239266
if vim.tbl_contains(self.special_files, node.absolute_path) or vim.tbl_contains(self.special_files, node.name) then

0 commit comments

Comments
 (0)