Skip to content

Commit 18272f8

Browse files
authored
fix(view): refresh opened files highlight on buffer read, unload (#1827)
1 parent e8ea62c commit 18272f8

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

lua/nvim-tree.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,15 @@ local function setup_autocommands(opts)
356356

357357
create_nvim_tree_autocmd("BufReadPost", {
358358
callback = function()
359-
if filters.config.filter_no_buffer then
359+
if filters.config.filter_no_buffer or renderer.config.highlight_opened_files then
360360
reloaders.reload_explorer()
361361
end
362362
end,
363363
})
364364

365365
create_nvim_tree_autocmd("BufUnload", {
366366
callback = function(data)
367-
if filters.config.filter_no_buffer then
367+
if filters.config.filter_no_buffer or renderer.config.highlight_opened_files then
368368
reloaders.reload_explorer(nil, data.buf)
369369
end
370370
end,

lua/nvim-tree/actions/reloaders/reloaders.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function M.reload_explorer(_, unloaded_bufnr)
4444
local projects = git.reload()
4545
refresh_nodes(core.get_explorer(), projects, unloaded_bufnr)
4646
if view.is_visible() then
47-
renderer.draw()
47+
renderer.draw(unloaded_bufnr)
4848
end
4949
event_running = false
5050
end

lua/nvim-tree/renderer/builder.lua

+9-7
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function Builder:_highlight_opened_files(node, offset, icon_length, git_icons_le
206206
self:_insert_highlight("NvimTreeOpenedFile", from, to)
207207
end
208208

209-
function Builder:_build_file(node, padding, git_highlight, git_icons_tbl)
209+
function Builder:_build_file(node, padding, git_highlight, git_icons_tbl, unloaded_bufnr)
210210
local offset = string.len(padding)
211211

212212
local icon = self:_build_file_icon(node, offset)
@@ -228,7 +228,9 @@ function Builder:_build_file(node, padding, git_highlight, git_icons_tbl)
228228
self:_insert_highlight("NvimTreeImageFile", col_start, col_end)
229229
end
230230

231-
local should_highlight_opened_files = self.highlight_opened_files and vim.fn.bufloaded(node.absolute_path) > 0
231+
local should_highlight_opened_files = self.highlight_opened_files
232+
and vim.fn.bufloaded(node.absolute_path) > 0
233+
and vim.fn.bufnr(node.absolute_path) ~= unloaded_bufnr
232234
if should_highlight_opened_files then
233235
self:_highlight_opened_files(node, offset, #icon, git_icons_length)
234236
end
@@ -238,7 +240,7 @@ function Builder:_build_file(node, padding, git_highlight, git_icons_tbl)
238240
end
239241
end
240242

241-
function Builder:_build_line(node, idx, num_children)
243+
function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
242244
local padding = pad.get_padding(self.depth, idx, num_children, node, self.markers)
243245

244246
if string.len(padding) > 0 then
@@ -262,13 +264,13 @@ function Builder:_build_line(node, idx, num_children)
262264
elseif is_symlink then
263265
self:_build_symlink(node, padding, git_highlight, git_icons_tbl)
264266
else
265-
self:_build_file(node, padding, git_highlight, git_icons_tbl)
267+
self:_build_file(node, padding, git_highlight, git_icons_tbl, unloaded_bufnr)
266268
end
267269
self.index = self.index + 1
268270

269271
if node.open then
270272
self.depth = self.depth + 1
271-
self:build(node)
273+
self:build(node, unloaded_bufnr)
272274
self.depth = self.depth - 1
273275
end
274276
end
@@ -287,12 +289,12 @@ function Builder:_get_nodes_number(nodes)
287289
return i
288290
end
289291

290-
function Builder:build(tree)
292+
function Builder:build(tree, unloaded_bufnr)
291293
local num_children = self:_get_nodes_number(tree.nodes)
292294
local idx = 1
293295
for _, node in ipairs(tree.nodes) do
294296
if not node.hidden then
295-
self:_build_line(node, idx, num_children)
297+
self:_build_line(node, idx, num_children, unloaded_bufnr)
296298
idx = idx + 1
297299
end
298300
end

lua/nvim-tree/renderer/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ local picture_map = {
4646
gif = true,
4747
}
4848

49-
function M.draw()
49+
function M.draw(unloaded_bufnr)
5050
local bufnr = view.get_bufnr()
5151
if not core.get_explorer() or not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
5252
return
@@ -73,7 +73,7 @@ function M.draw()
7373
:configure_symlink_destination(M.config.symlink_destination)
7474
:configure_filter(live_filter.filter, live_filter.prefix)
7575
:build_header(view.is_root_folder_visible(core.get_cwd()))
76-
:build(core.get_explorer())
76+
:build(core.get_explorer(), unloaded_bufnr)
7777
:unwrap()
7878
end
7979

0 commit comments

Comments
 (0)