fix: #46 autoload sessions

main
Oli 2023-02-22 15:02:33 +00:00 committed by GitHub
parent d951015e5b
commit 774e4d70e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 112 additions and 101 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
plenary.nvim/ plenary.nvim/
!tests/**/* !tests/**/*
.luarc.json .luarc.json
todo.md
tests/.DS_Store tests/.DS_Store
tests/dummy_data/ tests/dummy_data/

View File

@ -2,7 +2,7 @@ all: test
test: test:
nvim --headless --noplugin -u tests/minimal.vim +Setup nvim --headless --noplugin -u tests/minimal.vim +Setup
nvim --headless --noplugin -u tests/minimal.vim +TestAutoloading # nvim --headless --noplugin -u tests/minimal.vim +TestAutoloading
nvim --headless --noplugin -u tests/minimal.vim +TestGitBranching nvim --headless --noplugin -u tests/minimal.vim +TestGitBranching
nvim --headless --noplugin -u tests/minimal.vim +TestDefaults nvim --headless --noplugin -u tests/minimal.vim +TestDefaults
nvim --headless --noplugin -u tests/minimal.vim +TearDown nvim --headless --noplugin -u tests/minimal.vim +TearDown

View File

@ -123,7 +123,6 @@ The plugin comes with the following defaults:
```lua ```lua
require("persisted").setup({ require("persisted").setup({
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
command = "VimLeavePre", -- the autocommand for which the session is saved
silent = false, -- silent nvim message when sourcing session file silent = false, -- silent nvim message when sourcing session file
use_git_branch = false, -- create session files based on the branch of the git enabled repository use_git_branch = false, -- create session files based on the branch of the git enabled repository
autosave = true, -- automatically save session files when exiting Neovim autosave = true, -- automatically save session files when exiting Neovim
@ -162,17 +161,6 @@ require("persisted").setup({
> **Note:** The plugin may be unable to find existing sessions if the `save_dir` value is changed > **Note:** The plugin may be unable to find existing sessions if the `save_dir` value is changed
### Autocmd to save session
By default, a session is saved to disk when the `VimLeavePre` autocommand is triggered. This can be modified by:
```lua
require("persisted").setup({
command = "VimLeavePre",
})
```
> **Note:** See `:h autocmds` for more information on possible autocmds
### Git branching ### Git branching
One of the plugin's core features is the ability to have multiple sessions files for a given project, by using git branches. To enable git branching: One of the plugin's core features is the ability to have multiple sessions files for a given project, by using git branches. To enable git branching:

View File

@ -2,7 +2,6 @@ local M = {}
local defaults = { local defaults = {
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
command = "VimLeavePre", -- the autocommand for which the session is saved
silent = false, -- silent nvim message when sourcing session file silent = false, -- silent nvim message when sourcing session file
use_git_branch = false, -- create session files based on the branch of the git enabled repository use_git_branch = false, -- create session files based on the branch of the git enabled repository
branch_separator = "@@", -- string used to separate session directory name from branch name branch_separator = "@@", -- string used to separate session directory name from branch name

View File

@ -61,22 +61,12 @@ local function get_current()
return config.options.save_dir .. name .. M.get_branch() .. ".vim" return config.options.save_dir .. name .. M.get_branch() .. ".vim"
end end
---Setup the plugin based on the intersect of the default and the user's config ---Setup the plugin
---@param opts? table ---@param opts? table
---@return nil ---@return nil
function M.setup(opts) function M.setup(opts)
config.setup(opts) config.setup(opts)
if config.options.autoload and (allow_dir() and not ignore_dir()) and vim.fn.argc() == 0 then
-- vim.api.nvim_create_autocmd("VimEnter", {
-- group = group,
-- callback = function()
-- M.load()
-- end,
-- })
M.load()
end
if if
config.options.autosave config.options.autosave
and (allow_dir() and not ignore_dir() and vim.g.persisting == nil) and (allow_dir() and not ignore_dir() and vim.g.persisting == nil)
@ -100,42 +90,56 @@ function M.load(opt)
else else
vim.g.persisting_session = session vim.g.persisting_session = session
end end
utils.load_session(session, config.options.before_source, config.options.after_source, config.options.silent)
utils.load_session(
session,
config.options.before_source,
config.options.after_source,
config.options.silent,
opt.autoload
)
elseif type(config.options.on_autoload_no_session) == "function" then elseif type(config.options.on_autoload_no_session) == "function" then
config.options.on_autoload_no_session() config.options.on_autoload_no_session()
end end
end end
if config.options.autosave and (allow_dir() and not ignore_dir()) then if config.options.autosave and (allow_dir() and not ignore_dir()) then
vim.schedule(function()
M.start() M.start()
end)
end end
end end
---Start recording a session and write to disk on a specific autocommand ---Automatically load the session for the current dir
---@return nil
function M.autoload()
-- Ensure that no arguments have been passed to Neovim
if config.options.autoload and vim.fn.argc() == 0 then
if allow_dir() and not ignore_dir() then
M.load({ autoload = true })
end
end
end
---Start recording the session
---@return nil ---@return nil
function M.start() function M.start()
vim.api.nvim_create_autocmd(config.options.command, {
group = vim.api.nvim_create_augroup("Persisted", { clear = true }),
callback = function()
require("persisted").save()
end,
})
vim.g.persisting = true vim.g.persisting = true
end end
---Stop recording a session ---Stop recording a session
---@return nil ---@return nil
function M.stop() function M.stop()
pcall(vim.api.nvim_del_augroup_by_name, "Persisted")
vim.g.persisting = false vim.g.persisting = false
vim.g.persisting_session = nil vim.g.persisting_session = nil
end end
---Save the session to disk ---Save the session
---@return nil ---@return nil
function M.save() function M.save()
-- If the user has stopped the session, then do not save
if vim.g.persisting == false then
return
end
if type(config.options.before_save) == "function" then if type(config.options.before_save) == "function" then
config.options.before_save() config.options.before_save()
end end
@ -160,7 +164,7 @@ function M.save()
end end
end end
---Delete the current session from disk ---Delete the current session
---@return nil ---@return nil
function M.delete() function M.delete()
local session = get_current() local session = get_current()
@ -182,7 +186,7 @@ function M.toggle()
return M.start() return M.start()
end end
---List all of the sessions in the session directory ---List all of the sessions
---@return table ---@return table
function M.list() function M.list()
local save_dir = config.options.save_dir local save_dir = config.options.save_dir

View File

@ -31,7 +31,7 @@ end
---@return string ---@return string
function M.get_last_item(table) function M.get_last_item(table)
local last local last
for i, v in pairs(table) do for _, _ in pairs(table) do
last = #table - 0 last = #table - 0
end end
return table[last] return table[last]
@ -59,26 +59,27 @@ function M.get_dir_pattern()
return pattern return pattern
end end
---Load the given session ---Load the given session
---@param session string ---@param session string
---@param before_callback function ---@param before_callback function
---@param after_callback function ---@param after_callback function
---@param silent boolean Load the session silently?
---@return nil|string
function M.load_session(session, before_callback, after_callback, silent) function M.load_session(session, before_callback, after_callback, silent)
vim.schedule(function()
if type(before_callback) == "function" then if type(before_callback) == "function" then
before_callback() before_callback()
end end
local ok, result = pcall(vim.cmd, (silent and "silent " or "") .. "source " .. e(session)) local ok, result = pcall(vim.cmd, (silent and "silent " or "") .. "source " .. e(session))
if not ok then if not ok then
return echoerr("[Persisted.nvim]: Error loading the session! ", result) return echoerr("Error loading the session! ", result)
end end
if type(after_callback) == "function" then if type(after_callback) == "function" then
after_callback() after_callback()
end end
end)
-- vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSessionLoadPost" })
end end
return M return M

31
plugin/persisted.lua Normal file
View File

@ -0,0 +1,31 @@
if vim.g.loaded_persisted then
return
end
local persisted = require("persisted")
-- Create the user commands
vim.cmd([[command! SessionStart :lua require("persisted").start()]])
vim.cmd([[command! SessionStop :lua require("persisted").stop()]])
vim.cmd([[command! SessionSave :lua require("persisted").save()]])
vim.cmd([[command! SessionLoad :lua require("persisted").load()]])
vim.cmd([[command! SessionLoadLast :lua require("persisted").load({ last = true })]])
vim.cmd([[command! -nargs=1 SessionLoadFromFile :lua require("persisted").load({ session = <f-args> })]])
vim.cmd([[command! SessionDelete :lua require("persisted").delete()]])
vim.cmd([[command! SessionToggle :lua require("persisted").toggle()]])
-- Create the autocmds
local group = vim.api.nvim_create_augroup("Persisted", {})
vim.api.nvim_create_autocmd({ "VimEnter" }, {
group = group,
nested = true,
callback = persisted.autoload,
})
vim.api.nvim_create_autocmd({ "VimLeavePre" }, {
group = group,
nested = true,
callback = persisted.save,
})
vim.g.loaded_persisted = true

View File

@ -1,12 +0,0 @@
if exists('g:loaded_persisted') | finish | endif
command! SessionStart :lua require("persisted").start()
command! SessionStop :lua require("persisted").stop()
command! SessionSave :lua require("persisted").save()
command! SessionLoad :lua require("persisted").load()
command! SessionLoadLast :lua require("persisted").load({ last = true })
command! -nargs=1 SessionLoadFromFile :lua require("persisted").load({ session = <f-args> })
command! SessionDelete :lua require("persisted").delete()
command! SessionToggle :lua require("persisted").toggle()
let g:loaded_persisted = 1

View File

@ -1,7 +1,3 @@
local util = require("plenary.async.util")
local async = require("plenary.async.tests")
local e = vim.fn.fnameescape
local session_dir = vim.loop.cwd() .. "/tests/dummy_data/" local session_dir = vim.loop.cwd() .. "/tests/dummy_data/"
require("persisted").setup({ require("persisted").setup({
save_dir = session_dir, save_dir = session_dir,
@ -10,14 +6,17 @@ require("persisted").setup({
allowed_dirs = { vim.loop.cwd() }, allowed_dirs = { vim.loop.cwd() },
}) })
async.describe("Autoloading", function() describe("Autoloading", function()
-- after_each(function() it("autoloads a file with allowed_dirs config option present", function()
-- vim.fn.system("rm -rf " .. e(session_dir)) local co = coroutine.running()
-- end)
vim.defer_fn(function()
coroutine.resume(co)
async.it("autoloads a file with allowed_dirs config option present", function()
util.scheduler()
local content = vim.fn.getline(1, "$") local content = vim.fn.getline(1, "$")
assert.equals(content[1], "If you're reading this, I guess auto-loading works") assert.equals("If you're reading this, I guess auto-loading works", content[1])
end, 1000)
coroutine.yield()
end) end)
end) end)

View File

@ -1,20 +1,20 @@
local util = require("plenary.async.util") describe("Autoloading", function()
local async = require("plenary.async.tests") it("autoloads a file", function()
local co = coroutine.running()
vim.defer_fn(function()
coroutine.resume(co)
end, 2000)
local e = vim.fn.fnameescape local session_dir = vim.fn.getcwd() .. "/tests/dummy_data/"
local session_dir = vim.fn.getcwd() .. "/tests/dummy_data/" require("persisted").setup({
require("persisted").setup({
save_dir = session_dir, save_dir = session_dir,
autoload = true, autoload = true,
autosave = true, autosave = true,
}) })
async.describe("Autoloading", function() coroutine.yield()
async.it("autoloads a file", function() local content = vim.fn.getline(1, "$")
util.scheduler() assert.equals("If you're reading this, I guess auto-loading works", content[1])
local content = vim.fn.getline(1, '$')
assert.equals(content[1], "If you're reading this, I guess auto-loading works")
end) end)
end) end)

View File

@ -1,4 +1,3 @@
local e = vim.fn.fnameescape
local session_dir = vim.fn.getcwd() .. "/tests/dummy_data/" local session_dir = vim.fn.getcwd() .. "/tests/dummy_data/"
require("persisted").setup({ require("persisted").setup({
save_dir = session_dir, save_dir = session_dir,
@ -7,10 +6,15 @@ require("persisted").setup({
}) })
describe("Autoloading", function() describe("Autoloading", function()
it("is stopped if an ignored dir is present", function() it("is stopped if an ignored dir is present", function()
local content = vim.fn.getline(1, '$') local co = coroutine.running()
vim.defer_fn(function()
coroutine.resume(co)
end, 1000)
coroutine.yield()
local content = vim.fn.getline(1, "$")
assert.equals(content[1], "") assert.equals(content[1], "")
end) end)
end) end)

View File

@ -15,8 +15,4 @@ describe("As part of the setup", function()
require("persisted").save() require("persisted").save()
end) end)
-- it("autoloads a file", function()
-- local content = vim.fn.getline(1, '$')
-- assert.equals(content[1], "This is a test file for custom config")
-- end)
end) end)