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

[BUG] Lose color on file when switch session? #198

Closed
tmpm697 opened this issue Jan 12, 2023 · 15 comments
Closed

[BUG] Lose color on file when switch session? #198

tmpm697 opened this issue Jan 12, 2023 · 15 comments
Assignees
Labels
bug Something isn't working

Comments

@tmpm697
Copy link

tmpm697 commented Jan 12, 2023

Describe the bug
when i switch session, color on file not appear correctly (less colors)

To Reproduce
Steps to reproduce the behavior:
the color corrects again if i open new file to edit or switch to other buffers and then switch back -- meaning if you just switch to other session and stay and keep use the buffer that restored session bring you in, that buffer is not colored correctly and it'll keep it that way unless you change buffer that somehow will trigger to re-correct the color.

Expected behavior
color display same as when edit file

Screenshots
after switched session:
image

use normally (expected behavior):
image
Baseline (please complete the following information):

  • Result of set sessionoptions?: sessionoptions=blank,buffers,curdir,folds,help,tabpages,winsize,terminal
  • OS. e.g uname -a: archlinux
  • Neovim version nvim --version nightly
  • URL to your current config (if public)

Additional context
Add any other context about the problem here.

@tmpm697 tmpm697 added the bug Something isn't working label Jan 12, 2023
@tmpm697
Copy link
Author

tmpm697 commented Jan 13, 2023

after switched session, if i issue :TSPlaygroundToggle, it'll show empty buffer --> so treesitter not loaded after switch session, why?

@rmagatti
Copy link
Owner

rmagatti commented Jan 13, 2023

This is entirely outside of auto-session's control. If I were to guess, something with your plugins is causing this, some form of incorrectly configured lazy loading can be a common cause for treesitter not loading.

@tmpm697
Copy link
Author

tmpm697 commented Jan 13, 2023

it happens with other colorscheme too.

i use your treesitter config: https://github.com/rmagatti/dotfiles/blob/master/nvim/lua/rmagatti/treesitter.lua#L109
same result.

