-- One session per directory: the windows and buffers of the last time the IDE was closed there. -- Saved on leaving, restored when asked for (the dashboard, sr). Session files live in -- the IDE's own state directory and nowhere else: a Session.vim found in a project is not read. local M = {} local function dir() return vim.fn.stdpath("state") .. "/sessions" end --- The session's name for a directory: readable, and unique to the full path. function M.name(cwd) cwd = cwd or vim.fn.getcwd() local base = vim.fn.fnamemodify(cwd, ":t"):gsub("[^%w%-_.]", "_") if base == "" then base = "root" end return base .. "-" .. vim.fn.sha256(cwd):sub(1, 12) .. ".vim" end function M.exists(cwd) return vim.fn.filereadable(dir() .. "/" .. M.name(cwd)) == 1 end local function has_files() for _, b in ipairs(vim.api.nvim_list_bufs()) do if vim.bo[b].buflisted and vim.bo[b].buftype == "" and vim.api.nvim_buf_get_name(b) ~= "" then return true end end return false end function M.save(quiet, leaving) if not has_files() then if not quiet then vim.notify("Session: no files are open") end return false end -- A session holds the code's windows: the bars are put away while it is written, and are -- laid out again by the layout itself, the next time as now. local bars, layout = pcall(require, "llembas.layout") if bars then pcall(layout.remember_widths) pcall(layout.hide) end local ok, err = pcall(function() require("mini.sessions").write(M.name(), { verbose = false }) end) if bars and not leaving then pcall(layout.apply) end if not quiet then vim.notify(ok and "Session saved for " .. vim.fn.getcwd() or "Session: " .. tostring(err)) end return ok end function M.restore() if not M.exists() then return vim.notify("Session: none saved for " .. vim.fn.getcwd()) end local bars, layout = pcall(require, "llembas.layout") if bars then pcall(layout.hide) end local ok, err = pcall(function() require("mini.sessions").read(M.name(), { verbose = false }) end) if bars then vim.schedule(function() pcall(layout.apply) end) end if not ok then vim.notify("Session: " .. tostring(err), vim.log.levels.WARN) end end function M.delete() if not M.exists() then return vim.notify("Session: none saved for " .. vim.fn.getcwd()) end vim.fn.delete(dir() .. "/" .. M.name()) vim.notify("Session forgotten for " .. vim.fn.getcwd()) end function M.setup() vim.fn.mkdir(dir(), "p") require("mini.sessions").setup({ autoread = false, autowrite = false, directory = dir(), file = "", -- never a session file from the working directory verbose = { read = false, write = false, delete = false }, }) vim.api.nvim_create_autocmd("VimLeavePre", { group = vim.api.nvim_create_augroup("llembas_ide_sessions", { clear = true }), callback = function() if vim.g.llembas_ide_no_session then return end M.save(true, true) end, }) end return M