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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
},
},
},
},
}
Loading