i saw you use packer instead of lazy.vim, can that be problem? (as in picture, i've set the priority to 999 and lazy load is set to false)

@rmagatti
Copy link
Owner

@tmpm697 your issue also sounds similar to an issue I had a while back. Here's a commit I made where I describe why it was happening to me.
rmagatti/dotfiles@d2f47cc

@tmpm697
Copy link
Author

tmpm697 commented Jan 13, 2023

I sometime get unwanted and random error echo smth contain BufReadPre but idk that cause the issue.

I try to have a minimal steps here:

1. cd ~/.config/nvim/pack/default/start; git clone auto-session, plenary.nvim, telescope.nvim, telescope-project.nvim
2. vi ~/.config/nvim/init.lua
vim.cmd([[colorscheme habamax]])
require("auto-session").setup {
  log_level = "error",
  cwd_change_handling = {
    restore_upcoming_session = true, 
    pre_cwd_changed_hook = nil,
  },
}
require("telescope").load_extension("project")
4. :qa!, nvim, then use :Telescope project to create two project/session name A, B by cd to folder A and then B and create the project
5. cd ~, nvim, then use :Telescope project to switch to A, :e ./fileA # assume it contains some highlight with comment started with #
6. switch to B and do same as 5. , :e ./fileB <...>
7. :qa!, cd ~, nvim , :Telescope project select A, now you can see that there's no highlight color in comment 

All above basically mean, that we instead of using any kind of plugin manager, use default plugin location of nvim for plugin ~/.config/nvim/pack/default/start, git clone all plugins under here, nvim will automatically recognize them.
Then we create project A and B, under each of them has a file with comment portion highlighted as picture below.
Switch back and forth A and B and see the result.

after switch session:
image

normally if we nvim /path/to/file
image

EDIT: From what i can tell, only nui.nvim set BufReadPre, but with above plugins, none of them try to set autocmd target BufReadPre

@tmpm697
Copy link
Author

tmpm697 commented Jan 14, 2023

Now i try with this minimal init.lua config:

nvim ~/.config/nvim/init.lua
vim.cmd([[colorscheme habamax]])
require("auto-session").setup({
	log_level = "error",
	cwd_change_handling = {
		restore_upcoming_session = true,
		pre_cwd_changed_hook = nil,
	},
})

mkdir -p ~/.config/nvim/pack/default/start
cd ~/.config/nvim/pack/default/start
git clone https://github.com/rmagatti/auto-session

cd "$(mktemp -d)" # this is folder A
nvim
:e /what/ever/lua/luafile.lua #edit a lua file for rich color
:qa

cd "$(mktemp -d)" # this is folder B
nvim
:e edit /what/ever/javascript/file.js #edit a javascript file for rich color
:qa

cd "$(mktemp -d)" # whatever folder
nvim
:source /home/user/.local/share/nvim/sessions/\%tmp\%tmp.NEXO2gs4i1\%luafile.vim  # restore session from folder A

Now  I always got this error:
Error detected while processing /home/user/.local/share/nvim/sessions/\%tmp\%tmp.NEXO2gs4i1\%luafile.vim:
line   53:
E108: No such variable: "SessionLoad"

And then session A still loaded and colors was correct (:colorscheme returns habamax)

Why with above minimal config I always get error when source session vim file? this is nvim nightly @rmagatti
rm /home/user/.local/share/nvim/sessions/* and re-do above steps still same error when try to load session.

Edit: if i cd to /home/user/.local/share/nvim/sessions and nvim -S "%tmp%tmp.NEXO2gs4i1%luafile.vim", it will echo error:

Error detected while processing command line:
E499: Empty file name for '%' or '#', only works with ":p:h"

@tmpm697
Copy link
Author

tmpm697 commented Jan 14, 2023

I tried withgennaro-tedesco/nvim-possession and somehow switch session worked and colors not lose.
Sorry i can't give an PR for this, i just need something work so i can continue to work on my current workload.

@tmpm697
Copy link
Author

tmpm697 commented Jan 14, 2023

I'm guessing but this issue can be auto-session tried to remove/clear current buffers before switching?

@tmpm697
Copy link
Author

tmpm697 commented Jan 14, 2023

yeah this caused the issue: https://github.com/rmagatti/auto-session/blob/main/lua/auto-session-autocmds.lua#L44
if i don't set restore_upcoming_session, color shows correct, so the action to clear up current buffers before switch actually causes it.

@tmpm697
Copy link
Author

tmpm697 commented Jan 14, 2023

can you help to clarify why you need to check for session_dir before restore? mksessions saves it to .vim file then we can just get the file and restore?
https://github.com/rmagatti/auto-session/blob/main/lua/auto-session.lua#L491

I don't seem to find out the code that you call to clean up buffers before switching, can you point it out?

@tmpm697
Copy link
Author

tmpm697 commented Jan 15, 2023

Weird that if i use auto-session and let it save some sessions, then disable it and open nvim to manually :source /path/to/session, the colors display correctly.

@tmpm697
Copy link
Author

tmpm697 commented Jan 15, 2023

If i enable debug mode, i got this:

debug:  ==== Pager mode
debug:  sessions_dir, session_file
debug:  ==== Using session DIR
debug:  ==== Session Name:
debug:  ==== is_readable
info:  Session restored from /home/user/.local/share/nvim/sessions/\%home\%user\%.local\%share\%nvim\%lazy\%auto-session\%lua.vim
debug:  Running post-restore command: Neotree action=show

so why these values are empty?

debug:  ==== Pager mode
debug:  sessions_dir, session_file
debug:  ==== Using session DIR
debug:  ==== Session Name:
debug:  ==== is_readable

If i :source /path/to/file manually i got this:

debug:    cwd: /home/user/.local/share/nvim/lazy/auto-session/lua
debug:    target: /home/user/.config/nvim
debug:    changed window: false
debug:    scope: global
debug:  ==== is_allowed_dirs_enabled
debug:  bypass_save_by_filetype: false
debug:  ==== SaveSession
debug:  Running pre-save command: Neotree action=close
debug:  Session saved at /home/user/.local/share/nvim/sessions/\%home\%user\%.local\%share\%nvim\%lazy\%auto-session\%lua.vim
debug:  DirChanged
debug:    cwd: /home/user/.config/nvim
debug:    changed window: false
debug:    scope: global
debug:  sessions_dir, session_file
debug:  ==== Using session DIR
debug:  ==== Session Name:
debug:  ==== is_readable
info:  Session restored from /home/user/.local/share/nvim/sessions/\%home\%user\%.config\%nvim.vim
"~/.config/nvim/lua/lazynvim.lua" 45L, 780B
Error detected while processing /home/user/.local/share/nvim/sessions/%home%user%.config%nvim.vim:
line   52:
E108: No such variable: "SessionLoad"

@tmpm697
Copy link
Author

tmpm697 commented Jan 15, 2023

tmpm697 21:51:33
what would causing %bd! not clean up all buffers with my config, what options in my config can cause this issue?
note that i'm using cokeline buffer bar

seandewar 21:52:55
tmpm697: probably a plugin

tmpm697 21:53:01
yes that's cokeline.

tmpm697 21:53:20
if i disable it, %bd! gives result same as with --clean
i'm fked with this as this is reason why auto-session not restore my color correctly.
tks for the info

this issue seems from https://github.com/noib3/nvim-cokeline that does not clean all buffers but keep minimal one, it probably overwrite %bd! behavior and causes this plugin to lose color.

if i do %bd! manually with cokeline enable, the un-deleted buffer loses color also, so it lost color before switch session with auto-session.

@rmagatti
Copy link
Owner

To be clear, this still has nothing to do with auto-session. It's the combination of your plugins and the order of operations of those plugins loading that seem to be causing issues for you. Auto Session does not control whether color or really anything else gets restored, that's controlled by vim's own session management, auto-session merely wraps that. :h Session

@nnako
Copy link

nnako commented Feb 5, 2023

sorry for still posting into this already closed issue... but ...

it seems that the filetype somehow gets lost when switching sessions. so, when adding e.g. a set filetype=python within the session's vim configuration file somewhere below the respective edit <filepath> entry, the color is properly restored for this file type after switching to that session:

tabnext
edit ~/_Python/LIB__freeplane/src/freeplane.py
set filetype=python  " <-- this one is the added line
argglobal
setlocal fdm=manual
setlocal fde=0
setlocal fmr={{{,}}}
...

I don't know though, whether this "problem" should be fixed on the auto-session side or rather within some core component of neovim. but on the auto-session side, it would definitely solve the issue, for now, if the buffer's file type would be stored alongside the other information into the session's vim file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants