Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(renderer): fix jumping to the top when opening files. #1274

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions lua/neo-tree/ui/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,11 @@ end
M.position = {
save = function(state)
if state.tree and M.window_exists(state) then
local success, node = pcall(state.tree.get_node, state.tree)
if success and node then
_, state.position.node_id = pcall(node.get_id, node)
local win_state = vim.fn.winsaveview()
state.position.topline = win_state.topline
end
local win_state = vim.api.nvim_win_call(state.winid, vim.fn.winsaveview)
state.position.topline = win_state.topline
state.position.lnum = win_state.lnum
log.debug("Saved cursor position with lnum: " .. state.position.lnum)
log.debug("Saved window position with topline: " .. state.position.topline)
-- Only need to restore the cursor state once per save, comes
-- into play when some actions fire multiple times per "iteration"
-- within the scope of where we need to perform the restore operation
Expand All @@ -660,15 +659,16 @@ M.position = {
state.position.is.restorable = true
end,
restore = function(state)
if not state.position.node_id then
log.debug("No node_id to restore to")
return
end
if state.position.is.restorable then
log.debug("Restoring position to node_id: " .. state.position.node_id)
M.focus_node(state, state.position.node_id, true)
if state.position.topline then
vim.fn.winrestview({ topline = state.position.topline })
if state.position.topline and state.position.lnum then
log.debug("Restoring window position to topline: " .. state.position.topline)
log.debug("Restoring cursor position to lnum: " .. state.position.lnum)
vim.api.nvim_win_call(state.winid, function()
vim.fn.winrestview({ topline = state.position.topline, lnum = state.position.lnum })
end)
end
if state.position.node_id then
M.focus_node(state, state.position.node_id, true)
end
else
log.debug("Position is not restorable")
Expand Down Expand Up @@ -1126,6 +1126,10 @@ render_tree = function(state)
local add_blank_line_at_top = require("neo-tree").config.add_blank_line_at_top
local should_auto_expand = state.window.auto_expand_width and state.current_position ~= "float"
local should_pre_render = should_auto_expand or state.current_position == "current"

log.debug("render_tree: Saving position")
M.position.save(state)

if should_pre_render and M.tree_is_visible(state) then
log.trace("pre-rendering tree")
state._in_pre_render = true
Expand All @@ -1149,6 +1153,9 @@ render_tree = function(state)
state.tree:render()
end
end

log.debug("render_tree: Restoring position")
M.position.restore(state)
end

---Draws the given nodes on the screen.
Expand Down
Loading