Skip to content

Commit 5edc521

Browse files
committed
fix: prevent invalid window id by wrapping state.position.save in pcall, fixes #102
1 parent 4231abe commit 5edc521

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ local navigate_internal = function(path, path_to_reveal, callback)
144144
handled = follow_internal(callback, true)
145145
end
146146
if not handled then
147-
state.position.save()
147+
local success, msg = pcall(state.position.save)
148+
if success then
149+
log.trace("navigate_internal: position saved")
150+
else
151+
log.trace("navigate_internal: FAILED to save position: ", msg)
152+
end
148153
fs_scan.get_items_async(state, nil, nil, callback)
149154
end
150155
end

lua/neo-tree/sources/manager.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ local function create_state(tabnr, sd)
3636
state.dirty = true
3737
state.position = {
3838
save = function()
39-
if state.tree and renderer.is_window_valid(state.winid) then
39+
if state.tree and renderer.window_exists(state) then
4040
local node = state.tree:get_node()
4141
if node then
4242
state.position.node_id = node:get_id()
@@ -180,7 +180,7 @@ M.dir_changed = function(source_name)
180180
if state.path and cwd == state.path then
181181
return
182182
end
183-
if renderer.window_exists(state) then
183+
if state.path and renderer.window_exists(state) then
184184
M.navigate(source_name, cwd)
185185
else
186186
state.path = cwd

lua/neo-tree/ui/renderer.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,10 @@ M.window_exists = function(state)
489489
local isvalid = M.is_window_valid(state.winid)
490490
window_exists = isvalid and (vim.api.nvim_win_get_number(state.winid) > 0)
491491
if not window_exists then
492+
state.winid = nil
492493
local bufnr = utils.get_value(state, "bufnr", 0, true)
493494
if bufnr > 0 and vim.api.nvim_buf_is_valid(bufnr) then
495+
state.bufnr = nil
494496
local success, err = pcall(vim.api.nvim_buf_delete, bufnr, { force = true })
495497
if not success and err:match("E523") then
496498
vim.schedule_wrap(function()

0 commit comments

Comments
 (0)