gennaro-tedesco / nvim-possession

📌 the no-nonsense session manager
MIT License
215 stars 7 forks source link

nvim-tree not loading when restoring a session #13

Closed carlosflorencio closed 1 year ago

carlosflorencio commented 1 year ago

Hi,

When restoring a session which had nvim-tree opened, the plugin doesn't seem to be started, the buffer stays blank:

image

Thanks for the plugin btw.

gennaro-tedesco commented 1 year ago

Good evening! By "restoring" do you mean autoload = true (namely restoring previous directory session at startup) or do you mean by selecting a session from the sessions list?

Would the session be restored normally if you manually run vim.cmd.source(<sessionpath>) (this is to understand whether the problem is with the loading mechanism or with the session file itself that cannot re-open the nvim-tree buffer)?

carlosflorencio commented 1 year ago

Hi!

I mean restore by selecting a session from the list, sourcing the session file directly with vim.cmd.source(<sessionpath>) has the same behaviour (blank nvim-tree buffer).

gennaro-tedesco commented 1 year ago

I have reproduced it: the reason is that nvim-tree does not save the content of the buffer upon closing, and hence the buffer does not exist in the list that is loaded in memory by sourcing the session. You can easily verify this by comparing, say, with other temporary-buffers like vim-fugitive that are instead saved. Perform the following experiment:

  1. open a file in a directory
  2. open a buffer with say vim-fugitive (or other plugins)
  3. open a nvim-tree buffer
  4. save and close

Now inspect the session file (located at ~/.local/share/nvim/sessions/<sessionfile>) and you will see entries like

badd +X filename
badd +X //fugitive
badd ...

but in the above list you will not see nvim-tree; instead, you will probably find an entry below with

file/edit NvimTree

which remembers the existence of such window but without having saved the content of the buffer.

This is a behaviour specific to nvim-tree and unless they have an option to explicitly save the buffer content upon closing there isn't much we can do, as it doesn't depend on the session.

carlosflorencio commented 1 year ago

I see, thanks for the investigation.

A possible solution could be to toggle the nvim-tree after loading a session, like this. Just tested and doesn't seem to work.

https://github.com/nvim-tree/nvim-tree.lua/pull/307 should already handle this scenario when the event SessionLoadPost is fired.

gennaro-tedesco commented 1 year ago

I see, what they do is basically define user events that trigger as post-hooks, after loading. It should be possible to add it; I will work on it in the next days and see if I get a working prototype :)

gennaro-tedesco commented 1 year ago

I have drafted this PR to address the issue: would you mind helping me test it? You could install the specific branch post_hooks and require at setup

require("nvim-possession").setup({
    post_hook = function()
        require('nvim-tree').toggle(false, true)
    end
})

(see README for more instructions about the feature).

carlosflorencio commented 1 year ago

Yep, seems to work like a charm :). nvim-tree opens after restoring a session (without focus).

gennaro-tedesco commented 1 year ago

Merged to main and released in v0.0.4. You can now simply update and you're good to go :)

carlosflorencio commented 1 year ago

Awesome work 💪.