romgrk / barbar.nvim

The neovim tabline plugin.
2.23k stars 83 forks source link

Builtin session integration doesn't work with persistence.nvim #364

Closed Iron-E closed 1 year ago

Iron-E commented 1 year ago

Just double checking that you run :doautocmd User SessionSavePre before exiting vim.

even using :doautocmd User SessionSavePre it not save.

If so, can you post one of the session files that persistence.nvim is generating? Make sure to censor personally identifying information, I don't need any actual filenames, just the leading setup info.

Since the order of buffers is lost, my guess is that the global barbar session restore variable isn't being stored by persistence.nvim, in which case I'll download the plugin and try it later as well

The session file:

let SessionLoad = 1
let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1
let v:this_session=expand("<sfile>:p")
let VM_mouse_mappings =  0
let HowMuch_no_mappings =  1
let VM_default_mappings =  1
let VM_persistent_registers =  0
let VM_highlight_matches = "underline"
let VM_check_mappings =  1
let VM_theme = "olive"
silent only
silent tabonly
cd ~/.config/nvim
if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
  let s:wipebuf = bufnr('%')
endif
let s:shortmess_save = &shortmess
if &shortmess =~ 'A'
  set shortmess=aoOA
else
  set shortmess=aoO
endif
badd +0 lua/plugins/init.lua
argglobal
%argdel
edit lua/plugins/init.lua
argglobal
balt lua/plugins/init.lua
let s:l = 120 - ((22 * winheight(0) + 22) / 45)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 120
normal! 026|
tabnext 1
if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0 && getbufvar(s:wipebuf, '&buftype') isnot# 'terminal'
  silent exe 'bwipe ' . s:wipebuf
endif
unlet! s:wipebuf
set winheight=1 winwidth=20
let &shortmess = s:shortmess_save
let s:sx = expand("<sfile>:p:r")."x.vim"
if filereadable(s:sx)
  exe "source " . fnameescape(s:sx)
endif
let &g:so = s:so_save | let &g:siso = s:siso_save
set hlsearch
nohlsearch
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :

Originally posted by @otavioschwanck in https://github.com/romgrk/barbar.nvim/issues/361#issuecomment-1442134965

otavioschwanck commented 1 year ago

Check: https://github.com/romgrk/barbar.nvim/issues/361#issuecomment-1442142991

Iron-E commented 1 year ago

Re: https://github.com/romgrk/barbar.nvim/issues/361#issuecomment-1442142991

(click to expand) This is what a session file looks like for me when doing nvim a b c d, then:
BufferPin 
doautocmd User SessionSavePre
mksession! foo.vim
qa
…and then nvim -S foo.vim
let SessionLoad = 1
let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1
let v:this_session=expand(":p")
let Bufferline__session_restore = "lua require'bufferline.state'.restore_buffers {{name = \"a\",pinned = true,},{name = \"b\",pinned = false,},{name = \"c\",pinned = false,},{name = \"d\",pinned = false,}}"
silent only
silent tabonly
cd /tmp
if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
  let s:wipebuf = bufnr('%')
endif
let s:shortmess_save = &shortmess
if &shortmess =~ 'A'
  set shortmess=aoOA
else
  set shortmess=aoO
endif
badd +0 a
badd +0 b
badd +0 c
badd +0 d
argglobal
%argdel
$argadd a
$argadd b
$argadd c
$argadd d
edit a
argglobal
setlocal fdm=marker
setlocal fde=0
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
let s:l = 1 - ((0 * winheight(0) + 19) / 38)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 1
normal! 0
tabnext 1
if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0 && getbufvar(s:wipebuf, '&buftype') isnot# 'terminal'
  silent exe 'bwipe ' . s:wipebuf
endif
unlet! s:wipebuf
set winheight=1 winwidth=20
let &shortmess = s:shortmess_save
let s:sx = expand(":p:r")."x.vim"
if filereadable(s:sx)
  exe "source " . fnameescape(s:sx)
