Skip to content

Commit

Permalink
feat(recipes): add auto-session-restore (#1036)
Browse files Browse the repository at this point in the history
* feat(recipes): add auto-session-restore

* feat(auto-session-restore): fix stdin handling and fallback to alpha when no session available
  • Loading branch information
Kamilcuk committed Jun 15, 2024
1 parent d383aaf commit 6e4580b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lua/astrocommunity/recipes/auto-session-restore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# AstroLSP - Automatically Restore Previous Session

**Website:** <https://docs.astronvim.com/recipes/sessions/#automatically-restore-previous-session>

This plugin specification configures AstroLSP to automatically
restore their previous session for a given directory when opening Neovim with no arguments.
49 changes: 49 additions & 0 deletions lua/astrocommunity/recipes/auto-session-restore/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
return {
{
"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()
-- Logic copied from https://github.com/AstroNvim/AstroNvim/blob/365aa6e083dcd25fa3d1c8a2515d7e71a03d51d3/lua/astronvim/plugins/alpha.lua#L49
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 possible, load session
if not pcall(function() require("resession").load(vim.fn.getcwd(), { dir = "dirsession" }) end) then
-- if session was not loaded, if possible, load alpha
require("lazy").load { plugins = { "alpha-nvim" } }
if pcall(function() require("alpha").start(true) end) then
vim.schedule(function() vim.cmd.doautocmd "FileType" end)
end
end
end,
},
},
},
},
},
}

0 comments on commit 6e4580b

Please sign in to comment.