fix(view): refresh opened files highlight on buffer read, unload (#1827)

This commit is contained in:
Richard Li 2022-12-16 16:36:00 +13:00 committed by GitHub
parent e8ea62c198
commit 18272f8df3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 12 deletions

View File

@ -356,7 +356,7 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufReadPost", { create_nvim_tree_autocmd("BufReadPost", {
callback = function() callback = function()
if filters.config.filter_no_buffer then if filters.config.filter_no_buffer or renderer.config.highlight_opened_files then
reloaders.reload_explorer() reloaders.reload_explorer()
end end
end, end,
@ -364,7 +364,7 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufUnload", { create_nvim_tree_autocmd("BufUnload", {
callback = function(data) callback = function(data)
if filters.config.filter_no_buffer then if filters.config.filter_no_buffer or renderer.config.highlight_opened_files then
reloaders.reload_explorer(nil, data.buf) reloaders.reload_explorer(nil, data.buf)
end end
end, end,

View File

@ -44,7 +44,7 @@ function M.reload_explorer(_, unloaded_bufnr)
local projects = git.reload() local projects = git.reload()
refresh_nodes(core.get_explorer(), projects, unloaded_bufnr) refresh_nodes(core.get_explorer(), projects, unloaded_bufnr)
if view.is_visible() then if view.is_visible() then
renderer.draw() renderer.draw(unloaded_bufnr)
end end
event_running = false event_running = false
end end

View File

@ -206,7 +206,7 @@ function Builder:_highlight_opened_files(node, offset, icon_length, git_icons_le
self:_insert_highlight("NvimTreeOpenedFile", from, to) self:_insert_highlight("NvimTreeOpenedFile", from, to)
end end
function Builder:_build_file(node, padding, git_highlight, git_icons_tbl) function Builder:_build_file(node, padding, git_highlight, git_icons_tbl, unloaded_bufnr)
local offset = string.len(padding) local offset = string.len(padding)
local icon = self:_build_file_icon(node, offset) local icon = self:_build_file_icon(node, offset)
@ -228,7 +228,9 @@ function Builder:_build_file(node, padding, git_highlight, git_icons_tbl)
self:_insert_highlight("NvimTreeImageFile", col_start, col_end) self:_insert_highlight("NvimTreeImageFile", col_start, col_end)
end end
local should_highlight_opened_files = self.highlight_opened_files and vim.fn.bufloaded(node.absolute_path) > 0 local should_highlight_opened_files = self.highlight_opened_files
and vim.fn.bufloaded(node.absolute_path) > 0
and vim.fn.bufnr(node.absolute_path) ~= unloaded_bufnr
if should_highlight_opened_files then if should_highlight_opened_files then
self:_highlight_opened_files(node, offset, #icon, git_icons_length) self:_highlight_opened_files(node, offset, #icon, git_icons_length)
end end
@ -238,7 +240,7 @@ function Builder:_build_file(node, padding, git_highlight, git_icons_tbl)
end end
end end
function Builder:_build_line(node, idx, num_children) function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
local padding = pad.get_padding(self.depth, idx, num_children, node, self.markers) local padding = pad.get_padding(self.depth, idx, num_children, node, self.markers)
if string.len(padding) > 0 then if string.len(padding) > 0 then
@ -262,13 +264,13 @@ function Builder:_build_line(node, idx, num_children)
elseif is_symlink then elseif is_symlink then
self:_build_symlink(node, padding, git_highlight, git_icons_tbl) self:_build_symlink(node, padding, git_highlight, git_icons_tbl)
else else
self:_build_file(node, padding, git_highlight, git_icons_tbl) self:_build_file(node, padding, git_highlight, git_icons_tbl, unloaded_bufnr)
end end
self.index = self.index + 1 self.index = self.index + 1
if node.open then if node.open then
self.depth = self.depth + 1 self.depth = self.depth + 1
self:build(node) self:build(node, unloaded_bufnr)
self.depth = self.depth - 1 self.depth = self.depth - 1
end end
end end
@ -287,12 +289,12 @@ function Builder:_get_nodes_number(nodes)
return i return i
end end
function Builder:build(tree) function Builder:build(tree, unloaded_bufnr)
local num_children = self:_get_nodes_number(tree.nodes) local num_children = self:_get_nodes_number(tree.nodes)
local idx = 1 local idx = 1
for _, node in ipairs(tree.nodes) do for _, node in ipairs(tree.nodes) do
if not node.hidden then if not node.hidden then
self:_build_line(node, idx, num_children) self:_build_line(node, idx, num_children, unloaded_bufnr)
idx = idx + 1 idx = idx + 1
end end
end end

View File

@ -46,7 +46,7 @@ local picture_map = {
gif = true, gif = true,
} }
function M.draw() function M.draw(unloaded_bufnr)
local bufnr = view.get_bufnr() local bufnr = view.get_bufnr()
if not core.get_explorer() or not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then if not core.get_explorer() or not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
return return
@ -73,7 +73,7 @@ function M.draw()
:configure_symlink_destination(M.config.symlink_destination) :configure_symlink_destination(M.config.symlink_destination)
:configure_filter(live_filter.filter, live_filter.prefix) :configure_filter(live_filter.filter, live_filter.prefix)
:build_header(view.is_root_folder_visible(core.get_cwd())) :build_header(view.is_root_folder_visible(core.get_cwd()))
:build(core.get_explorer()) :build(core.get_explorer(), unloaded_bufnr)
:unwrap() :unwrap()
end end