2024-02-12 19:38:37 -07:00
|
|
|
local fs = vim.fs -- "vim.fs" is a dependency, so must be loaded early.
|
2023-06-03 03:06:00 -07:00
|
|
|
local uv = vim.uv
|
2023-12-13 06:04:24 -07:00
|
|
|
local uri_encode = vim.uri_encode --- @type function
|
2023-03-26 03:42:15 -07:00
|
|
|
|
2023-03-26 04:49:38 -07:00
|
|
|
--- @type (fun(modename: string): fun()|string)[]
|
|
|
|
local loaders = package.loaders
|
|
|
|
|
2023-03-26 03:42:15 -07:00
|
|
|
local M = {}
|
|
|
|
|
2024-03-06 03:03:55 -07:00
|
|
|
---@alias CacheHash {mtime: {nsec: integer, sec: integer}, size: integer, type?: string}
|
2023-03-26 03:42:15 -07:00
|
|
|
---@alias CacheEntry {hash:CacheHash, chunk:string}
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
--- @class vim.loader.find.Opts
|
|
|
|
--- @inlinedoc
|
|
|
|
---
|
|
|
|
--- Search for modname in the runtime path.
|
|
|
|
--- (default: `true`)
|
|
|
|
--- @field rtp? boolean
|
|
|
|
---
|
|
|
|
--- Extra paths to search for modname
|
|
|
|
--- (default: `{}`)
|
|
|
|
--- @field paths? string[]
|
|
|
|
---
|
|
|
|
--- List of patterns to use when searching for modules.
|
|
|
|
--- A pattern is a string added to the basename of the Lua module being searched.
|
|
|
|
--- (default: `{"/init.lua", ".lua"}`)
|
|
|
|
--- @field patterns? string[]
|
|
|
|
---
|
|
|
|
--- Search for all matches.
|
|
|
|
--- (default: `false`)
|
|
|
|
--- @field all? boolean
|
|
|
|
|
|
|
|
--- @class vim.loader.ModuleInfo
|
|
|
|
--- @inlinedoc
|
|
|
|
---
|
|
|
|
--- Path of the module
|
|
|
|
--- @field modpath string
|
|
|
|
---
|
|
|
|
--- Name of the module
|
|
|
|
--- @field modname string
|
|
|
|
---
|
|
|
|
--- The fs_stat of the module path. Won't be returned for `modname="*"`
|
|
|
|
--- @field stat? uv.uv_fs_t
|
2023-03-26 03:42:15 -07:00
|
|
|
|
|
|
|
---@alias LoaderStats table<string, {total:number, time:number, [string]:number?}?>
|
|
|
|
|
2023-07-18 07:42:30 -07:00
|
|
|
---@nodoc
|
2023-03-26 03:42:15 -07:00
|
|
|
M.path = vim.fn.stdpath('cache') .. '/luac'
|
2023-07-18 07:42:30 -07:00
|
|
|
|
|
|
|
---@nodoc
|
2023-03-26 03:42:15 -07:00
|
|
|
M.enabled = false
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
---@class (private) Loader
|
|
|
|
---@field private _rtp string[]
|
|
|
|
---@field private _rtp_pure string[]
|
|
|
|
---@field private _rtp_key string
|
|
|
|
---@field private _hashes? table<string, CacheHash>
|
2023-03-26 03:42:15 -07:00
|
|
|
local Loader = {
|
2023-08-01 08:28:28 -07:00
|
|
|
VERSION = 4,
|
2024-02-27 08:20:32 -07:00
|
|
|
---@type table<string, table<string,vim.loader.ModuleInfo>>
|
2023-03-26 03:42:15 -07:00
|
|
|
_indexed = {},
|
|
|
|
---@type table<string, string[]>
|
|
|
|
_topmods = {},
|
|
|
|
_loadfile = loadfile,
|
|
|
|
---@type LoaderStats
|
|
|
|
_stats = {
|
|
|
|
find = { total = 0, time = 0, not_found = 0 },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-31 05:05:22 -07:00
|
|
|
--- @param path string
|
2023-04-13 09:34:47 -07:00
|
|
|
--- @return CacheHash
|
2023-03-31 05:05:22 -07:00
|
|
|
--- @private
|
|
|
|
function Loader.get_hash(path)
|
2023-04-13 09:34:47 -07:00
|
|
|
if not Loader._hashes then
|
|
|
|
return uv.fs_stat(path) --[[@as CacheHash]]
|
|
|
|
end
|
|
|
|
|
2023-03-31 05:05:22 -07:00
|
|
|
if not Loader._hashes[path] then
|
|
|
|
-- Note we must never save a stat for a non-existent path.
|
|
|
|
-- For non-existent paths fs_stat() will return nil.
|
|
|
|
Loader._hashes[path] = uv.fs_stat(path)
|
|
|
|
end
|
|
|
|
return Loader._hashes[path]
|
|
|
|
end
|
|
|
|
|
2023-03-26 05:01:48 -07:00
|
|
|
local function normalize(path)
|
2024-05-14 09:29:28 -07:00
|
|
|
return fs.normalize(path, { expand_env = false, _fast = true })
|
2023-03-26 03:42:15 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
--- 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.
|
|
|
|
--- @return string[] rtp, boolean updated
|
|
|
|
---@private
|
|
|
|
function Loader.get_rtp()
|
|
|
|
if vim.in_fast_event() then
|
|
|
|
return (Loader._rtp or {}), false
|
|
|
|
end
|
|
|
|
local updated = false
|
|
|
|
local key = vim.go.rtp
|
|
|
|
if key ~= Loader._rtp_key then
|
|
|
|
Loader._rtp = {}
|
|
|
|
for _, path in ipairs(vim.api.nvim_get_runtime_file('', true)) do
|
2023-03-26 05:01:48 -07:00
|
|
|
path = normalize(path)
|
2023-03-26 03:42:15 -07:00
|
|
|
-- skip after directories
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
updated = true
|
|
|
|
Loader._rtp_key = key
|
|
|
|
end
|
|
|
|
return Loader._rtp, updated
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns the cache file name
|
|
|
|
---@param name string can be a module name, or a file name
|
|
|
|
---@return string file_name
|
|
|
|
---@private
|
|
|
|
function Loader.cache_file(name)
|
2023-08-01 08:28:28 -07:00
|
|
|
local ret = ('%s/%s'):format(M.path, uri_encode(name, 'rfc2396'))
|
2023-03-26 03:42:15 -07:00
|
|
|
return ret:sub(-4) == '.lua' and (ret .. 'c') or (ret .. '.luac')
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Saves the cache entry for a given module or file
|
|
|
|
---@param name string module name or filename
|
|
|
|
---@param entry CacheEntry
|
|
|
|
---@private
|
|
|
|
function Loader.write(name, entry)
|
|
|
|
local cname = Loader.cache_file(name)
|
|
|
|
local f = assert(uv.fs_open(cname, 'w', 438))
|
|
|
|
local header = {
|
|
|
|
Loader.VERSION,
|
|
|
|
entry.hash.size,
|
|
|
|
entry.hash.mtime.sec,
|
|
|
|
entry.hash.mtime.nsec,
|
|
|
|
}
|
|
|
|
uv.fs_write(f, table.concat(header, ',') .. '\0')
|
|
|
|
uv.fs_write(f, entry.chunk)
|
|
|
|
uv.fs_close(f)
|
|
|
|
end
|
|
|
|
|
2023-03-31 05:05:22 -07:00
|
|
|
--- @param path string
|
|
|
|
--- @param mode integer
|
|
|
|
--- @return string? data
|
|
|
|
local function readfile(path, mode)
|
|
|
|
local f = uv.fs_open(path, 'r', mode)
|
|
|
|
if f then
|
|
|
|
local hash = assert(uv.fs_fstat(f))
|
|
|
|
local data = uv.fs_read(f, hash.size, 0) --[[@as string?]]
|
|
|
|
uv.fs_close(f)
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-26 03:42:15 -07:00
|
|
|
--- Loads the cache entry for a given module or file
|
|
|
|
---@param name string module name or filename
|
|
|
|
---@return CacheEntry?
|
|
|
|
---@private
|
|
|
|
function Loader.read(name)
|
|
|
|
local cname = Loader.cache_file(name)
|
2023-03-31 05:05:22 -07:00
|
|
|
local data = readfile(cname, 438)
|
|
|
|
if data then
|
2023-03-26 03:42:15 -07:00
|
|
|
local zero = data:find('\0', 1, true)
|
|
|
|
if not zero then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
---@type integer[]|{[0]:integer}
|
|
|
|
local header = vim.split(data:sub(1, zero - 1), ',')
|
|
|
|
if tonumber(header[1]) ~= Loader.VERSION then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return {
|
|
|
|
hash = {
|
|
|
|
size = tonumber(header[2]),
|
|
|
|
mtime = { sec = tonumber(header[3]), nsec = tonumber(header[4]) },
|
|
|
|
},
|
|
|
|
chunk = data:sub(zero + 1),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-22 03:44:51 -07:00
|
|
|
--- The `package.loaders` loader for Lua files using the cache.
|
2023-03-26 03:42:15 -07:00
|
|
|
---@param modname string module name
|
|
|
|
---@return string|function
|
|
|
|
---@private
|
|
|
|
function Loader.loader(modname)
|
2023-04-13 09:34:47 -07:00
|
|
|
Loader._hashes = {}
|
2023-03-26 03:42:15 -07:00
|
|
|
local ret = M.find(modname)[1]
|
|
|
|
if ret then
|
2023-03-31 05:05:22 -07:00
|
|
|
-- Make sure to call the global loadfile so we respect any augmentations done elsewhere.
|
|
|
|
-- E.g. profiling
|
|
|
|
local chunk, err = loadfile(ret.modpath)
|
2023-04-13 09:34:47 -07:00
|
|
|
Loader._hashes = nil
|
2023-03-26 03:42:15 -07:00
|
|
|
return chunk or error(err)
|
|
|
|
end
|
2023-04-13 09:34:47 -07:00
|
|
|
Loader._hashes = nil
|
2024-07-19 22:46:27 -07:00
|
|
|
return ("\n\tcache_loader: module '%s' not found"):format(modname)
|
2023-03-26 03:42:15 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
--- The `package.loaders` loader for libs
|
|
|
|
---@param modname string module name
|
|
|
|
---@return string|function
|
|
|
|
---@private
|
|
|
|
function Loader.loader_lib(modname)
|
|
|
|
local sysname = uv.os_uname().sysname:lower() or ''
|
|
|
|
local is_win = sysname:find('win', 1, true) and not sysname:find('darwin', 1, true)
|
|
|
|
local ret = M.find(modname, { patterns = is_win and { '.dll' } or { '.so' } })[1]
|
|
|
|
if ret then
|
|
|
|
-- 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
|
|
|
|
local chunk, err = package.loadlib(ret.modpath, 'luaopen_' .. funcname:gsub('%.', '_'))
|
|
|
|
return chunk or error(err)
|
|
|
|
end
|
2024-07-19 22:46:27 -07:00
|
|
|
return ("\n\tcache_loader_lib: module '%s' not found"):format(modname)
|
2023-03-26 03:42:15 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
--- `loadfile` using the cache
|
2023-03-31 05:05:22 -07:00
|
|
|
--- Note this has the mode and env arguments which is supported by LuaJIT and is 5.1 compatible.
|
2023-03-26 03:42:15 -07:00
|
|
|
---@param filename? string
|
2024-02-15 10:16:04 -07:00
|
|
|
---@param _mode? "b"|"t"|"bt"
|
2023-03-26 03:42:15 -07:00
|
|
|
---@param env? table
|
|
|
|
---@return function?, string? error_message
|
|
|
|
---@private
|
2024-02-15 10:16:04 -07:00
|
|
|
function Loader.loadfile(filename, _mode, env)
|
2023-06-22 03:44:51 -07:00
|
|
|
-- ignore mode, since we byte-compile the Lua source files
|
2024-02-15 10:16:04 -07:00
|
|
|
return Loader.load(normalize(filename), { env = env })
|
2023-03-26 03:42:15 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Checks whether two cache hashes are the same based on:
|
|
|
|
--- * file size
|
|
|
|
--- * mtime in seconds
|
|
|
|
--- * mtime in nanoseconds
|
|
|
|
---@param h1 CacheHash
|
|
|
|
---@param h2 CacheHash
|
|
|
|
---@private
|
|
|
|
function Loader.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
|
|
|
|
|
|
|
|
--- Loads the given module path using the cache
|
|
|
|
---@param modpath string
|
2023-03-31 05:05:22 -07:00
|
|
|
---@param opts? {mode?: "b"|"t"|"bt", env?:table} (table|nil) Options for loading the module:
|
2023-03-26 03:42:15 -07:00
|
|
|
--- - 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()|
|
|
|
|
---@return function?, string? error_message
|
|
|
|
---@private
|
|
|
|
function Loader.load(modpath, opts)
|
|
|
|
opts = opts or {}
|
2023-03-31 05:05:22 -07:00
|
|
|
local hash = Loader.get_hash(modpath)
|
2023-03-26 03:42:15 -07:00
|
|
|
---@type function?, string?
|
|
|
|
local chunk, err
|
|
|
|
|
|
|
|
if not hash then
|
|
|
|
-- trigger correct error
|
2023-03-26 05:23:34 -07:00
|
|
|
return Loader._loadfile(modpath, opts.mode, opts.env)
|
2023-03-26 03:42:15 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local entry = Loader.read(modpath)
|
|
|
|
if entry and Loader.eq(entry.hash, hash) then
|
|
|
|
-- found in cache and up to date
|
|
|
|
chunk, err = load(entry.chunk --[[@as string]], '@' .. modpath, opts.mode, opts.env)
|
|
|
|
if not (err and err:find('cannot load incompatible bytecode', 1, true)) then
|
|
|
|
return chunk, err
|
|
|
|
end
|
|
|
|
end
|
|
|
|
entry = { hash = hash, modpath = modpath }
|
|
|
|
|
|
|
|
chunk, err = Loader._loadfile(modpath, opts.mode, opts.env)
|
|
|
|
if chunk then
|
|
|
|
entry.chunk = string.dump(chunk)
|
|
|
|
Loader.write(modpath, entry)
|
|
|
|
end
|
|
|
|
return chunk, err
|
|
|
|
end
|
|
|
|
|
2023-06-22 03:44:51 -07:00
|
|
|
--- Finds Lua modules for the given module name.
|
2023-03-26 03:42:15 -07:00
|
|
|
---@param modname string Module name, or `"*"` to find the top-level modules instead
|
2024-02-27 08:20:32 -07:00
|
|
|
---@param opts? vim.loader.find.Opts Options for finding a module:
|
|
|
|
---@return vim.loader.ModuleInfo[]
|
2023-03-26 03:42:15 -07:00
|
|
|
function M.find(modname, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
|
|
|
|
modname = modname:gsub('/', '.')
|
|
|
|
local basename = modname:gsub('%.', '/')
|
|
|
|
local idx = modname:find('.', 1, true)
|
|
|
|
|
|
|
|
-- HACK: fix incorrect require statements. Really not a fan of keeping this,
|
2023-06-22 03:44:51 -07:00
|
|
|
-- but apparently the regular Lua loader also allows this
|
2023-03-26 03:42:15 -07:00
|
|
|
if idx == 1 then
|
|
|
|
modname = modname:gsub('^%.+', '')
|
|
|
|
basename = modname:gsub('%.', '/')
|
|
|
|
idx = modname:find('.', 1, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- get the top-level module name
|
|
|
|
local topmod = idx and modname:sub(1, idx - 1) or modname
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
---@type vim.loader.ModuleInfo[]
|
2023-03-26 03:42:15 -07:00
|
|
|
local results = {}
|
|
|
|
|
|
|
|
-- Only continue if we haven't found anything yet or we want to find all
|
|
|
|
local function continue()
|
|
|
|
return #results == 0 or opts.all
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Checks if the given paths contain the top-level module.
|
|
|
|
-- If so, it tries to find the module path for the given module name.
|
|
|
|
---@param paths string[]
|
|
|
|
local function _find(paths)
|
|
|
|
for _, path in ipairs(paths) do
|
|
|
|
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
|
|
|
|
for _, pattern in ipairs(patterns) do
|
|
|
|
local modpath = path .. pattern
|
|
|
|
Loader._stats.find.stat = (Loader._stats.find.stat or 0) + 1
|
2023-03-31 05:05:22 -07:00
|
|
|
local hash = Loader.get_hash(modpath)
|
2023-03-26 03:42:15 -07:00
|
|
|
if hash then
|
|
|
|
results[#results + 1] = { modpath = modpath, stat = hash, modname = modname }
|
|
|
|
if not continue() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- always check the rtp first
|
|
|
|
if opts.rtp ~= false then
|
|
|
|
_find(Loader._rtp or {})
|
|
|
|
if continue() then
|
|
|
|
local rtp, updated = Loader.get_rtp()
|
|
|
|
if updated then
|
|
|
|
_find(rtp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check any additional paths
|
|
|
|
if continue() and opts.paths then
|
|
|
|
_find(opts.paths)
|
|
|
|
end
|
|
|
|
|
|
|
|
if #results == 0 then
|
|
|
|
-- module not found
|
|
|
|
Loader._stats.find.not_found = Loader._stats.find.not_found + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
return results
|
|
|
|
end
|
|
|
|
|
2023-03-31 05:05:22 -07:00
|
|
|
--- Resets the cache for the path, or all the paths
|
2023-03-26 03:42:15 -07:00
|
|
|
--- if path is nil.
|
|
|
|
---@param path string? path to reset
|
|
|
|
function M.reset(path)
|
|
|
|
if path then
|
2023-03-26 05:01:48 -07:00
|
|
|
Loader._indexed[normalize(path)] = nil
|
2023-03-26 03:42:15 -07:00
|
|
|
else
|
|
|
|
Loader._indexed = {}
|
|
|
|
end
|
2023-03-31 05:05:22 -07:00
|
|
|
|
|
|
|
-- Path could be a directory so just clear all the hashes.
|
2023-04-13 09:34:47 -07:00
|
|
|
if Loader._hashes then
|
|
|
|
Loader._hashes = {}
|
|
|
|
end
|
2023-03-26 03:42:15 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Enables the experimental Lua module loader:
|
|
|
|
--- * overrides loadfile
|
2023-06-22 03:44:51 -07:00
|
|
|
--- * adds the Lua loader using the byte-compilation cache
|
2023-03-26 03:42:15 -07:00
|
|
|
--- * adds the libs loader
|
2023-06-22 03:44:51 -07:00
|
|
|
--- * removes the default Nvim loader
|
2023-03-26 03:42:15 -07:00
|
|
|
function M.enable()
|
|
|
|
if M.enabled then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
M.enabled = true
|
|
|
|
vim.fn.mkdir(vim.fn.fnamemodify(M.path, ':p'), 'p')
|
|
|
|
_G.loadfile = Loader.loadfile
|
2023-06-22 03:44:51 -07:00
|
|
|
-- add Lua loader
|
2023-03-26 04:49:38 -07:00
|
|
|
table.insert(loaders, 2, Loader.loader)
|
2023-03-26 03:42:15 -07:00
|
|
|
-- add libs loader
|
2023-03-26 04:49:38 -07:00
|
|
|
table.insert(loaders, 3, Loader.loader_lib)
|
2023-06-22 03:44:51 -07:00
|
|
|
-- remove Nvim loader
|
2023-03-26 04:49:38 -07:00
|
|
|
for l, loader in ipairs(loaders) do
|
2023-03-26 03:42:15 -07:00
|
|
|
if loader == vim._load_package then
|
2023-03-26 04:49:38 -07:00
|
|
|
table.remove(loaders, l)
|
2023-03-26 03:42:15 -07:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Disables the experimental Lua module loader:
|
|
|
|
--- * removes the loaders
|
2023-06-22 03:44:51 -07:00
|
|
|
--- * adds the default Nvim loader
|
2023-03-26 03:42:15 -07:00
|
|
|
function M.disable()
|
|
|
|
if not M.enabled then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
M.enabled = false
|
|
|
|
_G.loadfile = Loader._loadfile
|
2023-03-26 04:49:38 -07:00
|
|
|
for l, loader in ipairs(loaders) do
|
2023-03-26 03:42:15 -07:00
|
|
|
if loader == Loader.loader or loader == Loader.loader_lib then
|
2023-03-26 04:49:38 -07:00
|
|
|
table.remove(loaders, l)
|
2023-03-26 03:42:15 -07:00
|
|
|
end
|
|
|
|
end
|
2023-03-26 04:49:38 -07:00
|
|
|
table.insert(loaders, 2, vim._load_package)
|
2023-03-26 03:42:15 -07:00
|
|
|
end
|
|
|
|
|
2023-06-22 03:44:51 -07:00
|
|
|
--- Return the top-level \`/lua/*` modules for this path
|
|
|
|
---@param path string path to check for top-level Lua modules
|
2023-03-26 03:42:15 -07:00
|
|
|
---@private
|
|
|
|
function Loader.lsmod(path)
|
|
|
|
if not Loader._indexed[path] then
|
|
|
|
Loader._indexed[path] = {}
|
2024-02-12 19:38:37 -07:00
|
|
|
for name, t in fs.dir(path .. '/lua') do
|
2023-03-26 03:42:15 -07:00
|
|
|
local modpath = path .. '/lua/' .. name
|
|
|
|
-- HACK: type is not always returned due to a bug in luv
|
2023-03-31 05:05:22 -07:00
|
|
|
t = t or Loader.get_hash(modpath).type
|
2023-03-26 03:42:15 -07:00
|
|
|
---@type string
|
|
|
|
local topname
|
|
|
|
local ext = name:sub(-4)
|
|
|
|
if ext == '.lua' or ext == '.dll' then
|
|
|
|
topname = name:sub(1, -5)
|
|
|
|
elseif name:sub(-3) == '.so' then
|
|
|
|
topname = name:sub(1, -4)
|
|
|
|
elseif t == 'link' or t == 'directory' then
|
|
|
|
topname = name
|
|
|
|
end
|
|
|
|
if topname then
|
|
|
|
Loader._indexed[path][topname] = { modpath = modpath, modname = topname }
|
|
|
|
Loader._topmods[topname] = Loader._topmods[topname] or {}
|
2023-04-14 01:39:57 -07:00
|
|
|
if not vim.list_contains(Loader._topmods[topname], path) then
|
2023-03-26 03:42:15 -07:00
|
|
|
table.insert(Loader._topmods[topname], path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return Loader._indexed[path]
|
|
|
|
end
|
|
|
|
|
2023-03-26 05:23:34 -07:00
|
|
|
--- Tracks the time spent in a function
|
|
|
|
--- @generic F: function
|
|
|
|
--- @param f F
|
|
|
|
--- @return F
|
|
|
|
--- @private
|
|
|
|
function Loader.track(stat, f)
|
|
|
|
return function(...)
|
2023-06-03 03:06:00 -07:00
|
|
|
local start = vim.uv.hrtime()
|
2023-03-26 05:23:34 -07:00
|
|
|
local r = { f(...) }
|
|
|
|
Loader._stats[stat] = Loader._stats[stat] or { total = 0, time = 0 }
|
|
|
|
Loader._stats[stat].total = Loader._stats[stat].total + 1
|
|
|
|
Loader._stats[stat].time = Loader._stats[stat].time + uv.hrtime() - start
|
|
|
|
return unpack(r, 1, table.maxn(r))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
---@class (private) vim.loader._profile.Opts
|
2023-03-31 01:43:13 -07:00
|
|
|
---@field loaders? boolean Add profiling to the loaders
|
2023-03-26 05:23:34 -07:00
|
|
|
|
2023-04-04 10:07:33 -07:00
|
|
|
--- Debug function that wraps all loaders and tracks stats
|
2023-03-26 03:42:15 -07:00
|
|
|
---@private
|
2024-02-27 08:20:32 -07:00
|
|
|
---@param opts vim.loader._profile.Opts?
|
2023-03-31 01:43:13 -07:00
|
|
|
function M._profile(opts)
|
|
|
|
Loader.get_rtp = Loader.track('get_rtp', Loader.get_rtp)
|
|
|
|
Loader.read = Loader.track('read', Loader.read)
|
|
|
|
Loader.loader = Loader.track('loader', Loader.loader)
|
|
|
|
Loader.loader_lib = Loader.track('loader_lib', Loader.loader_lib)
|
|
|
|
Loader.loadfile = Loader.track('loadfile', Loader.loadfile)
|
|
|
|
Loader.load = Loader.track('load', Loader.load)
|
|
|
|
M.find = Loader.track('find', M.find)
|
|
|
|
Loader.lsmod = Loader.track('lsmod', Loader.lsmod)
|
|
|
|
|
|
|
|
if opts and opts.loaders then
|
|
|
|
for l, loader in pairs(loaders) do
|
|
|
|
local loc = debug.getinfo(loader, 'Sn').source:sub(2)
|
|
|
|
loaders[l] = Loader.track('loader ' .. l .. ': ' .. loc, loader)
|
|
|
|
end
|
2023-03-26 03:42:15 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Prints all cache stats
|
|
|
|
---@param opts? {print?:boolean}
|
|
|
|
---@return LoaderStats
|
|
|
|
---@private
|
|
|
|
function M._inspect(opts)
|
|
|
|
if opts and opts.print then
|
|
|
|
local function ms(nsec)
|
|
|
|
return math.floor(nsec / 1e6 * 1000 + 0.5) / 1000 .. 'ms'
|
|
|
|
end
|
|
|
|
local chunks = {} ---@type string[][]
|
|
|
|
---@type string[]
|
|
|
|
local stats = vim.tbl_keys(Loader._stats)
|
|
|
|
table.sort(stats)
|
|
|
|
for _, stat in ipairs(stats) do
|
|
|
|
vim.list_extend(chunks, {
|
|
|
|
{ '\n' .. stat .. '\n', 'Title' },
|
|
|
|
{ '* total: ' },
|
|
|
|
{ tostring(Loader._stats[stat].total) .. '\n', 'Number' },
|
|
|
|
{ '* time: ' },
|
|
|
|
{ ms(Loader._stats[stat].time) .. '\n', 'Bold' },
|
|
|
|
{ '* avg time: ' },
|
|
|
|
{ ms(Loader._stats[stat].time / Loader._stats[stat].total) .. '\n', 'Bold' },
|
|
|
|
})
|
|
|
|
for k, v in pairs(Loader._stats[stat]) do
|
2023-04-14 01:39:57 -07:00
|
|
|
if not vim.list_contains({ 'time', 'total' }, k) then
|
2023-03-26 03:42:15 -07:00
|
|
|
chunks[#chunks + 1] = { '* ' .. k .. ':' .. string.rep(' ', 9 - #k) }
|
|
|
|
chunks[#chunks + 1] = { tostring(v) .. '\n', 'Number' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
vim.api.nvim_echo(chunks, true, {})
|
|
|
|
end
|
|
|
|
return Loader._stats
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|