endif
let &g:so = s:so_save | let &g:siso = s:siso_save
set hlsearch
nohlsearch
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :

Note line 4:

let Bufferline__session_restore = "lua require'bufferline.state'.restore_buffers {{name = \"a\",pinned = true,},{name = \"b\",pinned = false,},{name = \"c\",pinned = false,},{name = \"d\",pinned = false,}}"

That variable was missing in the session file generated by persistence.nvim, so I'll have to do some investigating.

otavioschwanck commented 1 year ago
mksession! foo.vim

here, even with mksession! it doesn't save the pin, maybe something in my config?

otavioschwanck commented 1 year ago
doautocmd User SessionSavePre

Nevermind, it works (i forgot the doautocmd User SessionSavePre)

Iron-E commented 1 year ago

here, even with mksession! it doesn't save the pin, maybe something in my config?

Hmm, perhaps. That should definitely be working.

Since persistence.nvim is normally being used, vim.opt.sessionoptions:append 'globals' would need to be added to your config for this to work without it (although I think you already did that).

If your dots are online I can take a look.

Iron-E commented 1 year ago

Nevermind, it works (i forgot the doautocmd User SessionSavePre)

Ah, gotcha. So long as persistence.nvim still isn't working I'll keep this open

otavioschwanck commented 1 year ago

my dotfiles:

https://github.com/otavioschwanck/mood-nvim

