rmagatti / auto-session

A small automated session manager for Neovim
MIT License
1.24k stars 47 forks source link

[FEATURE] Auto save manually named sessions #386

Open kqvanity opened 2 days ago

kqvanity commented 2 days ago

Is your feature request related to a problem? Please describe.

Describe the solution you'd like I'd favor support for named sessions for multiple reasons

Describe alternatives you've considered I've tried setting up hooks to named of the current session when a session-save event is triggered, but it became recursive

Additional context N/A

cameronr commented 2 days ago

Thanks for filling the new issue!

Just to make sure we're on the same page, auto-session already supports named sessions, just call :SessionSave with an argument, e.g. :SessionSave mysession. That will save a session called mysession that isn't named for the cwd. However, manually named session aren't currently used when autosaving on exit (it alway saves to a session name derived from the cwd).

I don't think it would hard to add the ability for manually named sessions to be autosaved on exit (but there isn't currently a way to autoload them on start). That would look like this:

  1. start nvim, have some files open
  2. Save a session via :SessionSave mysession or restore a manually named session via :SessionSearch or :SessionRestore
  3. open/close files
  4. exit nvim
  5. auto-session saves to mysession (instead of session named for cwd)

Would that work for your workflow?

kqvanity commented 2 days ago

Thanks for filling the new issue!

I should be the one thanking you for taking the time to consider this feature.

Would that work for your workflow?

Exactly.

Ff named sessions would be be auto-updated, would that allow loading/reading single separate files e.g., shell profiles, separate singular config files, etc.?

cameronr commented 2 days ago

Ok, I think I have it working the way I described above.

If you want to test it, you can temporary change the top of your auto-session config to:

  -- 'rmagatti/auto-session',
  'cameronr/auto-session',

After changing the config, you'll have to open Lazy (or whatever plugin manager you use) and update the plugin to actually pull it down (and restart nvim just to be sure).

Ff named sessions would be be auto-updated, would that allow loading/reading single separate files e.g., shell profiles, separate singular config files, etc.?

I'm not quite sure what that means. You could have a manually named session for whatever file(s) you wanted to. The only issue is you'll have to manually load them since they're not named after the cwd. But once loaded, they should update when you exit (if you use my fork above).

I'm curious, though, why you'd want a session for a single file?

kqvanity commented 1 day ago

I did test out yesterday, and noticed flakey behavior that i've been trying to reproduce. namely, If I have multiple named sessions opened. it seems like the very first one is the only that get auto-updated. Removing ~/.local/share/nvim/auto_session/session_control.json solves the issue temporarily however.

not quite sure what that means.

I'm curious, though, why you'd want a session for a single file?

The ideal workflow for me would be having a mix of CWD and named sessions for entire projects/dir that I explicitly SessionSave. It'd be a huge convenience to also be able to auto restore/load let's say, shell profiles when directly opening them e.g., nvim .zshrc

cameronr commented 1 day ago

