From fa710f6ddd6da354f3410d6d7ef42d306a54f145 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Sun, 3 Apr 2022 14:32:52 +0200 Subject: refactor: re-enable packer.sync() in LvimReload (#2410) --- lua/lvim/plugin-loader.lua | 65 ++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 34 deletions(-) (limited to 'lua/lvim/plugin-loader.lua') diff --git a/lua/lvim/plugin-loader.lua b/lua/lvim/plugin-loader.lua index 7612bc37..e42be52e 100644 --- a/lua/lvim/plugin-loader.lua +++ b/lua/lvim/plugin-loader.lua @@ -3,6 +3,7 @@ local plugin_loader = {} local utils = require "lvim.utils" local Log = require "lvim.core.log" local join_paths = utils.join_paths +local in_headless = #vim.api.nvim_list_uis() == 0 -- we need to reuse this outside of init() local compile_path = join_paths(get_config_dir(), "plugin", "packer_compiled.lua") @@ -22,10 +23,6 @@ function plugin_loader.init(opts) log = { level = "warn" }, git = { clone_timeout = 300, - subcommands = { - -- this is more efficient than what Packer is using - fetch = "fetch --no-tags --no-recurse-submodules --update-shallow --progress", - }, }, display = { open_fn = function() @@ -34,14 +31,8 @@ function plugin_loader.init(opts) }, } - local in_headless = #vim.api.nvim_list_uis() == 0 if in_headless then init_opts.display = nil - - -- NOTE: `lvim.log.level` may not be loaded from the user's config yet - init_opts.log.level = lvim.log and lvim.log.level or "info" - else - vim.cmd [[autocmd User PackerComplete lua require('lvim.utils.hooks').run_on_packer_complete()]] end if vim.fn.empty(vim.fn.glob(install_path)) > 0 then @@ -54,6 +45,9 @@ function plugin_loader.init(opts) local status_ok, packer = pcall(require, "packer") if status_ok then + packer.on_complete = vim.schedule_wrap(function() + require("lvim.utils.hooks").run_on_packer_complete() + end) packer.init(init_opts) end end @@ -83,6 +77,18 @@ function plugin_loader.recompile() end end +function plugin_loader.reload(configurations) + _G.packer_plugins = _G.packer_plugins or {} + for k, v in pairs(_G.packer_plugins) do + if k ~= "packer.nvim" then + _G.packer_plugins[v] = nil + end + end + plugin_loader.load(configurations) + + pcall_packer_command "sync" +end + function plugin_loader.load(configurations) Log:debug "loading plugins configuration" local packer_available, packer = pcall(require, "packer") @@ -120,31 +126,22 @@ function plugin_loader.get_core_plugins() return list end -function plugin_loader.sync_core_plugins(opts) - opts = opts or {} - Log:debug(string.format("Syncing core plugins with snapshot file [%s]", default_snapshot)) - local packer = require "packer" - local a = require "packer.async" - local async = a.sync - local await = a.wait - local main = a.main +function plugin_loader.load_snapshot(snapshot_file) + snapshot_file = snapshot_file or default_snapshot + if not in_headless then + vim.notify("Syncing core plugins is in progress..", vim.log.levels.INFO, { title = "lvim" }) + end + Log:debug(string.format("Using snapshot file [%s]", snapshot_file)) local core_plugins = plugin_loader.get_core_plugins() - async(function() - await(packer.rollback(default_snapshot, unpack(core_plugins))) - :map_ok(function(ok) --NOTE: these may not be doing anything, use PackerComplete for now - await(main) - Log:debug(string.format("Rollback snapshot file [%s] completed", default_snapshot)) - if next(ok.failed) then - Log:warn(string.format("Couldn't rollback %s", vim.inspect(ok.failed))) - end - pcall(opts.on_complete, ok) - end) - :map_err(function(err) - await(main) - Log:error(err) - pcall(opts.on_error, err) - end) - end)() + require("packer").rollback(snapshot_file, unpack(core_plugins)) +end + +function plugin_loader.sync_core_plugins() + -- problem: rollback() will get stuck if a plugin directory doesn't exist + -- solution: call sync() beforehand + -- see https://github.com/wbthomason/packer.nvim/issues/862 + vim.cmd [[autocmd User PackerComplete ++once lua require("lvim.plugin-loader").load_snapshot() ]] + pcall_packer_command "sync" end return plugin_loader -- cgit v1.2.3