fix: #46 autoload sessions
parent
d951015e5b
commit
774e4d70e9
|
|
@ -1,6 +1,7 @@
|
|||
plenary.nvim/
|
||||
!tests/**/*
|
||||
.luarc.json
|
||||
todo.md
|
||||
|
||||
tests/.DS_Store
|
||||
tests/dummy_data/
|
||||
|
|
|
|||
2
Makefile
2
Makefile
|
|
@ -2,7 +2,7 @@ all: test
|
|||
|
||||
test:
|
||||
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 +TestDefaults
|
||||
nvim --headless --noplugin -u tests/minimal.vim +TearDown
|
||||
|
|
|
|||
12
README.md
12
README.md
|
|
@ -123,7 +123,6 @@ The plugin comes with the following defaults:
|
|||
```lua
|
||||
require("persisted").setup({
|
||||
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
|
||||
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
|
||||
|
|
@ -162,17 +161,6 @@ require("persisted").setup({
|
|||
|
||||
> **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
|
||||
|
||||
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:
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ local M = {}
|
|||
|
||||
local defaults = {
|
||||
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
|
||||
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
|
||||
|
|
|
|||
|
|
@ -61,22 +61,12 @@ local function get_current()
|
|||
return config.options.save_dir .. name .. M.get_branch() .. ".vim"
|
||||
end
|
||||
|
||||
---Setup the plugin based on the intersect of the default and the user's config
|
||||
---Setup the plugin
|
||||
---@param opts? table
|
||||
---@return nil
|
||||
function M.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
|
||||
config.options.autosave
|
||||
and (allow_dir() and not ignore_dir() and vim.g.persisting == nil)
|
||||
|
|
@ -100,42 +90,56 @@ function M.load(opt)
|
|||
else
|
||||
vim.g.persisting_session = session
|
||||
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
|
||||
config.options.on_autoload_no_session()
|
||||
end
|
||||
end
|
||||
|
||||
if config.options.autosave and (allow_dir() and not ignore_dir()) then
|
||||
vim.schedule(function()
|
||||
M.start()
|
||||
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
|
||||
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
|
||||
end
|
||||
|
||||
---Stop recording a session
|
||||
---@return nil
|
||||
function M.stop()
|
||||
pcall(vim.api.nvim_del_augroup_by_name, "Persisted")
|
||||
vim.g.persisting = false
|
||||
vim.g.persisting_session = nil
|
||||
end
|
||||
|
||||
---Save the session to disk
|
||||
---Save the session
|
||||
---@return nil
|
||||
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
|
||||
config.options.before_save()
|
||||
end
|
||||
|
|
@ -160,7 +164,7 @@ function M.save()
|
|||
end
|
||||
end
|
||||
|
||||
---Delete the current session from disk
|
||||
---Delete the current session
|
||||
---@return nil
|
||||
function M.delete()
|
||||
local session = get_current()
|
||||
|
|
@ -182,7 +186,7 @@ function M.toggle()
|
|||
return M.start()
|
||||
end
|
||||
|
||||
---List all of the sessions in the session directory
|
||||
---List all of the sessions
|
||||
---@return table
|
||||
function M.list()
|
||||
local save_dir = config.options.save_dir
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ end
|
|||
---@return string
|
||||
function M.get_last_item(table)
|
||||
local last
|
||||
for i, v in pairs(table) do
|
||||
for _, _ in pairs(table) do
|
||||
last = #table - 0
|
||||
end
|
||||
return table[last]
|
||||
|
|
@ -59,26 +59,27 @@ function M.get_dir_pattern()
|
|||
return pattern
|
||||
end
|
||||
|
||||
|
||||
---Load the given session
|
||||
---@param session string
|
||||
---@param before_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)
|
||||
vim.schedule(function()
|
||||
if type(before_callback) == "function" then
|
||||
before_callback()
|
||||
end
|
||||
|
||||
local ok, result = pcall(vim.cmd, (silent and "silent " or "") .. "source " .. e(session))
|
||||
if not ok then
|
||||
return echoerr("[Persisted.nvim]: Error loading the session! ", result)
|
||||
return echoerr("Error loading the session! ", result)
|
||||
end
|
||||
|
||||
if type(after_callback) == "function" then
|
||||
after_callback()
|
||||
end
|
||||
end)
|
||||
|
||||
-- vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSessionLoadPost" })
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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/"
|
||||
require("persisted").setup({
|
||||
save_dir = session_dir,
|
||||
|
|
@ -10,14 +6,17 @@ require("persisted").setup({
|
|||
allowed_dirs = { vim.loop.cwd() },
|
||||
})
|
||||
|
||||
async.describe("Autoloading", function()
|
||||
-- after_each(function()
|
||||
-- vim.fn.system("rm -rf " .. e(session_dir))
|
||||
-- end)
|
||||
describe("Autoloading", function()
|
||||
it("autoloads a file with allowed_dirs config option present", function()
|
||||
local co = coroutine.running()
|
||||
|
||||
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, "$")
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
local util = require("plenary.async.util")
|
||||
local async = require("plenary.async.tests")
|
||||
describe("Autoloading", function()
|
||||
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/"
|
||||
require("persisted").setup({
|
||||
local session_dir = vim.fn.getcwd() .. "/tests/dummy_data/"
|
||||
require("persisted").setup({
|
||||
save_dir = session_dir,
|
||||
autoload = true,
|
||||
autosave = true,
|
||||
})
|
||||
})
|
||||
|
||||
async.describe("Autoloading", function()
|
||||
coroutine.yield()
|
||||
|
||||
async.it("autoloads a file", function()
|
||||
util.scheduler()
|
||||
local content = vim.fn.getline(1, '$')
|
||||
assert.equals(content[1], "If you're reading this, I guess auto-loading works")
|
||||
local content = vim.fn.getline(1, "$")
|
||||
assert.equals("If you're reading this, I guess auto-loading works", content[1])
|
||||
end)
|
||||
|
||||
end)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
local e = vim.fn.fnameescape
|
||||
local session_dir = vim.fn.getcwd() .. "/tests/dummy_data/"
|
||||
require("persisted").setup({
|
||||
save_dir = session_dir,
|
||||
|
|
@ -7,10 +6,15 @@ require("persisted").setup({
|
|||
})
|
||||
|
||||
describe("Autoloading", 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], "")
|
||||
end)
|
||||
|
||||
end)
|
||||
|
|
|
|||
|
|
@ -15,8 +15,4 @@ describe("As part of the setup", function()
|
|||
|
||||
require("persisted").save()
|
||||
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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue