resession.nvim icon indicating copy to clipboard operation
resession.nvim copied to clipboard

[Question] Persisting folds states and cursors positions

Open RaafatTurki opened this issue 3 years ago • 6 comments

How could I persist my fold states and cursor position per file?

just to be clear on how I'm using this plugin, I'm doing nothing fancy or out of the ordinary, I'm creating and loading sessions and I have the "automatically save a session when you exit Neovim" bit from the README, nothing more.

As for the "per file" in my question, consider the following scenario:

Steps: 1 - :e aaa.lua 2 - move my cursor to line 20 and open a folded function there. 3 - :e bbb.lua 4 - move my cursor to line 10 and open a folded function there. 5 - save session require 'resession'.save('test') 6 - exit and reopen nvim 7 - load session require 'resession'.load('test')

Observations:

  • when opening bbb.lua cursor position is persisted but not the fold state at line 10
  • when opening aaa.lua neither the cursor position nor the fold state at line 20 are persisted

I hope this has made my question clearer. cheers!

RaafatTurki avatar Oct 31 '22 13:10 RaafatTurki

This is a very clear request and it's a feature that I would love to support! At the moment, I don't think there are...really any decent APIs for the fold state in Neovim. I've had to interact with it a bit for my other plugin aerial.nvim, and I had to do a bunch of hacks to make it work anything close to correctly. For something like saving and restoring fold state...I think this is going to require some changes in Neovim core. There's already an open issue for it, we just have to wait for some brave soul to put up a PR https://github.com/neovim/neovim/issues/19226

stevearc avatar Nov 01 '22 03:11 stevearc

That's good to hear! yes the fold API would be a very nice interface. However I do remember :mksession being able to store folds along with their states, I don't remember how exactly tho (I could look into it if you'd like).

That was all about persisting folds, what do you think about persisting cursors positions?

RaafatTurki avatar Nov 01 '22 12:11 RaafatTurki

:mksession encodes the folds with raw normal! zc commands. It would be a gross hack but I could technically do the same thing, except for the fact that I don't think there's an API to get the fold status.

I'm a bit confused about the question of persisting cursor positions, as that's something we should already be saving/restoring. Do you have a repro where that doesn't happen?

stevearc avatar Nov 01 '22 15:11 stevearc

encodes the folds with raw normal! zc commands ...

That's correct, but I don't think the fold API PR will appear anytime soon.

I'm a bit confused about the question of persisting cursor positions ...

Even better than a repro, A showcase of the "issue".

Notice:

  • the cursor was sitting at main.lua:3 and side.lua:2
  • then we've quit (auto session save) while main.lua is open.
  • then upon returning (loading the session) the cursor position for main.lua is naturally restored (as you've noted)
  • however side.lua cursor position is reset to the top (the issue)

The issue is that resession is only saving a single cursor position, what I want is for it to save a cursor position for each file (not open buffers)

I hope this has made it clearer.

RaafatTurki avatar Nov 01 '22 16:11 RaafatTurki

It does! I've moved the discussion of cursor position to a new issue, so we can separate it out. That's something I think I can address in the short-term, whereas the folding issue is probably not. We can leave this issue open for folds, and I'll come back to it once an API is available. Unfortunately, if there's no way to get the current state of the folds, there's no way to save/restore them :/

stevearc avatar Nov 01 '22 16:11 stevearc