Hmm, session_control.json is only used for the alternate session feature (in :SessionSearch you can swap to session before the current session with <C-s>; it's not used for anything else so I'd be surprised if it was involved somehow. If you have multiple nvim sessions running all loaded from the same session, the last one that exits will write it's state to the session file and that will be what's loaded the next time nvim is started.

When you say you have "multiple named sessions opened", do you mean in multiple instances of nvim or something else?

I really want to make sure I understand your use case. When you run nvim .zshrc what session are you hoping to have it load? Are there other files you want it to open at the same time?

Is it possible for you to describe your desired setup with actual examples, maybe with this template:

Session #n

For example:

Session 1:

Session 2:

kqvanity commented 1 day ago

When you say you have "multiple named sessions opened", do you mean in multiple instances of nvim or something else?

Having multiple nvim instances, yes. $ nvim ... $ nvim. That's my stock config. If you can't reproduce this flakey behavior, then it's probably on my end (even though I've tried so many times to figure out what's wrong`

    {
      -- 'rmagatti/auto-session',
      'cameronr/auto-session',
      lazy = false,

      ---enables autocomplete for opts
      ---@module "auto-session"
      ---@type AutoSession.Config
      opts = {
          enabled = true, -- Enables/disables auto creating, saving and restoring
          root_dir = vim.fn.stdpath "data" .. "/sessions/", -- Root dir where sessions will be stored
          auto_save = true, -- Enables/disables auto saving session on exit
          auto_restore = false, -- Enables/disables auto restoring session on start
          auto_create = false, -- Enables/disables auto creating new session files. Can take a function that should return true/false if a new session file should be created or not
          suppressed_dirs = nil, -- Suppress session restore/create in certain directories
          -- suppressed_dirs = { '~/', '~/Projects', '~/Downloads', '/' },
          allowed_dirs = nil, -- Allow session restore/create in certain directories
          auto_restore_last_session = false, -- On startup, loads the last saved session if session for cwd does not exist
          use_git_branch = false, -- Include git branch name in session name
          lazy_support = true, -- Automatically detect if Lazy.nvim is being used and wait until Lazy is done to make sure session is restored correctly. Does nothing if Lazy isn't being used. Can be disabled if a problem is suspected or for debugging
          bypass_save_filetypes = { 'alpha', 'dashboard' }, -- -- List of file types to bypass auto save when the only buffer open is one of the file types listed, useful to ignore dashboards (or whatever dashboard you use)
          close_unsupported_windows = true, -- Close windows that aren't backed by normal file before autosaving a session
          args_allow_single_directory = true, -- Follow normal sesion save/load logic if launched with a single directory as the only argument
          args_allow_files_auto_save = false, -- Allow saving a session even when launched with a file argument (or multiple files/dirs). It does not load any existing session first. While you can just set this to true, you probably want to set it to a function that decides when to save a session when launched with file args. See documentation for more detail
          continue_restore_on_error = false, -- Keep loading the session even if there's an error
          cwd_change_handling = true, -- Follow cwd changes, saving a session before change and restoring after
          log_level = "info", -- Sets the log level of the plugin (debug, info, warn, error).

          session_lens = {
            load_on_setup = true, -- Initialize on startup (requires Telescope)
            theme_conf = { -- Pass through for Telescope theme options
              -- layout_config = { -- As one example, can change width/height of picker
              --   width = 0.8,    -- percent of window
              --   height = 0.5,
              -- },
            },
            previewer = false, -- File preview for session picker

            mappings = {
              -- Mode can be a string or a table, e.g. {"i", "n"} for both insert and normal mode
              delete_session = { "i", "<C-D>" },
              alternate_session = { "i", "<C-S>" },
              copy_session = { "i", "<C-Y>" },
            },

            session_control = {
              control_dir = vim.fn.stdpath "data" .. "/auto_session/", -- Auto session control dir, for control files, like alternating between two sessions with session-lens
              control_filename = "session_control.json", -- File name of the session control file
            },

          },
        }
        -- log_level = 'debug',
    },

Is it possible for you to describe your desired setup with actual examples, maybe with this template:

Session 1

Session 2

Mind you that individual files would be loaded automatically without even having designated session names for them in session_search for example

cameronr commented 13 hours ago

Thanks for the detailed response! I'll break my response into sections:

  1. Config

While it's not necessary, you don't have to include the default values in your config so you could trim your config to:

          auto_restore = false, -- Enables/disables auto restoring session on start
          auto_create = false, -- Enables/disables auto creating new session files. Can take a function that should return true/false if a new session file should be created or not
          bypass_save_filetypes = { 'alpha', 'dashboard' }, -- -- List of file types to bypass auto save when the only buffer open is one of the file types listed, useful to ignore dashboards (or whatever dashboard you use)
          continue_restore_on_error = false, -- Keep loading the session even if there's an error
          cwd_change_handling = true, -- Follow cwd changes, saving a session before change and restoring after
          log_level = "info", -- Sets the log level of the plugin (debug, info, warn, error).

Also, I'm curious why you have continue_restore_on_error set to false? Outside of some edge cases, that should really be set to true. Have you had any session load errors?

  1. Flakiness: can you explain more what flaky behavior you're seeing? Is it sessions not restoring correctly? Or is it something else that's not working? The more detail you can provide the easier it will be for me to understand / potentially reproduce the issue.

  2. Session 1 behavior: With my fork, that should now work. If it's not working for you, please let me know.

  3. For single files, what state are you trying to restore? If it's remembering the cursor position (line/column), you can use this autocmd (it doesn't even need a session, just uses marks, can go anywhere in your nvim config, doesn't have to go in your auto-session config):

-- from: https://www.reddit.com/r/neovim/comments/1abd2cq/what_are_your_favorite_tricks_using_neovim/
vim.api.nvim_create_autocmd('BufReadPost', {
  desc = 'Open file at the last position it was edited earlier',
  command = 'silent! normal! g`"zv',
})

Beyond remembering your cursor position, is there other data you're trying to restore with single file sessions?