Skip to content

Commit 4659f98

Browse files
authored
fix(renderer): save position properly on initial open (#1907)
1 parent 20244be commit 4659f98

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

lua/neo-tree/ui/renderer.lua

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -688,16 +688,17 @@ end
688688
---Saves a window position to be restored later
689689
---@param state neotree.State
690690
---@param force boolean?
691+
---@return boolean saved
691692
M.position.save = function(state, force)
692693
if not force and state.position.topline and state.position.lnum then
693694
log.debug("There's already a position saved to be restored. Cannot save another.")
694-
return
695+
return false
695696
end
696697
if not state.tree then
697-
return
698+
return false
698699
end
699700
if not M.window_exists(state) then
700-
return
701+
return false
701702
end
702703

703704
local win_state = vim.api.nvim_win_call(state.winid, vim.fn.winsaveview)
@@ -713,6 +714,7 @@ M.position.save = function(state, force)
713714
local b = vim.fn.getpos("v")
714715
state.position.visual_selection = { a, b }
715716
end
717+
return true
716718
end
717719

718720
---Queues a node to focus
@@ -1161,22 +1163,26 @@ M.acquire_window = function(state)
11611163
vim.api.nvim_buf_set_name(state.bufnr, bufname)
11621164
vim.api.nvim_set_current_win(state.winid)
11631165
-- Used to track the position of the cursor within the tree as it gains and loses focus
1164-
local restored_after_window_change = false
1165-
win:on({ "CursorMoved", "ModeChanged" }, function(args)
1166-
if win.winid == vim.api.nvim_get_current_win() and restored_after_window_change then
1167-
M.position.save(state, true)
1166+
local wait_for_save = true
1167+
win:on({ "CursorMoved", "ModeChanged" }, function()
1168+
if state.winid == vim.api.nvim_get_current_win() then
1169+
if not wait_for_save then
1170+
M.position.save(state, true)
1171+
elseif M.position.save(state) then
1172+
wait_for_save = false
1173+
end
11681174
end
11691175
end)
11701176
win:on({ "BufDelete" }, function()
11711177
M.position.save(state)
11721178
end)
1173-
win:on({ "WinEnter" }, function(args)
1179+
win:on({ "WinEnter" }, function()
11741180
M.position.restore_selection(state)
1175-
if win.winid == vim.api.nvim_get_current_win() then
1181+
if state.winid == vim.api.nvim_get_current_win() then
11761182
M.position.restore(state)
1177-
restored_after_window_change = true
1183+
wait_for_save = false
11781184
else
1179-
restored_after_window_change = false
1185+
wait_for_save = true
11801186
end
11811187
end)
11821188
win:on({ "BufDelete" }, function()

tests/neo-tree/command/command_spec.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ describe("Command", function()
143143
-- It seems like in headless mode, CursorMoved is not emitted.
144144
vim.api.nvim_exec_autocmds("CursorMoved", {})
145145
verify.filesystem_tree_node_is(testfile)
146+
147+
-- toggle on and off
148+
require("neo-tree.command").execute({
149+
toggle = true,
150+
})
151+
require("neo-tree.command").execute({
152+
toggle = true,
153+
})
154+
verify.filesystem_tree_node_is(testfile)
146155
end)
147156
end)
148157

0 commit comments

Comments
 (0)