2023-03-15 14:10:44 +00:00
|
|
|
---@diagnostic disable: duplicate-doc-alias
|
|
|
|
---@diagnostic disable: duplicate-doc-field
|
|
|
|
---@diagnostic disable: duplicate-set-field
|
|
|
|
|
|
|
|
-- interop with the native Neovim loader
|
|
|
|
if vim.loader then
|
|
|
|
return vim.loader
|
|
|
|
end
|
|
|
|
|
|
|
|
-- NEOVIM
|
2022-11-30 13:19:50 +00:00
|
|
|
local uv = vim.loop
|
2022-11-22 20:12:33 +00:00
|
|
|
|
|
|
|
local M = {}
|
2022-11-30 13:19:50 +00:00
|
|
|
|
|
|
|
---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
|
2023-02-14 10:00:56 +00:00
|
|
|
---@alias CacheEntry {hash:CacheHash, chunk:string}
|
2023-02-13 11:01:56 +00:00
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
---@class ModuleFindOpts
|
|
|
|
---@field all? boolean Search for all matches (defaults to `false`)
|
2023-02-13 11:01:56 +00:00
|
|
|
---@field rtp? boolean Search for modname in the runtime path (defaults to `true`)
|
|
|
|
---@field patterns? string[] Paterns to use (defaults to `{"/init.lua", ".lua"}`)
|
|
|
|
---@field paths? string[] Extra paths to search for modname
|
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
---@class ModuleInfo
|
|
|
|
---@field modpath string Path of the module
|
|
|
|
---@field modname string Name of the module
|
|
|
|
---@field stat? uv_fs_t File stat of the module path
|
|
|
|
|
2023-03-14 17:33:16 +00:00
|
|
|
M.VERSION = 3
|
2023-03-14 12:07:56 +00:00
|
|
|
M.path = vim.fn.stdpath("cache") .. "/luac"
|
2023-02-13 11:01:56 +00:00
|
|
|
M.enabled = false
|
2023-03-14 12:07:56 +00:00
|
|
|
---@type table<string, {total:number, time:number, [string]:number?}?>
|
2023-02-14 10:00:56 +00:00
|
|
|
M.stats = {
|
|
|
|
find = { total = 0, time = 0, not_found = 0 },
|
|
|
|
}
|
2023-02-13 11:01:56 +00:00
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
---@class Loader
|
2023-02-13 11:01:56 +00:00
|
|
|
---@field _rtp string[]
|
2023-02-14 10:00:56 +00:00
|
|
|
---@field _rtp_pure string[]
|
2023-02-13 11:01:56 +00:00
|
|
|
---@field _rtp_key string
|
2023-03-15 14:10:44 +00:00
|
|
|
local Loader = {
|
|
|
|
---@type table<string, table<string,ModuleInfo>>
|
2023-02-14 10:00:56 +00:00
|
|
|
_indexed = {},
|
|
|
|
---@type table<string, string[]>
|
2023-02-13 11:01:56 +00:00
|
|
|
_topmods = {},
|
|
|
|
_loadfile = loadfile,
|
2022-12-28 16:57:59 +00:00
|
|
|
}
|
2022-12-26 17:35:02 +00:00
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- Tracks the time spent in a function
|
|
|
|
---@private
|
2023-03-14 17:32:29 +00:00
|
|
|
function M._track(stat, start)
|
2023-02-14 10:00:56 +00:00
|
|
|
M.stats[stat] = M.stats[stat] or { total = 0, time = 0 }
|
|
|
|
M.stats[stat].total = M.stats[stat].total + 1
|
|
|
|
M.stats[stat].time = M.stats[stat].time + uv.hrtime() - start
|
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- slightly faster/different version than vim.fs.normalize
|
2023-03-15 14:10:44 +00:00
|
|
|
--- we also need to have it here, since the loader will load vim.fs
|
2023-02-13 11:01:56 +00:00
|
|
|
---@private
|
2023-03-15 14:10:44 +00:00
|
|
|
function Loader.normalize(path)
|
2023-02-13 11:01:56 +00:00
|
|
|
if path:sub(1, 1) == "~" then
|
2023-02-14 10:00:56 +00:00
|
|
|
local home = vim.loop.os_homedir() or "~"
|
2023-02-13 11:01:56 +00:00
|
|
|
if home:sub(-1) == "\\" or home:sub(-1) == "/" then
|
|
|
|
home = home:sub(1, -2)
|
2022-12-19 11:21:20 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
path = home .. path:sub(2)
|
2022-12-19 11:21:20 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
path = path:gsub("\\", "/"):gsub("/+", "/")
|
|
|
|
return path:sub(-1) == "/" and path:sub(1, -2) or path
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- Gets the rtp excluding after directories.
|
|
|
|
--- The result is cached, and will be updated if the runtime path changes.
|
|
|
|
--- When called from a fast event, the cached value will be returned.
|
2023-03-14 11:56:58 +00:00
|
|
|
--- @return string[] rtp, boolean updated
|
2023-02-13 11:01:56 +00:00
|
|
|
---@private
|
2023-03-15 14:10:44 +00:00
|
|
|
function Loader.get_rtp()
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
2023-02-13 11:01:56 +00:00
|
|
|
if vim.in_fast_event() then
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("get_rtp", start)
|
2023-03-15 14:10:44 +00:00
|
|
|
return (Loader._rtp or {}), false
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
local updated = false
|
2023-02-13 11:01:56 +00:00
|
|
|
local key = vim.go.rtp
|
2023-03-15 14:10:44 +00:00
|
|
|
if key ~= Loader._rtp_key then
|
|
|
|
Loader._rtp = {}
|
2023-02-13 11:01:56 +00:00
|
|
|
for _, path in ipairs(vim.api.nvim_get_runtime_file("", true)) do
|
2023-03-15 14:10:44 +00:00
|
|
|
path = Loader.normalize(path)
|
2023-02-13 11:01:56 +00:00
|
|
|
-- skip after directories
|
2023-03-15 14:10:44 +00:00
|
|
|
if path:sub(-6, -1) ~= "/after" and not (Loader._indexed[path] and vim.tbl_isempty(Loader._indexed[path])) then
|
|
|
|
Loader._rtp[#Loader._rtp + 1] = path
|
2022-12-19 11:21:20 +00:00
|
|
|
end
|
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
updated = true
|
2023-03-15 14:10:44 +00:00
|
|
|
Loader._rtp_key = key
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("get_rtp", start)
|
2023-03-15 14:10:44 +00:00
|
|
|
return Loader._rtp, updated
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- Returns the cache file name
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param name string can be a module name, or a file name
|
2023-03-14 11:56:58 +00:00
|
|
|
---@return string file_name
|
2023-02-13 11:01:56 +00:00
|
|
|
---@private
|
2023-03-15 14:10:44 +00:00
|
|
|
function Loader.cache_file(name)
|
2023-02-14 10:00:56 +00:00
|
|
|
local ret = M.path .. "/" .. name:gsub("[/\\:]", "%%")
|
|
|
|
return ret:sub(-4) == ".lua" and (ret .. "c") or (ret .. ".luac")
|
2023-01-03 15:17:15 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- Saves the cache entry for a given module or file
|
2023-03-14 11:56:58 +00:00
|
|
|
---@param name string module name or filename
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param entry CacheEntry
|
|
|
|
---@private
|
2023-03-15 14:10:44 +00:00
|
|
|
function Loader.write(name, entry)
|
|
|
|
local cname = Loader.cache_file(name)
|
2023-02-13 11:01:56 +00:00
|
|
|
local f = assert(uv.fs_open(cname, "w", 438))
|
|
|
|
local header = {
|
|
|
|
M.VERSION,
|
|
|
|
entry.hash.size,
|
|
|
|
entry.hash.mtime.sec,
|
|
|
|
entry.hash.mtime.nsec,
|
|
|
|
}
|
2023-03-14 17:33:16 +00:00
|
|
|
uv.fs_write(f, table.concat(header, ",") .. "\0")
|
2023-02-13 11:01:56 +00:00
|
|
|
uv.fs_write(f, entry.chunk)
|
|
|
|
uv.fs_close(f)
|
2022-12-02 11:43:34 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- Loads the cache entry for a given module or file
|
2023-03-14 11:56:58 +00:00
|
|
|
---@param name string module name or filename
|
2023-02-13 11:01:56 +00:00
|
|
|
---@return CacheEntry?
|
|
|
|
---@private
|
2023-03-15 14:10:44 +00:00
|
|
|
function Loader.read(name)
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
2023-03-15 14:10:44 +00:00
|
|
|
local cname = Loader.cache_file(name)
|
2023-02-13 11:01:56 +00:00
|
|
|
local f = uv.fs_open(cname, "r", 438)
|
|
|
|
if f then
|
|
|
|
local hash = uv.fs_fstat(f) --[[@as CacheHash]]
|
|
|
|
local data = uv.fs_read(f, hash.size, 0) --[[@as string]]
|
|
|
|
uv.fs_close(f)
|
|
|
|
|
2023-03-14 17:33:16 +00:00
|
|
|
local zero = data:find("\0", 1, true)
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
---@type integer[]|{[0]:integer}
|
2023-03-14 17:33:16 +00:00
|
|
|
local header = vim.split(data:sub(1, zero - 1), ",")
|
|
|
|
if tonumber(header[1]) ~= M.VERSION then
|
2023-02-13 11:01:56 +00:00
|
|
|
return
|
2022-12-29 08:06:23 +00:00
|
|
|
end
|
2023-03-14 17:33:16 +00:00
|
|
|
M._track("read", start)
|
2023-02-13 11:01:56 +00:00
|
|
|
return {
|
2023-03-14 17:33:16 +00:00
|
|
|
hash = { size = tonumber(header[2]), mtime = { sec = tonumber(header[3]), nsec = tonumber(header[4]) } },
|
|
|
|
chunk = data:sub(zero + 1),
|
2023-02-13 11:01:56 +00:00
|
|
|
}
|
2022-12-29 08:06:23 +00:00
|
|
|
end
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("read", start)
|
2022-12-29 08:06:23 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- The `package.loaders` loader for lua files using the cache.
|
2023-03-14 11:56:58 +00:00
|
|
|
---@param modname string module name
|
|
|
|
---@return string|function
|
2023-02-13 11:01:56 +00:00
|
|
|
---@private
|
2023-03-15 14:10:44 +00:00
|
|
|
function Loader.loader(modname)
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
2023-03-15 14:10:44 +00:00
|
|
|
local ret = M.find(modname)[1]
|
|
|
|
if ret then
|
|
|
|
local chunk, err = Loader.load(ret.modpath, { hash = ret.stat })
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("loader", start)
|
2023-02-15 16:08:36 +00:00
|
|
|
return chunk or error(err)
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("loader", start)
|
2023-03-14 12:07:56 +00:00
|
|
|
return "\ncache_loader: module " .. modname .. " not found"
|
2023-02-14 10:00:56 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- The `package.loaders` loader for libs
|
2023-03-14 11:56:58 +00:00
|
|
|
---@param modname string module name
|
|
|
|
---@return string|function
|
2023-02-14 10:00:56 +00:00
|
|
|
---@private
|
2023-03-15 14:10:44 +00:00
|
|
|
function Loader.loader_lib(modname)
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
2023-03-20 07:01:30 +00:00
|
|
|
local sysname = uv.os_uname().sysname:lower() or ""
|
|
|
|
local is_win = sysname:find("win", 1, true) and not sysname:find("darwin", 1, true)
|
2023-03-15 14:10:44 +00:00
|
|
|
local ret = M.find(modname, { patterns = is_win and { ".dll" } or { ".so" } })[1]
|
2023-02-14 10:00:56 +00:00
|
|
|
---@type function?, string?
|
2023-03-15 14:10:44 +00:00
|
|
|
if ret then
|
2023-02-14 10:00:56 +00:00
|
|
|
-- Making function name in Lua 5.1 (see src/loadlib.c:mkfuncname) is
|
|
|
|
-- a) strip prefix up to and including the first dash, if any
|
|
|
|
-- b) replace all dots by underscores
|
|
|
|
-- c) prepend "luaopen_"
|
|
|
|
-- So "foo-bar.baz" should result in "luaopen_bar_baz"
|
|
|
|
local dash = modname:find("-", 1, true)
|
|
|
|
local funcname = dash and modname:sub(dash + 1) or modname
|
2023-03-15 14:10:44 +00:00
|
|
|
local chunk, err = package.loadlib(ret.modpath, "luaopen_" .. funcname:gsub("%.", "_"))
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("loader_lib", start)
|
2023-02-15 16:08:36 +00:00
|
|
|
return chunk or error(err)
|
2023-02-14 10:00:56 +00:00
|
|
|
end
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("loader_lib", start)
|
2023-03-14 12:07:56 +00:00
|
|
|
return "\ncache_loader_lib: module " .. modname .. " not found"
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- `loadfile` using the cache
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param filename? string
|
|
|
|
---@param mode? "b"|"t"|"bt"
|
|
|
|
---@param env? table
|
2023-03-15 14:10:44 +00:00
|
|
|
---@param hash? CacheHash
|
2023-02-13 11:01:56 +00:00
|
|
|
---@return function?, string? error_message
|
|
|
|
---@private
|
2023-03-14 17:33:39 +00:00
|
|
|
-- luacheck: ignore 312
|
2023-03-15 14:10:44 +00:00
|
|
|
function Loader.loadfile(filename, mode, env, hash)
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
2023-03-15 14:10:44 +00:00
|
|
|
filename = Loader.normalize(filename)
|
2023-03-14 11:56:58 +00:00
|
|
|
mode = nil -- ignore mode, since we byte-compile the lua source files
|
2023-03-15 14:10:44 +00:00
|
|
|
local chunk, err = Loader.load(filename, { mode = mode, env = env, hash = hash })
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("loadfile", start)
|
2023-02-14 10:00:56 +00:00
|
|
|
return chunk, err
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- Checks whether two cache hashes are the same based on:
|
|
|
|
--- * file size
|
|
|
|
--- * mtime in seconds
|
|
|
|
--- * mtime in nanoseconds
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param h1 CacheHash
|
|
|
|
---@param h2 CacheHash
|
|
|
|
---@private
|
2023-03-15 14:10:44 +00:00
|
|
|
function Loader.eq(h1, h2)
|
2023-02-13 11:01:56 +00:00
|
|
|
return h1 and h2 and h1.size == h2.size and h1.mtime.sec == h2.mtime.sec and h1.mtime.nsec == h2.mtime.nsec
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- Loads the given module path using the cache
|
2022-12-19 12:34:37 +00:00
|
|
|
---@param modpath string
|
2023-03-14 13:07:41 +00:00
|
|
|
---@param opts? {hash?: CacheHash, mode?: "b"|"t"|"bt", env?:table} (table|nil) Options for loading the module:
|
|
|
|
--- - hash: (table) the hash of the file to load if it is already known. (defaults to `vim.loop.fs_stat({modpath})`)
|
|
|
|
--- - mode: (string) the mode to load the module with. "b"|"t"|"bt" (defaults to `nil`)
|
|
|
|
--- - env: (table) the environment to load the module in. (defaults to `nil`)
|
|
|
|
---@see |luaL_loadfile()|
|
2022-12-19 12:34:37 +00:00
|
|
|
---@return function?, string? error_message
|
2023-03-15 14:10:44 +00:00
|
|
|
---@private
|
|
|
|
function Loader.load(modpath, opts)
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
opts = opts or {}
|
|
|
|
local hash = opts.hash or uv.fs_stat(modpath)
|
2023-02-14 10:00:56 +00:00
|
|
|
---@type function?, string?
|
|
|
|
local chunk, err
|
|
|
|
|
2022-12-19 12:34:37 +00:00
|
|
|
if not hash then
|
|
|
|
-- trigger correct error
|
2023-03-15 14:10:44 +00:00
|
|
|
chunk, err = Loader._loadfile(modpath, opts.mode, opts.env)
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("load", start)
|
2023-02-14 10:00:56 +00:00
|
|
|
return chunk, err
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
local entry = Loader.read(modpath)
|
|
|
|
if entry and Loader.eq(entry.hash, hash) then
|
2023-02-13 11:01:56 +00:00
|
|
|
-- found in cache and up to date
|
2023-02-14 10:00:56 +00:00
|
|
|
-- selene: allow(incorrect_standard_library_use)
|
|
|
|
chunk, err = load(entry.chunk --[[@as string]], "@" .. modpath, opts.mode, opts.env)
|
2023-02-13 11:01:56 +00:00
|
|
|
if not (err and err:find("cannot load incompatible bytecode", 1, true)) then
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("load", start)
|
2023-02-13 11:01:56 +00:00
|
|
|
return chunk, err
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
entry = { hash = hash, modpath = modpath }
|
2022-12-19 12:34:37 +00:00
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
chunk, err = Loader._loadfile(modpath, opts.mode, opts.env)
|
2022-12-19 12:34:37 +00:00
|
|
|
if chunk then
|
2022-12-02 08:20:48 +00:00
|
|
|
entry.chunk = string.dump(chunk)
|
2023-03-15 14:10:44 +00:00
|
|
|
Loader.write(modpath, entry)
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("load", start)
|
2022-12-19 12:34:37 +00:00
|
|
|
return chunk, err
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
--- Finds lua modules for the given module name.
|
|
|
|
---@param modname string Module name, or `"*"` to find the top-level modules instead
|
|
|
|
---@param opts? ModuleFindOpts (table|nil) Options for finding a module:
|
2023-03-14 13:07:41 +00:00
|
|
|
--- - rtp: (boolean) Search for modname in the runtime path (defaults to `true`)
|
|
|
|
--- - paths: (string[]) Extra paths to search for modname (defaults to `{}`)
|
2023-03-16 13:13:18 +00:00
|
|
|
--- - patterns: (string[]) List of patterns to use when searching for modules.
|
2023-03-16 22:53:36 +00:00
|
|
|
--- A pattern is a string added to the basename of the Lua module being searched.
|
|
|
|
--- (defaults to `{"/init.lua", ".lua"}`)
|
|
|
|
--- - all: (boolean) Return all matches instead of just the first one (defaults to `false`)
|
2023-03-15 14:10:44 +00:00
|
|
|
---@return ModuleInfo[] (list) A list of results with the following properties:
|
|
|
|
--- - modpath: (string) the path to the module
|
|
|
|
--- - modname: (string) the name of the module
|
|
|
|
--- - stat: (table|nil) the fs_stat of the module path. Won't be returned for `modname="*"`
|
2023-03-14 11:56:58 +00:00
|
|
|
function M.find(modname, opts)
|
2022-12-28 16:57:59 +00:00
|
|
|
local start = uv.hrtime()
|
2023-02-14 10:00:56 +00:00
|
|
|
opts = opts or {}
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
modname = modname:gsub("/", ".")
|
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)
|
2023-02-15 17:19:57 +00:00
|
|
|
|
2023-03-14 11:56:58 +00:00
|
|
|
-- HACK: fix incorrect require statements. Really not a fan of keeping this,
|
|
|
|
-- but apparently the regular lua loader also allows this
|
2023-02-28 11:38:10 +00:00
|
|
|
if idx == 1 then
|
|
|
|
modname = modname:gsub("^%.+", "")
|
|
|
|
basename = modname:gsub("%.", "/")
|
|
|
|
idx = modname:find(".", 1, true)
|
|
|
|
end
|
2023-03-14 11:56:58 +00:00
|
|
|
|
|
|
|
-- get the top-level module name
|
2022-12-28 16:57:59 +00:00
|
|
|
local topmod = idx and modname:sub(1, idx - 1) or modname
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
-- OPTIM: search for a directory first when topmod == modname
|
|
|
|
local patterns = opts.patterns or (topmod == modname and { "/init.lua", ".lua" } or { ".lua", "/init.lua" })
|
|
|
|
for p, pattern in ipairs(patterns) do
|
|
|
|
patterns[p] = "/lua/" .. basename .. pattern
|
|
|
|
end
|
2022-12-28 16:57:59 +00:00
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
---@type ModuleInfo[]
|
|
|
|
local results = {}
|
|
|
|
|
|
|
|
-- Only continue if we haven't found anything yet or we want to find all
|
|
|
|
---@private
|
|
|
|
local function continue()
|
|
|
|
return #results == 0 or opts.all
|
|
|
|
end
|
|
|
|
|
2023-03-14 11:56:58 +00:00
|
|
|
-- Checks if the given paths contain the top-level module.
|
|
|
|
-- If so, it tries to find the module path for the given module name.
|
2023-02-14 10:00:56 +00:00
|
|
|
---@param paths string[]
|
2023-03-14 11:56:58 +00:00
|
|
|
---@private
|
2023-02-14 10:00:56 +00:00
|
|
|
local function _find(paths)
|
|
|
|
for _, path in ipairs(paths) do
|
2023-03-15 14:10:44 +00:00
|
|
|
if topmod == "*" then
|
|
|
|
for _, r in pairs(Loader.lsmod(path)) do
|
|
|
|
results[#results + 1] = r
|
|
|
|
if not continue() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif Loader.lsmod(path)[topmod] then
|
2023-02-14 10:00:56 +00:00
|
|
|
for _, pattern in ipairs(patterns) do
|
|
|
|
local modpath = path .. pattern
|
|
|
|
M.stats.find.stat = (M.stats.find.stat or 0) + 1
|
|
|
|
local hash = uv.fs_stat(modpath)
|
|
|
|
if hash then
|
2023-03-15 14:10:44 +00:00
|
|
|
results[#results + 1] = { modpath = modpath, stat = hash, modname = modname }
|
|
|
|
if not continue() then
|
|
|
|
return
|
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
end
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-14 11:56:58 +00:00
|
|
|
-- always check the rtp first
|
2023-02-14 10:00:56 +00:00
|
|
|
if opts.rtp ~= false then
|
2023-03-15 14:10:44 +00:00
|
|
|
_find(Loader._rtp or {})
|
|
|
|
if continue() then
|
|
|
|
local rtp, updated = Loader.get_rtp()
|
2023-02-14 10:00:56 +00:00
|
|
|
if updated then
|
2023-03-15 14:10:44 +00:00
|
|
|
_find(rtp)
|
2023-02-14 10:00:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-03-14 11:56:58 +00:00
|
|
|
|
|
|
|
-- check any additional paths
|
2023-03-15 14:10:44 +00:00
|
|
|
if continue() and opts.paths then
|
|
|
|
_find(opts.paths)
|
2023-02-14 10:00:56 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("find", start)
|
2023-03-15 14:10:44 +00:00
|
|
|
if #results == 0 then
|
|
|
|
-- module not found
|
|
|
|
M.stats.find.not_found = M.stats.find.not_found + 1
|
2023-02-14 10:00:56 +00:00
|
|
|
end
|
2023-03-14 17:33:39 +00:00
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
return results
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
--- Resets the topmods cache for the path, or all the paths
|
|
|
|
--- if path is nil.
|
|
|
|
---@param path string? path to reset
|
2023-02-13 11:01:56 +00:00
|
|
|
function M.reset(path)
|
2023-03-15 14:10:44 +00:00
|
|
|
if path then
|
|
|
|
Loader._indexed[Loader.normalize(path)] = nil
|
|
|
|
else
|
|
|
|
Loader._indexed = {}
|
|
|
|
end
|
2022-12-19 11:21:20 +00:00
|
|
|
end
|
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
--- Enables the experimental Lua module loader:
|
|
|
|
--- * overrides loadfile
|
|
|
|
--- * adds the lua loader using the byte-compilation cache
|
2023-03-14 13:07:41 +00:00
|
|
|
--- * adds the libs loader
|
2023-03-15 14:10:44 +00:00
|
|
|
--- * removes the default Neovim loader
|
2023-02-13 11:01:56 +00:00
|
|
|
function M.enable()
|
|
|
|
if M.enabled then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
M.enabled = true
|
|
|
|
vim.fn.mkdir(vim.fn.fnamemodify(M.path, ":p"), "p")
|
|
|
|
-- selene: allow(global_usage)
|
2023-03-15 14:10:44 +00:00
|
|
|
_G.loadfile = Loader.loadfile
|
2023-02-14 10:00:56 +00:00
|
|
|
-- add lua loader
|
2023-03-15 14:10:44 +00:00
|
|
|
table.insert(package.loaders, 2, Loader.loader)
|
2023-02-14 10:00:56 +00:00
|
|
|
-- add libs loader
|
2023-03-15 14:10:44 +00:00
|
|
|
table.insert(package.loaders, 3, Loader.loader_lib)
|
2023-02-14 10:00:56 +00:00
|
|
|
-- remove Neovim loader
|
|
|
|
for l, loader in ipairs(package.loaders) do
|
|
|
|
if loader == vim._load_package then
|
|
|
|
table.remove(package.loaders, l)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-03-14 18:54:44 +00:00
|
|
|
|
|
|
|
-- this will reset the top-mods in case someone adds a new
|
|
|
|
-- top-level lua module to a path already on the rtp
|
|
|
|
vim.api.nvim_create_autocmd("BufWritePost", {
|
|
|
|
group = vim.api.nvim_create_augroup("cache_topmods_reset", { clear = true }),
|
|
|
|
callback = function(event)
|
|
|
|
local bufname = event.match ---@type string
|
|
|
|
local idx = bufname:find("/lua/", 1, true)
|
|
|
|
if idx then
|
|
|
|
M.reset(bufname:sub(1, idx - 1))
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2022-12-29 00:25:05 +00:00
|
|
|
end
|
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
--- Disables the experimental Lua module loader:
|
|
|
|
--- * removes the loaders
|
|
|
|
--- * adds the default Neovim loader
|
2023-02-13 11:01:56 +00:00
|
|
|
function M.disable()
|
|
|
|
if not M.enabled then
|
|
|
|
return
|
2022-12-02 11:43:34 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
M.enabled = false
|
|
|
|
-- selene: allow(global_usage)
|
2023-03-15 14:10:44 +00:00
|
|
|
_G.loadfile = Loader._loadfile
|
2023-02-13 11:01:56 +00:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for l, loader in ipairs(package.loaders) do
|
2023-03-15 14:10:44 +00:00
|
|
|
if loader == Loader.loader or loader == Loader.loader_lib then
|
2023-02-13 11:01:56 +00:00
|
|
|
table.remove(package.loaders, l)
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
2022-12-02 11:43:34 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
table.insert(package.loaders, 2, vim._load_package)
|
2023-03-14 18:54:44 +00:00
|
|
|
vim.api.nvim_del_augroup_by_name("cache_topmods_reset")
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- Return the top-level `/lua/*` modules for this path
|
|
|
|
---@param path string path to check for top-level lua modules
|
2023-03-15 14:10:44 +00:00
|
|
|
---@private
|
|
|
|
function Loader.lsmod(path)
|
|
|
|
if not Loader._indexed[path] then
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
2023-03-15 14:10:44 +00:00
|
|
|
Loader._indexed[path] = {}
|
2023-02-13 11:01:56 +00:00
|
|
|
local handle = vim.loop.fs_scandir(path .. "/lua")
|
|
|
|
while handle do
|
|
|
|
local name, t = vim.loop.fs_scandir_next(handle)
|
|
|
|
if not name then
|
|
|
|
break
|
|
|
|
end
|
2023-03-15 14:10:44 +00:00
|
|
|
local modpath = path .. "/lua/" .. name
|
2023-02-13 11:01:56 +00:00
|
|
|
-- HACK: type is not always returned due to a bug in luv
|
2023-03-15 14:10:44 +00:00
|
|
|
t = t or uv.fs_stat(modpath).type
|
2023-02-13 11:01:56 +00:00
|
|
|
---@type string
|
|
|
|
local topname
|
2023-02-15 16:08:36 +00:00
|
|
|
local ext = name:sub(-4)
|
|
|
|
if ext == ".lua" or ext == ".dll" then
|
2023-02-13 11:01:56 +00:00
|
|
|
topname = name:sub(1, -5)
|
2023-02-15 16:08:36 +00:00
|
|
|
elseif name:sub(-3) == ".so" then
|
|
|
|
topname = name:sub(1, -4)
|
2023-02-13 11:01:56 +00:00
|
|
|
elseif t == "link" or t == "directory" then
|
|
|
|
topname = name
|
|
|
|
end
|
|
|
|
if topname then
|
2023-03-15 14:10:44 +00:00
|
|
|
Loader._indexed[path][topname] = { modpath = modpath, modname = topname }
|
|
|
|
Loader._topmods[topname] = Loader._topmods[topname] or {}
|
|
|
|
if not vim.tbl_contains(Loader._topmods[topname], path) then
|
|
|
|
table.insert(Loader._topmods[topname], path)
|
2023-02-14 10:00:56 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("lsmod", start)
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
2023-03-15 14:10:44 +00:00
|
|
|
return Loader._indexed[path]
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- Debug function that wrapps all loaders and tracks stats
|
2023-03-15 14:10:44 +00:00
|
|
|
---@private
|
2023-02-14 10:00:56 +00:00
|
|
|
function M.profile_loaders()
|
|
|
|
for l, loader in pairs(package.loaders) do
|
|
|
|
local loc = debug.getinfo(loader, "Sn").source:sub(2)
|
|
|
|
package.loaders[l] = function(modname)
|
|
|
|
local start = vim.loop.hrtime()
|
|
|
|
local ret = loader(modname)
|
2023-03-14 17:32:29 +00:00
|
|
|
M._track("loader " .. l .. ": " .. loc, start)
|
|
|
|
M._track("loader_all", start)
|
2023-02-14 10:00:56 +00:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-14 13:07:41 +00:00
|
|
|
--- Prints all cache stats
|
2023-03-15 14:10:44 +00:00
|
|
|
---@private
|
2023-02-13 11:01:56 +00:00
|
|
|
function M.inspect()
|
2023-03-14 11:56:58 +00:00
|
|
|
---@private
|
2023-02-13 11:01:56 +00:00
|
|
|
local function ms(nsec)
|
|
|
|
return math.floor(nsec / 1e6 * 1000 + 0.5) / 1000 .. "ms"
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
local chunks = {} ---@type string[][]
|
2023-02-14 10:00:56 +00:00
|
|
|
---@type string[]
|
|
|
|
local stats = vim.tbl_keys(M.stats)
|
|
|
|
table.sort(stats)
|
|
|
|
for _, stat in ipairs(stats) do
|
|
|
|
vim.list_extend(chunks, {
|
|
|
|
{ "\n" .. stat .. "\n", "Title" },
|
|
|
|
{ "* total: " },
|
|
|
|
{ tostring(M.stats[stat].total) .. "\n", "Number" },
|
|
|
|
{ "* time: " },
|
|
|
|
{ ms(M.stats[stat].time) .. "\n", "Bold" },
|
|
|
|
{ "* avg time: " },
|
|
|
|
{ ms(M.stats[stat].time / M.stats[stat].total) .. "\n", "Bold" },
|
|
|
|
})
|
|
|
|
for k, v in pairs(M.stats[stat]) do
|
|
|
|
if not vim.tbl_contains({ "time", "total" }, k) then
|
|
|
|
chunks[#chunks + 1] = { "* " .. k .. ":" .. string.rep(" ", 9 - #k) }
|
|
|
|
chunks[#chunks + 1] = { tostring(v) .. "\n", "Number" }
|
|
|
|
end
|
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
|
|
|
vim.api.nvim_echo(chunks, true, {})
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
M._Cache = Loader
|
2022-11-22 20:12:33 +00:00
|
|
|
|
|
|
|
return M
|