Skip to content

Commit 82df846

Browse files
committed
fix(files): add is_window_valid before using tree:get_node(), fixes #33
1 parent cfb8b3a commit 82df846

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

lua/neo-tree/sources/filesystem/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ M.navigate = function(path, path_to_reveal, callback)
113113
end)
114114
else
115115
local previously_focused = nil
116-
if state.tree then
116+
if state.tree and renderer.is_window_valid(state.winid) then
117117
local node = state.tree:get_node()
118118
if node then
119119
-- keep the current node selected
@@ -122,7 +122,7 @@ M.navigate = function(path, path_to_reveal, callback)
122122
end
123123
fs_scan.get_items_async(state, nil, nil, function ()
124124
local current_winid = vim.api.nvim_get_current_win()
125-
if current_winid == state.winid and previously_focused then
125+
if path_changed and current_winid == state.winid and previously_focused then
126126
local currently_focused = state.tree:get_node():get_id()
127127
if currently_focused ~= previously_focused then
128128
renderer.focus_node(state, previously_focused, false)

lua/neo-tree/ui/renderer.lua

+17-7
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ M.close_floating_window = function(source_name)
4141
local valid_window_was_closed = false
4242
for _, win in ipairs(found_windows) do
4343
if not valid_window_was_closed then
44-
local winid = win.winid
45-
if type(winid) == "number" and winid > 0 then
46-
valid_window_was_closed = vim.api.nvim_win_is_valid(winid)
47-
end
44+
valid_window_was_closed = M.is_window_valid(win.winid)
4845
end
4946
-- regardless of whether the window is valid or not, nui will cleanup
5047
win:unmount()
@@ -413,6 +410,20 @@ local create_window = function(state)
413410
return win
414411
end
415412

413+
---Determines is the givin winid is valid and the window still exists.
414+
---@param winid any
415+
---@return boolean
416+
M.is_window_valid = function(winid)
417+
if winid == nil then
418+
return false
419+
end
420+
if type(winid) == "number" and winid > 0 then
421+
return vim.api.nvim_win_is_valid(winid)
422+
else
423+
return false
424+
end
425+
end
426+
416427
---Determines if the window exists and is valid.
417428
---@param state table The current state of the plugin.
418429
---@return boolean True if the window exists and is valid, false otherwise.
@@ -421,9 +432,8 @@ M.window_exists = function(state)
421432
if state.winid == nil then
422433
window_exists = false
423434
else
424-
local winid = utils.get_value(state, "winid", 0, true)
425-
local isvalid = winid > 0 and vim.api.nvim_win_is_valid(winid)
426-
window_exists = isvalid and (vim.api.nvim_win_get_number(winid) > 0)
435+
local isvalid = M.is_window_valid(state.winid)
436+
window_exists = isvalid and (vim.api.nvim_win_get_number(state.winid) > 0)
427437
if not window_exists then
428438
local bufnr = utils.get_value(state, "bufnr", 0, true)
429439
if bufnr > 0 and vim.api.nvim_buf_is_valid(bufnr) then

0 commit comments

Comments
 (0)