-
Notifications
You must be signed in to change notification settings - Fork 232
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
feat(setup): add option to restore tree on session restore if possible #1366
base: main
Are you sure you want to change the base?
Conversation
return {
"pysan3/neo-tree.nvim",
branch = "restore-session-experimental",
version = false,
opts = {
auto_restore_session_experimental = true,
-- ... Caution: You need to have For the case of vimscript, Caution2: It will be wonky (or not work at all) when you set the default neo-tree position to float or current. Caution3: If you had more than one neo-tree sources opened, only one of them will be respected, with filesystem being most prioritized. Caution4: Neotree must be loaded before session restore is done.
Or |
@SparrowMike Sorry to ping you many times. Did you have lazy = false? I added Caution 4 above. |
record.mp4This is what I'm seeing. The one I used to switch to a different nvim process is tmux (yellow-ish selector) so don't care about that.
|
@pysan3 There's no reason to be hacky about this. We already store all details about the tree in buffer local variables every time a window is shown. If there is any extra information you may need, you can easily add it here: neo-tree.nvim/lua/neo-tree/ui/renderer.lua Lines 1030 to 1036 in 459c603
Those buffer variables should be saved as part of the session and you can use them to restore the tree exactly as it was. I would think that auto-sesion also saves and restores the cwd of the window/tab so you shouldn't have to specify a |
Are you sure about that? When I |
You're right, I guess it only save global variables with an upper case and not buffer local variables, which is frustrating. The ideal solution is to add an API to |
I was actually thinking of enhancing I haven't yet solidified the API yet or even started implementing it, but I think this'll be the solution to a lot of session restoring mechanisms. |
@pysan3 I have run through a test using your fork and branch and it seems there is progress, the tree no longer seems to open full screen. However there are still a few issues, one instance when I switched sessions the tree remained open with the cwd of the previous session. I am also seeing tabs get opened for the session directory and The error that you see flash on the screen is the following
I slowed down on my actions in this screen recording, please let me know if you are able to understand, if not I will go through and explain all actions through text as well. This is my config for neo-tree, outside of using the default implementation from return {
"pysan3/neo-tree.nvim",
branch = "restore-session-experimental",
version = false,
opts = {
auto_restore_session_experimental = true,
},
} This is my auto-session configuration return {
"rmagatti/auto-session",
config = function()
require("auto-session").setup({
auto_session_last_session_dir = vim.fn.stdpath("data") .. "/sessions/",
auto_session_root_dir = vim.fn.stdpath("data") .. "/sessions/",
auto_session_enabled = true,
log_level = vim.log.levels.ERROR,
auto_session_suppress_dirs = { "~/", "~/Projects", "~/Downloads", "/" },
auto_save_enabled = true,
auto_session_create_enabled = true,
session_lens = {
buftypes_to_ignore = {},
load_on_setup = true,
theme_conf = { border = true },
previewer = false,
},
})
vim.keymap.set("n", "<leader>ls", require("auto-session.session-lens").search_session, {
noremap = true,
})
end,
} Screen.Recording.2024-02-28.at.10.08.55.AM.mp4 |
@josephcrawfordSRH That helps a lot! Thank you. OMG vim.list_contains is only included in nvim-nightly... |
Fixed it. Could you update and try again @josephcrawfordSRH ? |
@pysan3 I have updated and tested again, I feel it is getting much closer. I am still seeing the tab for the session appear and the tree goes to being full screen. I managed to figure out how to fix it by mixing up my keys :). Hitting Screen.Recording.2024-02-28.at.1.10.06.PM.mp4 |
@josephcrawfordSRH I believe netrw hijacking and session restore is colliding. Could you disable netrw and test it out? Put this in the top line of your init.vim. let loaded_netrwPlugin = 1 |
This did not help at all... I stand corrected, after saving my sessions again I was actually able to flip back and forth between sessions having the tree work properly. For about 3 switches anyway after that the tree was again full screen. I am not sure why it would act like that initially it got my hopes up :D I think it has something to do with the session directory being opened in a buffer tab in bufferline or something. It seems like if I close the buffer and save my session I can jump back and forth without issue. As soon as I try to jump with the buffer open it goes full screen. Screen.Recording.2024-02-29.at.8.42.55.AM.1.mp4 |
@josephcrawfordSRH Could you repost your neo-tree config? This is more of an issue on the auto-session side that your auto-session does not take care of unlisted buffers carefully, but I can fix it on our end (and it'll be easier for us to solve anyways). However I cannot reproduce the issue on my end since I deal with those buffers myself in my config. Could you fill in this -- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")
-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)
-- install plugins
local plugins = {
"folke/tokyonight.nvim",
-- add any other plugins here
}
local neotree_config = {
"nvim-neo-tree/neo-tree.nvim",
dependencies = { "MunifTanjim/nui.nvim", "nvim-tree/nvim-web-devicons", "nvim-lua/plenary.nvim" },
cmd = { "Neotree" },
keys = {
{ "<Leader>e", "<Cmd>Neotree<CR>" }, -- change or remove this line if relevant.
},
opts = {
-- Your config here
-- ...
},
}
table.insert(plugins, neotree_config)
require("lazy").setup(plugins, {
root = root .. "/plugins",
})
vim.cmd.colorscheme("tokyonight")
-- add anything else here |
I would love to provide you with that, but neo-tree is bundled with lazyvim. I am not sure where the default opts are for neo-tree when it comes to the lazy vim internals. All I have for my own configuration which overrides the defaults is the following. return {
"pysan3/neo-tree.nvim",
branch = "restore-session-experimental",
version = false,
lazy = false,
opts = {
auto_restore_session_experimental = true,
},
} I have only installed LazyVIm and added the auto-session plugin along with the neo-tree modifications that were requested last week to see about getting this working. My auto session configuration is the following. return {
"rmagatti/auto-session",
config = function()
require("auto-session").setup({
auto_session_last_session_dir = vim.fn.stdpath("data") .. "/sessions/",
auto_session_root_dir = vim.fn.stdpath("data") .. "/sessions/",
auto_session_enabled = true,
log_level = vim.log.levels.ERROR,
auto_session_suppress_dirs = { "~/", "~/Projects", "~/Downloads", "/" },
auto_save_enabled = true,
auto_session_create_enabled = true,
session_lens = {
buftypes_to_ignore = {},
load_on_setup = true,
theme_conf = { border = true },
previewer = false,
},
})
vim.keymap.set("n", "<leader>ls", require("auto-session.session-lens").search_session, {
noremap = true,
})
vim.o.sessionoptions = "blank,buffers,curdir,folds,help,tabpages,winsize,winpos,terminal,localoptions"
end,
} To make it easier to replicate I am attaching my nvim configs, you can launch nvim with these, I believe |
Could you use vimscript instead? I think this might fix the issue. set sessionoptions+=winpos,terminal,folds
set sessionoptions+=blank And add lazy = false to auto-session's spec. |
And delete all old sessions and start from scratch. |
This did not help! What finally did end up working was adding the following to my vim.o.sessionoptions = "blank,buffers,curdir,folds,help,tabpages,winsize,winpos,terminal,localoptions" I am going to give this a good test but jumping between 2 projects seems to work just fine now. |
As I said, put the vim script in init.lua / init.vim and please do nothing else. Also please delete all existing sessions while you have no neovim instance opened. |
will try it now |
I did exactly as you stated, closed all nvim instances, deleted sessions, removed localoptions, opened nvim, opened a buffer, saved the session, closed nvim, went to a new project, opened nvim, opened a buffer, saved the project. Switched to my other project just to see the following. Screen.Recording.2024-03-05.at.10.06.13.AM.mp4 |
After a few more tests it kind of works but the buffers are hidden while the tree is full-size. I close the tree it shows my open buffer, i re-open the tree it is full-size. Screen.Recording.2024-03-05.at.10.24.24.AM.mp4 |
Let me reiterate. Do not use vim.o. If you really want to use lua for whatever reason, use vim.opt and :append. Do not reset the whole value. Please read It looks like you still have vim.o in your auto-session config function. Didn't you say you deleted that? PLEASE DELETE THAT LINE. Search for sessionoptions in the entire config and be exactly sure that you don't have it set other than init.lua. And use :append because I asked you to run If you don't understand the difference between vim.o and vim.opt, THEY ARE DIFFERENT. Please don't self judge and do what you think is correct. |
I had deleted it from the auto-sessions.lua file, here is a screen recording of what you have asked. Sorry for thinking that Screen.Recording.2024-03-05.at.11.18.25.AM.mp4 |
Thank you. Finally we are on the same table. I can see the issue but I cannot reproduce it on my end. Why do you have neo-tree opened right after neovim is launched with Why do you not have the welcome screen that should be installed with LazyVim by default? Could you zip your config and send it to me again? I think you made some changes from last time. |
@pysan3 this is my alias for I think the tree opens because of my alias, this is how I open my project folders. Attached is my config again. I just cleared my sessions and tested again using simply the I did find one issue where when my initial session was in Screen.Recording.2024-03-05.at.3.50.05.PM.mp4 |
True, but this is not as easy due to how session files are constructed.
And I know it's tedious but please don't forget to delete the session dir again... Here is the config after I made a couple of changes (will extract to |
The most tedious part is having to have auto-session lazy=false as I have to manually load it all the time |
I think it's the other way around. Lazy = true won't load the plugin on startup and you have to manually load it. |
Not sure if this is the right place to post this but I eventually got neo-tree functioning with auto-session by ensuring the neo-tree buffer doesn't save to the session at all and using the auto-session hooks to handle opening/closing neo-tree. return {
'rmagatti/auto-session',
config = function()
require("auto-session").setup {
log_level = "error",
auto_session_suppress_dirs = { "~/", "~/projects", "~/Downloads", "/" },
pre_save_cmds = {"tabdo Neotree close"},
post_restore_cmds = {"Neotree"}
}
end
} While your specific neo-tree config doesn't really matter in this case it is helpful to have the |
@TimCreasman are you also using the experimental branch |
@josephcrawfordSRH I was not when I first posted but I just tried it out and it is working on the experimental branch too. It shouldn't really matter what neo-tree branch you use since my approach is not even saving the neo-tree buffer to the session. |
This is a very good solution to this problem. I spent too long trying to figure out a solution to restore Neotree with auto-sessions. This solution works perfectly! Thanks @TimCreasman |
@TimCreasman I have put this in place however, it does not appear to work for me when using the experimental branch. I switch sessions and the tree shows the structure from the previous session, I may have something amiss so I will mess with this a bit more later today. Edited: I have tested using the main branch, I cannot seem to get this to actually work for me. Yes it closes when a session is saved but when I then switch sessions it is open with the previous session in the tree which strikes me as odd. If I close the tree and re-open the tree then the tree has the correct session structure. auto-session configuration return {
"rmagatti/auto-session",
lazy = false,
config = function()
require("auto-session").setup({
auto_session_suppress_dirs = { "~/", "~/Downloads", "/" },
auto_session_enable_last_session = false,
auto_session_last_session_dir = vim.fn.stdpath("data") .. "/sessions/",
auto_session_root_dir = vim.fn.stdpath("data") .. "/sessions/",
auto_session_enabled = true,
log_level = vim.log.levels.ERROR,
auto_save_enabled = true,
auto_session_create_enabled = true,
pre_save_cmds = { "tabdo Neotree close" },
post_restore_cmds = { "Neotree" },
session_lens = {
buftypes_to_ignore = {},
load_on_setup = true,
theme_conf = { border = true },
previewer = false,
-- path_display = { "shorten" },
},
})
local keymap = vim.keymap
keymap.set("n", "<leader>wr", "<cmd>SessionRestore<CR>", { desc = "Restore session for cwd" }) -- restore last workspace session for current directory
keymap.set("n", "<leader>ws", "<cmd>SessionSave<CR>", { desc = "Save session for auto session root dir" }) -- save workspace session for current working directory
keymap.set("n", "<leader>wl", require("auto-session.session-lens").search_session, {
desc = "List saved sessions",
noremap = true,
})
end,
} neotree configuration return {
"pysan3/neo-tree.nvim",
lazy = false,
opts = {
filesystem = {
filtered_items = {
visible = true,
-- hide_dotfiles = false,
-- hide_gitignore = false,
},
},
},
}
|
Did you try setting filesystem = {
follow_current_file = { enabled = true }, -- try adding this
...
},
... |
@TimCreasman I have added that and so long as I do not have the auto-session commands in place it kind of works. When I switch sessions the tree is not open, I can open it with a hotkey and it shows the proper session tree, however, when I add the commands you outlined above and I switch sessions I can see the tree flicker as it is closing and then re-opening but it is always showing the original session. I have tried starting in one session, switching to 2 other sessions one after the other and the tree always shows the first session I was in. Screen.Recording.2024-08-27.at.10.15.53.AM.mp4 |
Experimental flag.
I'll do it correct in the rewrite.
Ref: #1365