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

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

Merged
merged 2 commits into from
Jun 15, 2024

Conversation

Kamilcuk
Copy link
Contributor

@Kamilcuk Kamilcuk commented Jun 13, 2024

📑 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.

Copy link

github-actions bot commented Jun 13, 2024

Review Checklist

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

Proper conventional commit scoping:

  • If you are adding a new plugin, the scope would be the name of the category it is being added into. ex. feat(utility): added noice.nvim plugin

  • If you are modifying a pre-existing plugin or pack, the scope would be the name of the plugin folder. ex. fix(noice-nvim): fix LSP handler error

  • Pull request title has the appropriate conventional commit type and scope where the scope is the name of the pre-existing directory in the project as described above

  • README is properly formatted and uses fenced in links with <url> unless they are inside a [title](url)

  • Proper usage of opts table rather than setting things up with the config function.

@Uzaaft
Copy link
Member

Uzaaft commented Jun 13, 2024

Do you have the link to our documentation?

@Uzaaft
Copy link
Member

Uzaaft commented Jun 14, 2024

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

Copy link
Member

@mehalter mehalter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small suggestion to improve the performance. Thanks so much for opening this up, it's a great addition! Also really like the improvement for the StdinReadPost

lua/astrocommunity/recipes/auto-session-restore/init.lua Outdated Show resolved Hide resolved
@mehalter
Copy link
Member

@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
Copy link
Contributor Author

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
Copy link
Member

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

Copy link
Member

@mehalter mehalter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks so much!

@mehalter mehalter merged commit 6e4580b into AstroNvim:main Jun 15, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants