Skip to content

Commit 52a9efd

Browse files
authored
fix(renderer): add checks to make sure tree is still visible throughout renderer module (#654)
may fix #651
1 parent 3f1380c commit 52a9efd

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

lua/neo-tree/ui/renderer.lua

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ local start_resize_monitor = function()
3636
check_window_size = function()
3737
local windows_exist = false
3838
local success, err = pcall(manager._for_each_state, nil, function(state)
39-
if state.win_width and M.window_exists(state) then
39+
if state.win_width and M.tree_is_visible(state) then
4040
windows_exist = true
4141
local current_size = utils.get_inner_win_width(state.winid)
4242
if current_size ~= state.win_width then
@@ -245,10 +245,10 @@ create_nodes = function(source_items, state, level)
245245
local nodeData = {
246246
id = hidden[#hidden].id .. "_hidden_message",
247247
name = "(forced to show "
248-
.. #hidden
249-
.. " hidden "
250-
.. (#hidden > 1 and "items" or "item")
251-
.. ")",
248+
.. #hidden
249+
.. " hidden "
250+
.. (#hidden > 1 and "items" or "item")
251+
.. ")",
252252
type = "message",
253253
level = level,
254254
is_last_child = show_indent_marker_for_message,
@@ -285,7 +285,7 @@ M.render_component = function(component, item, state, remaining_width)
285285
local component_func = state.components[component[1]]
286286
if component_func then
287287
local success, component_data, wanted_width =
288-
pcall(component_func, component, item, state, remaining_width)
288+
pcall(component_func, component, item, state, remaining_width)
289289
if success then
290290
if component_data == nil then
291291
return { {} }
@@ -325,11 +325,10 @@ local prepare_node = function(item, state)
325325
local line = item.line
326326
-- Only use it once, we don't want to accidentally use stale data
327327
item.line = nil
328-
if
329-
line
330-
and item.wanted_width
331-
and state.longest_node
332-
and item.wanted_width <= state.longest_node
328+
if line
329+
and item.wanted_width
330+
and state.longest_node
331+
and item.wanted_width <= state.longest_node
333332
then
334333
return line
335334
end
@@ -357,7 +356,7 @@ local prepare_node = function(item, state)
357356
end
358357
for _, component in ipairs(renderer) do
359358
local component_data, component_wanted_width =
360-
M.render_component(component, item, state, remaining_cols)
359+
M.render_component(component, item, state, remaining_cols)
361360
local actual_width = 0
362361
if component_data then
363362
for _, data in ipairs(component_data) do
@@ -543,6 +542,9 @@ M.collapse_all_nodes = function(tree)
543542
end
544543

545544
M.expand_to_node = function(state, node)
545+
if not M.tree_is_visible(state) then
546+
return
547+
end
546548
local tree = state.tree
547549
if type(node) == "string" then
548550
node = tree:get_node(node)
@@ -596,7 +598,7 @@ M.position = {
596598
---Redraw the tree without relaoding from the source.
597599
---@param state table State of the tree.
598600
M.redraw = function(state)
599-
if state.tree and M.window_exists(state) then
601+
if state.tree and M.tree_is_visible(state) then
600602
log.trace("Redrawing tree", state.name, state.id)
601603
render_tree(state)
602604
log.trace(" Redrawing tree done", state.name, state.id)
@@ -937,8 +939,8 @@ M.window_exists = function(state)
937939
window_exists = false
938940
elseif position == "current" then
939941
window_exists = vim.api.nvim_win_is_valid(winid)
940-
and vim.api.nvim_buf_is_valid(bufnr)
941-
and vim.api.nvim_win_get_buf(winid) == bufnr
942+
and vim.api.nvim_buf_is_valid(bufnr)
943+
and vim.api.nvim_win_get_buf(winid) == bufnr
942944
else
943945
local isvalid = M.is_window_valid(winid)
944946
window_exists = isvalid and (vim.api.nvim_win_get_number(winid) > 0)
@@ -958,12 +960,19 @@ M.window_exists = function(state)
958960
return window_exists
959961
end
960962

963+
---Determines if a specific tree is open.
964+
---@param state table The current state of the plugin.
965+
---@return boolean
966+
M.tree_is_visible = function(state)
967+
return M.window_exists(state) and vim.api.nvim_win_get_buf(state.winid) == state.bufnr
968+
end
969+
961970
---Renders the given tree and expands window width if needed
962971
--@param state table The state containing tree to render. Almost same as state.tree:render()
963972
render_tree = function(state)
964973
local should_auto_expand = state.window.auto_expand_width and state.current_position ~= "float"
965974
local should_pre_render = should_auto_expand or state.current_position == "current"
966-
if should_pre_render then
975+
if should_pre_render and M.tree_is_visible(state) then
967976
log.trace("pre-rendering tree")
968977
state._in_pre_render = true
969978
state.tree:render()
@@ -975,7 +984,9 @@ render_tree = function(state)
975984
state.win_width = state.longest_node
976985
end
977986
end
978-
state.tree:render()
987+
if M.tree_is_visible(state) then
988+
state.tree:render()
989+
end
979990
end
980991

981992
---Draws the given nodes on the screen.

0 commit comments

Comments
 (0)