1
0

plugin/resession: Handle errors with pcall and fix session name

This commit is contained in:
Patrick Auernig 2024-12-06 14:24:07 +01:00
parent 472bfc9bcf
commit 8168c4bf04

View File

@ -1,3 +1,41 @@
local function started_without_args()
return vim.fn.argc(-1) == 0
end
local function session_name()
local working_dir = vim.fn.getcwd()
local git_branch = vim.fn.systemlist("git branch --show-current")[1]
if vim.v.shell_error == 0 then
return working_dir .. "-" .. git_branch .. ".git"
else
return working_dir
end
end
local function load_session()
if not started_without_args() then
return
end
local ok = pcall(require("resession").load, session_name(), { dir = "dirsession", silence_errors = true })
if not ok then
vim.notify("Failed to load session", vim.log.levels.WARN)
end
end
local function save_session()
if not started_without_args() then
return
end
local ok = pcall(require("resession").save, session_name(), { dir = "dirsession", notify = false })
if not ok then
vim.notify("Failed to save session", vim.log.levels.WARN)
end
end
local spec = { local spec = {
"stevearc/resession.nvim", "stevearc/resession.nvim",
} }
@ -5,36 +43,8 @@ local spec = {
spec.config = function() spec.config = function()
local resession = require("resession") local resession = require("resession")
local function session_name() vim.api.nvim_create_autocmd("VimEnter", { callback = load_session, })
local working_dir = vim.fn.getcwd() vim.api.nvim_create_autocmd("VimLeavePre", { callback = save_session })
local git_branch = vim.fn.system("git branch --show-current")
if vim.v.shell_error == 0 then
return working_dir .. git_branch
else
return working_dir
end
end
local function started_without_args()
return vim.fn.argc(-1) == 0
end
-- Only restore and save session if NeoVim was started without any arguments
vim.api.nvim_create_autocmd("VimEnter", {
callback = function()
if started_without_args() then
resession.load(session_name(), { dir = "dirsession", silence_errors = true })
end
end
})
vim.api.nvim_create_autocmd("VimLeavePre", {
callback = function()
if started_without_args() then
resession.save(session_name(), { dir = "dirsession", notify = false })
end
end
})
resession.setup({}) resession.setup({})
end end