2022-12-23 15:34:55 +08:00
|
|
|
---@class LazyUtilCore
|
2022-11-21 05:33:47 +08:00
|
|
|
local M = {}
|
|
|
|
|
2022-11-29 19:02:25 +08:00
|
|
|
---@alias LazyProfile {data: string|{[string]:string}, time: number, [number]:LazyProfile}
|
2022-11-21 05:33:47 +08:00
|
|
|
|
|
|
|
---@type LazyProfile[]
|
|
|
|
M._profiles = { { name = "lazy" } }
|
|
|
|
|
2022-11-29 19:02:25 +08:00
|
|
|
---@param data (string|{[string]:string})?
|
2022-11-21 05:33:47 +08:00
|
|
|
---@param time number?
|
2022-11-29 19:02:25 +08:00
|
|
|
function M.track(data, time)
|
|
|
|
if data then
|
2022-11-21 05:34:55 +08:00
|
|
|
local entry = {
|
2022-11-29 19:02:25 +08:00
|
|
|
data = data,
|
2022-11-23 23:10:16 +08:00
|
|
|
time = time or vim.loop.hrtime(),
|
2022-11-21 05:34:55 +08:00
|
|
|
}
|
|
|
|
table.insert(M._profiles[#M._profiles], entry)
|
|
|
|
|
|
|
|
if not time then
|
|
|
|
table.insert(M._profiles, entry)
|
|
|
|
end
|
2022-11-23 23:10:16 +08:00
|
|
|
return entry
|
2022-11-21 05:34:55 +08:00
|
|
|
else
|
2022-11-25 05:03:00 +08:00
|
|
|
---@type LazyProfile
|
2022-11-21 05:34:55 +08:00
|
|
|
local entry = table.remove(M._profiles)
|
2022-11-23 23:10:16 +08:00
|
|
|
entry.time = vim.loop.hrtime() - entry.time
|
|
|
|
return entry
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
2023-01-02 18:26:40 +08:00
|
|
|
---@generic F: fun()
|
|
|
|
---@param data (string|{[string]:string})?
|
|
|
|
---@param fn F
|
|
|
|
---@return F
|
|
|
|
function M.trackfn(data, fn)
|
|
|
|
return function(...)
|
|
|
|
M.track(data)
|
|
|
|
local ok, ret = pcall(fn, ...)
|
|
|
|
M.track()
|
|
|
|
if not ok then
|
|
|
|
error(ret)
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-23 05:41:44 +08:00
|
|
|
---@param name string
|
2022-12-23 17:18:19 +08:00
|
|
|
---@return string
|
2022-12-23 05:41:44 +08:00
|
|
|
function M.normname(name)
|
2022-12-23 17:18:19 +08:00
|
|
|
local ret = name:lower():gsub("^n?vim%-", ""):gsub("%.n?vim$", ""):gsub("%.lua", ""):gsub("[^a-z]+", "")
|
|
|
|
return ret
|
2022-12-23 05:41:44 +08:00
|
|
|
end
|
|
|
|
|
2022-12-23 17:18:19 +08:00
|
|
|
---@return string
|
2022-12-20 14:19:41 +08:00
|
|
|
function M.norm(path)
|
|
|
|
if path:sub(1, 1) == "~" then
|
2022-12-20 15:47:01 +08:00
|
|
|
local home = vim.loop.os_homedir()
|
|
|
|
if home:sub(-1) == "\\" or home:sub(-1) == "/" then
|
|
|
|
home = home:sub(1, -2)
|
|
|
|
end
|
|
|
|
path = home .. path:sub(2)
|
2022-12-20 14:19:41 +08:00
|
|
|
end
|
2022-12-31 16:55:50 +08:00
|
|
|
path = path:gsub("\\", "/"):gsub("/+", "/")
|
2022-12-21 17:17:10 +08:00
|
|
|
return path:sub(-1) == "/" and path:sub(1, -2) or path
|
2022-12-20 14:19:41 +08:00
|
|
|
end
|
|
|
|
|
2022-12-30 18:52:09 +08:00
|
|
|
---@param opts? string|{msg:string, on_error:fun(msg)}
|
|
|
|
function M.try(fn, opts)
|
|
|
|
opts = type(opts) == "string" and { msg = opts } or opts or {}
|
|
|
|
local msg = opts.msg
|
2022-11-26 05:50:17 +08:00
|
|
|
-- error handler
|
|
|
|
local error_handler = function(err)
|
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
local trace = {}
|
|
|
|
local level = 1
|
|
|
|
while true do
|
|
|
|
local info = debug.getinfo(level, "Sln")
|
|
|
|
if not info then
|
|
|
|
break
|
|
|
|
end
|
2022-12-02 16:22:15 +08:00
|
|
|
if info.what ~= "C" and not info.source:find("lazy.nvim") then
|
2022-11-26 05:50:17 +08:00
|
|
|
local source = info.source:sub(2)
|
2022-12-03 22:48:06 +08:00
|
|
|
if source:find(Config.options.root, 1, true) == 1 then
|
|
|
|
source = source:sub(#Config.options.root + 1)
|
2022-11-26 05:50:17 +08:00
|
|
|
end
|
|
|
|
source = vim.fn.fnamemodify(source, ":p:~:.")
|
|
|
|
local line = " - " .. source .. ":" .. info.currentline
|
|
|
|
if info.name then
|
|
|
|
line = line .. " _in_ **" .. info.name .. "**"
|
|
|
|
end
|
|
|
|
table.insert(trace, line)
|
|
|
|
end
|
|
|
|
level = level + 1
|
|
|
|
end
|
2022-11-28 14:36:32 +08:00
|
|
|
msg = msg .. "\n\n" .. err
|
|
|
|
if #trace > 0 then
|
|
|
|
msg = msg .. "\n\n# stacktrace:\n" .. table.concat(trace, "\n")
|
|
|
|
end
|
2022-12-30 18:52:09 +08:00
|
|
|
if opts.on_error then
|
|
|
|
opts.on_error(msg)
|
|
|
|
else
|
|
|
|
vim.schedule(function()
|
|
|
|
M.error(msg)
|
|
|
|
end)
|
|
|
|
end
|
2022-11-26 05:50:17 +08:00
|
|
|
return err
|
|
|
|
end
|
|
|
|
|
|
|
|
---@type boolean, any
|
|
|
|
local ok, result = xpcall(fn, error_handler)
|
|
|
|
return ok and result or nil
|
|
|
|
end
|
|
|
|
|
2022-12-02 16:22:15 +08:00
|
|
|
function M.get_source()
|
|
|
|
local f = 2
|
|
|
|
while true do
|
|
|
|
local info = debug.getinfo(f, "S")
|
|
|
|
if not info then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
if info.what ~= "C" and not info.source:find("lazy.nvim", 1, true) then
|
|
|
|
return info.source:sub(2)
|
|
|
|
end
|
|
|
|
f = f + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 22:35:40 +08:00
|
|
|
-- Fast implementation to check if a table is a list
|
|
|
|
---@param t table
|
|
|
|
function M.is_list(t)
|
|
|
|
local i = 0
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for _ in pairs(t) do
|
|
|
|
i = i + 1
|
|
|
|
if t[i] == nil then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-11-21 05:33:47 +08:00
|
|
|
function M.very_lazy()
|
2022-11-21 05:34:55 +08:00
|
|
|
local function _load()
|
|
|
|
vim.defer_fn(function()
|
|
|
|
vim.cmd("do User VeryLazy")
|
2023-01-02 18:26:40 +08:00
|
|
|
end, 50)
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
vim.api.nvim_create_autocmd("User", {
|
|
|
|
pattern = "LazyDone",
|
|
|
|
once = true,
|
|
|
|
callback = function()
|
|
|
|
if vim.v.vim_did_enter == 1 then
|
|
|
|
_load()
|
|
|
|
else
|
|
|
|
vim.api.nvim_create_autocmd("VimEnter", {
|
|
|
|
once = true,
|
|
|
|
callback = function()
|
|
|
|
_load()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
2022-11-25 05:03:00 +08:00
|
|
|
---@alias FileType "file"|"directory"|"link"
|
|
|
|
---@param path string
|
2022-11-28 18:35:47 +08:00
|
|
|
---@param fn fun(path: string, name:string, type:FileType):boolean?
|
2022-11-25 05:03:00 +08:00
|
|
|
function M.ls(path, fn)
|
|
|
|
local handle = vim.loop.fs_scandir(path)
|
|
|
|
while handle do
|
|
|
|
local name, t = vim.loop.fs_scandir_next(handle)
|
|
|
|
if not name then
|
|
|
|
break
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
2022-11-28 18:35:47 +08:00
|
|
|
if fn(path .. "/" .. name, name, t) == false then
|
|
|
|
break
|
|
|
|
end
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
2022-11-25 05:03:00 +08:00
|
|
|
---@param path string
|
|
|
|
---@param fn fun(path: string, name:string, type:FileType)
|
|
|
|
function M.walk(path, fn)
|
|
|
|
M.ls(path, function(child, name, type)
|
|
|
|
if type == "directory" then
|
|
|
|
M.walk(child, fn)
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
2022-11-25 05:03:00 +08:00
|
|
|
fn(child, name, type)
|
|
|
|
end)
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
2022-12-21 16:03:40 +08:00
|
|
|
---@param modname string
|
2022-11-25 05:03:00 +08:00
|
|
|
---@param fn fun(modname:string, modpath:string)
|
2023-01-02 03:19:09 +08:00
|
|
|
function M.lsmod(modname, fn)
|
|
|
|
local Cache = require("lazy.core.cache")
|
2023-01-04 05:31:18 +08:00
|
|
|
local root = Cache.find_root(modname)
|
2023-01-02 03:19:09 +08:00
|
|
|
if not root then
|
|
|
|
return
|
2022-12-21 16:03:40 +08:00
|
|
|
end
|
2023-01-02 03:19:09 +08:00
|
|
|
|
2023-01-04 05:31:18 +08:00
|
|
|
if vim.loop.fs_stat(root .. ".lua") then
|
|
|
|
fn(modname, root .. ".lua")
|
2022-12-21 16:03:40 +08:00
|
|
|
end
|
2023-01-02 03:19:09 +08:00
|
|
|
|
2022-11-25 05:03:00 +08:00
|
|
|
M.ls(root, function(path, name, type)
|
2023-01-02 22:04:32 +08:00
|
|
|
if name == "init.lua" then
|
|
|
|
fn(modname, path)
|
|
|
|
elseif (type == "file" or type == "link") and name:sub(-4) == ".lua" then
|
2023-01-02 03:19:09 +08:00
|
|
|
fn(modname .. "." .. name:sub(1, -5), path)
|
2022-11-25 05:03:00 +08:00
|
|
|
elseif type == "directory" and vim.loop.fs_stat(path .. "/init.lua") then
|
2022-12-21 16:03:40 +08:00
|
|
|
fn(modname .. "." .. name, path .. "/init.lua")
|
2022-11-25 05:03:00 +08:00
|
|
|
end
|
|
|
|
end)
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
2022-12-02 23:52:22 +08:00
|
|
|
---@param msg string|string[]
|
2023-01-04 07:27:31 +08:00
|
|
|
---@param opts? {lang:string, title:string}
|
2023-01-03 23:17:15 +08:00
|
|
|
function M.notify(msg, level, opts)
|
|
|
|
if vim.in_fast_event() then
|
|
|
|
vim.schedule(function()
|
|
|
|
M.notify(msg, level, opts)
|
|
|
|
end)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
opts = opts or {}
|
2022-12-04 06:15:50 +08:00
|
|
|
if type(msg) == "table" then
|
|
|
|
msg = table.concat(
|
|
|
|
vim.tbl_filter(function(line)
|
|
|
|
return line or false
|
|
|
|
end, msg),
|
|
|
|
"\n"
|
|
|
|
)
|
|
|
|
end
|
2023-01-03 23:17:15 +08:00
|
|
|
local lang = opts.lang or "markdown"
|
2022-11-26 05:50:17 +08:00
|
|
|
vim.notify(msg, level, {
|
|
|
|
on_open = function(win)
|
2023-01-03 23:17:15 +08:00
|
|
|
pcall(require, "nvim-treesitter")
|
2022-11-26 05:50:17 +08:00
|
|
|
vim.wo[win].conceallevel = 3
|
|
|
|
vim.wo[win].concealcursor = ""
|
|
|
|
vim.wo[win].spell = false
|
|
|
|
local buf = vim.api.nvim_win_get_buf(win)
|
2023-01-03 23:17:15 +08:00
|
|
|
if not pcall(vim.treesitter.start, buf, lang) then
|
|
|
|
vim.bo[buf].filetype = lang
|
|
|
|
vim.bo[buf].syntax = lang
|
2022-12-27 19:35:02 +08:00
|
|
|
end
|
2022-11-26 05:50:17 +08:00
|
|
|
end,
|
2023-01-03 23:17:15 +08:00
|
|
|
title = "lazy.nvim" .. (opts.title and ": " .. opts.title or ""),
|
2022-11-21 05:34:55 +08:00
|
|
|
})
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
2022-12-02 23:52:22 +08:00
|
|
|
---@param msg string|string[]
|
2022-11-26 05:50:17 +08:00
|
|
|
function M.error(msg)
|
|
|
|
M.notify(msg, vim.log.levels.ERROR)
|
|
|
|
end
|
|
|
|
|
2022-12-02 23:52:22 +08:00
|
|
|
---@param msg string|string[]
|
2022-11-23 05:35:06 +08:00
|
|
|
function M.info(msg)
|
2022-11-26 05:50:17 +08:00
|
|
|
M.notify(msg, vim.log.levels.INFO)
|
2022-11-23 05:35:06 +08:00
|
|
|
end
|
|
|
|
|
2022-12-02 23:52:22 +08:00
|
|
|
---@param msg string|string[]
|
|
|
|
function M.warn(msg)
|
|
|
|
M.notify(msg, vim.log.levels.WARN)
|
|
|
|
end
|
|
|
|
|
2023-01-03 23:17:15 +08:00
|
|
|
---@param msg string|table
|
|
|
|
---@param level? number
|
2023-01-04 07:27:31 +08:00
|
|
|
---@param opts? {lang:string, title:string}
|
2023-01-03 23:17:15 +08:00
|
|
|
function M.debug(msg, level, opts)
|
|
|
|
if not require("lazy.core.config").options.debug then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
opts = opts or {}
|
|
|
|
if type(msg) == "string" then
|
2023-01-04 07:27:31 +08:00
|
|
|
M.notify(msg, level, opts)
|
2023-01-03 23:17:15 +08:00
|
|
|
else
|
|
|
|
opts.lang = "lua"
|
|
|
|
M.notify(vim.inspect(msg), level, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-21 05:33:47 +08:00
|
|
|
return M
|