Skip to content

Commit 1122b20

Browse files
fix(renderer): fix restore position after focus/show toggle (#1288)
1 parent 51d86e2 commit 1122b20

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lua/neo-tree/ui/renderer.lua

+12-3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ end
112112
---@param state table State of the source to close
113113
---@param focus_prior_window boolean | nil if true or nil, focus the window that was previously focused
114114
M.close = function(state, focus_prior_window)
115+
116+
log.debug("Closing window, but saving position first.")
117+
M.position.save(state)
118+
115119
if focus_prior_window == nil then
116120
focus_prior_window = true
117121
end
@@ -639,6 +643,11 @@ end
639643
---Functions to save and restore the focused node.
640644
M.position = {
641645
save = function(state)
646+
if state.position.topline and state.position.lnum then
647+
log.debug("There's already a position saved to be restored. Cannot save another.")
648+
return
649+
end
650+
642651
if state.tree and M.window_exists(state) then
643652
local win_state = vim.api.nvim_win_call(state.winid, vim.fn.winsaveview)
644653
state.position.topline = win_state.topline
@@ -666,6 +675,9 @@ M.position = {
666675
vim.api.nvim_win_call(state.winid, function()
667676
vim.fn.winrestview({ topline = state.position.topline, lnum = state.position.lnum })
668677
end)
678+
-- Clear saved position, so that we can save another position later.
679+
state.position.topline = nil
680+
state.position.lnum = nil
669681
end
670682
if state.position.node_id then
671683
log.debug("Focusing on node_id: " .. state.position.node_id)
@@ -1218,9 +1230,6 @@ draw = function(nodes, state, parent_id)
12181230
-- draw winbar / statusbar
12191231
require("neo-tree.ui.selector").set_source_selector(state)
12201232

1221-
-- Restore the cursor position/focused node in the tree based on the state
1222-
-- when it was last closed
1223-
M.position.restore(state)
12241233
state._ready = true
12251234
end
12261235

0 commit comments

Comments
 (0)