Merge remote-tracking branch 'upstream/main' into feat/prioritize-cond

This commit is contained in:
MurdeRM3L0DY 2023-02-23 14:25:24 +01:00
commit 7e39270651
27 changed files with 1212 additions and 905 deletions

View file

@ -1,524 +1,420 @@
local ffi = require("ffi")
---@diagnostic disable-next-line: no-unknown
local uv = vim.loop
local M = {}
M.dirty = false
M.VERSION = "1" .. jit.version
---@class LazyCacheConfig
M.config = {
enabled = true,
path = vim.fn.stdpath("cache") .. "/lazy/cache",
-- 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
disable_events = { "UIEnter", "BufReadPre" },
ttl = 3600 * 24 * 5, -- keep unused modules for up to 5 days
}
M.debug = false
---@type CacheHash
local cache_hash
---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
---@alias CacheEntry {hash:CacheHash, modpath:string, chunk:string, used:number}
---@type table<string,CacheEntry?>
M.cache = {}
M.enabled = true
---@type string[]
M.rtp = nil
M.rtp_total = 0
---@alias CacheEntry {hash:CacheHash, chunk:string}
---@class CacheFindOpts
---@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
M.VERSION = 2
M.path = vim.fn.stdpath("cache") .. "/lazy/luac"
M.enabled = false
M.stats = {
find = { total = 0, time = 0, rtp = 0, unloaded = 0, index = 0, stat = 0, not_found = 0 },
autoload = { total = 0, time = 0 },
find = { total = 0, time = 0, not_found = 0 },
}
M.me = debug.getinfo(1, "S").source:sub(2)
M.me = vim.fn.fnamemodify(M.me, ":p:h:h:h:h"):gsub("\\", "/")
---@type table<string, string[]>
M.topmods = { lazy = { M.me } }
---@type table<string, string[]>
M.indexed = { [M.me] = { "lazy" } }
M.indexed_unloaded = false
M.indexed_rtp = 0
-- selene:allow(global_usage)
M._loadfile = _G.loadfile
-- checks whether the cached modpath is still valid
function M.check_path(modname, modpath)
-- HACK: never return packer paths
if modpath:find("/site/pack/packer/", 1, true) then
return false
end
---@class ModuleCache
---@field _rtp string[]
---@field _rtp_pure string[]
---@field _rtp_key string
local Cache = {
---@type table<string, table<string,true>>
_indexed = {},
---@type table<string, string[]>
_topmods = {},
_loadfile = loadfile,
}
-- check rtp excluding plugins. This is a very small list, so should be fast
for _, path in ipairs(M.get_rtp()) do
if modpath:find(path .. "/", 1, true) == 1 then
return true
end
end
-- the correct lazy path should be part of rtp.
-- so if we get here, this is folke using the local dev instance ;)
if modname and (modname == "lazy" or modname:sub(1, 5) == "lazy.") then
return false
end
return modname and M.check_autoload(modname, modpath)
function M.track(stat, start)
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
function M.check_autoload(modname, modpath)
local start = uv.hrtime()
M.stats.autoload.total = M.stats.autoload.total + 1
-- slightly faster/different version than vim.fs.normalize
-- we also need to have it here, since the cache will load vim.fs
---@private
function Cache.normalize(path)
if path:sub(1, 1) == "~" then
local home = vim.loop.os_homedir() or "~"
if home:sub(-1) == "\\" or home:sub(-1) == "/" then
home = home:sub(1, -2)
end
path = home .. path:sub(2)
end
path = path:gsub("\\", "/"):gsub("/+", "/")
return path:sub(-1) == "/" and path:sub(1, -2) or path
end
-- check plugins. Again fast, since we check the plugin name from the path.
-- only needed when the plugin mod has been loaded
---@type LazyCorePlugin
local Plugin = package.loaded["lazy.core.plugin"]
if Plugin then
local plugin = Plugin.find(modpath)
if plugin and modpath:find(plugin.dir, 1, true) == 1 then
-- we're not interested in loader time, so calculate delta here
M.stats.autoload.time = M.stats.autoload.time + uv.hrtime() - start
-- don't load if we're loading specs or if the plugin is already loaded
if not (Plugin.loading or plugin._.loaded) then
if plugin.module == false then
error("Plugin " .. plugin.name .. " is not loaded and is configured with module=false")
end
require("lazy.core.loader").load(plugin, { require = modname })
---@private
function Cache.get_rtp()
local start = uv.hrtime()
if vim.in_fast_event() then
M.track("get_rtp", start)
return (Cache._rtp or {}), false
end
local updated = false
local key = vim.go.rtp
if key ~= Cache._rtp_key then
Cache._rtp = {}
for _, path in ipairs(vim.api.nvim_get_runtime_file("", true)) do
path = Cache.normalize(path)
-- skip after directories
if path:sub(-6, -1) ~= "/after" and not (Cache._indexed[path] and vim.tbl_isempty(Cache._indexed[path])) then
Cache._rtp[#Cache._rtp + 1] = path
end
return true
end
updated = true
Cache._rtp_key = key
end
M.track("get_rtp", start)
return Cache._rtp, updated
end
---@param name string can be a module name, or a file name
---@private
function Cache.cache_file(name)
local ret = M.path .. "/" .. name:gsub("[/\\:]", "%%")
return ret:sub(-4) == ".lua" and (ret .. "c") or (ret .. ".luac")
end
---@param entry CacheEntry
---@private
function Cache.write(name, entry)
local cname = Cache.cache_file(name)
local f = assert(uv.fs_open(cname, "w", 438))
local header = {
M.VERSION,
entry.hash.size,
entry.hash.mtime.sec,
entry.hash.mtime.nsec,
}
uv.fs_write(f, ffi.string(ffi.new("const uint32_t[4]", header), 16))
uv.fs_write(f, entry.chunk)
uv.fs_close(f)
end
---@return CacheEntry?
---@private
function Cache.read(name)
local start = uv.hrtime()
local cname = Cache.cache_file(name)
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)
---@type integer[]|{[0]:integer}
local header = ffi.cast("uint32_t*", ffi.new("const char[16]", data:sub(1, 16)))
if header[0] ~= M.VERSION then
return
end
M.track("read", start)
return {
hash = { size = header[1], mtime = { sec = header[2], nsec = header[3] } },
chunk = data:sub(16 + 1),
}
end
M.track("read", start)
end
---@param modname string
---@private
function Cache.loader(modname)
local start = uv.hrtime()
local modpath, hash = Cache.find(modname)
if modpath then
local chunk, err = M.load(modpath, { hash = hash })
M.track("loader", start)
return chunk or error(err)
end
M.track("loader", start)
return "\nlazy_loader: module " .. modname .. " not found"
end
---@param modname string
---@private
function Cache.loader_lib(modname)
local start = uv.hrtime()
local modpath = Cache.find(modname, { patterns = jit.os:find("Windows") and { ".dll" } or { ".so" } })
---@type function?, string?
if modpath 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(modpath, "luaopen_" .. funcname:gsub("%.", "_"))
M.track("loader_lib", start)
return chunk or error(err)
end
M.track("loader_lib", start)
return "\nlazy_loader_lib: module " .. modname .. " not found"
end
---@param filename? string
---@param mode? "b"|"t"|"bt"
---@param env? table
---@return function?, string? error_message
---@private
function Cache.loadfile(filename, mode, env)
local start = uv.hrtime()
filename = Cache.normalize(filename)
local chunk, err = M.load(filename, { mode = mode, env = env })
M.track("loadfile", start)
return chunk, err
end
---@param h1 CacheHash
---@param h2 CacheHash
---@private
function Cache.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
---@param modpath string
---@param opts? {hash?: CacheHash, mode?: "b"|"t"|"bt", env?:table}
---@return function?, string? error_message
---@private
function M.load(modpath, opts)
local start = uv.hrtime()
opts = opts or {}
local hash = opts.hash or uv.fs_stat(modpath)
---@type function?, string?
local chunk, err
if not hash then
-- trigger correct error
chunk, err = Cache._loadfile(modpath, opts.mode, opts.env)
M.track("load", start)
return chunk, err
end
local entry = Cache.read(modpath)
if entry and Cache.eq(entry.hash, hash) then
-- found in cache and up to date
-- selene: allow(incorrect_standard_library_use)
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
M.track("load", start)
return chunk, err
end
end
entry = { hash = hash, modpath = modpath }
chunk, err = Cache._loadfile(modpath, opts.mode, opts.env)
if chunk then
entry.chunk = string.dump(chunk)
Cache.write(modpath, entry)
end
M.track("load", start)
return chunk, err
end
---@param modname string
---@param opts? CacheFindOpts
---@return string? modpath, CacheHash? hash, CacheEntry? entry
function Cache.find(modname, opts)
local start = uv.hrtime()
opts = opts or {}
modname = modname:gsub("/", ".")
local basename = modname:gsub("%.", "/")
local idx = modname:find(".", 1, true)
-- HACK: some plugins try to load invalid relative paths (see #543)
if idx == 1 then
modname = modname:gsub("^%.+", "")
basename = modname:gsub("%.", "/")
idx = modname:find(".", 1, true)
end
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
---@param paths string[]
local function _find(paths)
for _, path in ipairs(paths) do
if M.lsmod(path)[topmod] then
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
return modpath, hash
end
end
end
end
end
---@type string, CacheHash
local modpath, hash
if opts.rtp ~= false then
modpath, hash = _find(Cache._rtp or {})
if not modpath then
local rtp, updated = Cache.get_rtp()
if updated then
modpath, hash = _find(rtp)
end
end
end
if (not modpath) and opts.paths then
modpath, hash = _find(opts.paths)
end
M.track("find", start)
if modpath then
return modpath, hash
end
-- module not found
M.stats.find.not_found = M.stats.find.not_found + 1
end
--- Resets the topmods cache for the path
---@param path string
function M.reset(path)
Cache._indexed[Cache.normalize(path)] = nil
end
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)
_G.loadfile = Cache.loadfile
-- add lua loader
table.insert(package.loaders, 2, Cache.loader)
-- add libs loader
table.insert(package.loaders, 3, Cache.loader_lib)
-- 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
M.stats.autoload.time = M.stats.autoload.time + uv.hrtime() - start
return false
end
function M.disable()
if not M.enabled then
return
end
-- selene:allow(global_usage)
_G.loadfile = M._loadfile
M.enabled = false
if M.debug and vim.tbl_count(M.topmods) > 1 then
M.log(M.topmods, { level = vim.log.levels.WARN, title = "topmods" })
end
if M.debug and false then
local stats = vim.deepcopy(M.stats)
stats.time = (stats.time or 0) / 1e6
stats.find.time = (stats.find.time or 0) / 1e6
stats.autoload.time = (stats.autoload.time or 0) / 1e6
M.log(stats, { title = "stats" })
end
end
---@param msg string|table
---@param opts? LazyNotifyOpts
function M.log(msg, opts)
if M.debug then
msg = vim.deepcopy(msg)
vim.schedule(function()
require("lazy.core.util").debug(msg, opts)
end)
end
end
function M.check_loaded(modname)
-- selene: allow(global_usage)
_G.loadfile = Cache._loadfile
---@diagnostic disable-next-line: no-unknown
local mod = package.loaded[modname]
if type(mod) == "table" then
return function()
return mod
for l, loader in ipairs(package.loaders) do
if loader == Cache.loader or loader == Cache.loader_lib then
table.remove(package.loaders, l)
end
end
table.insert(package.loaders, 2, vim._load_package)
end
---@param modname string
---@return fun()|string
function M.loader(modname)
modname = modname:gsub("/", ".")
local entry = M.cache[modname]
local chunk, err
if entry then
if M.check_path(modname, entry.modpath) then
M.stats.find.total = M.stats.find.total + 1
chunk, err = M.load(modname, entry.modpath)
else
M.cache[modname] = nil
M.dirty = true
end
end
if not chunk then
-- find the modpath and load the module
local modpath = M.find(modname)
if modpath then
M.check_autoload(modname, modpath)
if M.enabled then
chunk, err = M.load(modname, modpath)
else
chunk = M.check_loaded(modname)
if not chunk then
chunk, err = M._loadfile(modpath)
end
-- Return the top-level `/lua/*` modules for this path
---@return string[]
function M.lsmod(path)
if not Cache._indexed[path] then
local start = uv.hrtime()
Cache._indexed[path] = {}
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
end
end
return chunk or err or ("module " .. modname .. " not found")
end
---@param modpath string
---@return any, string?
function M.loadfile(modpath)
modpath = modpath:gsub("\\", "/")
return M.load(modpath, modpath)
end
---@param modkey string
---@param modpath string
---@return function?, string? error_message
function M.load(modkey, modpath)
local chunk, err
chunk = M.check_loaded(modkey)
if chunk then
return chunk
end
modpath = modpath:gsub("\\", "/")
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
entry.modpath = modpath
entry.used = os.time()
if M.eq(entry.hash, hash) then
-- found in cache and up to date
chunk, err = loadstring(entry.chunk --[[@as string]], "@" .. entry.modpath)
if not (err and err:find("cannot load incompatible bytecode", 1, true)) then
return chunk, err
end
end
else
entry = { hash = hash, modpath = modpath, used = os.time() }
M.cache[modkey] = entry
end
entry.hash = hash
if M.debug then
M.log("`" .. modpath .. "`", { level = vim.log.levels.WARN, title = "Cache.load" })
end
chunk, err = M._loadfile(entry.modpath)
M.dirty = true
if chunk then
entry.chunk = string.dump(chunk)
else
M.cache[modkey] = nil
end
return chunk, err
end
-- 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
return false
end
M.indexed[path] = {}
Util.ls(path .. "/lua", function(_, name, t)
-- HACK: type is not always returned due to a bug in luv
t = t or uv.fs_stat(path .. "/" .. name).type
---@type string
local topname
if name:sub(-4) == ".lua" then
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
M.topmods[topname] = M.topmods[topname] or {}
if not vim.tbl_contains(M.topmods[topname], path) then
table.insert(M.topmods[topname], path)
end
if not vim.tbl_contains(M.indexed[path], topname) then
table.insert(M.indexed[path], topname)
end
end
end)
return true
end
return false
end
function M.get_topmods(path)
M._index(path)
return M.indexed[path] or {}
end
---@param modname string
---@return string?
function M.find_root(modname)
if M.cache[modname] then
-- check if modname is in cache
local modpath = M.cache[modname].modpath
if M.check_path(modname, modpath) and uv.fs_stat(modpath) then
local root = modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
return root
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 then
if M.check_path(child, entry.modpath) and uv.fs_stat(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
Cache._indexed[path][topname] = true
Cache._topmods[topname] = Cache._topmods[topname] or {}
if not vim.tbl_contains(Cache._topmods[topname], path) then
table.insert(Cache._topmods[topname], path)
end
end
end
M.track("lsmod", start)
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
end
return Cache._indexed[path]
end
---@param modname string
---@param opts? {patterns?:string[]}
---@return string?
---@param opts? CacheFindOpts
---@return string? modpath
function M.find(modname, opts)
opts = opts or {}
M.stats.find.total = M.stats.find.total + 1
local start = uv.hrtime()
local basename = modname:gsub("%.", "/")
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" }
if opts.patterns then
vim.list_extend(patterns, opts.patterns)
end
-- check top-level mods to find the module
local function _find()
for _, toppath in ipairs(M.topmods[topmod] or {}) do
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
local rtp = M.list_rtp()
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()
end
end
-- update unloaded
if not modpath and not M.indexed_unloaded then
M.indexed_unloaded = true
local updated = false
---@type LazyCoreConfig
local Config = package.loaded["lazy.core.config"]
if Config and Config.spec then
for _, plugin in pairs(Config.spec.plugins) do
if not (M.indexed[plugin.dir] or plugin._.loaded or plugin.module == false) then
updated = M._index(plugin.dir) or updated
end
end
end
if updated then
modpath = _find()
end
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
local modpath = Cache.find(modname, opts)
return modpath
end
-- returns the cached RTP excluding plugin dirs
function M.get_rtp()
local rtp = M.list_rtp()
if not M.rtp or #rtp ~= M.rtp_total then
M.rtp_total = #rtp
M.rtp = {}
---@type table<string,true>
local skip = {}
-- only skip plugins once Config has been setup
---@type LazyCoreConfig
local Config = package.loaded["lazy.core.config"]
if Config then
for _, plugin in pairs(Config.plugins) do
if plugin.name ~= "lazy.nvim" then
skip[plugin.dir] = true
end
end
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)
M.track("loader " .. l .. ": " .. loc, start)
M.track("loader_all", start)
return ret
end
for _, path in ipairs(rtp) do
---@type string
path = path:gsub("\\", "/")
if not skip[path] and not path:find("after/?$") then
M.rtp[#M.rtp + 1] = path
end
end
function M.inspect()
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(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
end
return M.rtp
vim.api.nvim_echo(chunks, true, {})
end
function M.list_rtp()
return vim.api.nvim_get_runtime_file("", true)
end
---@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
---@diagnostic disable-next-line: no-unknown
for k, v in pairs(opts.performance.cache) do
---@diagnostic disable-next-line: no-unknown
M.config[k] = v
end
end
M.debug = opts and opts.debug
M.enabled = M.config.enabled
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)
end
return M
end
---@return CacheHash?
function M.hash(file)
local ok, ret = pcall(uv.fs_stat, file)
return ok and ret or nil
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()
vim.fn.mkdir(vim.fn.fnamemodify(M.config.path, ":p:h"), "p")
local f = assert(uv.fs_open(M.config.path, "w", 438))
uv.fs_write(f, M.VERSION)
uv.fs_write(f, "\0")
for modname, entry in pairs(M.cache) do
if entry.used > os.time() - M.config.ttl then
entry.modname = modname
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))
uv.fs_write(f, modname)
uv.fs_write(f, entry.chunk)
uv.fs_write(f, entry.modpath)
end
end
uv.fs_close(f)
end
function M.load_cache()
M.cache = {}
local f = uv.fs_open(M.config.path, "r", 438)
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)
local zero = data:find("\0", 1, true)
if not zero then
return
end
if M.VERSION ~= data:sub(1, zero - 1) then
return
end
local offset = zero + 1
while offset + 1 < #data do
local header = ffi.cast("uint32_t*", ffi.new("const char[28]", data:sub(offset, offset + 27)))
offset = offset + 28
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]
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],
}
end
end
end
function M.autosave()
vim.api.nvim_create_autocmd("VimLeavePre", {
callback = function()
if M.dirty then
local hash = M.hash(M.config.path)
-- 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,
})
end
M._Cache = Cache
return M

