From bf2701664343224dce2fb8cfab17b9b76331a3ec Mon Sep 17 00:00:00 2001 From: olimorris Date: Tue, 7 Jun 2022 12:55:20 -0700 Subject: [PATCH] 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 --- .github/workflows/ci.yml | 6 +---- README.md | 1 - lua/persisted/init.lua | 24 ++++++++++++-------- tests/autoload/autoload_allowed_dir_spec.lua | 12 ++++++---- tests/autoload/autoload_session_spec.lua | 8 +++++-- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 056f4f8..063f397 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/README.md b/README.md index 7df242f..8771d5c 100644 --- a/README.md +++ b/README.md @@ -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 }) ``` diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index e729535..23b7442 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -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 - 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() + 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 - M.start() + 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 diff --git a/tests/autoload/autoload_allowed_dir_spec.lua b/tests/autoload/autoload_allowed_dir_spec.lua index d61cc92..1cf25dd 100644 --- a/tests/autoload/autoload_allowed_dir_spec.lua +++ b/tests/autoload/autoload_allowed_dir_spec.lua @@ -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) diff --git a/tests/autoload/autoload_session_spec.lua b/tests/autoload/autoload_session_spec.lua index 9d4bb8c..3409392 100644 --- a/tests/autoload/autoload_session_spec.lua +++ b/tests/autoload/autoload_session_spec.lua @@ -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)