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

122 lines
2.8 KiB
Lua
Raw Normal View History

2022-11-21 05:33:47 +08: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-21 05:34:55 +08:00
if name then
local entry = {
name = name,
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.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"
---@alias DirEntry {name: string, path: string, type: FileType}[]
2022-11-21 05:33:47 +08:00
---@param path string
2022-11-25 05:03:00 +08:00
---@param fn fun(path: string, name:string, type:FileType)
function M.scandir(path, fn)
2022-11-21 05:34:55 +08:00
local dir = vim.loop.fs_opendir(path, nil, 100)
if dir then
2022-11-25 05:03:00 +08:00
local entries = vim.loop.fs_readdir(dir) --[[@as DirEntry[]]
2022-11-21 05:34:55 +08:00
while entries do
for _, entry in ipairs(entries) do
entry.path = path .. "/" .. entry.name
2022-11-25 05:03:00 +08:00
fn(path .. "/" .. entry.name, entry.name, entry.type)
2022-11-21 05:34:55 +08:00
end
entries = vim.loop.fs_readdir(dir)
end
vim.loop.fs_closedir(dir)
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.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-25 05:03:00 +08:00
fn(path .. "/" .. name, name, t)
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)
if type == "file" and name:sub(-4) == ".lua" then
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.error(msg)
2022-11-21 05:34:55 +08:00
vim.notify(msg, vim.log.levels.ERROR, {
title = "lazy.nvim",
})
2022-11-21 05:33:47 +08:00
end
2022-11-23 05:35:06 +08:00
function M.info(msg)
vim.notify(msg, vim.log.levels.INFO, {
title = "lazy.nvim",
})
end
2022-11-21 05:33:47 +08:00
return M