Skip to content

Commit

Permalink
fix(renderer): use winsaveview/winrestview to restore cursor as well
Browse files Browse the repository at this point in the history
We can use `lnum` from winsaveview to set our cursor and not depend
on M.follow_focus and state.tree.get_node (which can be nil sometimes).

This way we don't trigger M.follow_focus window centering algorithm when
expanding or closing nodes.
  • Loading branch information
georgeguimaraes committed Dec 27, 2023
1 parent 42a079f commit 0acea00
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions lua/neo-tree/ui/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,12 @@ 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.api.nvim_win_call(state.winid, vim.fn.winsaveview)
log.trace("win_state: " .. vim.inspect(win_state))
state.position.topline = win_state.topline
log.debug("Saved cursor position with node_id: " .. state.position.node_id)
log.debug("Saved window position with topline: " .. state.position.topline)
end
local win_state = vim.api.nvim_win_call(state.winid, vim.fn.winsaveview)
log.trace("win_state: " .. vim.inspect(win_state))
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 @@ -663,17 +660,12 @@ 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 cursor position to node_id: " .. state.position.node_id)
M.focus_node(state, state.position.node_id, true)
if state.position.topline then
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 })
vim.fn.winrestview({ topline = state.position.topline, lnum = state.position.lnum })
end)
end
else
Expand Down

0 comments on commit 0acea00

Please sign in to comment.