View file

@ -21,12 +21,17 @@ M.defaults = {
log = { "--since=3 days ago" }, -- show commits from the last 3 days
timeout = 120, -- kill processes that take more than 2 minutes
url_format = "https://github.com/%s.git",
-- lazy.nvim requires git >=2.19.0. If you really want to use lazy with an older version,
-- then set the below to false. This is should work, but is NOT supported and will
-- increase downloads a lot.
filter = true,
},
dev = {
-- directory where you store your local plugin projects
path = "~/projects",
---@type string[] plugins that match these patterns will use your local versions instead of being fetched from GitHub
patterns = {}, -- For example {"folke"}
fallback = false, -- Fallback to git when local plugin doesn't exist
},
install = {
-- install missing plugins on startup. This doesn't increase startup time.
@ -48,7 +53,7 @@ M.defaults = {
init = "",
import = "",
keys = "",
lazy = " ",
lazy = "󰒲 ",
loaded = "",
not_loaded = "",
plugin = "",
@ -108,13 +113,14 @@ M.defaults = {
notify = true, -- get a notification when changes are found
},
performance = {
---@type LazyCacheConfig
cache = nil,
cache = {
enabled = true,
},
reset_packpath = true, -- reset the package path to improve startup time
rtp = {
reset = true, -- reset the runtime path to $VIMRUNTIME and your config directory
---@type string[]
paths = {}, -- add any custom paths here that you want to indluce in the rtp
paths = {}, -- add any custom paths here that you want to includes in the rtp
---@type string[] list any plugins you want to disable here
disabled_plugins = {
-- "gzip",
@ -141,7 +147,7 @@ M.defaults = {
debug = false,
}
M.version = "9.3.1" -- x-release-please-version
M.version = "9.8.5" -- x-release-please-version
M.ns = vim.api.nvim_create_namespace("lazy")
@ -221,7 +227,6 @@ function M.setup(opts)
pattern = "VeryLazy",
once = true,
callback = function()
require("lazy.core.cache").autosave()
require("lazy.view.commands").setup()
if M.options.change_detection.enabled then
require("lazy.manage.reloader").enable()

View file

@ -14,16 +14,22 @@ end
---@param cmd string
function M:_add(cmd)
vim.api.nvim_create_user_command(cmd, function(event)
local command = {
cmd = cmd,
bang = event.bang or nil,
mods = event.smods,
args = event.fargs,
count = event.count >= 0 and event.range == 0 and event.count or nil,
}
if event.range == 1 then
command.range = { event.line1 }
elseif event.range == 2 then
command.range = { event.line1, event.line2 }
end
self:_load(cmd)
vim.cmd(
("%s %s%s%s %s"):format(
event.mods or "",
event.line1 == event.line2 and "" or event.line1 .. "," .. event.line2,
cmd,
event.bang and "!" or "",
event.args or ""
)
)
vim.cmd(command)
end, {
bang = true,
range = true,

View file

@ -3,7 +3,7 @@ local Loader = require("lazy.core.loader")
---@class LazyKeys
---@field [1] string lhs
---@field [2]? string|fun() rhs
---@field [2]? string|fun()|false rhs
---@field desc? string
---@field mode? string|string[]
---@field noremap? boolean
@ -14,42 +14,6 @@ local Loader = require("lazy.core.loader")
---@class LazyKeysHandler:LazyHandler
local M = {}
---@param feed string
function M.replace_special(feed)
for special, key in pairs({ leader = vim.g.mapleader or "\\", localleader = vim.g.maplocalleader or "\\" }) do
local pattern = "<"
for i = 1, #special do
pattern = pattern .. "[" .. special:sub(i, i) .. special:upper():sub(i, i) .. "]"
end
pattern = pattern .. ">"
feed = feed:gsub(pattern, key)
end
return feed
end
function M.retrigger(keys)
local pending = ""
while true do
---@type number|string
local c = vim.fn.getchar(0)
if c == 0 then
break
end
c = type(c) == "number" and vim.fn.nr2char(c) or c
pending = pending .. c
end
local op = vim.v.operator
if op and op ~= "" and vim.api.nvim_get_mode().mode:find("o") then
keys = "<esc>" .. op .. keys
end
local feed = keys .. pending
feed = M.replace_special(feed)
if vim.v.count ~= 0 then
feed = vim.v.count .. feed
end
vim.api.nvim_input(feed)
end
---@param value string|LazyKeys
function M.parse(value)
local ret = vim.deepcopy(value)
@ -108,9 +72,16 @@ function M:_add(keys)
Util.track({ keys = lhs })
Loader.load(plugins, { keys = lhs })
M.retrigger(lhs)
Util.track()
end, opts)
local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true)
-- insert instead of append the lhs
vim.api.nvim_feedkeys(feed, "i", false)
end, {
desc = opts.desc,
-- we do not return anything, but this is still needed to make operator pending mappings work
expr = true,
})
end
---@param keys LazyKeys

View file

@ -4,6 +4,7 @@ local Handler = require("lazy.core.handler")
local Cache = require("lazy.core.cache")
local Plugin = require("lazy.core.plugin")
---@class LazyCoreLoader
local M = {}
local DEFAULT_PRIORITY = 50
@ -63,7 +64,8 @@ function M.install_missing()
for _, plugin in pairs(Config.plugins) do
if not (plugin._.installed or Plugin.has_errors(plugin)) then
for _, colorscheme in ipairs(Config.options.install.colorscheme) do
if pcall(vim.cmd.colorscheme, colorscheme) then
M.colorscheme(colorscheme)
if vim.g.colors_name or pcall(vim.cmd.colorscheme, colorscheme) then
break
end
end
@ -71,7 +73,7 @@ function M.install_missing()
-- remove and installed plugins from indexed, so cache will index again
for _, p in pairs(Config.plugins) do
if p._.installed then
Cache.indexed[p.dir] = nil
Cache.reset(p.dir)
end
end
-- reload plugins
@ -180,6 +182,82 @@ function M.load(plugins, reason, opts)
end
end
---@param plugin LazyPlugin
function M.deactivate(plugin)
if not plugin._.loaded then
return
end
local main = M.get_main(plugin)
if main then
Util.try(function()
local mod = require(main)
if mod.deactivate then
mod.deactivate(plugin)
end
end, "Failed to deactivate plugin " .. plugin.name)
end
-- execute deactivate when needed
if plugin.deactivate then
Util.try(function()
plugin.deactivate(plugin)
end, "Failed to deactivate plugin " .. plugin.name)
end
-- disable handlers
Handler.disable(plugin)
-- remove loaded lua modules
Util.walkmods(plugin.dir .. "/lua", function(modname)
package.loaded[modname] = nil
package.preload[modname] = nil
end)
-- clear vim.g.loaded_ for plugins
Util.ls(plugin.dir .. "/plugin", function(_, name, type)
if type == "file" then
vim.g["loaded_" .. name:gsub("%..*", "")] = nil
end
end)
-- set as not loaded
plugin._.loaded = nil
end
--- reload a plugin
---@param plugin LazyPlugin
function M.reload(plugin)
M.deactivate(plugin)
local load = false -- plugin._.loaded ~= nil
-- enable handlers
Handler.enable(plugin)
-- run init
if plugin.init then
Util.try(function()
plugin.init(plugin)
end, "Failed to run `init` for **" .. plugin.name .. "**")
end
-- if this is a start plugin, load it now
if plugin.lazy == false then
load = true
end
for _, event in ipairs(plugin.event or {}) do
if event == "VimEnter" or event == "UIEnter" or event:find("VeryLazy") then
load = true
break
end
end
if load then
M.load(plugin, { start = "reload" })
end
end
---@param plugin LazyPlugin
---@param reason {[string]:string}
---@param opts? {force:boolean} when force is true, we skip the cond check
@ -241,22 +319,11 @@ function M.config(plugin)
plugin.config(plugin, opts)
end
else
local normname = Util.normname(plugin.name)
---@type string[]
local mods = {}
for _, modname in ipairs(Cache.get_topmods(plugin.dir)) do
mods[#mods + 1] = modname
local modnorm = Util.normname(modname)
-- if we found an exact match, then use that
if modnorm == normname then
mods = { modname }
break
end
end
if #mods == 1 then
local main = M.get_main(plugin)
if main then
fn = function()
local opts = Plugin.values(plugin, "opts", false)
require(mods[1]).setup(opts)
require(main).setup(opts)
end
else
return Util.error(
@ -267,6 +334,26 @@ function M.config(plugin)
Util.try(fn, "Failed to run `config` for " .. plugin.name)
end
---@param plugin LazyPlugin
function M.get_main(plugin)
if plugin.main then
return plugin.main
end
local normname = Util.normname(plugin.name)
---@type string[]
local mods = {}
for modname, _ in pairs(Cache.lsmod(plugin.dir)) do
mods[#mods + 1] = modname
local modnorm = Util.normname(modname)
-- if we found an exact match, then use that
if modnorm == normname then
mods = { modname }
break
end
end
return #mods == 1 and mods[1] or nil
end
---@param path string
function M.packadd(path)
M.source_runtime(path, "plugin")
@ -364,4 +451,36 @@ function M.colorscheme(name)
end
end
function M.auto_load(modname, modpath)
local plugin = Plugin.find(modpath)
if plugin and modpath:find(plugin.dir, 1, true) == 1 then
-- don't load if we're loading specs or if the plugin is already loaded
if not (Plugin.loading or plugin._.loaded) then
if plugin.module == false then
error("Plugin " .. plugin.name .. " is not loaded and is configured with module=false")
end
M.load(plugin, { require = modname })
end
return true
end
return false
end
---@param modname string
function M.loader(modname)
local paths = Util.get_unloaded_rtp(modname)
local modpath, hash = Cache._Cache.find(modname, { rtp = false, paths = paths })
-- print(modname .. " " .. paths[1])
if modpath then
M.auto_load(modname, modpath)
local mod = package.loaded[modname]
if type(mod) == "table" then
return function()
return mod
end
end
return Cache.load(modpath, { hash = hash })
end
end
return M

View file

@ -91,7 +91,10 @@ function Spec:add(plugin, results, is_dep)
end
end
-- dev plugins
if plugin.dev then
if
plugin.dev
and (not Config.options.dev.fallback or vim.fn.isdirectory(Config.options.dev.path .. "/" .. plugin.name) == 1)
then
plugin.dir = Config.options.dev.path .. "/" .. plugin.name
else
-- remote plugin
@ -251,7 +254,15 @@ function Spec:import(spec)
self.modules[#self.modules + 1] = spec.import
local imported = 0
---@type string[]
local modnames = {}
Util.lsmod(spec.import, function(modname)
modnames[#modnames + 1] = modname
end)
table.sort(modnames)
for _, modname in ipairs(modnames) do
imported = imported + 1
Util.track({ import = modname })
self.importing = modname
@ -270,7 +281,7 @@ function Spec:import(spec)
Util.track()
end,
})
end)
end
if imported == 0 then
self:error("No specs found for module " .. spec.import)
end

View file

@ -201,11 +201,62 @@ function M.walk(path, fn)
end)
end
---@param root string
---@param fn fun(modname:string, modpath:string)
---@param modname? string
function M.walkmods(root, fn, modname)
modname = modname and (modname:gsub("%.$", "") .. ".") or ""
M.ls(root, function(path, name, type)
if name == "init.lua" then
fn(modname:gsub("%.$", ""), path)
elseif (type == "file" or type == "link") and name:sub(-4) == ".lua" then
fn(modname .. name:sub(1, -5), path)
elseif type == "directory" then
M.walkmods(path, fn, modname .. name .. ".")
end
end)
end
---@param modname string
function M.get_unloaded_rtp(modname)
modname = modname:gsub("/", ".")
local idx = modname:find(".", 1, true)
local topmod = idx and modname:sub(1, idx - 1) or modname
topmod = M.normname(topmod)
local rtp = {}
local Config = require("lazy.core.config")
if Config.spec then
for _, plugin in pairs(Config.spec.plugins) do
if not (plugin._.loaded or plugin.module == false) then
if topmod == M.normname(plugin.name) then
table.insert(rtp, 1, plugin.dir)
else
table.insert(rtp, plugin.dir)
end
end
end
end
return rtp
end
function M.find_root(modname)
local Cache = require("lazy.core.cache")
local modpath = Cache.find(modname, {
rtp = true,
paths = M.get_unloaded_rtp(modname),
patterns = { "", ".lua" },
})
if modpath then
local root = modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
return root
end
end
---@param modname string
---@param fn fun(modname:string, modpath:string)
function M.lsmod(modname, fn)
local Cache = require("lazy.core.cache")
local root = Cache.find_root(modname)
local root = M.find_root(modname)
if not root then
return
end

View file

@ -14,21 +14,27 @@ end
function M.fix_indent(str)
local lines = vim.split(str, "\n")
local first = table.remove(lines, 1)
local width = 120
for _, line in ipairs(lines) do
width = math.min(width, #line:match("^%s*"))
if not line:find("^%s*$") then
width = math.min(width, #line:match("^%s*"))
end
end
for l, line in ipairs(lines) do
lines[l] = line:sub(width + 1)
end
table.insert(lines, 1, first)
return table.concat(lines, "\n")
end
---@alias ReadmeBlock {content:string, lang?:string}
---@param contents table<string, ReadmeBlock|string>
function M.save(contents)
local readme = Util.read_file("README.md")
---@param readme_file? string
function M.save(contents, readme_file)
local readme = Util.read_file(readme_file or "README.md")
for tag, block in pairs(contents) do
if type(block) == "string" then
block = { content = block, lang = "lua" }
@ -48,7 +54,7 @@ function M.save(contents)
end
end
Util.write_file("README.md", readme)
Util.write_file(readme_file or "README.md", readme)
vim.cmd.checktime()
end
@ -125,12 +131,7 @@ function M.colors()
end
function M.update()
local cache_config = M.extract("lua/lazy/core/cache.lua", "\nM%.config = ({.-\n})")
local config = M.extract("lua/lazy/core/config.lua", "\nM%.defaults = ({.-\n})")
config = config:gsub(
"\n%s*%-%-%-@type LazyCacheConfig.*cache = nil,",
"\n" .. M.indent("cache = " .. cache_config .. ",", 4)
)
config = config:gsub("%s*debug = false.\n", "\n")
M.save({
bootstrap = M.extract("lua/lazy/init.lua", "function M%.bootstrap%(%)\n(.-)\nend"),

View file

@ -5,6 +5,12 @@ local M = {}
function M.check()
vim.health.report_start("lazy.nvim")
if vim.fn.executable("git") == 1 then
vim.health.report_ok("Git installed")
else
vim.health.report_error("Git not installd?")
end
local sites = vim.opt.packpath:get()
local default_site = vim.fn.stdpath("data") .. "/site"
if not vim.tbl_contains(sites, default_site) then
@ -14,7 +20,7 @@ function M.check()
local existing = false
for _, site in pairs(sites) do
for _, packs in ipairs(vim.fn.expand(site .. "/pack/*", false, true)) do
if not packs:find("/dist$") and vim.loop.fs_stat(packs) then
if not packs:find("[/\\]dist$") and vim.loop.fs_stat(packs) then
existing = true
vim.health.report_warn("found existing packages at `" .. packs .. "`")
end

View file

@ -34,7 +34,15 @@ function M.setup(spec, opts)
local start = vim.loop.hrtime()
-- load module cache before anything else
require("lazy.core.cache").setup(opts)
local enable_cache = not (
opts
and opts.performance
and opts.performance.cache
and opts.performance.cache.enabled == false
)
if enable_cache then
require("lazy.core.cache").enable()
end
require("lazy.stats").track("LazyStart")
@ -42,6 +50,12 @@ function M.setup(spec, opts)
local Config = require("lazy.core.config")
local Loader = require("lazy.core.loader")
table.insert(package.loaders, 3, Loader.loader)
if vim.g.profile_loaders then
require("lazy.core.cache").profile_loaders()
end
Util.track({ plugin = "lazy.nvim" }) -- setup start
Util.track("module", vim.loop.hrtime() - start)

View file

@ -13,7 +13,14 @@ M.reported = {}
function M.start()
M.fast_check()
M.schedule()
if M.schedule() > 0 and not M.has_errors() then
Manage.log({
clear = false,
show = false,
check = true,
concurrency = Config.options.checker.concurrency,
})
end
end
function M.schedule()
@ -21,6 +28,7 @@ function M.schedule()
local next_check = State.checker.last_check + Config.options.checker.frequency - os.time()
next_check = math.max(next_check, 0)
vim.defer_fn(M.check, next_check * 1000)
return next_check
end
---@param opts? {report:boolean} report defaults to true
@ -39,20 +47,23 @@ function M.fast_check(opts)
M.report(opts.report ~= false)
end
function M.has_errors()
for _, plugin in pairs(Config.plugins) do
if Plugin.has_errors(plugin) then
return true
end
end
return false
end
function M.check()
State.checker.last_check = os.time()
State.write() -- update state
local errors = false
for _, plugin in pairs(Config.plugins) do
if Plugin.has_errors(plugin) then
errors = true
break
end
end
if errors then
if M.has_errors() then
M.schedule()
else
Manage.check({
clear = false,
show = false,
concurrency = Config.options.checker.concurrency,
}):wait(function()

View file

@ -136,13 +136,13 @@ function M.check(opts)
}, opts)
end
---@param opts? ManagerOpts
---@param opts? ManagerOpts | {check?:boolean}
function M.log(opts)
opts = M.opts(opts, { mode = "log" })
return M.run({
pipeline = {
{ "git.origin", check = true },
"git.log",
{ "git.log", check = opts.check },
},
plugins = function(plugin)
return plugin.url and plugin._.installed
@ -174,7 +174,10 @@ function M.sync(opts)
end)
opts.show = false
end
local clean = M.clean(opts)
local clean_opts = vim.deepcopy(opts)
clean_opts.plugins = nil
local clean = M.clean(clean_opts)
local install = M.install(opts)
local update = M.update(opts)
clean:wait(function()

View file

@ -2,7 +2,7 @@ local Config = require("lazy.core.config")
local M = {}
---@type table<vim.loop.Process, true>
---@type table<uv.uv_process_t, true>
M.running = {}
M.signals = {
@ -49,7 +49,7 @@ local uv = vim.loop
---@field on_line? fun(string)
---@field on_exit? fun(ok:boolean, output:string)
---@field timeout? number
---@field env? string[]
---@field env? table<string,string>
---@param opts? ProcessOpts
---@param cmd string
@ -57,31 +57,31 @@ function M.spawn(cmd, opts)
opts = opts or {}
opts.timeout = opts.timeout or (Config.options.git and Config.options.git.timeout * 1000)
local env = {
"GIT_TERMINAL_PROMPT=0",
"GIT_SSH_COMMAND=ssh -oBatchMode=yes",
}
if opts.env then
vim.list_extend(env, opts.env)
---@type table<string, string>
local env = vim.tbl_extend("force", {
GIT_SSH_COMMAND = "ssh -oBatchMode=yes",
}, uv.os_environ(), opts.env or {})
env.GIT_DIR = nil
env.GIT_TERMINAL_PROMPT = "0"
---@type string[]
local env_flat = {}
for k, v in pairs(env) do
env_flat[#env_flat + 1] = k .. "=" .. v
end
for key, value in
pairs(uv.os_environ() --[[@as string[] ]])
do
table.insert(env, key .. "=" .. value)
end
local stdout = uv.new_pipe()
local stderr = uv.new_pipe()
local stdout = assert(uv.new_pipe())
local stderr = assert(uv.new_pipe())
local output = ""
---@type vim.loop.Process
---@type uv.uv_process_t
local handle = nil
---@type uv.uv_timer_t
local timeout
local killed = false
if opts.timeout then
timeout = uv.new_timer()
timeout = assert(uv.new_timer())
timeout:start(opts.timeout, 0, function()
if M.kill(handle) then
killed = true
@ -93,7 +93,7 @@ function M.spawn(cmd, opts)
stdio = { nil, stdout, stderr },
args = opts.args,
cwd = opts.cwd,
env = env,
env = env_flat,
}, function(exit_code, signal)
M.running[handle] = nil
if timeout then
@ -103,7 +103,7 @@ function M.spawn(cmd, opts)
handle:close()
stdout:close()
stderr:close()
local check = uv.new_check()
local check = assert(uv.new_check())
check:start(function()
if not stdout:is_closing() or not stderr:is_closing() then
return

View file

@ -1,4 +1,3 @@
local Cache = require("lazy.core.cache")
local Config = require("lazy.core.config")
local Util = require("lazy.util")
local Plugin = require("lazy.core.plugin")
@ -6,12 +5,11 @@ local Loader = require("lazy.core.loader")
local M = {}
---@type table<string, CacheHash>
---@type table<string, vim.loop.Stat>
M.files = {}
---@type vim.loop.Timer
M.timer = nil
M.root = nil
function M.enable()
if M.timer then
@ -19,7 +17,6 @@ function M.enable()
end
if #Config.spec.modules > 0 then
M.timer = vim.loop.new_timer()
M.root = vim.fn.stdpath("config") .. "/lua"
M.check(true)
M.timer:start(2000, 2000, M.check)
end
@ -32,6 +29,12 @@ function M.disable()
end
end
---@param h1 vim.loop.Stat
---@param h2 vim.loop.Stat
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.check(start)
---@type table<string,true>
local checked = {}
@ -41,10 +44,10 @@ function M.check(start)
-- spec is a module
local function check(_, modpath)
checked[modpath] = true
local hash = Cache.hash(modpath)
local hash = vim.loop.fs_stat(modpath)
if hash then
if M.files[modpath] then
if not Cache.eq(M.files[modpath], hash) then
if not M.eq(M.files[modpath], hash) then
M.files[modpath] = hash
table.insert(changes, { file = modpath, what = "changed" })
end

View file

@ -15,7 +15,7 @@ M.log = {
return true
end
local stat = vim.loop.fs_stat(plugin.dir .. "/.git")
return stat and stat.type ~= "directory"
return not (stat and stat.type == "directory")
end,
---@param opts {args?: string[], updated?:boolean, check?:boolean}
run = function(self, opts)
@ -64,11 +64,18 @@ M.clone = {
local args = {
"clone",
self.plugin.url,
"--filter=blob:none",
"--recurse-submodules",
"--progress",
}
if Config.options.git.filter then
args[#args + 1] = "--filter=blob:none"
end
if self.plugin.submodules ~= false then
args[#args + 1] = "--recurse-submodules"
end
args[#args + 1] = "--progress"
if self.plugin.branch then
vim.list_extend(args, { "-b", self.plugin.branch })
end
@ -152,6 +159,10 @@ M.fetch = {
"--progress",
}
if self.plugin.submodules == false then
table.remove(args, 2)
end
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
@ -203,6 +214,10 @@ M.checkout = {
"--recurse-submodules",
}
if self.plugin.submodules == false then
table.remove(args, 3)
end
if lock then
table.insert(args, lock.commit)
elseif target.tag then

View file

@ -19,6 +19,7 @@
---@class LazyPluginHooks
---@field init? fun(self:LazyPlugin) Will always be run
---@field deactivate? fun(self:LazyPlugin) Unload/Stop a plugin
---@field config? fun(self:LazyPlugin, opts:table)|true Will be executed when loading the plugin
---@field build? string|fun(self:LazyPlugin)|(string|fun(self:LazyPlugin))[]
---@field opts? PluginOpts
@ -36,10 +37,12 @@
---@field commit? string
---@field version? string
---@field pin? boolean
---@field submodules? boolean Defaults to true
---@class LazyPluginBase
---@field [1] string?
---@field name string display name and name used for plugin config files
---@field main? string Entry module that has setup & deactivate
---@field url string?
---@field dir string
---@field enabled? boolean|(fun():boolean)

View file

@ -23,8 +23,10 @@ function M.open(uri)
elseif vim.fn.has("macunix") == 1 then
cmd = { "open", uri }
else
if vim.fn.executable("xdg-open") then
if vim.fn.executable("xdg-open") == 1 then
cmd = { "xdg-open", uri }
elseif vim.fn.executable("wslview") == 1 then
cmd = { "wslview", uri }
else
cmd = { "open", uri }
end

View file

@ -53,7 +53,7 @@ function M:init(opts)
}
self:mount()
self:on_key(ViewConfig.keys.close, self.close)
self:on({ "BufDelete", "BufLeave", "BufHidden" }, self.close, { once = true })
self:on({ "BufDelete", "BufHidden" }, self.close, { once = true })
return self
end
@ -107,9 +107,11 @@ function M:mount()
local function opts()
vim.bo[self.buf].bufhidden = "wipe"
vim.wo[self.win].conceallevel = 3
vim.wo[self.win].foldenable = false
vim.wo[self.win].spell = false
vim.wo[self.win].wrap = true
vim.wo[self.win].winhighlight = "Normal:LazyNormal"
vim.wo[self.win].colorcolumn = ""
end
opts()

View file

@ -77,7 +77,7 @@ function M.create()
vim.keymap.set("n", ViewConfig.keys.abort, function()
require("lazy.manage.process").abort()
return "<c-c>"
return ViewConfig.keys.abort
end, { silent = true, buffer = self.buf, expr = true })
-- plugin details

View file

@ -160,7 +160,7 @@ end
function M:help()
self:append("Help", "LazyH2"):nl():nl()
self:append("Use "):append("<C-c>", "LazySpecial"):append(" to abort all running tasks."):nl():nl()
self:append("Use "):append(ViewConfig.keys.abort, "LazySpecial"):append(" to abort all running tasks."):nl():nl()
self:append("You can press "):append("<CR>", "LazySpecial"):append(" on a plugin to show its details."):nl():nl()
@ -670,33 +670,19 @@ function M:debug()
end)
self:nl()
self:append("Cache.find()", "LazyH2"):nl()
self:props({
{ "total", Cache.stats.find.total, "Number" },
{ "time", self:ms(Cache.stats.find.time, 3), "Bold" },
{ "avg time", self:ms(Cache.stats.find.time / Cache.stats.find.total, 3), "Bold" },
{ "index", Cache.stats.find.index, "Number" },
{ "fs_stat", Cache.stats.find.stat, "Number" },
{ "not found", Cache.stats.find.not_found, "Number" },
}, { indent = 2 })
self:nl()
self:append("Cache.autoload()", "LazyH2"):nl()
self:props({
{ "total", Cache.stats.autoload.total, "Number" },
{ "time", self:ms(Cache.stats.autoload.time, 3), "Bold" },
{ "avg time", self:ms(Cache.stats.autoload.time / Cache.stats.autoload.total, 3), "Bold" },
}, { indent = 2 })
self:nl()
self:append("Cache", "LazyH2"):nl()
local Cache = require("lazy.core.cache")
Util.foreach(Cache.cache, function(modname, entry)
local kb = math.floor(#entry.chunk / 10.24) / 100
self:append("", "LazySpecial", { indent = 2 }):append(modname):append(" " .. kb .. "Kb", "Bold")
if entry.modpath ~= modname then
self:append(" " .. vim.fn.fnamemodify(entry.modpath, ":p:~:."), "LazyComment")
Util.foreach(Cache.stats, function(name, stats)
self:append(name, "LazyH2"):nl()
local props = {
{ "total", stats.total or 0, "Number" },
{ "time", self:ms(stats.time or 0, 3), "Bold" },
{ "avg time", self:ms((stats.time or 0) / (stats.total or 0), 3), "Bold" },
}
for k, v in pairs(stats) do
if k ~= "total" and k ~= "time" then
props[#props + 1] = { k, v, "Number" }
end
end
self:props(props, { indent = 2 })
self:nl()
end)
end