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 name: Tests
on: on: [push, pull_request]
push:
branches: [main]
pull_request:
branches: [main]
# Cancel any in-progress CI runs for a PR if it is updated # Cancel any in-progress CI runs for a PR if it is updated
concurrency: concurrency:

View File

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

View File

@ -20,7 +20,6 @@ local function setup_commands()
]]) ]])
end end
---Does the current working directory allow for the auto-saving and loading? ---Does the current working directory allow for the auto-saving and loading?
---@return boolean ---@return boolean
local function allow_dir() local function allow_dir()
@ -106,27 +105,34 @@ function M.load(opt)
local session = opt.last and get_last() or get_current() local session = opt.last and get_last() or get_current()
if session and vim.fn.filereadable(session) ~= 0 then if session and vim.fn.filereadable(session) ~= 0 then
local ok, result = pcall(vim.cmd, "source " .. e(session)) vim.schedule(function()
if not ok then local ok, result = pcall(vim.cmd, "source " .. e(session))
return utils.echoerr("Error loading the session! ", result) if not ok then
end return utils.echoerr("Error loading the session! ", result)
config.options.after_source() end
config.options.after_source()
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
M.start() vim.schedule(function()
M.start()
end)
end end
end end
---Start recording a session and write to disk on a specific autocommand ---Start recording a session and write to disk on a specific autocommand
---@return nil ---@return nil
function M.start() function M.start()
vim.cmd(string.format([[ vim.cmd(string.format(
[[
augroup Persisted augroup Persisted
autocmd! autocmd!
autocmd %s * lua require("persisted").save() autocmd %s * lua require("persisted").save()
augroup end augroup end
]], config.options.command)) ]],
config.options.command
))
vim.g.persisting = true vim.g.persisting = true
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 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({
@ -7,15 +10,14 @@ require("persisted").setup({
allowed_dirs = { vim.fn.getcwd() }, allowed_dirs = { vim.fn.getcwd() },
}) })
describe("Autoloading", function() async.describe("Autoloading", function()
-- after_each(function() -- after_each(function()
-- vim.fn.system("rm -rf " .. e(session_dir)) -- vim.fn.system("rm -rf " .. e(session_dir))
-- end) -- end)
it("autoloads a file with allowed_dirs config option present", function() async.it("autoloads a file with allowed_dirs config option present", function()
local content = vim.fn.getline(1, '$') util.scheduler()
local content = vim.fn.getline(1, "$")
assert.equals(content[1], "If you're reading this, I guess auto-loading works") assert.equals(content[1], "If you're reading this, I guess auto-loading works")
end) end)
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 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({
@ -6,9 +9,10 @@ require("persisted").setup({
autosave = true, 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, '$') local content = vim.fn.getline(1, '$')
assert.equals(content[1], "If you're reading this, I guess auto-loading works") assert.equals(content[1], "If you're reading this, I guess auto-loading works")
end) end)