2022-12-23 07:34:55 +00:00
|
|
|
---@class LazyUtilCore
|
2022-11-20 21:33:47 +00:00
|
|
|
local M = {}
|
|
|
|
|
2022-11-29 11:02:25 +00:00
|
|
|
---@alias LazyProfile {data: string|{[string]:string}, time: number, [number]:LazyProfile}
|
2022-11-20 21:33:47 +00:00
|
|
|
|
|
|
|
---@type LazyProfile[]
|
|
|
|
M._profiles = { { name = "lazy" } }
|
2024-06-25 12:23:25 +01:00
|
|
|
M.is_win = jit.os:find("Windows")
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-29 11:02:25 +00:00
|
|
|
---@param data (string|{[string]:string})?
|
2022-11-20 21:33:47 +00:00
|
|
|
---@param time number?
|
2022-11-29 11:02:25 +00:00
|
|
|
function M.track(data, time)
|
|
|
|
if data then
|
2022-11-20 21:34:55 +00:00
|
|
|
local entry = {
|
2022-11-29 11:02:25 +00:00
|
|
|
data = data,
|
2024-03-22 07:58:36 +00:00
|
|
|
time = time or vim.uv.hrtime(),
|
2022-11-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
table.insert(M._profiles[#M._profiles], entry)
|
|
|
|
|
|
|
|
if not time then
|
|
|
|
table.insert(M._profiles, entry)
|
|
|
|
end
|
2022-11-23 15:10:16 +00:00
|
|
|
return entry
|
2022-11-20 21:34:55 +00:00
|
|
|
else
|
2022-11-24 21:03:00 +00:00
|
|
|
---@type LazyProfile
|
2022-11-20 21:34:55 +00:00
|
|
|
local entry = table.remove(M._profiles)
|
2024-03-22 07:58:36 +00:00
|
|
|
entry.time = vim.uv.hrtime() - entry.time
|
2022-11-23 15:10:16 +00:00
|
|
|
return entry
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2024-06-30 12:35:11 +01:00
|
|
|
function M.exiting()
|
|
|
|
return vim.v.exiting ~= vim.NIL
|
|
|
|
end
|
|
|
|
|
2024-06-24 15:42:57 +01:00
|
|
|
---@generic T
|
|
|
|
---@param list T[]
|
|
|
|
---@param fn fun(v: T):boolean?
|
|
|
|
---@return T[]
|
|
|
|
function M.filter(fn, list)
|
|
|
|
local ret = {}
|
|
|
|
for _, v in ipairs(list) do
|
|
|
|
if fn(v) then
|
|
|
|
table.insert(ret, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2023-01-02 10:26:40 +00: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-22 21:41:44 +00:00
|
|
|
---@param name string
|
2022-12-23 09:18:19 +00:00
|
|
|
---@return string
|
2022-12-22 21:41:44 +00:00
|
|
|
function M.normname(name)
|
2022-12-23 09:18:19 +00:00
|
|
|
local ret = name:lower():gsub("^n?vim%-", ""):gsub("%.n?vim$", ""):gsub("%.lua", ""):gsub("[^a-z]+", "")
|
|
|
|
return ret
|
2022-12-22 21:41:44 +00:00
|
|
|
end
|
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
---@return string
|
2022-12-20 06:19:41 +00:00
|
|
|
function M.norm(path)
|
|
|
|
if path:sub(1, 1) == "~" then
|
2024-03-22 07:58:36 +00:00
|
|
|
local home = vim.uv.os_homedir()
|
2022-12-20 07:47:01 +00:00
|
|
|
if home:sub(-1) == "\\" or home:sub(-1) == "/" then
|
|
|
|
home = home:sub(1, -2)
|
|
|
|
end
|
|
|
|
path = home .. path:sub(2)
|
2022-12-20 06:19:41 +00:00
|
|
|
end
|
2022-12-31 08:55:50 +00:00
|
|
|
path = path:gsub("\\", "/"):gsub("/+", "/")
|
2022-12-21 09:17:10 +00:00
|
|
|
return path:sub(-1) == "/" and path:sub(1, -2) or path
|
2022-12-20 06:19:41 +00:00
|
|
|
end
|
|
|
|
|
2023-10-10 18:12:35 +01:00
|
|
|
---@param opts? {level?: number}
|
|
|
|
function M.pretty_trace(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
local trace = {}
|
|
|
|
local level = opts.level or 2
|
|
|
|
while true do
|
|
|
|
local info = debug.getinfo(level, "Sln")
|
|
|
|
if not info then
|
|
|
|
break
|
|
|
|
end
|
2023-10-22 13:24:27 +01:00
|
|
|
if info.what ~= "C" and (Config.options.debug or not info.source:find("lazy.nvim")) then
|
2023-10-10 18:12:35 +01:00
|
|
|
local source = info.source:sub(2)
|
|
|
|
if source:find(Config.options.root, 1, true) == 1 then
|
|
|
|
source = source:sub(#Config.options.root + 1)
|
|
|
|
end
|
|
|
|
source = vim.fn.fnamemodify(source, ":p:~:.") --[[@as string]]
|
|
|
|
local line = " - " .. source .. ":" .. info.currentline
|
|
|
|
if info.name then
|
|
|
|
line = line .. " _in_ **" .. info.name .. "**"
|
|
|
|
end
|
|
|
|
table.insert(trace, line)
|
|
|
|
end
|
|
|
|
level = level + 1
|
|
|
|
end
|
|
|
|
return #trace > 0 and ("\n\n# stacktrace:\n" .. table.concat(trace, "\n")) or ""
|
|
|
|
end
|
|
|
|
|
2024-06-17 23:36:11 +01:00
|
|
|
---@generic R
|
2024-06-22 21:18:26 +01:00
|
|
|
---@param fn fun():R?
|
2022-12-30 10:52:09 +00:00
|
|
|
---@param opts? string|{msg:string, on_error:fun(msg)}
|
2024-06-17 23:36:11 +01:00
|
|
|
---@return R
|
2022-12-30 10:52:09 +00:00
|
|
|
function M.try(fn, opts)
|
|
|
|
opts = type(opts) == "string" and { msg = opts } or opts or {}
|
|
|
|
local msg = opts.msg
|
2022-11-25 21:50:17 +00:00
|
|
|
-- error handler
|
|
|
|
local error_handler = function(err)
|
2023-10-10 18:12:35 +01:00
|
|
|
msg = (msg and (msg .. "\n\n") or "") .. err .. M.pretty_trace()
|
2022-12-30 10:52:09 +00:00
|
|
|
if opts.on_error then
|
|
|
|
opts.on_error(msg)
|
|
|
|
else
|
|
|
|
vim.schedule(function()
|
|
|
|
M.error(msg)
|
|
|
|
end)
|
|
|
|
end
|
2022-11-25 21:50:17 +00:00
|
|
|
return err
|
|
|
|
end
|
|
|
|
|
|
|
|
---@type boolean, any
|
|
|
|
local ok, result = xpcall(fn, error_handler)
|
|
|
|
return ok and result or nil
|
|
|
|
end
|
|
|
|
|
2022-12-02 08:22:15 +00:00
|
|
|
function M.get_source()
|
|
|
|
local f = 2
|
|
|
|
while true do
|
|
|
|
local info = debug.getinfo(f, "S")
|
|
|
|
if not info then
|
|
|
|
break
|
|
|
|
end
|
2023-10-09 11:38:43 +01:00
|
|
|
if info.what ~= "C" and not info.source:find("lazy.nvim", 1, true) and info.source ~= "@vim/loader.lua" then
|
2022-12-02 08:22:15 +00:00
|
|
|
return info.source:sub(2)
|
|
|
|
end
|
|
|
|
f = f + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 14:35:40 +00: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-20 21:33:47 +00:00
|
|
|
function M.very_lazy()
|
2022-11-20 21:34:55 +00:00
|
|
|
local function _load()
|
2023-01-11 12:40:19 +00:00
|
|
|
vim.schedule(function()
|
2023-01-16 11:37:28 +00:00
|
|
|
if vim.v.exiting ~= vim.NIL then
|
|
|
|
return
|
|
|
|
end
|
2023-01-07 08:12:51 +00:00
|
|
|
vim.g.did_very_lazy = true
|
2023-01-13 08:00:15 +00:00
|
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "VeryLazy", modeline = false })
|
2023-01-11 12:40:19 +00:00
|
|
|
end)
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
vim.api.nvim_create_autocmd("User", {
|
|
|
|
pattern = "LazyDone",
|
|
|
|
once = true,
|
|
|
|
callback = function()
|
|
|
|
if vim.v.vim_did_enter == 1 then
|
|
|
|
_load()
|
|
|
|
else
|
2023-01-11 12:40:19 +00:00
|
|
|
vim.api.nvim_create_autocmd("UIEnter", {
|
2022-11-20 21:34:55 +00:00
|
|
|
once = true,
|
|
|
|
callback = function()
|
|
|
|
_load()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2022-11-24 21:03:00 +00:00
|
|
|
---@alias FileType "file"|"directory"|"link"
|
|
|
|
---@param path string
|
2022-11-28 10:35:47 +00:00
|
|
|
---@param fn fun(path: string, name:string, type:FileType):boolean?
|
2022-11-24 21:03:00 +00:00
|
|
|
function M.ls(path, fn)
|
2024-03-22 07:58:36 +00:00
|
|
|
local handle = vim.uv.fs_scandir(path)
|
2022-11-24 21:03:00 +00:00
|
|
|
while handle do
|
2024-03-22 07:58:36 +00:00
|
|
|
local name, t = vim.uv.fs_scandir_next(handle)
|
2022-11-24 21:03:00 +00:00
|
|
|
if not name then
|
|
|
|
break
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
2023-01-17 13:35:21 +00:00
|
|
|
|
|
|
|
local fname = path .. "/" .. name
|
|
|
|
|
|
|
|
-- HACK: type is not always returned due to a bug in luv,
|
|
|
|
-- so fecth it with fs_stat instead when needed.
|
|
|
|
-- see https://github.com/folke/lazy.nvim/issues/306
|
2024-03-22 07:58:36 +00:00
|
|
|
if fn(fname, name, t or vim.uv.fs_stat(fname).type) == false then
|
2022-11-28 10:35:47 +00:00
|
|
|
break
|
|
|
|
end
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2022-11-24 21:03:00 +00: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-20 21:34:55 +00:00
|
|
|
end
|
2022-11-24 21:03:00 +00:00
|
|
|
fn(child, name, type)
|
|
|
|
end)
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2023-01-23 18:18:48 +00:00
|
|
|
---@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
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param modname string
|
2024-06-24 20:56:43 +01:00
|
|
|
---@return string
|
|
|
|
function M.topmod(modname)
|
|
|
|
return modname:match("^[^./]+") or modname
|
|
|
|
end
|
|
|
|
|
|
|
|
---@type table<string, string[]>
|
|
|
|
M.unloaded_cache = {}
|
|
|
|
|
|
|
|
---@param modname string
|
|
|
|
---@param opts? {cache?:boolean}
|
|
|
|
function M.get_unloaded_rtp(modname, opts)
|
|
|
|
opts = opts or {}
|
2023-02-13 11:01:56 +00:00
|
|
|
|
2024-06-24 20:56:43 +01:00
|
|
|
local topmod = M.topmod(modname)
|
|
|
|
if opts.cache and M.unloaded_cache[topmod] then
|
|
|
|
return M.unloaded_cache[topmod], true
|
|
|
|
end
|
|
|
|
|
|
|
|
local norm = M.normname(topmod)
|
|
|
|
|
|
|
|
---@type string[]
|
2023-02-13 11:01:56 +00:00
|
|
|
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
|
2024-06-24 20:56:43 +01:00
|
|
|
if norm == M.normname(plugin.name) then
|
2023-02-13 11:01:56 +00:00
|
|
|
table.insert(rtp, 1, plugin.dir)
|
|
|
|
else
|
|
|
|
table.insert(rtp, plugin.dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-06-24 20:56:43 +01:00
|
|
|
M.unloaded_cache[topmod] = rtp
|
|
|
|
return rtp, false
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.find_root(modname)
|
2024-06-24 20:56:43 +01:00
|
|
|
local paths, cached = M.get_unloaded_rtp(modname, { cache = true })
|
|
|
|
|
2023-03-15 14:10:44 +00:00
|
|
|
local ret = require("lazy.core.cache").find(modname, {
|
2023-02-13 11:01:56 +00:00
|
|
|
rtp = true,
|
2024-06-24 20:56:43 +01:00
|
|
|
paths = paths,
|
2024-07-01 06:07:49 +01:00
|
|
|
patterns = { ".lua", "" },
|
2023-03-15 14:10:44 +00:00
|
|
|
})[1]
|
2024-06-24 20:56:43 +01:00
|
|
|
|
|
|
|
if not ret and cached then
|
|
|
|
paths = M.get_unloaded_rtp(modname)
|
|
|
|
ret = require("lazy.core.cache").find(modname, {
|
|
|
|
rtp = false,
|
|
|
|
paths = paths,
|
2024-07-01 06:07:49 +01:00
|
|
|
patterns = { ".lua", "" },
|
2024-06-24 20:56:43 +01:00
|
|
|
})[1]
|
|
|
|
end
|
2023-03-15 14:10:44 +00:00
|
|
|
if ret then
|
2024-07-01 06:07:49 +01:00
|
|
|
return ret.modpath:gsub("%.lua$", ""), ret.modpath
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-21 08:03:40 +00:00
|
|
|
---@param modname string
|
2022-11-24 21:03:00 +00:00
|
|
|
---@param fn fun(modname:string, modpath:string)
|
2023-01-01 19:19:09 +00:00
|
|
|
function M.lsmod(modname, fn)
|
2024-07-01 06:07:49 +01:00
|
|
|
local root, match = M.find_root(modname)
|
2023-01-01 19:19:09 +00:00
|
|
|
if not root then
|
|
|
|
return
|
2022-12-21 08:03:40 +00:00
|
|
|
end
|
2023-01-01 19:19:09 +00:00
|
|
|
|
2024-07-01 06:07:49 +01:00
|
|
|
if match:sub(-4) == ".lua" then
|
|
|
|
fn(modname, match)
|
|
|
|
if not vim.uv.fs_stat(root) then
|
|
|
|
return
|
|
|
|
end
|
2022-12-21 08:03:40 +00:00
|
|
|
end
|
2023-01-01 19:19:09 +00:00
|
|
|
|
2022-11-24 21:03:00 +00:00
|
|
|
M.ls(root, function(path, name, type)
|
2023-01-02 14:04:32 +00:00
|
|
|
if name == "init.lua" then
|
|
|
|
fn(modname, path)
|
|
|
|
elseif (type == "file" or type == "link") and name:sub(-4) == ".lua" then
|
2023-01-01 19:19:09 +00:00
|
|
|
fn(modname .. "." .. name:sub(1, -5), path)
|
2024-03-22 07:58:36 +00:00
|
|
|
elseif type == "directory" and vim.uv.fs_stat(path .. "/init.lua") then
|
2022-12-21 08:03:40 +00:00
|
|
|
fn(modname .. "." .. name, path .. "/init.lua")
|
2022-11-24 21:03:00 +00:00
|
|
|
end
|
|
|
|
end)
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2023-01-17 13:00:33 +00:00
|
|
|
---@generic T
|
|
|
|
---@param list T[]
|
|
|
|
---@param add T[]
|
|
|
|
---@return T[]
|
|
|
|
function M.extend(list, add)
|
|
|
|
local idx = {}
|
|
|
|
for _, v in ipairs(list) do
|
|
|
|
idx[v] = v
|
|
|
|
end
|
|
|
|
for _, a in ipairs(add) do
|
|
|
|
if not idx[a] then
|
|
|
|
table.insert(list, a)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return list
|
|
|
|
end
|
|
|
|
|
2023-10-10 18:12:35 +01:00
|
|
|
---@alias LazyNotifyOpts {lang?:string, title?:string, level?:number, once?:boolean, stacktrace?:boolean, stacklevel?:number}
|
2023-01-10 07:42:20 +00:00
|
|
|
|
2022-12-02 15:52:22 +00:00
|
|
|
---@param msg string|string[]
|
2023-01-10 07:42:20 +00:00
|
|
|
---@param opts? LazyNotifyOpts
|
|
|
|
function M.notify(msg, opts)
|
2023-01-03 15:17:15 +00:00
|
|
|
if vim.in_fast_event() then
|
2023-01-10 07:42:20 +00:00
|
|
|
return vim.schedule(function()
|
|
|
|
M.notify(msg, opts)
|
2023-01-03 15:17:15 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
opts = opts or {}
|
2022-12-03 22:15:50 +00:00
|
|
|
if type(msg) == "table" then
|
|
|
|
msg = table.concat(
|
|
|
|
vim.tbl_filter(function(line)
|
|
|
|
return line or false
|
|
|
|
end, msg),
|
|
|
|
"\n"
|
|
|
|
)
|
|
|
|
end
|
2023-10-10 18:12:35 +01:00
|
|
|
if opts.stacktrace then
|
|
|
|
msg = msg .. M.pretty_trace({ level = opts.stacklevel or 2 })
|
|
|
|
end
|
2023-01-03 15:17:15 +00:00
|
|
|
local lang = opts.lang or "markdown"
|
2023-10-10 18:12:35 +01:00
|
|
|
local n = opts.once and vim.notify_once or vim.notify
|
|
|
|
n(msg, opts.level or vim.log.levels.INFO, {
|
2022-11-25 21:50:17 +00:00
|
|
|
on_open = function(win)
|
2023-10-03 23:19:29 +01:00
|
|
|
local ok = pcall(function()
|
|
|
|
vim.treesitter.language.add("markdown")
|
|
|
|
end)
|
|
|
|
if not ok then
|
|
|
|
pcall(require, "nvim-treesitter")
|
|
|
|
end
|
2022-11-25 21:50:17 +00: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 15:17:15 +00:00
|
|
|
if not pcall(vim.treesitter.start, buf, lang) then
|
|
|
|
vim.bo[buf].filetype = lang
|
|
|
|
vim.bo[buf].syntax = lang
|
2022-12-27 11:35:02 +00:00
|
|
|
end
|
2022-11-25 21:50:17 +00:00
|
|
|
end,
|
2023-01-10 07:42:20 +00:00
|
|
|
title = opts.title or "lazy.nvim",
|
2022-11-20 21:34:55 +00:00
|
|
|
})
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2022-12-02 15:52:22 +00:00
|
|
|
---@param msg string|string[]
|
2023-01-10 07:42:20 +00:00
|
|
|
---@param opts? LazyNotifyOpts
|
|
|
|
function M.error(msg, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
opts.level = vim.log.levels.ERROR
|
|
|
|
M.notify(msg, opts)
|
2022-11-25 21:50:17 +00:00
|
|
|
end
|
|
|
|
|
2022-12-02 15:52:22 +00:00
|
|
|
---@param msg string|string[]
|
2023-01-10 07:42:20 +00:00
|
|
|
---@param opts? LazyNotifyOpts
|
|
|
|
function M.info(msg, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
opts.level = vim.log.levels.INFO
|
|
|
|
M.notify(msg, opts)
|
2022-11-22 21:35:06 +00:00
|
|
|
end
|
|
|
|
|
2022-12-02 15:52:22 +00:00
|
|
|
---@param msg string|string[]
|
2023-01-10 07:42:20 +00:00
|
|
|
---@param opts? LazyNotifyOpts
|
|
|
|
function M.warn(msg, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
opts.level = vim.log.levels.WARN
|
|
|
|
M.notify(msg, opts)
|
2022-12-02 15:52:22 +00:00
|
|
|
end
|
|
|
|
|
2023-01-03 15:17:15 +00:00
|
|
|
---@param msg string|table
|
2023-01-10 07:42:20 +00:00
|
|
|
---@param opts? LazyNotifyOpts
|
|
|
|
function M.debug(msg, opts)
|
2023-01-03 15:17:15 +00:00
|
|
|
if not require("lazy.core.config").options.debug then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
opts = opts or {}
|
2023-01-10 07:42:20 +00:00
|
|
|
if opts.title then
|
|
|
|
opts.title = "lazy.nvim: " .. opts.title
|
|
|
|
end
|
2023-01-03 15:17:15 +00:00
|
|
|
if type(msg) == "string" then
|
2023-01-10 07:42:20 +00:00
|
|
|
M.notify(msg, opts)
|
2023-01-03 15:17:15 +00:00
|
|
|
else
|
|
|
|
opts.lang = "lua"
|
2023-01-10 07:42:20 +00:00
|
|
|
M.notify(vim.inspect(msg), opts)
|
2023-01-03 15:17:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-08 13:58:17 +00:00
|
|
|
local function can_merge(v)
|
|
|
|
return type(v) == "table" and (vim.tbl_isempty(v) or not M.is_list(v))
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Merges the values similar to vim.tbl_deep_extend with the **force** behavior,
|
|
|
|
--- but the values can be any type, in which case they override the values on the left.
|
|
|
|
--- Values will me merged in-place in the first left-most table. If you want the result to be in
|
|
|
|
--- a new table, then simply pass an empty table as the first argument `vim.merge({}, ...)`
|
|
|
|
--- Supports clearing values by setting a key to `vim.NIL`
|
2023-01-08 17:45:30 +00:00
|
|
|
---@generic T
|
|
|
|
---@param ... T
|
|
|
|
---@return T
|
2023-01-08 13:58:17 +00:00
|
|
|
function M.merge(...)
|
2023-10-13 10:37:52 +01:00
|
|
|
local ret = select(1, ...)
|
2023-01-10 06:32:28 +00:00
|
|
|
if ret == vim.NIL then
|
|
|
|
ret = nil
|
|
|
|
end
|
2023-10-13 10:37:52 +01:00
|
|
|
for i = 2, select("#", ...) do
|
|
|
|
local value = select(i, ...)
|
2024-06-06 22:27:29 +01:00
|
|
|
if can_merge(ret) and can_merge(value) then
|
2023-01-08 13:58:17 +00:00
|
|
|
for k, v in pairs(value) do
|
|
|
|
ret[k] = M.merge(ret[k], v)
|
|
|
|
end
|
|
|
|
elseif value == vim.NIL then
|
|
|
|
ret = nil
|
2023-10-13 10:37:52 +01:00
|
|
|
elseif value ~= nil then
|
2023-01-08 13:58:17 +00:00
|
|
|
ret = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2023-10-08 19:22:01 +01:00
|
|
|
function M.lazy_require(module)
|
|
|
|
local mod = nil
|
|
|
|
-- if already loaded, return the module
|
|
|
|
-- otherwise return a lazy module
|
|
|
|
return type(package.loaded[module]) == "table" and package.loaded[module]
|
|
|
|
or setmetatable({}, {
|
|
|
|
__index = function(_, key)
|
|
|
|
mod = mod or require(module)
|
|
|
|
return mod[key]
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2024-06-07 08:02:52 +01:00
|
|
|
---@param t table
|
|
|
|
---@param key string|string[]
|
|
|
|
---@return any
|
|
|
|
function M.key_get(t, key)
|
|
|
|
local path = type(key) == "table" and key or vim.split(key, ".", true)
|
|
|
|
local value = t
|
|
|
|
for _, k in ipairs(path) do
|
|
|
|
if type(value) ~= "table" then
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
value = value[k]
|
|
|
|
end
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param t table
|
|
|
|
---@param key string|string[]
|
|
|
|
---@param value any
|
|
|
|
function M.key_set(t, key, value)
|
|
|
|
local path = type(key) == "table" and key or vim.split(key, ".", true)
|
|
|
|
local last = t
|
|
|
|
for i = 1, #path - 1 do
|
|
|
|
local k = path[i]
|
|
|
|
if type(last[k]) ~= "table" then
|
|
|
|
last[k] = {}
|
|
|
|
end
|
|
|
|
last = last[k]
|
|
|
|
end
|
|
|
|
last[path[#path]] = value
|
|
|
|
end
|
|
|
|
|
2022-11-20 21:33:47 +00:00
|
|
|
return M
|