fix(cache): OptionSet is not triggered during startup, so use #rtp instead to see if it changed

This commit is contained in:
Folke Lemaitre 2022-12-29 00:48:59 +01:00
parent 370b1b982e
commit 9997523841
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 28 additions and 23 deletions

View File

@ -30,6 +30,7 @@ M.cache = {}
M.enabled = true M.enabled = true
---@type string[] ---@type string[]
M.rtp = nil M.rtp = nil
M.rtp_total = 0
M.stats = { M.stats = {
find = { total = 0, time = 0, rtp = 0, unloaded = 0, index = 0, stat = 0, not_found = 0 }, find = { total = 0, time = 0, rtp = 0, unloaded = 0, index = 0, stat = 0, not_found = 0 },
autoload = { total = 0, time = 0 }, autoload = { total = 0, time = 0 },
@ -40,8 +41,8 @@ M.me = vim.fn.fnamemodify(M.me, ":p:h:h:h:h"):gsub("\\", "/")
M.topmods = { lazy = { [M.me] = M.me } } M.topmods = { lazy = { [M.me] = M.me } }
---@type table<string, true> ---@type table<string, true>
M.indexed = { [M.me] = true } M.indexed = { [M.me] = true }
M.indexed_rtp = false
M.indexed_unloaded = false M.indexed_unloaded = false
M.indexed_rtp = 0
-- selene:allow(global_usage) -- selene:allow(global_usage)
M._loadfile = _G.loadfile M._loadfile = _G.loadfile
@ -165,7 +166,7 @@ function M.load(modkey, modpath)
end end
entry.hash = hash entry.hash = hash
if M.debug then if M.debug and M.enabled then
vim.schedule(function() vim.schedule(function()
vim.notify("[cache:load] " .. modpath) vim.notify("[cache:load] " .. modpath)
end) end)
@ -192,7 +193,7 @@ function M._index(path)
---@type LazyUtilCore ---@type LazyUtilCore
local Util = package.loaded["lazy.core.util"] local Util = package.loaded["lazy.core.util"]
if not Util then if not Util then
return return false
end end
M.indexed[path] = true M.indexed[path] = true
Util.ls(path .. "/lua", function(_, name, t) Util.ls(path .. "/lua", function(_, name, t)
@ -207,7 +208,9 @@ function M._index(path)
M.topmods[topname][path] = path M.topmods[topname][path] = path
end end
end) end)
return true
end end
return false
end end
---@param modname string ---@param modname string
@ -238,27 +241,34 @@ function M.find(modname)
local modpath = _find() local modpath = _find()
if not modpath then if not modpath then
-- update rtp -- update rtp
if not M.indexed_rtp then local rtp = vim.api.nvim_list_runtime_paths()
for _, path in ipairs(vim.api.nvim_list_runtime_paths()) do if #rtp ~= M.indexed_rtp then
M._index(path) M.indexed_rtp = #rtp
local updated = false
for _, path in ipairs(rtp) do
updated = M._index(path) or updated
end
if updated then
modpath = _find()
end end
M.indexed_rtp = true
modpath = _find()
end end
-- update unloaded -- update unloaded
if not modpath and not M.indexed_unloaded then if not modpath and not M.indexed_unloaded then
M.indexed_unloaded = true
local updated = false
---@type LazyCoreConfig ---@type LazyCoreConfig
local Config = package.loaded["lazy.core.config"] local Config = package.loaded["lazy.core.config"]
if Config then if Config then
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
if not (M.indexed[plugin.dir] or plugin._.loaded or plugin.module == false) then if not (M.indexed[plugin.dir] or plugin._.loaded or plugin.module == false) then
M._index(plugin.dir) updated = M._index(plugin.dir) or updated
end end
end end
end end
M.indexed_unloaded = true if updated then
modpath = _find() modpath = _find()
end
end end
-- module not found -- module not found
@ -273,13 +283,16 @@ end
-- returns the cached RTP excluding plugin dirs -- returns the cached RTP excluding plugin dirs
function M.get_rtp() function M.get_rtp()
if not M.rtp then local rtp = vim.api.nvim_list_runtime_paths()
if not M.rtp or #rtp ~= M.rtp_total then
M.rtp_total = #rtp
M.rtp = {} M.rtp = {}
---@type table<string,true> ---@type table<string,true>
local skip = {} local skip = {}
-- only skip plugins once Config has been setup -- only skip plugins once Config has been setup
if package.loaded["lazy.core.config"] then ---@type LazyCoreConfig
local Config = require("lazy.core.config") local Config = package.loaded["lazy.core.config"]
if Config then
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
if plugin.name ~= "lazy.nvim" then if plugin.name ~= "lazy.nvim" then
skip[plugin.dir] = true skip[plugin.dir] = true
@ -287,6 +300,7 @@ function M.get_rtp()
end end
end end
for _, path in ipairs(vim.api.nvim_list_runtime_paths()) do for _, path in ipairs(vim.api.nvim_list_runtime_paths()) do
---@type string
path = path:gsub("\\", "/") path = path:gsub("\\", "/")
if not skip[path] and not path:find("after/?$") then if not skip[path] and not path:find("after/?$") then
M.rtp[#M.rtp + 1] = path M.rtp[#M.rtp + 1] = path
@ -309,15 +323,6 @@ function M.setup(opts)
M.debug = opts and opts.debug M.debug = opts and opts.debug
M.enabled = M.config.enabled M.enabled = M.config.enabled
-- reset rtp when it changes
vim.api.nvim_create_autocmd("OptionSet", {
pattern = "runtimepath",
callback = function()
M.rtp = nil
M.indexed_rtp = false
end,
})
if M.enabled then if M.enabled then
table.insert(package.loaders, 2, M.loader) table.insert(package.loaders, 2, M.loader)
M.load_cache() M.load_cache()