Skip to content

Commit 953313e

Browse files
fix(renderer): fix jumping to the top when opening files (#1274)
1 parent 134c466 commit 953313e

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

lua/neo-tree/ui/renderer.lua

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -640,12 +640,11 @@ end
640640
M.position = {
641641
save = function(state)
642642
if state.tree and M.window_exists(state) then
643-
local success, node = pcall(state.tree.get_node, state.tree)
644-
if success and node then
645-
_, state.position.node_id = pcall(node.get_id, node)
646-
local win_state = vim.fn.winsaveview()
647-
state.position.topline = win_state.topline
648-
end
643+
local win_state = vim.api.nvim_win_call(state.winid, vim.fn.winsaveview)
644+
state.position.topline = win_state.topline
645+
state.position.lnum = win_state.lnum
646+
log.debug("Saved cursor position with lnum: " .. state.position.lnum)
647+
log.debug("Saved window position with topline: " .. state.position.topline)
649648
-- Only need to restore the cursor state once per save, comes
650649
-- into play when some actions fire multiple times per "iteration"
651650
-- within the scope of where we need to perform the restore operation
@@ -660,15 +659,16 @@ M.position = {
660659
state.position.is.restorable = true
661660
end,
662661
restore = function(state)
663-
if not state.position.node_id then
664-
log.debug("No node_id to restore to")
665-
return
666-
end
667662
if state.position.is.restorable then
668-
log.debug("Restoring position to node_id: " .. state.position.node_id)
669-
M.focus_node(state, state.position.node_id, true)
670-
if state.position.topline then
671-
vim.fn.winrestview({ topline = state.position.topline })
663+
if state.position.topline and state.position.lnum then
664+
log.debug("Restoring window position to topline: " .. state.position.topline)
665+
log.debug("Restoring cursor position to lnum: " .. state.position.lnum)
666+
vim.api.nvim_win_call(state.winid, function()
667+
vim.fn.winrestview({ topline = state.position.topline, lnum = state.position.lnum })
668+
end)
669+
end
670+
if state.position.node_id then
671+
M.focus_node(state, state.position.node_id, true)
672672
end
673673
else
674674
log.debug("Position is not restorable")
@@ -1126,6 +1126,10 @@ render_tree = function(state)
11261126
local add_blank_line_at_top = require("neo-tree").config.add_blank_line_at_top
11271127
local should_auto_expand = state.window.auto_expand_width and state.current_position ~= "float"
11281128
local should_pre_render = should_auto_expand or state.current_position == "current"
1129+
1130+
log.debug("render_tree: Saving position")
1131+
M.position.save(state)
1132+
11291133
if should_pre_render and M.tree_is_visible(state) then
11301134
log.trace("pre-rendering tree")
11311135
state._in_pre_render = true
@@ -1149,6 +1153,9 @@ render_tree = function(state)
11491153
state.tree:render()
11501154
end
11511155
end
1156+
1157+
log.debug("render_tree: Restoring position")
1158+
M.position.restore(state)
11521159
end
11531160

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

0 commit comments

Comments
 (0)