2022-11-30 13:19:50 +00:00
|
|
|
local ffi = require("ffi")
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
local uv = vim.loop
|
2022-11-22 20:12:33 +00:00
|
|
|
|
|
|
|
local M = {}
|
2022-11-30 13:19:50 +00:00
|
|
|
M.dirty = false
|
2022-12-29 07:01:05 +00:00
|
|
|
M.VERSION = "1" .. jit.version
|
2022-11-22 20:12:33 +00:00
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@class LazyCacheConfig
|
|
|
|
M.config = {
|
|
|
|
enabled = true,
|
2022-12-23 06:48:40 +00:00
|
|
|
path = vim.fn.stdpath("cache") .. "/lazy/cache",
|
2022-12-03 17:59:28 +00:00
|
|
|
-- Once one of the following events triggers, caching will be disabled.
|
|
|
|
-- To cache all modules, set this to `{}`, but that is not recommended.
|
|
|
|
-- The default is to disable on:
|
|
|
|
-- * VimEnter: not useful to cache anything else beyond startup
|
|
|
|
-- * BufReadPre: this will be triggered early when opening a file from the command line directly
|
2023-01-02 08:39:28 +00:00
|
|
|
disable_events = { "UIEnter", "BufReadPre" },
|
2022-12-25 15:26:17 +00:00
|
|
|
ttl = 3600 * 24 * 5, -- keep unused modules for up to 5 days
|
2022-12-02 11:43:34 +00:00
|
|
|
}
|
2022-12-02 15:52:22 +00:00
|
|
|
M.debug = false
|
2022-12-02 11:43:34 +00:00
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
---@type CacheHash
|
|
|
|
local cache_hash
|
|
|
|
|
|
|
|
---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
|
2022-12-02 08:20:48 +00:00
|
|
|
---@alias CacheEntry {hash:CacheHash, modpath:string, chunk:string, used:number}
|
2022-11-30 13:19:50 +00:00
|
|
|
---@type table<string,CacheEntry?>
|
|
|
|
M.cache = {}
|
2022-12-02 08:20:48 +00:00
|
|
|
M.enabled = true
|
2022-12-19 11:21:20 +00:00
|
|
|
---@type string[]
|
|
|
|
M.rtp = nil
|
2022-12-28 23:48:59 +00:00
|
|
|
M.rtp_total = 0
|
2022-12-28 16:57:59 +00:00
|
|
|
M.stats = {
|
|
|
|
find = { total = 0, time = 0, rtp = 0, unloaded = 0, index = 0, stat = 0, not_found = 0 },
|
|
|
|
autoload = { total = 0, time = 0 },
|
|
|
|
}
|
2022-12-28 22:50:25 +00:00
|
|
|
M.me = debug.getinfo(1, "S").source:sub(2)
|
|
|
|
M.me = vim.fn.fnamemodify(M.me, ":p:h:h:h:h"):gsub("\\", "/")
|
2023-01-01 17:52:07 +00:00
|
|
|
---@type table<string, string[]>
|
|
|
|
M.topmods = { lazy = { M.me } }
|
2022-12-30 19:14:46 +00:00
|
|
|
---@type table<string, string[]>
|
|
|
|
M.indexed = { [M.me] = { "lazy" } }
|
2022-12-28 16:57:59 +00:00
|
|
|
M.indexed_unloaded = false
|
2022-12-28 23:48:59 +00:00
|
|
|
M.indexed_rtp = 0
|
2022-12-19 12:34:37 +00:00
|
|
|
-- selene:allow(global_usage)
|
|
|
|
M._loadfile = _G.loadfile
|
2022-12-19 11:21:20 +00:00
|
|
|
|
2022-12-19 20:11:28 +00:00
|
|
|
-- checks whether the cached modpath is still valid
|
2022-12-19 11:21:20 +00:00
|
|
|
function M.check_path(modname, modpath)
|
2022-12-26 17:35:02 +00:00
|
|
|
-- HACK: never return packer paths
|
|
|
|
if modpath:find("/site/pack/packer/", 1, true) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2022-12-19 20:11:28 +00:00
|
|
|
-- check rtp excluding plugins. This is a very small list, so should be fast
|
2022-12-19 11:21:20 +00:00
|
|
|
for _, path in ipairs(M.get_rtp()) do
|
|
|
|
if modpath:find(path, 1, true) == 1 then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2022-11-22 20:12:33 +00:00
|
|
|
|
2022-12-19 11:21:20 +00:00
|
|
|
-- the correct lazy path should be part of rtp.
|
|
|
|
-- so if we get here, this is folke using the local dev instance ;)
|
2023-01-01 19:19:09 +00:00
|
|
|
if modname and modname:sub(1, 4) == "lazy" then
|
2022-12-19 11:21:20 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-01-01 19:19:09 +00:00
|
|
|
return modname and M.check_autoload(modname, modpath)
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.check_autoload(modname, modpath)
|
|
|
|
local start = uv.hrtime()
|
|
|
|
M.stats.autoload.total = M.stats.autoload.total + 1
|
2022-12-19 11:21:20 +00:00
|
|
|
-- check plugins. Again fast, since we check the plugin name from the path.
|
|
|
|
-- only needed when the plugin mod has been loaded
|
2022-12-28 16:57:59 +00:00
|
|
|
---@type LazyCorePlugin
|
|
|
|
local Plugin = package.loaded["lazy.core.plugin"]
|
|
|
|
if Plugin then
|
|
|
|
local plugin = Plugin.find(modpath)
|
2022-12-19 11:21:20 +00:00
|
|
|
if plugin and modpath:find(plugin.dir, 1, true) == 1 then
|
2022-12-28 16:57:59 +00:00
|
|
|
-- we're not interested in loader time, so calculate delta here
|
|
|
|
M.stats.autoload.time = M.stats.autoload.time + uv.hrtime() - start
|
2022-12-19 11:21:20 +00:00
|
|
|
if not plugin._.loaded then
|
2022-12-20 15:02:22 +00:00
|
|
|
if plugin.module == false then
|
|
|
|
error("Plugin " .. plugin.name .. " is not loaded and is configured with module=false")
|
|
|
|
end
|
2022-12-19 11:21:20 +00:00
|
|
|
require("lazy.core.loader").load(plugin, { require = modname })
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-12-28 16:57:59 +00:00
|
|
|
M.stats.autoload.time = M.stats.autoload.time + uv.hrtime() - start
|
2022-12-19 11:21:20 +00:00
|
|
|
return false
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
|
2022-12-03 17:59:28 +00:00
|
|
|
function M.disable()
|
2022-12-02 08:20:48 +00:00
|
|
|
if not M.enabled then
|
2022-12-02 11:43:34 +00:00
|
|
|
return
|
|
|
|
end
|
2022-12-19 12:34:37 +00:00
|
|
|
-- selene:allow(global_usage)
|
|
|
|
_G.loadfile = M._loadfile
|
2022-12-02 11:43:34 +00:00
|
|
|
M.enabled = false
|
|
|
|
end
|
|
|
|
|
2022-12-29 08:06:23 +00:00
|
|
|
function M.check_loaded(modname)
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
local mod = package.loaded[modname]
|
|
|
|
if type(mod) == "table" then
|
|
|
|
return function()
|
|
|
|
return mod
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@param modname string
|
2022-12-29 17:40:28 +00:00
|
|
|
---@return fun()|string
|
2022-12-02 11:43:34 +00:00
|
|
|
function M.loader(modname)
|
2022-12-29 17:26:39 +00:00
|
|
|
modname = modname:gsub("/", ".")
|
2022-12-02 08:20:48 +00:00
|
|
|
local entry = M.cache[modname]
|
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
local chunk, err
|
2022-12-19 11:21:20 +00:00
|
|
|
if entry and M.check_path(modname, entry.modpath) then
|
2022-12-28 16:57:59 +00:00
|
|
|
M.stats.find.total = M.stats.find.total + 1
|
2022-12-19 12:34:37 +00:00
|
|
|
chunk, err = M.load(modname, entry.modpath)
|
2022-12-21 13:56:46 +00:00
|
|
|
end
|
|
|
|
if not chunk then
|
2022-12-19 12:34:37 +00:00
|
|
|
-- find the modpath and load the module
|
|
|
|
local modpath = M.find(modname)
|
|
|
|
if modpath then
|
2022-12-28 16:57:59 +00:00
|
|
|
M.check_autoload(modname, modpath)
|
|
|
|
if M.enabled then
|
|
|
|
chunk, err = M.load(modname, modpath)
|
|
|
|
else
|
2022-12-29 08:06:23 +00:00
|
|
|
chunk = M.check_loaded(modname)
|
|
|
|
if not chunk then
|
|
|
|
chunk, err = M._loadfile(modpath)
|
|
|
|
end
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
2022-12-02 18:16:21 +00:00
|
|
|
end
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
2022-12-29 17:40:28 +00:00
|
|
|
return chunk or err or ("module " .. modname .. " not found")
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param modpath string
|
|
|
|
---@return any, string?
|
|
|
|
function M.loadfile(modpath)
|
2022-12-20 07:31:19 +00:00
|
|
|
modpath = modpath:gsub("\\", "/")
|
2022-12-19 12:34:37 +00:00
|
|
|
return M.load(modpath, modpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param modkey string
|
|
|
|
---@param modpath string
|
|
|
|
---@return function?, string? error_message
|
|
|
|
function M.load(modkey, modpath)
|
2022-12-29 08:06:23 +00:00
|
|
|
local chunk, err
|
|
|
|
chunk = M.check_loaded(modkey)
|
|
|
|
if chunk then
|
|
|
|
return chunk
|
|
|
|
end
|
2022-12-20 07:31:19 +00:00
|
|
|
modpath = modpath:gsub("\\", "/")
|
2022-12-19 12:34:37 +00:00
|
|
|
local hash = M.hash(modpath)
|
|
|
|
if not hash then
|
|
|
|
-- trigger correct error
|
|
|
|
return M._loadfile(modpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
local entry = M.cache[modkey]
|
|
|
|
if entry then
|
2022-12-22 08:10:11 +00:00
|
|
|
entry.modpath = modpath
|
2022-12-19 12:34:37 +00:00
|
|
|
entry.used = os.time()
|
2022-12-02 08:20:48 +00:00
|
|
|
if M.eq(entry.hash, hash) then
|
|
|
|
-- found in cache and up to date
|
2022-12-29 08:06:23 +00:00
|
|
|
chunk, err = loadstring(entry.chunk --[[@as string]], "@" .. entry.modpath)
|
2022-12-29 07:25:24 +00:00
|
|
|
if not (err and err:find("cannot load incompatible bytecode", 1, true)) then
|
|
|
|
return chunk, err
|
|
|
|
end
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
else
|
2022-12-19 12:34:37 +00:00
|
|
|
entry = { hash = hash, modpath = modpath, used = os.time() }
|
|
|
|
M.cache[modkey] = entry
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
2022-12-19 12:34:37 +00:00
|
|
|
entry.hash = hash
|
|
|
|
|
2022-12-28 23:48:59 +00:00
|
|
|
if M.debug and M.enabled then
|
2022-12-02 15:52:22 +00:00
|
|
|
vim.schedule(function()
|
2022-12-19 12:34:37 +00:00
|
|
|
vim.notify("[cache:load] " .. modpath)
|
2022-12-02 15:52:22 +00:00
|
|
|
end)
|
|
|
|
end
|
2022-12-19 12:34:37 +00:00
|
|
|
|
2022-12-29 08:06:23 +00:00
|
|
|
chunk, err = M._loadfile(entry.modpath)
|
2022-12-30 10:35:09 +00:00
|
|
|
M.dirty = true
|
2022-12-19 12:34:37 +00:00
|
|
|
if chunk then
|
2022-12-02 08:20:48 +00:00
|
|
|
entry.chunk = string.dump(chunk)
|
2022-12-30 10:35:09 +00:00
|
|
|
else
|
|
|
|
M.cache[modkey] = nil
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-12-19 12:34:37 +00:00
|
|
|
return chunk, err
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
2022-12-16 14:08:09 +00:00
|
|
|
function M.require(modname)
|
2022-12-29 17:40:28 +00:00
|
|
|
local chunk = M.loader(modname)
|
|
|
|
if type(chunk) == "string" then
|
|
|
|
error(chunk)
|
|
|
|
end
|
2022-12-29 08:06:23 +00:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2022-12-29 17:40:28 +00:00
|
|
|
local mod = chunk()
|
2022-12-29 08:06:23 +00:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2022-12-28 08:00:39 +00:00
|
|
|
package.loaded[modname] = mod
|
|
|
|
return mod
|
2022-12-16 14:08:09 +00:00
|
|
|
end
|
|
|
|
|
2022-12-28 16:57:59 +00:00
|
|
|
-- index the top-level lua modules for this path
|
|
|
|
function M._index(path)
|
|
|
|
if not M.indexed[path] and path:sub(-6, -1) ~= "/after" then
|
|
|
|
M.stats.find.index = M.stats.find.index + 1
|
|
|
|
---@type LazyUtilCore
|
|
|
|
local Util = package.loaded["lazy.core.util"]
|
|
|
|
if not Util then
|
2022-12-28 23:48:59 +00:00
|
|
|
return false
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
2022-12-30 19:14:46 +00:00
|
|
|
M.indexed[path] = {}
|
2022-12-28 16:57:59 +00:00
|
|
|
Util.ls(path .. "/lua", function(_, name, t)
|
|
|
|
local topname
|
2022-12-29 17:40:28 +00:00
|
|
|
if name:sub(-4) == ".lua" then
|
2022-12-28 16:57:59 +00:00
|
|
|
topname = name:sub(1, -5)
|
2022-12-29 17:40:28 +00:00
|
|
|
elseif t == "link" or t == "directory" then
|
|
|
|
topname = name
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
if topname then
|
|
|
|
M.topmods[topname] = M.topmods[topname] or {}
|
2023-01-01 17:52:07 +00:00
|
|
|
if not vim.tbl_contains(M.topmods[topname], path) then
|
|
|
|
table.insert(M.topmods[topname], path)
|
|
|
|
end
|
2022-12-30 19:14:46 +00:00
|
|
|
table.insert(M.indexed[path], topname)
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
end)
|
2022-12-28 23:48:59 +00:00
|
|
|
return true
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
2022-12-28 23:48:59 +00:00
|
|
|
return false
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
|
2022-12-30 19:14:46 +00:00
|
|
|
function M.get_topmods(path)
|
|
|
|
M._index(path)
|
|
|
|
return M.indexed[path] or {}
|
|
|
|
end
|
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@param modname string
|
2023-01-01 19:19:09 +00:00
|
|
|
---@return string?, string?
|
|
|
|
function M.find_dir(modname)
|
|
|
|
if M.cache[modname] then
|
|
|
|
-- check if modname is in cache
|
|
|
|
local modpath = M.cache[modname].modpath
|
|
|
|
if M.check_path(modname, modpath) then
|
|
|
|
local root = modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
|
|
|
|
return root, modpath
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- in case modname is just a directory and not a real mod,
|
|
|
|
-- check for any children in the cache
|
|
|
|
for child, entry in pairs(M.cache) do
|
|
|
|
if child:find(modname, 1, true) == 1 and M.check_path(nil, entry.modpath) then
|
|
|
|
local basename = modname:gsub("%.", "/")
|
|
|
|
local childbase = child:gsub("%.", "/")
|
|
|
|
local ret = entry.modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
|
|
|
|
local idx = assert(ret:find(childbase, 1, true))
|
|
|
|
return ret:sub(1, idx - 1) .. basename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- not found in cache, so find the root with the special pattern
|
|
|
|
local modpath = M.find(modname, { patterns = { "" } })
|
|
|
|
if modpath then
|
|
|
|
local root = modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
|
|
|
|
return root, root ~= modpath and modpath or nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param modname string
|
|
|
|
---@param opts? {patterns?:string[]}
|
2022-12-19 12:34:37 +00:00
|
|
|
---@return string?
|
2023-01-01 19:19:09 +00:00
|
|
|
function M.find(modname, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
|
2022-12-28 16:57:59 +00:00
|
|
|
M.stats.find.total = M.stats.find.total + 1
|
|
|
|
local start = uv.hrtime()
|
2022-12-19 12:34:37 +00:00
|
|
|
local basename = modname:gsub("%.", "/")
|
2022-12-28 16:57:59 +00:00
|
|
|
local idx = modname:find(".", 1, true)
|
|
|
|
local topmod = idx and modname:sub(1, idx - 1) or modname
|
|
|
|
|
|
|
|
-- search for a directory first when topmod == modname
|
|
|
|
local patterns = topmod == modname and { "/init.lua", ".lua" } or { ".lua", "/init.lua" }
|
|
|
|
|
2023-01-01 19:19:09 +00:00
|
|
|
if opts.patterns then
|
|
|
|
vim.list_extend(patterns, opts.patterns)
|
|
|
|
end
|
|
|
|
|
2022-12-28 16:57:59 +00:00
|
|
|
-- check top-level mods to find the module
|
|
|
|
local function _find()
|
2023-01-01 17:52:07 +00:00
|
|
|
for _, toppath in ipairs(M.topmods[topmod] or {}) do
|
2022-12-28 16:57:59 +00:00
|
|
|
for _, pattern in ipairs(patterns) do
|
|
|
|
local path = toppath .. "/lua/" .. basename .. pattern
|
|
|
|
M.stats.find.stat = M.stats.find.stat + 1
|
|
|
|
if uv.fs_stat(path) then
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local modpath = _find()
|
|
|
|
if not modpath then
|
|
|
|
-- update rtp
|
2022-12-29 00:25:05 +00:00
|
|
|
local rtp = M.list_rtp()
|
2022-12-28 23:48:59 +00:00
|
|
|
if #rtp ~= M.indexed_rtp then
|
|
|
|
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()
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- update unloaded
|
|
|
|
if not modpath and not M.indexed_unloaded then
|
2022-12-28 23:48:59 +00:00
|
|
|
M.indexed_unloaded = true
|
|
|
|
local updated = false
|
2022-12-28 16:57:59 +00:00
|
|
|
---@type LazyCoreConfig
|
|
|
|
local Config = package.loaded["lazy.core.config"]
|
|
|
|
if Config then
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if not (M.indexed[plugin.dir] or plugin._.loaded or plugin.module == false) then
|
2022-12-28 23:48:59 +00:00
|
|
|
updated = M._index(plugin.dir) or updated
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-28 23:48:59 +00:00
|
|
|
if updated then
|
|
|
|
modpath = _find()
|
|
|
|
end
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- module not found
|
|
|
|
if not modpath then
|
|
|
|
M.stats.find.not_found = M.stats.find.not_found + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
M.stats.find.time = M.stats.find.time + uv.hrtime() - start
|
|
|
|
return modpath
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
|
|
|
|
2022-12-19 12:34:37 +00:00
|
|
|
-- returns the cached RTP excluding plugin dirs
|
2022-12-19 11:21:20 +00:00
|
|
|
function M.get_rtp()
|
2022-12-29 00:25:05 +00:00
|
|
|
local rtp = M.list_rtp()
|
2022-12-28 23:48:59 +00:00
|
|
|
if not M.rtp or #rtp ~= M.rtp_total then
|
|
|
|
M.rtp_total = #rtp
|
2022-12-19 11:21:20 +00:00
|
|
|
M.rtp = {}
|
2022-12-19 12:34:37 +00:00
|
|
|
---@type table<string,true>
|
2022-12-19 11:21:20 +00:00
|
|
|
local skip = {}
|
|
|
|
-- only skip plugins once Config has been setup
|
2022-12-28 23:48:59 +00:00
|
|
|
---@type LazyCoreConfig
|
|
|
|
local Config = package.loaded["lazy.core.config"]
|
|
|
|
if Config then
|
2022-12-19 12:34:37 +00:00
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2022-12-19 11:21:20 +00:00
|
|
|
if plugin.name ~= "lazy.nvim" then
|
|
|
|
skip[plugin.dir] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-29 00:04:39 +00:00
|
|
|
for _, path in ipairs(rtp) do
|
2022-12-28 23:48:59 +00:00
|
|
|
---@type string
|
2022-12-20 07:31:19 +00:00
|
|
|
path = path:gsub("\\", "/")
|
2022-12-20 22:35:06 +00:00
|
|
|
if not skip[path] and not path:find("after/?$") then
|
2022-12-19 11:21:20 +00:00
|
|
|
M.rtp[#M.rtp + 1] = path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return M.rtp
|
|
|
|
end
|
|
|
|
|
2022-12-29 00:25:05 +00:00
|
|
|
function M.list_rtp()
|
|
|
|
return vim.api.nvim_get_runtime_file("", true)
|
|
|
|
end
|
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@param opts? LazyConfig
|
|
|
|
function M.setup(opts)
|
|
|
|
-- no fancy deep extend here. just set the options
|
|
|
|
if opts and opts.performance and opts.performance.cache then
|
2022-12-19 12:34:37 +00:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2022-12-02 11:43:34 +00:00
|
|
|
for k, v in pairs(opts.performance.cache) do
|
2022-12-19 12:34:37 +00:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2022-12-02 11:43:34 +00:00
|
|
|
M.config[k] = v
|
|
|
|
end
|
|
|
|
end
|
2022-12-02 15:52:22 +00:00
|
|
|
M.debug = opts and opts.debug
|
2022-12-28 16:57:59 +00:00
|
|
|
M.enabled = M.config.enabled
|
2022-12-02 08:20:48 +00:00
|
|
|
|
2022-12-28 16:57:59 +00:00
|
|
|
if M.enabled then
|
|
|
|
table.insert(package.loaders, 2, M.loader)
|
|
|
|
M.load_cache()
|
|
|
|
-- selene:allow(global_usage)
|
|
|
|
_G.loadfile = M.loadfile
|
|
|
|
if #M.config.disable_events > 0 then
|
|
|
|
vim.api.nvim_create_autocmd(M.config.disable_events, { once = true, callback = M.disable })
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- we need to always add the loader since this will autoload unloaded modules
|
|
|
|
table.insert(package.loaders, M.loader)
|
2022-12-02 11:43:34 +00:00
|
|
|
end
|
2022-12-28 16:57:59 +00:00
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
return M
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
---@return CacheHash?
|
|
|
|
function M.hash(file)
|
2022-12-02 18:16:21 +00:00
|
|
|
local ok, ret = pcall(uv.fs_stat, file)
|
|
|
|
return ok and ret or nil
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param h1 CacheHash
|
|
|
|
---@param h2 CacheHash
|
|
|
|
function M.eq(h1, h2)
|
|
|
|
return h1 and h2 and h1.size == h2.size and h1.mtime.sec == h2.mtime.sec and h1.mtime.nsec == h2.mtime.nsec
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.save_cache()
|
2022-12-19 12:55:00 +00:00
|
|
|
vim.fn.mkdir(vim.fn.fnamemodify(M.config.path, ":p:h"), "p")
|
2022-12-02 11:43:34 +00:00
|
|
|
local f = assert(uv.fs_open(M.config.path, "w", 438))
|
2022-12-19 11:21:20 +00:00
|
|
|
uv.fs_write(f, M.VERSION)
|
2022-12-14 20:38:24 +00:00
|
|
|
uv.fs_write(f, "\0")
|
2022-11-30 13:19:50 +00:00
|
|
|
for modname, entry in pairs(M.cache) do
|
2022-12-25 15:26:17 +00:00
|
|
|
if entry.used > os.time() - M.config.ttl then
|
2022-11-30 13:19:50 +00:00
|
|
|
entry.modname = modname
|
2022-12-02 08:20:48 +00:00
|
|
|
local header = {
|
|
|
|
entry.hash.size,
|
|
|
|
entry.hash.mtime.sec,
|
|
|
|
entry.hash.mtime.nsec,
|
|
|
|
#modname,
|
|
|
|
#entry.chunk,
|
|
|
|
#entry.modpath,
|
|
|
|
entry.used,
|
|
|
|
}
|
|
|
|
uv.fs_write(f, ffi.string(ffi.new("const uint32_t[7]", header), 28))
|
2022-11-30 13:19:50 +00:00
|
|
|
uv.fs_write(f, modname)
|
|
|
|
uv.fs_write(f, entry.chunk)
|
2022-12-02 08:20:48 +00:00
|
|
|
uv.fs_write(f, entry.modpath)
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
uv.fs_close(f)
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.load_cache()
|
|
|
|
M.cache = {}
|
2022-12-02 11:43:34 +00:00
|
|
|
local f = uv.fs_open(M.config.path, "r", 438)
|
2022-11-30 13:19:50 +00:00
|
|
|
if f then
|
|
|
|
cache_hash = uv.fs_fstat(f) --[[@as CacheHash]]
|
|
|
|
local data = uv.fs_read(f, cache_hash.size, 0) --[[@as string]]
|
|
|
|
uv.fs_close(f)
|
|
|
|
|
2022-12-14 20:38:24 +00:00
|
|
|
local zero = data:find("\0", 1, true)
|
|
|
|
if not zero then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-12-19 11:21:20 +00:00
|
|
|
if M.VERSION ~= data:sub(1, zero - 1) then
|
2022-12-14 20:38:24 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local offset = zero + 1
|
2022-11-30 13:19:50 +00:00
|
|
|
while offset + 1 < #data do
|
2022-12-02 08:20:48 +00:00
|
|
|
local header = ffi.cast("uint32_t*", ffi.new("const char[28]", data:sub(offset, offset + 27)))
|
|
|
|
offset = offset + 28
|
2022-11-30 13:19:50 +00:00
|
|
|
local modname = data:sub(offset, offset + header[3] - 1)
|
|
|
|
offset = offset + header[3]
|
|
|
|
local chunk = data:sub(offset, offset + header[4] - 1)
|
|
|
|
offset = offset + header[4]
|
2022-12-02 08:20:48 +00:00
|
|
|
local file = data:sub(offset, offset + header[5] - 1)
|
|
|
|
offset = offset + header[5]
|
|
|
|
M.cache[modname] = {
|
|
|
|
hash = { size = header[0], mtime = { sec = header[1], nsec = header[2] } },
|
|
|
|
chunk = chunk,
|
|
|
|
modpath = file,
|
|
|
|
used = header[6],
|
|
|
|
}
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.autosave()
|
|
|
|
vim.api.nvim_create_autocmd("VimLeavePre", {
|
|
|
|
callback = function()
|
|
|
|
if M.dirty then
|
2022-12-02 11:43:34 +00:00
|
|
|
local hash = M.hash(M.config.path)
|
2022-11-30 13:19:50 +00:00
|
|
|
-- abort when the file was changed in the meantime
|
|
|
|
if hash == nil or M.eq(cache_hash, hash) then
|
|
|
|
M.save_cache()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|