From 8168c4bf04a42fad499c02cb43cd09f29c0be4c8 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Fri, 6 Dec 2024 14:24:07 +0100 Subject: [PATCH] plugin/resession: Handle errors with pcall and fix session name --- lua/valeth/plugins/resession.lua | 70 ++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/lua/valeth/plugins/resession.lua b/lua/valeth/plugins/resession.lua index 631817b..472c0d9 100644 --- a/lua/valeth/plugins/resession.lua +++ b/lua/valeth/plugins/resession.lua @@ -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 = { "stevearc/resession.nvim", } @@ -5,36 +43,8 @@ local spec = { spec.config = function() local resession = require("resession") - local function session_name() - local working_dir = vim.fn.getcwd() - 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 - }) + vim.api.nvim_create_autocmd("VimEnter", { callback = load_session, }) + vim.api.nvim_create_autocmd("VimLeavePre", { callback = save_session }) resession.setup({}) end