Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] restore nvim-tree folder structure base on deepest restored buffer? #196

Closed
tmpm697 opened this issue Jan 5, 2023 · 12 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@tmpm697
Copy link

tmpm697 commented Jan 5, 2023

I have some buffers to restore at startup of nvim.
I also have nvim-tree setup and start when nvim start.

current behavior is that when i start nvim: nvim, auto-session will restore and put me/cursor to buffer of nvim-tree with all folders closed.

but i want the folders are expanded bases on the deepest buffer on the session.
i.e:

tree
fder1/file1
fder2/fder3/file3
fder4/fder5/fder6/fder7/file8
fder9/fder10/fder11/fder12/fder13/fder14/file15

so if after restore session i have file3 in buffer, i want nvim-tree also expand its folder structure to fder3/
if after restore session i have file8 in buffer, i want nvim-tree also expand its folder struture to fder8/

@tmpm697 tmpm697 added the enhancement New feature or request label Jan 5, 2023
@rmagatti
Copy link
Owner

rmagatti commented Jan 6, 2023

Hey 👋 auto-session has exactly zero control over how nvim-tree gets restored. You can try to use a post_restore hook to run whatever command makes nvim-tree do what you want it to do.

Relevant issue:
#105 (comment)

@tmpm697
Copy link
Author

tmpm697 commented Jan 6, 2023

After doing some tests, this works for me on nvim v0.8.2:

lua << EOF
require("auto-session").setup{
  pre_save_cmds = {"NvimTreeClose"}
}
EOF

autocmd VimEnter * :NvimTreeToggle

With above config, worked means nvim-tree will expand to last open/focused buffer --> this is what i want.

Thanks.

@tmpm697 tmpm697 closed this as completed Jan 6, 2023
@tmpm697
Copy link
Author

tmpm697 commented Jan 10, 2023

@rmagatti : how can i restore nvimtree when i switch sessions?

I use auto-session with telescope-project.nvim and from your comment here I've set restore_upcoming_session = true,. I now can switch between projects by :Telescope project and then ctrl-l to switch to it.

It switched ok (all buffers restored ok) but I want nvim-tree also opened after switched to new project (despite of current/old session having nvim-tree open or not, switching to project always have nvim-tree closed)

I had this setup of auto-session:

    require("auto-session").setup({
      pre_save_cmds = { "NvimTreeClose" },
      pre_restore_cmds = { "NvimTreeOpen" },
      cwd_change_handling = {
        restore_upcoming_session = true,
        pre_restore_cmds = { "NvimTreeOpen" }, -- having this or not, still same result
      },
    })

note, this time i use lua instead of vimscript.
so how can i open nvimtree after switch project (or actually session)?

Basically i want to ask how to switch projects (sessions) and having it like exactly like you just restore a session? meaning switching == restoring behavior.

@rmagatti
Copy link
Owner

rmagatti commented Jan 10, 2023

It's likely just a timing issue.

You're using pre_restore_cmds but that only runs before the session is in fact restored. You want to do post_restore_cmds so NvimTreeOpen runs after the session has already been restored.

Also, note cwd_change_handling doesn't take pre_restore_cmds.

@tmpm697
Copy link
Author

tmpm697 commented Jan 10, 2023

tks but:

require("auto-session").setup({
  <..>
  post_restore_cmds = { "NvimTreeOpen" },
  <...>
})

will keep cursor at nvimtree buffer, that's not what i want, as i want: open nvim-tree at pre-restore -> restore -> cursor at last focused buffer ----> how can i do this?
or if use post_restore_cmds = { "NvimTreeOpen" }, how can i auto switch over buffer after switched to new project/session?

@rmagatti
Copy link
Owner

The cursor moves to the nvim-tree buffer because that's apparently the behaviour nvim-tree chooses. Auto Session has no control over what happens when you call NvimTreeOpen. You can try and check if there's a config in nvim-tree that disables auto-focusing the tree buffer.

@tmpm697
Copy link
Author

tmpm697 commented Jan 10, 2023

thanks, nvim-tree does not have that feature close but not focus, switched to neotree for that and worked like a charm now.

@tmpm697
Copy link
Author

tmpm697 commented Jan 10, 2023

I got this issue, step (clean up/remove all saved session files from cache):

1. use :Telescope project to create project/session A and B (with different directory)
2. cd "$(mktemp -d)"
3. nvim
4. :Telescope project and then ctrl-l to switch session A, and then open some buffers
5. same as 4 but with B
6. switch back and forth between A and B --> perfect, bufferline.nvim show its bar and buffers' name/title
7. exit nvim
8. nvim
9. select project A and B back and forth 
10. observe that there're no bufferline's bar exist --> bufferline completely not appear

why and how bufferline disappear when restore session and its' not happen in first session when create them. @rmagatti 

after 10 if i go to A project's folder and then start nvim, bufferline shows as usual.

@tmpm697
Copy link
Author

tmpm697 commented Jan 10, 2023

after 8 if stay in one of project A or B and open nvim, and switch A and B back and forth, the issue is not happens. Only if i cd to a folder that is not current A or B, the issue is happen @rmagatti

EDIT: again this issue is not happens when i'm open nvim under exactly path of a project (sub or parent folder still have issue)

@tmpm697
Copy link
Author

tmpm697 commented Jan 10, 2023

can you re-open this? or should i create a new one?

@rmagatti
Copy link
Owner

At this point, I don't think the issues you're seeing have anything to do with auto-session. If you'd like to open a discussion instead to get help with your config that should work better, or even reach out in the Neovim channel in Matrix. Happy to help with anything specific to auto-session though 👍

@tmpm697
Copy link
Author

tmpm697 commented Jan 12, 2023

switched to https://github.com/noib3/nvim-cokeline and don't see this issue: akinsho/bufferline.nvim#655

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants