@@ -522,8 +522,10 @@ M.focus_node = function(state, id, do_not_focus_window, relative_movement, botto
522
522
end
523
523
local success , err = pcall (vim .api .nvim_win_set_cursor , state .winid , { linenr , col })
524
524
525
- -- now ensure that the window is scrolled correctly
526
525
if success then
526
+ -- forget about cursor position as it is overwritten
527
+ M .position .clear (state )
528
+ -- now ensure that the window is scrolled correctly
527
529
local execute_win_command = function (cmd )
528
530
if vim .api .nvim_get_current_win () == state .winid then
529
531
vim .cmd (cmd )
@@ -647,59 +649,51 @@ M.position = {
647
649
log .debug (" There's already a position saved to be restored. Cannot save another." )
648
650
return
649
651
end
650
-
651
652
if state .tree and M .window_exists (state ) then
652
653
local win_state = vim .api .nvim_win_call (state .winid , vim .fn .winsaveview )
653
654
state .position .topline = win_state .topline
654
655
state .position .lnum = win_state .lnum
655
656
log .debug (" Saved cursor position with lnum: " .. state .position .lnum )
656
657
log .debug (" Saved window position with topline: " .. state .position .topline )
657
- -- Only need to restore the cursor state once per save, comes
658
- -- into play when some actions fire multiple times per "iteration"
659
- -- within the scope of where we need to perform the restore operation
660
- state .position .is .restorable = true
661
658
end
662
659
end ,
663
660
set = function (state , node_id )
664
661
if not type (node_id ) == " string" and node_id > " " then
665
662
return
666
663
end
667
664
state .position .node_id = node_id
668
- state .position .is .restorable = true
665
+ end ,
666
+ clear = function (state )
667
+ log .debug (" Forget about cursor position." )
668
+ -- Clear saved position, so that we can save another position later.
669
+ state .position .topline = nil
670
+ state .position .lnum = nil
671
+ -- After focusing a node, we clear it so that subsequent renderer.position.restore don't
672
+ -- focus on it anymore
673
+ state .position .node_id = nil
669
674
end ,
670
675
restore = function (state )
671
- if state .position .is .restorable then
672
- if state .position .topline and state .position .lnum then
673
- log .debug (" Restoring window position to topline: " .. state .position .topline )
674
- log .debug (" Restoring cursor position to lnum: " .. state .position .lnum )
675
- vim .api .nvim_win_call (state .winid , function ()
676
- vim .fn .winrestview ({ topline = state .position .topline , lnum = state .position .lnum })
677
- end )
678
- -- Clear saved position, so that we can save another position later.
679
- state .position .topline = nil
680
- state .position .lnum = nil
681
- end
682
- if state .position .node_id then
683
- log .debug (" Focusing on node_id: " .. state .position .node_id )
684
- M .focus_node (state , state .position .node_id , true )
685
- -- After focusing a node, we clear it so that subsequent renderer.position.restore don't
686
- -- focus on it anymore
687
- state .position .node_id = nil
688
- end
689
- else
690
- log .debug (" Position is not restorable" )
676
+ if state .position .topline and state .position .lnum then
677
+ log .debug (" Restoring window position to topline: " .. state .position .topline )
678
+ log .debug (" Restoring cursor position to lnum: " .. state .position .lnum )
679
+ vim .api .nvim_win_call (state .winid , function ()
680
+ vim .fn .winrestview ({ topline = state .position .topline , lnum = state .position .lnum })
681
+ end )
682
+ end
683
+ if state .position .node_id then
684
+ log .debug (" Focusing on node_id: " .. state .position .node_id )
685
+ M .focus_node (state , state .position .node_id , true )
691
686
end
692
- state .position .is . restorable = false
687
+ M .position .clear ( state )
693
688
end ,
694
- is = { restorable = true },
695
689
}
696
690
697
691
--- Redraw the tree without relaoding from the source.
698
692
--- @param state table State of the tree.
699
693
M .redraw = function (state )
700
694
if state .tree and M .tree_is_visible (state ) then
701
695
log .trace (" Redrawing tree" , state .name , state .id )
702
- -- every now and then this will fail because the window was closed in
696
+ -- every now and then this will fail because the window was closed in
703
697
-- betweeen the start of an async refresh and the redraw call.
704
698
-- This is not a problem, so we just ignore the error.
705
699
local success = pcall (render_tree , state )
@@ -938,7 +932,7 @@ local get_buffer = function(bufname, state)
938
932
vim .api .nvim_buf_set_option (bufnr , " filetype" , " neo-tree" )
939
933
vim .api .nvim_buf_set_option (bufnr , " modifiable" , false )
940
934
vim .api .nvim_buf_set_option (bufnr , " undolevels" , - 1 )
941
- autocmd .buf .define (bufnr , " WinLeave " , function ()
935
+ autocmd .buf .define (bufnr , " BufDelete " , function ()
942
936
M .position .save (state )
943
937
end )
944
938
end
@@ -1045,15 +1039,9 @@ M.acquire_window = function(state)
1045
1039
vim .api .nvim_buf_set_name (state .bufnr , bufname )
1046
1040
vim .api .nvim_set_current_win (state .winid )
1047
1041
-- Used to track the position of the cursor within the tree as it gains and loses focus
1048
- --
1049
- -- Note `WinEnter` is often too early to restore the cursor position so we do not set
1050
- -- that up here, and instead trigger those events manually after drawing the tree (not
1051
- -- to mention that it would be too late to register `WinEnter` here for the first
1052
- -- iteration of that event on the tree window)
1053
- win :on ({ " WinLeave" }, function ()
1042
+ win :on ({ " BufDelete" }, function ()
1054
1043
M .position .save (state )
1055
1044
end )
1056
-
1057
1045
win :on ({ " BufDelete" }, function ()
1058
1046
win :unmount ()
1059
1047
end , { once = true })
0 commit comments