lazy.nvim/lua/lazy/core/util.lua

170 lines
4.0 KiB
Lua
Raw Normal View History

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
function M.try(fn, msg)
-- 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
if info.what == "Lua" and not info.source:find("lazy.nvim") then
local source = info.source:sub(2)
if source:find(Config.options.packpath, 1, true) == 1 then
source = source:sub(#Config.options.packpath + 1):gsub("^/opt/", ""):gsub("^/start/", "")
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
vim.schedule(function()
M.error(msg)
end)
return err
end
---@type boolean, any
local ok, result = xpcall(fn, error_handler)
return ok and result or nil
end
-- 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")
end, 100)
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
---@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
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-11-25 05:03:00 +08:00
---@param root string
---@param fn fun(modname:string, modpath:string)
function M.lsmod(root, fn)
M.ls(root, function(path, name, type)
2022-11-26 20:57:17 +08:00
if type == "file" and name:sub(-4) == ".lua" and name ~= "init.lua" then
2022-11-25 05:03:00 +08:00
fn(name:sub(1, -5), path)
elseif type == "directory" and vim.loop.fs_stat(path .. "/init.lua") then
fn(name, path .. "/init.lua")
end
end)
2022-11-21 05:33:47 +08:00
end
function M.notify(msg, level)
vim.notify(msg, level, {
on_open = function(win)
vim.wo[win].conceallevel = 3
vim.wo[win].concealcursor = ""
vim.wo[win].spell = false
local buf = vim.api.nvim_win_get_buf(win)
vim.bo[buf].filetype = "markdown"
end,
2022-11-21 05:34:55 +08:00
title = "lazy.nvim",
})
2022-11-21 05:33:47 +08:00
end
function M.error(msg)
M.notify(msg, vim.log.levels.ERROR)
end
2022-11-23 05:35:06 +08:00
function M.info(msg)
M.notify(msg, vim.log.levels.INFO)
2022-11-23 05:35:06 +08:00
end
2022-11-21 05:33:47 +08:00
return M