(still using bufferline on it, i didn't push barbar)

otavioschwanck commented 1 year ago

@Iron-E

image

(saved with persistence.nvim)

It should be persisted, right?

lua = vim.g.Bufferline__session_restore returns nil

maybe changing the variable name to BufferlineSessionRestore fixes it ?

..

The SessionSavePre is not being called for some reason.

:au SessionSavePre:

image
Iron-E commented 1 year ago

It should be persisted, right?

Yeah, it should be.

lua = vim.g.Bufferline__session_restore returns nil

maybe changing the variable name to BufferlineSessionRestore fixes it ?

That's a good thought, though persistence.nvim also uses :mksession under the hood, so it would be strange for it to work in one setting and not the other (then again, computers are strange). I'm going to download the plugin and check it out.

The SessionSavePre is not being called for some reason.

To clarify, SessionSavePre is not an actual event. We are using the User event (:h User) with the SessionSavePre pattern, like so:

vim.api.nvim_exec_autocmds('User', {pattern = 'SessionSavePre'})

If you run :autocmd User SessionSavePre you should see barbar.nvim's autocommand.

Iron-E commented 1 year ago

@otavioschwanck I just tried persistence.nvim, and it worked for me. Here's my config (using lazy.nvim):

{'folke/persistence.nvim', opts = {
  dir = '/tmp/persistence.nvim', -- you can leave this out, I'm just setting it because I'm testing the plugin
  options = {'buffers', 'curdir', 'globals', 'tabpages', 'winsize'},
}},

Here's what I did:

  1. nvim a b c d
  2. Enter these commands (in order):
    BufferPin b
    doautocmd User SessionSavePre
    qa
  3. Open Neovim and do :lua require'persistence'.load {last = true}
Here's what the session file looked like for me
let SessionLoad = 1
let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1
let v:this_session=expand(":p")
let Bufferline__session_restore = "lua require'bufferline.state'.restore_buffers {{name = \"b\",pinned = true,},{name = \"a\",pinned = false,},{name = \"c\",pinned = false,},{name = \"d\",pinned = false,}}"
silent only
silent tabonly
cd /tmp
if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
  let s:wipebuf = bufnr('%')
endif
let s:shortmess_save = &shortmess
if &shortmess =~ 'A'
  set shortmess=aoOA
else
  set shortmess=aoO
endif
badd +1 a
badd +1 b
badd +1 c
badd +1 d
badd +2831 \[Command\ Line]
argglobal
%argdel
$argadd a
$argadd b
$argadd c
$argadd d
edit a
let s:save_splitbelow = &splitbelow
let s:save_splitright = &splitright
set splitbelow splitright
let &splitbelow = s:save_splitbelow
let &splitright = s:save_splitright
wincmd t
let s:save_winminheight = &winminheight
let s:save_winminwidth = &winminwidth
set winminheight=0
set winheight=1
set winminwidth=0
set winwidth=1
argglobal
let s:l = 1 - ((0 * winheight(0) + 15) / 30)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 1
normal! 0
tabnext 1
if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0 && getbufvar(s:wipebuf, '&buftype') isnot# 'terminal'
  silent exe 'bwipe ' . s:wipebuf
endif
unlet! s:wipebuf
set winheight=1 winwidth=20
let &shortmess = s:shortmess_save
let &winminheight = s:save_winminheight
let &winminwidth = s:save_winminwidth
let s:sx = expand(":p:r")."x.vim"
if filereadable(s:sx)
  exe "source " . fnameescape(s:sx)
endif
let &g:so = s:so_save | let &g:siso = s:siso_save
set hlsearch
nohlsearch
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :
Iron-E commented 1 year ago

I have also made a pull request (folke/persistence.nvim#22) which will automate the :doautocmd calls. The PR has an example configuration for this plugin.

otavioschwanck commented 1 year ago

@otavioschwanck I just tried persistence.nvim, and it worked for me. Here's my config (using lazy.nvim):

{'folke/persistence.nvim', opts = {
  dir = '/tmp/persistence.nvim', -- you can leave this out, I'm just setting it because I'm testing the plugin
  options = {'buffers', 'curdir', 'globals', 'tabpages', 'winsize'},
}},

Here's what I did:

  1. nvim a b c d
  2. Enter these commands (in order):
    BufferPin b
    doautocmd User SessionSavePre
    qa
  3. Open Neovim and do :lua require'persistence'.load {last = true}

Here's what the session file looked like for me

didn't work either:

persistence

if i call the autocmd, persistence don't save the session if i dont call, it saves (but not the pins)

..

i tried https://github.com/rmagatti/auto-session and same problem happens, no pins either. My neovim: 0.8.3.

With https://github.com/akinsho/bufferline.nvim the tabs are persisted without any issues

otavioschwanck commented 1 year ago

Tried your fix on persistence and same thing happen: Only the buffer position are being saved, not the pins:


{
    "Iron-E/persistence.nvim", branch = 'feat/pre-save',
    event = "BufReadPre", -- this will only start session saving when an actual file was opened
    module = "persistence",
    config = function()
      require("persistence").setup({
        options = { "buffers", "curdir", "tabpages", "winsize", "globals" }, -- sessionoptions used for saving
        pre_save = function() vim.api.nvim_exec_autocmds('User', {pattern = 'SessionSavePre'}) end,
      })

      -- require("scripts.auto-save-session").setup()
    end,
  }```
Iron-E commented 1 year ago

With https://github.com/akinsho/bufferline.nvim the tabs are persisted without any issues

Correct. I outlined the reason for this in folke/persistence.nvim#22— namely, bufferline.nvim sacrifices performance to cut down on the need for explicit configuration with session managers, whereas this plugin tries to preserve performance (but it requires configuration with session managers to function).


I made a fork of your dots to test this, and wasn't able to reproduce the issue. Most of the changes were just commenting things out (because I didn't have particular software installed, notably tmux), but these two files will be of interest:

You can view the full diff here.

Try out my fork of your config (if you have Neovim 0.9, you can change $NVIM_APPNAME to avoid messing with your current configuration) and see if you can still reproduce.


Edit: you're also welcome to try my config. Same thing about $NVIM_APPNAME

otavioschwanck commented 1 year ago

First, thanks for your help, you are awesome!

Second:

I tested on mood and it worked (the pin).

On my real private config (based on mood, it wont):

barbar

It only saves the tabs positions on the session, not the pinned (always saves as false):

let Bufferline__session_restore = "lua require'bufferline.state'.restore_buffers {{name = \"lua/plugins/lsp-zero.lua\",pinned = false,},{name = \"init.lua\",pinned = false,}}"

Using barbar and persistence the same way you did on mood.

EDIT: I invited you to my dotfiles.

EDIT 2: Debugging here, i found that the Load is being executed, but it doesn't pin =C:

image

EDIT 3:

Is getting inside here:

image

But the pin command is not working for some reason.

Just to remember: The buffer order is being keep, just the pin is not being set (even calling the state.toggle_pin(bufnr))

EDIT 4:

If i load some buffer before loading the session, it works. The problem is only i quickly load session

otavioschwanck commented 1 year ago

@Iron-E after a lot of debugging:

The problem is:

Tried to fix but no success!

Iron-E commented 1 year ago

@otavioschwanck thank you for the kind words!

I haven't had time to check out your dots (yet— I can later today), but have you tried applying some of the changes I made to mood-nvim in https://github.com/romgrk/barbar.nvim/issues/364#issuecomment-1442596194 ? Try copying over the two files I mentioned, especially "persistence.lua". From the sounds of it, the event or module key might be messing things up.

otavioschwanck commented 1 year ago

@otavioschwanck thank you for the kind words!

I haven't had time to check out your dots (yet— I can later today), but have you tried applying some of the changes I made to mood-nvim in #364 (comment) ? Try copying over the two files I mentioned, especially "persistence.lua". From the sounds of it, the event or module key might be messing things up.

i already removed. Look this comment: https://github.com/romgrk/barbar.nvim/issues/364#issuecomment-1444135373

At the end:

The session is being loaded successfully (i debugged the barbar gem), the order is being set but the pins is not being set.

If i open neovim, open a random file, and after that, load the session, the pins are loaded.

Iron-E commented 1 year ago

I've downloaded your dotfiles, and I can confirm that it does not work either for me. I'll do some investigating on my end.


Edit: @otavioschwanck I think I've located the problem. In "lua/plugins/lsp-zero.lua", if I comment out lsp.preset('recommended') or lsp.setup(), everything works. I don't have any experience with lsp-zero, so I hope that this is able to point you in the right direction.

Maybe it's a problem with managing nvim-cmp? I get this warning:

[lsp-zero] Settings for nvim_cmp should be handled by the user

It could be quite a few things— lsp-zero.nvim is ~100 lines shy of being bigger than barbar :smile:

otavioschwanck commented 1 year ago

I've downloaded your dotfiles, and I can confirm that it does not work either for me. I'll do some investigating on my end.

Edit: @otavioschwanck I think I've located the problem. In "lua/plugins/lsp-zero.lua", if I comment out lsp.preset('recommended') or lsp.setup(), everything works. I don't have any experience with lsp-zero, so I hope that this is able to point you in the right direction.

Maybe it's a problem with managing nvim-cmp? I get this warning:

[lsp-zero] Settings for nvim_cmp should be handled by the user

It could be quite a few things— lsp-zero.nvim is ~100 lines shy of being bigger than barbar 😄

the problem is with the nvim-cmp of lsp-zero.

changing to:


lsp.preset({ manage_nvim_cmp = false })```

The problem don't happen
Iron-E commented 1 year ago

Good to have it pinned down. I suppose they recommend installing nvim-cmp separately regardless (judging by the error message). Feel free to look at my config for snippets:

Should we close this?

otavioschwanck commented 1 year ago

Lets close this! Thanks for your help!

otavioschwanck commented 1 year ago

For who that don't wanna wait the persistence fix, just call persistence this way:

return {
  {
    "folke/persistence.nvim",
    event = "BufReadPre",
    config = function ()
      require("persistence").setup({
        options = {'buffers', 'curdir', 'globals', 'tabpages', 'winsize'},
      })
      vim.api.nvim_create_autocmd("VimLeavePre", {
        group = vim.api.nvim_create_augroup("persistence_pre_save", { clear = true }),
        callback = function ()
          vim.cmd("doautocmd User SessionSavePre")
          require('persistence').save()
        end
      })
    end
  }
}