2022-11-20 21:33:47 +00:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
---@alias LazyProfile {name: string, time: number, [number]:LazyProfile}
|
|
|
|
|
|
|
|
---@type LazyProfile[]
|
|
|
|
M._profiles = { { name = "lazy" } }
|
|
|
|
|
|
|
|
---@param name string?
|
|
|
|
---@param time number?
|
|
|
|
function M.track(name, time)
|
2022-11-20 21:34:55 +00:00
|
|
|
if name then
|
|
|
|
local entry = {
|
|
|
|
name = name,
|
|
|
|
time = time or M.time(),
|
|
|
|
}
|
|
|
|
table.insert(M._profiles[#M._profiles], entry)
|
|
|
|
|
|
|
|
if not time then
|
|
|
|
table.insert(M._profiles, entry)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local entry = table.remove(M._profiles)
|
|
|
|
entry.time = M.time() - entry.time
|
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.time()
|
2022-11-21 20:50:16 +00:00
|
|
|
return vim.loop.hrtime()
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.file_exists(file)
|
2022-11-20 21:34:55 +00:00
|
|
|
return vim.loop.fs_stat(file) ~= nil
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param ms number
|
|
|
|
---@param fn fun()
|
|
|
|
function M.throttle(ms, fn)
|
2022-11-20 21:34:55 +00:00
|
|
|
local timer = vim.loop.new_timer()
|
|
|
|
local running = false
|
|
|
|
local first = true
|
|
|
|
|
|
|
|
return function()
|
|
|
|
if not running then
|
|
|
|
if first then
|
|
|
|
fn()
|
|
|
|
first = false
|
|
|
|
end
|
|
|
|
|
|
|
|
timer:start(ms, 0, function()
|
|
|
|
running = false
|
|
|
|
vim.schedule(fn)
|
|
|
|
end)
|
|
|
|
|
|
|
|
running = true
|
|
|
|
end
|
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.very_lazy()
|
2022-11-20 21:34:55 +00: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-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param path string
|
|
|
|
function M.scandir(path)
|
2022-11-20 21:34:55 +00:00
|
|
|
---@type {name: string, path: string, type: "file"|"directory"|"link"}[]
|
|
|
|
local ret = {}
|
|
|
|
|
|
|
|
local dir = vim.loop.fs_opendir(path, nil, 100)
|
|
|
|
|
|
|
|
if dir then
|
|
|
|
---@type {name: string, path: string, type: "file"|"directory"|"link"}[]
|
|
|
|
local entries = vim.loop.fs_readdir(dir)
|
|
|
|
while entries do
|
|
|
|
for _, entry in ipairs(entries) do
|
|
|
|
entry.path = path .. "/" .. entry.name
|
|
|
|
table.insert(ret, entry)
|
|
|
|
end
|
|
|
|
entries = vim.loop.fs_readdir(dir)
|
|
|
|
end
|
|
|
|
vim.loop.fs_closedir(dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.profile()
|
2022-11-20 21:34:55 +00:00
|
|
|
local lines = { "# Profile" }
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-20 21:34:55 +00:00
|
|
|
---@param entry LazyProfile
|
|
|
|
local function _profile(entry, depth)
|
|
|
|
if entry.time < 0.5 then
|
|
|
|
-- Nothing
|
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-20 21:34:55 +00:00
|
|
|
table.insert(
|
|
|
|
lines,
|
2022-11-21 20:50:16 +00:00
|
|
|
(" "):rep(depth) .. "- " .. entry.name .. ": **" .. math.floor((entry.time or 0) / 1e6 * 100) / 100 .. "ms**"
|
2022-11-20 21:34:55 +00:00
|
|
|
)
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-20 21:34:55 +00:00
|
|
|
for _, child in ipairs(entry) do
|
|
|
|
_profile(child, depth + 1)
|
|
|
|
end
|
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-20 21:34:55 +00:00
|
|
|
for _, entry in ipairs(M._profiles[1]) do
|
|
|
|
_profile(entry, 1)
|
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-20 21:34:55 +00:00
|
|
|
M.markdown(lines)
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@return string?
|
|
|
|
function M.head(file)
|
2022-11-20 21:34:55 +00:00
|
|
|
local f = io.open(file)
|
|
|
|
if f then
|
2022-11-20 23:29:17 +00:00
|
|
|
local ret = f:read()
|
2022-11-20 21:34:55 +00:00
|
|
|
f:close()
|
2022-11-20 23:29:17 +00:00
|
|
|
return ret
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@return {branch: string, hash:string}?
|
|
|
|
function M.git_info(dir)
|
2022-11-20 22:25:56 +00:00
|
|
|
local line = M.head(dir .. "/.git/HEAD")
|
|
|
|
if line then
|
2022-11-20 21:34:55 +00:00
|
|
|
---@type string, string
|
2022-11-20 22:25:56 +00:00
|
|
|
local ref, branch = line:match("ref: (refs/heads/(.*))")
|
2022-11-20 21:34:55 +00:00
|
|
|
|
|
|
|
if ref then
|
|
|
|
return {
|
|
|
|
branch = branch,
|
|
|
|
hash = M.head(dir .. "/.git/" .. ref),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param msg string|string[]
|
|
|
|
---@param opts? table
|
|
|
|
function M.markdown(msg, opts)
|
2022-11-20 21:34:55 +00:00
|
|
|
if type(msg) == "table" then
|
|
|
|
msg = table.concat(msg, "\n") or msg
|
|
|
|
end
|
|
|
|
|
|
|
|
vim.notify(
|
|
|
|
msg,
|
|
|
|
vim.log.levels.INFO,
|
|
|
|
vim.tbl_deep_extend("force", {
|
|
|
|
title = "lazy.nvim",
|
|
|
|
on_open = function(win)
|
|
|
|
vim.wo[win].conceallevel = 3
|
|
|
|
vim.wo[win].concealcursor = "n"
|
|
|
|
vim.wo[win].spell = false
|
|
|
|
|
|
|
|
vim.treesitter.start(vim.api.nvim_win_get_buf(win), "markdown")
|
|
|
|
end,
|
|
|
|
}, opts or {})
|
|
|
|
)
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.error(msg)
|
2022-11-20 21:34:55 +00:00
|
|
|
vim.notify(msg, vim.log.levels.ERROR, {
|
|
|
|
title = "lazy.nvim",
|
|
|
|
})
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2022-11-22 20:12:33 +00:00
|
|
|
function M._dump(value, result)
|
|
|
|
local t = type(value)
|
|
|
|
if t == "number" or t == "boolean" then
|
|
|
|
table.insert(result, tostring(value))
|
|
|
|
elseif t == "string" then
|
|
|
|
table.insert(result, ("%q"):format(value))
|
|
|
|
elseif t == "table" then
|
|
|
|
table.insert(result, "{")
|
|
|
|
local i = 1
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for k, v in pairs(value) do
|
|
|
|
if k == i then
|
|
|
|
elseif type(k) == "string" then
|
|
|
|
table.insert(result, ("[%q]="):format(k))
|
|
|
|
else
|
|
|
|
table.insert(result, k .. "=")
|
|
|
|
end
|
|
|
|
M._dump(v, result)
|
|
|
|
table.insert(result, ",")
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
table.insert(result, "}")
|
|
|
|
else
|
|
|
|
error("Unsupported type " .. t)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.dump(value)
|
|
|
|
local result = {}
|
|
|
|
M._dump(value, result)
|
|
|
|
return table.concat(result, "")
|
|
|
|
end
|
|
|
|
|
2022-11-20 21:33:47 +00:00
|
|
|
return M
|