fix: #12 shift autoloading to the nvim event loop

A big shoutout to @simonmclean for this pull request. Previously we autoloaded a session file as soon as Neovim opened. The problem with this is that key plugins like LSP and Treesitter may not have been initialised. This would lead to syntax highlighting issues or LSPs not attaching to buffers in the session. Pushing this to Neovim's event loop via vim.schedule allows us to sensibly load the session after these key plugins. Fixing the tests was a little cumbersome as we needed to use plenary's async library but once figured out, we have some lovely robust async tests
main
olimorris 2022-06-07 12:55:20 -07:00
parent 66f4405794
commit bf27016643
5 changed files with 29 additions and 22 deletions

View File

@ -1,10 +1,6 @@
name: Tests
on:
push:
branches: [main]
pull_request:
branches: [main]
on: [push, pull_request]
# Cancel any in-progress CI runs for a PR if it is updated
concurrency:

View File

@ -222,7 +222,6 @@ require("persisted").setup({
after_source = function()
-- Reload the LSP servers
vim.lsp.stop_client(vim.lsp.get_active_clients())
vim.cmd("edit")
end
})
```

View File

@ -20,7 +20,6 @@ local function setup_commands()
]])
end
---Does the current working directory allow for the auto-saving and loading?
---@return boolean
local function allow_dir()
@ -106,27 +105,34 @@ function M.load(opt)
local session = opt.last and get_last() or get_current()
if session and vim.fn.filereadable(session) ~= 0 then
vim.schedule(function()
local ok, result = pcall(vim.cmd, "source " .. e(session))
if not ok then
return utils.echoerr("Error loading the session! ", result)
end
config.options.after_source()
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
---@return nil
function M.start()
vim.cmd(string.format([[
vim.cmd(string.format(
[[
augroup Persisted
autocmd!
autocmd %s * lua require("persisted").save()
augroup end
]], config.options.command))
]],
config.options.command
))
vim.g.persisting = true
end

View File

@ -1,3 +1,6 @@
local util = require("plenary.async.util")
local async = require("plenary.async.tests")
local e = vim.fn.fnameescape
local session_dir = vim.fn.getcwd() .. "/tests/dummy_data/"
require("persisted").setup({
@ -7,15 +10,14 @@ require("persisted").setup({
allowed_dirs = { vim.fn.getcwd() },
})
describe("Autoloading", function()
async.describe("Autoloading", function()
-- after_each(function()
-- vim.fn.system("rm -rf " .. e(session_dir))
-- end)
it("autoloads a file with allowed_dirs config option present", function()
local content = vim.fn.getline(1, '$')
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")
end)
end)

View File

@ -1,3 +1,6 @@
local util = require("plenary.async.util")
local async = require("plenary.async.tests")
local e = vim.fn.fnameescape
local session_dir = vim.fn.getcwd() .. "/tests/dummy_data/"
require("persisted").setup({
@ -6,9 +9,10 @@ require("persisted").setup({
autosave = true,
})
describe("Autoloading", function()
async.describe("Autoloading", function()
it("autoloads a file", function()
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")
end)