symbols-outline.nvim icon indicating copy to clipboard operation
symbols-outline.nvim copied to clipboard

Using with session breaks on reopen

Open ehaynes99 opened this issue 2 years ago • 7 comments

If you save sessions (various plugins for this, but they are all wrappers around the native :mksession), closing nvim with the view open and then restarting results in a broken outline view. Trying to toggle throws the first error below, and effectively every cursor movement afterwards throws the second error below.

Likely, this would be resolved by searching for an existing buffer at startup. I'll try to poke around at it as time allows. As a temporary workaround, I added this, which forces the buffer to be removed prior to updating the session:

vim.api.nvim_create_autocmd('FileType', {
  pattern = { 'Outline' },
  callback = function(args)
    vim.api.nvim_create_autocmd('VimLeavePre', {
      callback = function()
        vim.api.nvim_buf_delete(args.buf, { force = true })
      end
    })
  end,
})

Errors

Error executing vim.schedule lua callback: ...m/lazy/symbols-outline.nvim/lua/symbols-outline/view.lua:39: Failed to rename buffer
stack traceback:
        [C]: in function 'nvim_buf_set_name'
        ...m/lazy/symbols-outline.nvim/lua/symbols-outline/view.lua:39: in function 'setup_view'
        ...e/nvim/lazy/symbols-outline.nvim/lua/symbols-outline.lua:285: in function 'callback'
        /usr/share/nvim/runtime/lua/vim/lsp.lua:1951: in function 'handler'
        /usr/share/nvim/runtime/lua/vim/lsp.lua:1383: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>
Error detected while processing CursorHold Autocommands for "*":
Error executing lua callback: ...e/nvim/lazy/symbols-outline.nvim/lua/symbols-outline.lua:208: Cursor position outside buffer
stack traceback:
        [C]: in function 'nvim_win_set_cursor'
        ...e/nvim/lazy/symbols-outline.nvim/lua/symbols-outline.lua:208: in function '_highlight_current_item'
        ...e/nvim/lazy/symbols-outline.nvim/lua/symbols-outline.lua:19: in function <...e/nvim/lazy/symbols-outline.nvim/lua/symbols-outline.l
ua:18>

ehaynes99 avatar Jan 23 '23 22:01 ehaynes99

Presumably this is related to https://github.com/simrat39/symbols-outline.nvim/issues/182: when closing all buffers the symbols outline remains somehow open, however not part of badd buffers in the session file. I have seen similar behaviours with other such plugins (file trees or symbols) while working on nvim-possession and I could not figure out why it is so.

gennaro-tedesco avatar Jan 25 '23 23:01 gennaro-tedesco

I've been reworking my Vim config and started running into this error with both Symbols-outline.nvim and Neo-tree. I tried hacking together a fix that searches for buffers with specific names when the session is opened and re-executes the corresponding commands, but it's unusably brittle.

The workaround in comment 1 doesn't work for me -- I wonder if it's running after the session is saved in my setup. I don't know much about the ordering of autocommands for the same event.

showermat avatar Jan 26 '23 02:01 showermat

The workaround in comment 1 doesn't work for me -- I wonder if it's running after the session is saved in my setup. I don't know much about the ordering of autocommands for the same event.

They'll run in the order in which they're defined if they're triggered by the same event, so adding yours before load the plugin that saves the session should do it. You would also need to know what event that plugin is using, though, to be sure your command is not using an event that occurs too late.

FWIW, I'm using https://github.com/rmagatti/auto-session. It saves on VimLeave, which would happen after the VimLeavePre that I hooked into: https://github.com/rmagatti/auto-session/blob/ba1606202588a1d4cc68360d6ef9549f0fc464a1/plugin/auto-session.vim#L36

Maybe try VimLeave, but it depends on how your session is being saved. If the plugin also uses VimLeave, I'm not entirely sure about the

ehaynes99 avatar Feb 01 '23 00:02 ehaynes99

Right, I gave it another shot today and got it to work without too much fuss by disabling the autocommand in the session manager and setting my own autocommand that does some cleanup before saving. Ideally, the plugin or the session manager would find a way to make such workarounds unnecessary, but I get that that's a hard problem.

showermat avatar Feb 02 '23 00:02 showermat

@showermat care to share your config?

theoribeiro avatar Feb 03 '23 18:02 theoribeiro

It's pretty specific to my own setup, but sure. For xolox/vim-session with Neo-tree and Symbols-outline, I tell it not to automatically save:

let g:session_autosave = "no"

Then I define a function that closes Neo-tree and Symbols-outline, and then saves the session itself:

function! CleanupSession()
	Neotree close
	SymbolsOutlineClose
	SaveSession
	call xolox#session#auto_unlock()
endfunction

...and I call it from an autocommand:

autocmd VimLeavePre * call CleanupSession()

This effectively does the same thing as the "autosave" feature, but with my own customizations inserted beforehand.

Note that, as written, this relies on the pull request I opened a few days ago to keep SymbolsOutlineClose from raising an error if the pane isn't open.

showermat avatar Feb 04 '23 19:02 showermat

The issue is because the session file is not written correctly by symbols-tree plugin on close.

A stupid workaround for this;

Put the following in your vimrc:

function! FixSymbolsOutlineCloseCallback()
         SymbolsOutlineClose
         mksession /tmp/session.vim
         quitall
endfunction

Now simply open vim again and then do a source /tmp/session.vim and SymbolsOutline runs fine.

burntfalafel avatar Jun 05 '23 14:06 burntfalafel