AstroNvim / astrocommunity

A community repository of common plugin specifications
GNU General Public License v3.0
1.04k stars 212 forks source link

feat(recipes): add auto-session-restore #1036

Closed Kamilcuk closed 2 weeks ago

Kamilcuk commented 2 weeks ago

📑 Description

Adds auto-session-restore like in documentation, but also detect if nvim has been started with stdin redirected, like stream | nvim or like export MAPAGER='nvim +Man!'; man man.

The stdin detection is taken from https://github.com/thaerkh/vim-workspace/blob/master/plugin/workspace.vim#L268 .

ℹ Additional Information

Would be nice to add the stdin input handling also to documentation.

github-actions[bot] commented 2 weeks ago

Review Checklist

Does this PR follow the [Contribution Guidelines](development guidelines)? Following is a partial checklist:

Proper conventional commit scoping:

Uzaaft commented 2 weeks ago

Do you have the link to our documentation?

Uzaaft commented 2 weeks ago

Link to docs: https://docs.astronvim.com/recipes/sessions/

mehalter commented 2 weeks ago

@Kamilcuk here are 2 options:

  1. return true, this might work? hopefully but it might be the same as once = true:
return {
  "AstroNvim/astrocore",
  ---@type AstroCoreOpts
  opts = {
    autocmds = {
      -- disable alpha autostart
      alpha_autostart = false,
      restore_session = {
        {
          event = { "VimEnter", "StdinReadPost" },
          desc = "Restore previous directory session if neovim opened with no arguments",
          nested = true, -- trigger other autocommands as buffers open
          callback = function(args)
            -- Only load the session if nvim was started with no args
            if args.event == "VimEnter" and vim.fn.argc(-1) == 0 then
              -- try to load a directory session using the current working directory
              require("resession").load(vim.fn.getcwd(), { dir = "dirsession", silence_errors = true })
            end
            return true
          end,
        },
      },
    },
  },
}
  1. This will definitely work
return {
  "AstroNvim/astrocore",
  ---@type AstroCoreOpts
  opts = {
    autocmds = {
      -- disable alpha autostart
      alpha_autostart = false,
      restore_session = {
        {
          event = { "VimEnter", "StdinReadPost" },
          desc = "Restore previous directory session if neovim opened with no arguments",
          nested = true, -- trigger other autocommands as buffers open
          callback = function(args)
            -- Only load the session if nvim was started with no args
            if args.event == "VimEnter" and vim.fn.argc(-1) == 0 then
              -- try to load a directory session using the current working directory
              require("resession").load(vim.fn.getcwd(), { dir = "dirsession", silence_errors = true })
            end
            vim.api.nvim_del_augroup_by_name "restore_session"
          end,
        },
      },
    },
  },
}
Kamilcuk commented 2 weeks ago

hi:

  1. I am testing by doing callback = function(args) print(args.event) ..... and then executing man man. I am getting both events:
:messages                                                                                                                                                                                                         
StdinReadPost                                                                                                                                                                                                     
VimEnter     

Both events are printed, so it does not work.

  1. Yup, it works.

I would be happy with that, but now I have a better version:

  {
    "AstroNvim/astrocore",
    ---@type AstroCoreOpts
    opts = {
      autocmds = {
        -- disable alpha autostart
        alpha_autostart = false,
        restore_session = {
          {
            event = { "VimEnter" },
            desc = "Restore previous directory session if neovim opened with no arguments",
            nested = true, -- trigger other autocommands as buffers open
            callback = function(args)
              local should_skip
              local lines = vim.api.nvim_buf_get_lines(0, 0, 2, false)
              if
                vim.fn.argc() > 0 -- don't start when opening a file
                or #lines > 1 -- don't open if current buffer has more than 1 line
                or (#lines == 1 and lines[1]:len() > 0) -- don't open the current buffer if it has anything on the first line
                or #vim.tbl_filter(function(bufnr) return vim.bo[bufnr].buflisted end, vim.api.nvim_list_bufs()) > 1 -- don't open if any listed buffers
                or not vim.o.modifiable -- don't open if not modifiable
              then
                should_skip = true
              else
                for _, arg in pairs(vim.v.argv) do
                  if arg == "-b" or arg == "-c" or vim.startswith(arg, "+") or arg == "-S" then
                    should_skip = true
                    break
                  end
                end
              end
              if should_skip then return end
              if
                vim.tbl_contains(require("resession").list { dir = "dirsession" }, (vim.fn.getcwd():gsub("/", "_")))
              then
                require("resession").load(vim.fn.getcwd(), { dir = "dirsession" })
              else
                require("lazy").load { plugins = { "alpha-nvim" } }
                require("alpha").start(true)
                vim.schedule(function() vim.cmd.doautocmd "FileType" end)
              end
            end,
          },
        },
      },
    },
  },

I don't like the idea that alpha will not start never. So I copied code from https://github.com/AstroNvim/AstroNvim/blob/365aa6e083dcd25fa3d1c8a2515d7e71a03d51d3/lua/astronvim/plugins/alpha.lua#L44 . If there is a session, use it, otherwise show alpha. This actually works and shows alpha on directories without session, but loads session with directories with session. And also echo hi | nvim and man man both work, because I think they catch not vim.o.modifiable.

mehalter commented 2 weeks ago

Nice! I like this one! I think the only thing is adding protection against if alpha isn't installed