style: formatting

This commit is contained in:
Folke Lemaitre 2022-11-20 22:34:55 +01:00
parent e73626a344
commit 0219a531ed
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
9 changed files with 698 additions and 695 deletions

View File

@ -10,78 +10,78 @@ M.dirty = false
M.did_setup = false M.did_setup = false
function M.hash(modpath) function M.hash(modpath)
local stat = vim.loop.fs_stat(modpath) local stat = vim.loop.fs_stat(modpath)
if stat then if stat then
return stat.mtime.sec .. stat.mtime.nsec .. stat.size return stat.mtime.sec .. stat.mtime.nsec .. stat.size
end end
error("Could not hash " .. modpath) error("Could not hash " .. modpath)
end end
function M.load_cache() function M.load_cache()
local f = io.open(cache_file, "rb") local f = io.open(cache_file, "rb")
if f then if f then
M.cache = vim.mpack.decode(f:read("*a")) or {} M.cache = vim.mpack.decode(f:read("*a")) or {}
f:close() f:close()
end end
end end
function M.save_cache() function M.save_cache()
if M.dirty then if M.dirty then
for key, entry in pairs(M.cache) do for key, entry in pairs(M.cache) do
if not entry.used then if not entry.used then
M.cache[key] = nil M.cache[key] = nil
end end
entry.used = nil entry.used = nil
end end
local f = assert(io.open(cache_file, "wb")) local f = assert(io.open(cache_file, "wb"))
f:write(vim.mpack.encode(M.cache)) f:write(vim.mpack.encode(M.cache))
f:close() f:close()
end end
end end
function M.setup() function M.setup()
M.load_cache() M.load_cache()
vim.api.nvim_create_autocmd("VimLeave", { vim.api.nvim_create_autocmd("VimLeave", {
callback = function() callback = function()
M.save_cache() M.save_cache()
end, end,
}) })
end end
function M.load(modpath, modname) function M.load(modpath, modname)
if not M.did_setup then if not M.did_setup then
M.setup() M.setup()
M.did_setup = true M.did_setup = true
end end
if type(package.loaded[modname]) ~= "table" then if type(package.loaded[modname]) ~= "table" then
---@type fun()?, string? ---@type fun()?, string?
local chunk, err local chunk, err
local entry = M.cache[modname] local entry = M.cache[modname]
if entry and M.hash(modpath) == entry.hash then if entry and M.hash(modpath) == entry.hash then
entry.used = true entry.used = true
chunk, err = loadstring(entry.chunk, "@" .. modpath) chunk, err = loadstring(entry.chunk, "@" .. modpath)
end end
-- not cached, or failed to load chunk -- not cached, or failed to load chunk
if not chunk then if not chunk then
vim.schedule(function() vim.schedule(function()
vim.notify("not cached") vim.notify("not cached")
end) end)
chunk, err = loadfile(modpath) chunk, err = loadfile(modpath)
if chunk then if chunk then
M.cache[modname] = { hash = M.hash(modpath), chunk = string.dump(chunk, true), used = true } M.cache[modname] = { hash = M.hash(modpath), chunk = string.dump(chunk, true), used = true }
M.dirty = true M.dirty = true
end end
end end
if not chunk then if not chunk then
error(err) error(err)
end end
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = chunk() package.loaded[modname] = chunk()
end end
return package.loaded[modname] return package.loaded[modname]
end end
return M return M

View File

@ -4,19 +4,19 @@ local M = {}
---@class LazyConfig ---@class LazyConfig
M.defaults = { M.defaults = {
opt = true, opt = true,
plugins = {}, plugins = {},
plugins_local = { plugins_local = {
path = vim.fn.expand("~/projects"), path = vim.fn.expand("~/projects"),
patterns = { patterns = {
"folke", "folke",
}, },
}, },
plugins_config = { plugins_config = {
module = "plugins", module = "plugins",
path = vim.fn.stdpath("config") .. "/lua/plugins", path = vim.fn.stdpath("config") .. "/lua/plugins",
}, },
package_path = vim.fn.stdpath("data") .. "/site/pack/lazy", package_path = vim.fn.stdpath("data") .. "/site/pack/lazy",
} }
M.ns = vim.api.nvim_create_namespace("lazy") M.ns = vim.api.nvim_create_namespace("lazy")
@ -32,37 +32,37 @@ M.has_config = {}
---@param opts? LazyConfig ---@param opts? LazyConfig
function M.setup(opts) function M.setup(opts)
M.options = vim.tbl_deep_extend("force", M.defaults, opts or {}) M.options = vim.tbl_deep_extend("force", M.defaults, opts or {})
vim.fn.mkdir(M.options.package_path, "p") vim.fn.mkdir(M.options.package_path, "p")
for _, entry in ipairs(Util.scandir(M.options.plugins_config.path)) do for _, entry in ipairs(Util.scandir(M.options.plugins_config.path)) do
local name, modpath local name, modpath
if entry.type == "file" then if entry.type == "file" then
modpath = entry.path modpath = entry.path
name = entry.name:match("(.*)%.lua") name = entry.name:match("(.*)%.lua")
elseif entry.type == "directory" then elseif entry.type == "directory" then
modpath = M.options.plugins_config.path .. "/" .. entry.name .. "/init.lua" modpath = M.options.plugins_config.path .. "/" .. entry.name .. "/init.lua"
if vim.loop.fs_stat(modpath) then if vim.loop.fs_stat(modpath) then
name = entry.name name = entry.name
end end
end end
if name then if name then
M.has_config[M.options.plugins_config.module .. "." .. name] = modpath M.has_config[M.options.plugins_config.module .. "." .. name] = modpath
end end
end end
vim.api.nvim_create_autocmd("User", { vim.api.nvim_create_autocmd("User", {
pattern = "VeryLazy", pattern = "VeryLazy",
once = true, once = true,
callback = function() callback = function()
-- require("lazy.view").setup() -- require("lazy.view").setup()
end, end,
}) })
Util.very_lazy() Util.very_lazy()
end end
return M return M

View File

@ -2,69 +2,69 @@ local M = {}
---@param opts? LazyConfig ---@param opts? LazyConfig
function M.setup(opts) function M.setup(opts)
--FIXME: preload() --FIXME: preload()
local Util = require("lazy.util") local Util = require("lazy.util")
local Config = require("lazy.config") local Config = require("lazy.config")
local Plugin = require("lazy.plugin") local Plugin = require("lazy.plugin")
Util.track("lazy_setup") Util.track("lazy_setup")
Util.track("lazy_config") Util.track("lazy_config")
Config.setup(opts) Config.setup(opts)
Util.track() Util.track()
Util.track("plugin_normalize") Util.track("plugin_normalize")
Plugin.normalize(Config.options.plugins) Plugin.normalize(Config.options.plugins)
if not Config.plugins.lazy then if not Config.plugins.lazy then
Plugin.plugin({ Plugin.plugin({
"folke/lazy.nvim", "folke/lazy.nvim",
opt = false, opt = false,
}) })
end end
Util.track() Util.track()
Util.track("plugin_process") Util.track("plugin_process")
Plugin.process() Plugin.process()
Util.track() Util.track()
Util.track("lazy_install") Util.track("lazy_install")
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
if not plugin.installed then if not plugin.installed then
-- require("lazy.manager").install({ -- require("lazy.manager").install({
-- wait = true, -- wait = true,
-- }) -- })
break break
end end
end end
Util.track() Util.track()
Util.track("loader_setup") Util.track("loader_setup")
local Loader = require("lazy.loader") local Loader = require("lazy.loader")
Loader.setup() Loader.setup()
Util.track() Util.track()
Loader.init_plugins() Loader.init_plugins()
Util.track() -- end setup Util.track() -- end setup
vim.cmd("do User LazyDone") vim.cmd("do User LazyDone")
end end
function M.stats() function M.stats()
local ret = { local ret = {
count = 0, count = 0,
loaded = 0, loaded = 0,
} }
for _, plugin in pairs(require("lazy.config").plugins) do for _, plugin in pairs(require("lazy.config").plugins) do
ret.count = ret.count + 1 ret.count = ret.count + 1
if plugin.loaded then if plugin.loaded then
ret.loaded = ret.loaded + 1 ret.loaded = ret.loaded + 1
end end
end end
return ret return ret
end end
return M return M

View File

@ -6,18 +6,18 @@ local M = {}
---@alias LoaderType "event"|"ft"|"module"|"keys"|"cmd" ---@alias LoaderType "event"|"ft"|"module"|"keys"|"cmd"
---@type LoaderType[] ---@type LoaderType[]
M.types = { M.types = {
"event", "event",
"ft", "ft",
"module", "module",
"keys", "keys",
"cmd", "cmd",
} }
---@type table<LoaderType, table<string, string[]>> ---@type table<LoaderType, table<string, string[]>>
M.loaders = {} M.loaders = {}
for _, type in ipairs(M.types) do for _, type in ipairs(M.types) do
M.loaders[type] = {} M.loaders[type] = {}
end end
---@type LazyPlugin[] ---@type LazyPlugin[]
@ -25,203 +25,203 @@ M.need_setup = {}
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M.add(plugin) function M.add(plugin)
if plugin.init or plugin.opt == false and plugin.config then if plugin.init or plugin.opt == false and plugin.config then
table.insert(M.need_setup, plugin) table.insert(M.need_setup, plugin)
end end
for _, loader_type in ipairs(M.types) do for _, loader_type in ipairs(M.types) do
---@type string[]|string ---@type string[]|string
local loaders = plugin[loader_type] local loaders = plugin[loader_type]
if loaders then if loaders then
loaders = type(loaders) == "table" and loaders or { loaders } loaders = type(loaders) == "table" and loaders or { loaders }
---@cast loaders string[] ---@cast loaders string[]
for _, loader in ipairs(loaders) do for _, loader in ipairs(loaders) do
if not M.loaders[loader_type][loader] then if not M.loaders[loader_type][loader] then
M.loaders[loader_type][loader] = {} M.loaders[loader_type][loader] = {}
end end
table.insert(M.loaders[loader_type][loader], plugin.name) table.insert(M.loaders[loader_type][loader], plugin.name)
end end
end end
end end
end end
function M.setup() function M.setup()
local group = vim.api.nvim_create_augroup("lazy_loader", { local group = vim.api.nvim_create_augroup("lazy_loader", {
clear = true, clear = true,
}) })
-- modules -- modules
table.insert(package.loaders, 2, M.module) table.insert(package.loaders, 2, M.module)
-- events -- events
Util.track("loader_events") Util.track("loader_events")
for event, plugins in pairs(M.loaders.event) do for event, plugins in pairs(M.loaders.event) do
if event == "VimEnter" and vim.v.vim_did_enter == 1 then if event == "VimEnter" and vim.v.vim_did_enter == 1 then
M.load(plugins) M.load(plugins)
else else
local user_event = event:match("User (.*)") local user_event = event:match("User (.*)")
vim.api.nvim_create_autocmd(user_event and "User" or event, { vim.api.nvim_create_autocmd(user_event and "User" or event, {
once = true, once = true,
group = group, group = group,
pattern = user_event, pattern = user_event,
callback = function() callback = function()
Util.track("event: " .. (user_event or event)) Util.track("event: " .. (user_event or event))
M.load(plugins) M.load(plugins)
Util.track() Util.track()
end, end,
}) })
end end
end end
Util.track() Util.track()
-- filetypes -- filetypes
Util.track("loader_filetypes") Util.track("loader_filetypes")
for ft, plugins in pairs(M.loaders.ft) do for ft, plugins in pairs(M.loaders.ft) do
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd("FileType", {
once = true, once = true,
pattern = ft, pattern = ft,
group = group, group = group,
callback = function() callback = function()
Util.track("filetype: " .. ft) Util.track("filetype: " .. ft)
M.load(plugins) M.load(plugins)
Util.track() Util.track()
end, end,
}) })
end end
Util.track() Util.track()
-- keys -- keys
Util.track("loader_keys") Util.track("loader_keys")
for keys, plugins in pairs(M.loaders.keys or {}) do for keys, plugins in pairs(M.loaders.keys or {}) do
vim.keymap.set("n", keys, function() vim.keymap.set("n", keys, function()
vim.keymap.del("n", keys) vim.keymap.del("n", keys)
Util.track("keys: " .. keys) Util.track("keys: " .. keys)
M.load(plugins) M.load(plugins)
vim.api.nvim_input(keys) vim.api.nvim_input(keys)
Util.track() Util.track()
end) end)
end end
Util.track() Util.track()
-- commands -- commands
Util.track("loader_commands") Util.track("loader_commands")
for cmd, plugins in pairs(M.loaders.cmd or {}) do for cmd, plugins in pairs(M.loaders.cmd or {}) do
vim.api.nvim_create_user_command(cmd, function(event) vim.api.nvim_create_user_command(cmd, function(event)
vim.api.nvim_del_user_command(cmd) vim.api.nvim_del_user_command(cmd)
Util.track("cmd: " .. cmd) Util.track("cmd: " .. cmd)
M.load(plugins) M.load(plugins)
vim.cmd( vim.cmd(
("%s %s%s%s %s"):format( ("%s %s%s%s %s"):format(
event.mods or "", event.mods or "",
event.line1 == event.line2 and "" or event.line1 .. "," .. event.line2, event.line1 == event.line2 and "" or event.line1 .. "," .. event.line2,
cmd, cmd,
event.bang and "!" or "", event.bang and "!" or "",
event.args event.args
) )
) )
Util.track() Util.track()
end, { end, {
bang = true, bang = true,
nargs = "*", nargs = "*",
}) })
end end
Util.track() Util.track()
end end
function M.init_plugins() function M.init_plugins()
Util.track("loader_plugin_init") Util.track("loader_plugin_init")
for _, plugin in ipairs(M.need_setup) do for _, plugin in ipairs(M.need_setup) do
if plugin.init then if plugin.init then
Util.track(plugin.name) Util.track(plugin.name)
plugin.init() plugin.init()
Util.track() Util.track()
end end
if plugin.opt == false then if plugin.opt == false then
M.load(plugin) M.load(plugin)
end end
end end
Util.track() Util.track()
end end
---@param modname string ---@param modname string
function M.module(modname) function M.module(modname)
local idx = modname:find(".", 1, true) or #modname + 1 local idx = modname:find(".", 1, true) or #modname + 1
while idx do while idx do
local name = modname:sub(1, idx - 1) local name = modname:sub(1, idx - 1)
local plugins = M.loaders.module[name] local plugins = M.loaders.module[name]
if plugins then if plugins then
M.load(plugins) M.load(plugins)
M.loaders.module[name] = nil M.loaders.module[name] = nil
end end
idx = modname:find(".", idx + 1, true) idx = modname:find(".", idx + 1, true)
end end
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
local mod = package.loaded[modname] local mod = package.loaded[modname]
if type(mod) == "table" then if type(mod) == "table" then
return function() return function()
return mod return mod
end end
end end
end end
---@param plugins string|LazyPlugin|string[]|LazyPlugin[] ---@param plugins string|LazyPlugin|string[]|LazyPlugin[]
function M.load(plugins) function M.load(plugins)
if type(plugins) == "string" or plugins.name then if type(plugins) == "string" or plugins.name then
plugins = { plugins } plugins = { plugins }
end end
for _, plugin in ipairs(plugins) do for _, plugin in ipairs(plugins) do
if type(plugin) == "string" then if type(plugin) == "string" then
plugin = Config.plugins[plugin] plugin = Config.plugins[plugin]
end end
---@cast plugin LazyPlugin ---@cast plugin LazyPlugin
if not plugin.loaded then if not plugin.loaded then
plugin.loaded = true plugin.loaded = true
Util.track(plugin.name) Util.track(plugin.name)
M.packadd(plugin) M.packadd(plugin)
if plugin.requires then if plugin.requires then
M.load(plugin.requires) M.load(plugin.requires)
end end
if plugin.config then if plugin.config then
plugin.config() plugin.config()
end end
Util.track() Util.track()
end end
end end
end end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M.packadd(plugin) function M.packadd(plugin)
if plugin.opt then if plugin.opt then
vim.cmd.packadd(plugin.pack) vim.cmd.packadd(plugin.pack)
M.source_plugin_files(plugin, true) M.source_plugin_files(plugin, true)
else else
vim.opt.runtimepath:append(plugin.dir) vim.opt.runtimepath:append(plugin.dir)
M.source_plugin_files(plugin) M.source_plugin_files(plugin)
M.source_plugin_files(plugin, true) M.source_plugin_files(plugin, true)
end end
end end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
---@param after? boolean ---@param after? boolean
function M.source_plugin_files(plugin, after) function M.source_plugin_files(plugin, after)
local pattern = (after and "/after" or "") .. ("/plugin/" .. "**/*.\\(vim\\|lua\\)") local pattern = (after and "/after" or "") .. ("/plugin/" .. "**/*.\\(vim\\|lua\\)")
local _, entries = pcall(vim.fn.glob, plugin.dir .. "/" .. pattern, false, true) local _, entries = pcall(vim.fn.glob, plugin.dir .. "/" .. pattern, false, true)
if entries then if entries then
---@cast entries string[] ---@cast entries string[]
for _, file in ipairs(entries) do for _, file in ipairs(entries) do
vim.cmd("silent source " .. file) vim.cmd("silent source " .. file)
end end
end end
end end
return M return M

View File

@ -11,127 +11,127 @@ local M = {}
---@param opts? ManagerOpts ---@param opts? ManagerOpts
---@param filter? fun(plugin:LazyPlugin):boolean? ---@param filter? fun(plugin:LazyPlugin):boolean?
function M.run(operation, opts, filter) function M.run(operation, opts, filter)
opts = opts or {} opts = opts or {}
local plugins = opts.plugins or Config.plugins local plugins = opts.plugins or Config.plugins
if opts.clear then if opts.clear then
M.clear() M.clear()
end end
if opts.show then if opts.show then
require("lazy.view").show() require("lazy.view").show()
end end
---@type Runner ---@type Runner
local runner = Runner.new() local runner = Runner.new()
local on_done = function() local on_done = function()
vim.cmd([[do User LazyRender]]) vim.cmd([[do User LazyRender]])
end end
-- install missing plugins -- install missing plugins
for _, plugin in pairs(plugins) do for _, plugin in pairs(plugins) do
if filter == nil or filter(plugin) then if filter == nil or filter(plugin) then
runner:add(Task.new(plugin, operation)) runner:add(Task.new(plugin, operation))
end end
end end
if runner:is_empty() then if runner:is_empty() then
return on_done() return on_done()
end end
vim.cmd([[do User LazyRender]]) vim.cmd([[do User LazyRender]])
-- wait for install to finish -- wait for install to finish
runner:wait(function() runner:wait(function()
-- check if we need to do any post-install hooks -- check if we need to do any post-install hooks
for _, plugin in ipairs(runner:plugins()) do for _, plugin in ipairs(runner:plugins()) do
if plugin.dirty and (plugin.opt == false or plugin.run) then if plugin.dirty and (plugin.opt == false or plugin.run) then
runner:add(Task.new(plugin, "run")) runner:add(Task.new(plugin, "run"))
end end
plugin.dirty = false plugin.dirty = false
end end
-- wait for post-install to finish -- wait for post-install to finish
runner:wait(on_done) runner:wait(on_done)
end) end)
-- auto show if there are tasks running -- auto show if there are tasks running
if opts.show == nil then if opts.show == nil then
require("lazy.view").show() require("lazy.view").show()
end end
if opts.wait then if opts.wait then
runner:wait() runner:wait()
end end
return runner return runner
end end
---@param opts? ManagerOpts ---@param opts? ManagerOpts
function M.install(opts) function M.install(opts)
---@param plugin LazyPlugin ---@param plugin LazyPlugin
M.run("install", opts, function(plugin) M.run("install", opts, function(plugin)
return plugin.uri and not plugin.installed return plugin.uri and not plugin.installed
end) end)
end end
---@param opts? ManagerOpts ---@param opts? ManagerOpts
function M.update(opts) function M.update(opts)
---@param plugin LazyPlugin ---@param plugin LazyPlugin
M.run("update", opts, function(plugin) M.run("update", opts, function(plugin)
return plugin.uri and plugin.installed return plugin.uri and plugin.installed
end) end)
end end
---@param opts? ManagerOpts ---@param opts? ManagerOpts
function M.clean(opts) function M.clean(opts)
M.check_clean() M.check_clean()
---@param plugin LazyPlugin ---@param plugin LazyPlugin
M.run("clean", opts, function(plugin) M.run("clean", opts, function(plugin)
return plugin.uri == nil and plugin.installed return plugin.uri == nil and plugin.installed
end) end)
end end
function M.check_clean() function M.check_clean()
---@type table<string,boolean> ---@type table<string,boolean>
local packs = {} local packs = {}
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
packs[plugin.pack] = plugin.opt packs[plugin.pack] = plugin.opt
end end
for _, opt in ipairs({ "opt", "start" }) do for _, opt in ipairs({ "opt", "start" }) do
local site = Config.options.package_path .. "/" .. opt local site = Config.options.package_path .. "/" .. opt
if Util.file_exists(site) then if Util.file_exists(site) then
for _, pack in ipairs(Util.scandir(site)) do for _, pack in ipairs(Util.scandir(site)) do
if packs[pack.name] ~= (opt == "opt") then if packs[pack.name] ~= (opt == "opt") then
---@type LazyPlugin ---@type LazyPlugin
local plugin = { local plugin = {
name = pack.name, name = pack.name,
pack = pack.name, pack = pack.name,
dir = site .. "/" .. pack.name, dir = site .. "/" .. pack.name,
opt = opt == "opt", opt = opt == "opt",
installed = true, installed = true,
} }
Config.plugins[pack.name] = plugin Config.plugins[pack.name] = plugin
end end
end end
end end
end end
end end
function M.clear() function M.clear()
for pack, plugin in pairs(Config.plugins) do for pack, plugin in pairs(Config.plugins) do
-- clear finished tasks -- clear finished tasks
if plugin.tasks then if plugin.tasks then
---@param task Task ---@param task Task
plugin.tasks = vim.tbl_filter(function(task) plugin.tasks = vim.tbl_filter(function(task)
return task.running return task.running
end, plugin.tasks) end, plugin.tasks)
end end
-- clear cleaned plugins -- clear cleaned plugins
if plugin.uri == nil and not plugin.installed then if plugin.uri == nil and not plugin.installed then
Config.plugins[pack] = nil Config.plugins[pack] = nil
end end
end end
end end
return M return M

View File

@ -29,113 +29,113 @@ local M = {}
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M.plugin(plugin) function M.plugin(plugin)
local pkg = plugin[1] local pkg = plugin[1]
if type(pkg) ~= "string" then if type(pkg) ~= "string" then
Util.error("Invalid plugin spec " .. vim.inspect(plugin)) Util.error("Invalid plugin spec " .. vim.inspect(plugin))
end end
plugin.uri = plugin.uri or ("https://github.com/" .. pkg .. ".git") plugin.uri = plugin.uri or ("https://github.com/" .. pkg .. ".git")
plugin.pack = plugin.pack or plugin.name plugin.pack = plugin.pack or plugin.name
if not plugin.name then if not plugin.name then
local name = plugin.uri:gsub("%.git$", ""):match("/([^/]+)$") local name = plugin.uri:gsub("%.git$", ""):match("/([^/]+)$")
plugin.pack = name plugin.pack = name
if not name then if not name then
name = pkg:gsub("%W+", "_") name = pkg:gsub("%W+", "_")
end end
name = name:gsub("[%.%-]n?vim$", "") name = name:gsub("[%.%-]n?vim$", "")
name = name:gsub("^n?vim[%-%.]", "") name = name:gsub("^n?vim[%-%.]", "")
name = name:gsub("%.lua$", "") name = name:gsub("%.lua$", "")
name = name:gsub("%.", "_") name = name:gsub("%.", "_")
plugin.name = name:lower() plugin.name = name:lower()
end end
if Config.plugins[plugin.name] then if Config.plugins[plugin.name] then
for k, v in ipairs(plugin) do for k, v in ipairs(plugin) do
Config.plugins[plugin.name][k] = v Config.plugins[plugin.name][k] = v
end end
return Config.plugins[plugin.name] return Config.plugins[plugin.name]
else else
Config.plugins[plugin.name] = plugin Config.plugins[plugin.name] = plugin
end end
return plugin return plugin
end end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M.process_local(plugin) function M.process_local(plugin)
for _, pattern in ipairs(Config.options.plugins_local.patterns) do for _, pattern in ipairs(Config.options.plugins_local.patterns) do
if plugin[1]:find(pattern) then if plugin[1]:find(pattern) then
plugin.uri = Config.options.plugins_local.path .. "/" .. plugin.pack plugin.uri = Config.options.plugins_local.path .. "/" .. plugin.pack
return return
end end
end end
end end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M.process_config(plugin) function M.process_config(plugin)
local name = plugin.name local name = plugin.name
local modname = Config.options.plugins_config.module .. "." .. name local modname = Config.options.plugins_config.module .. "." .. name
local file = Config.has_config[modname] local file = Config.has_config[modname]
if file then if file then
-- use dofile and then add to modules. Since we know where to look, this is faster -- use dofile and then add to modules. Since we know where to look, this is faster
local ok, spec = pcall(Cache.load, file, modname) local ok, spec = pcall(Cache.load, file, modname)
if ok then if ok then
-- add to loaded modules -- add to loaded modules
if spec.requires then if spec.requires then
spec.requires = M.normalize(spec.requires) spec.requires = M.normalize(spec.requires)
end end
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
for k, v in pairs(spec) do for k, v in pairs(spec) do
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
plugin[k] = v plugin[k] = v
end end
M.plugin(plugin) M.plugin(plugin)
else else
Util.error("Failed to load plugin config for " .. name .. "\n" .. spec) Util.error("Failed to load plugin config for " .. name .. "\n" .. spec)
end end
end end
end end
---@param spec table ---@param spec table
---@param results? LazyPlugin[] ---@param results? LazyPlugin[]
function M.normalize(spec, results) function M.normalize(spec, results)
results = results or {} results = results or {}
if type(spec) == "string" then if type(spec) == "string" then
table.insert(results, M.plugin({ spec }).name) table.insert(results, M.plugin({ spec }).name)
elseif #spec > 1 or vim.tbl_islist(spec) then elseif #spec > 1 or vim.tbl_islist(spec) then
---@cast spec LazyPlugin[] ---@cast spec LazyPlugin[]
for _, s in ipairs(spec) do for _, s in ipairs(spec) do
M.normalize(s, results) M.normalize(s, results)
end end
else else
---@cast spec LazyPlugin ---@cast spec LazyPlugin
spec = M.plugin(spec) spec = M.plugin(spec)
if spec.requires then if spec.requires then
-- TODO: fix multiple requires in different packages -- TODO: fix multiple requires in different packages
spec.requires = M.normalize(spec.requires) spec.requires = M.normalize(spec.requires)
end end
table.insert(results, spec.name) table.insert(results, spec.name)
end end
return results return results
end end
function M.process() function M.process()
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
M.process_config(plugin) M.process_config(plugin)
end end
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
if plugin.opt == nil then if plugin.opt == nil then
plugin.opt = Config.options.opt plugin.opt = Config.options.opt
end end
plugin.dir = Config.options.package_path .. "/" .. (plugin.opt and "opt" or "start") .. "/" .. plugin.pack plugin.dir = Config.options.package_path .. "/" .. (plugin.opt and "opt" or "start") .. "/" .. plugin.pack
plugin.installed = Util.file_exists(plugin.dir) plugin.installed = Util.file_exists(plugin.dir)
M.process_local(plugin) M.process_local(plugin)
Loader.add(plugin) Loader.add(plugin)
end end
end end
return M return M

View File

@ -8,179 +8,179 @@ M._profiles = { { name = "lazy" } }
---@param name string? ---@param name string?
---@param time number? ---@param time number?
function M.track(name, time) function M.track(name, time)
if name then if name then
local entry = { local entry = {
name = name, name = name,
time = time or M.time(), time = time or M.time(),
} }
table.insert(M._profiles[#M._profiles], entry) table.insert(M._profiles[#M._profiles], entry)
if not time then if not time then
table.insert(M._profiles, entry) table.insert(M._profiles, entry)
end end
else else
local entry = table.remove(M._profiles) local entry = table.remove(M._profiles)
entry.time = M.time() - entry.time entry.time = M.time() - entry.time
end end
end end
function M.time() function M.time()
return vim.loop.hrtime() / 1000000 return vim.loop.hrtime() / 1000000
end end
function M.file_exists(file) function M.file_exists(file)
return vim.loop.fs_stat(file) ~= nil return vim.loop.fs_stat(file) ~= nil
end end
---@param ms number ---@param ms number
---@param fn fun() ---@param fn fun()
function M.throttle(ms, fn) function M.throttle(ms, fn)
local timer = vim.loop.new_timer() local timer = vim.loop.new_timer()
local running = false local running = false
local first = true local first = true
return function() return function()
if not running then if not running then
if first then if first then
fn() fn()
first = false first = false
end end
timer:start(ms, 0, function() timer:start(ms, 0, function()
running = false running = false
vim.schedule(fn) vim.schedule(fn)
end) end)
running = true running = true
end end
end end
end end
function M.very_lazy() function M.very_lazy()
local function _load() local function _load()
vim.defer_fn(function() vim.defer_fn(function()
vim.cmd("do User VeryLazy") vim.cmd("do User VeryLazy")
end, 100) end, 100)
end end
vim.api.nvim_create_autocmd("User", { vim.api.nvim_create_autocmd("User", {
pattern = "LazyDone", pattern = "LazyDone",
once = true, once = true,
callback = function() callback = function()
if vim.v.vim_did_enter == 1 then if vim.v.vim_did_enter == 1 then
_load() _load()
else else
vim.api.nvim_create_autocmd("VimEnter", { vim.api.nvim_create_autocmd("VimEnter", {
once = true, once = true,
callback = function() callback = function()
_load() _load()
end, end,
}) })
end end
end, end,
}) })
end end
---@param path string ---@param path string
function M.scandir(path) function M.scandir(path)
---@type {name: string, path: string, type: "file"|"directory"|"link"}[] ---@type {name: string, path: string, type: "file"|"directory"|"link"}[]
local ret = {} local ret = {}
local dir = vim.loop.fs_opendir(path, nil, 100) local dir = vim.loop.fs_opendir(path, nil, 100)
if dir then if dir then
---@type {name: string, path: string, type: "file"|"directory"|"link"}[] ---@type {name: string, path: string, type: "file"|"directory"|"link"}[]
local entries = vim.loop.fs_readdir(dir) local entries = vim.loop.fs_readdir(dir)
while entries do while entries do
for _, entry in ipairs(entries) do for _, entry in ipairs(entries) do
entry.path = path .. "/" .. entry.name entry.path = path .. "/" .. entry.name
table.insert(ret, entry) table.insert(ret, entry)
end end
entries = vim.loop.fs_readdir(dir) entries = vim.loop.fs_readdir(dir)
end end
vim.loop.fs_closedir(dir) vim.loop.fs_closedir(dir)
end end
return ret return ret
end end
function M.profile() function M.profile()
local lines = { "# Profile" } local lines = { "# Profile" }
---@param entry LazyProfile ---@param entry LazyProfile
local function _profile(entry, depth) local function _profile(entry, depth)
if entry.time < 0.5 then if entry.time < 0.5 then
-- Nothing -- Nothing
end end
table.insert( table.insert(
lines, lines,
(" "):rep(depth) .. "- " .. entry.name .. ": **" .. math.floor((entry.time or 0) * 100) / 100 .. "ms**" (" "):rep(depth) .. "- " .. entry.name .. ": **" .. math.floor((entry.time or 0) * 100) / 100 .. "ms**"
) )
for _, child in ipairs(entry) do for _, child in ipairs(entry) do
_profile(child, depth + 1) _profile(child, depth + 1)
end end
end end
for _, entry in ipairs(M._profiles[1]) do for _, entry in ipairs(M._profiles[1]) do
_profile(entry, 1) _profile(entry, 1)
end end
M.markdown(lines) M.markdown(lines)
end end
---@return string? ---@return string?
function M.head(file) function M.head(file)
local f = io.open(file) local f = io.open(file)
if f then if f then
f:close() f:close()
return f:read() return f:read()
end end
end end
---@return {branch: string, hash:string}? ---@return {branch: string, hash:string}?
function M.git_info(dir) function M.git_info(dir)
if M.head(dir .. "/.git/HEAD") then if M.head(dir .. "/.git/HEAD") then
---@type string, string ---@type string, string
local ref, branch = slot1:match("ref: (refs/heads/(.*))") local ref, branch = slot1:match("ref: (refs/heads/(.*))")
if ref then if ref then
return { return {
branch = branch, branch = branch,
hash = M.head(dir .. "/.git/" .. ref), hash = M.head(dir .. "/.git/" .. ref),
} }
end end
end end
end end
---@param msg string|string[] ---@param msg string|string[]
---@param opts? table ---@param opts? table
function M.markdown(msg, opts) function M.markdown(msg, opts)
if type(msg) == "table" then if type(msg) == "table" then
msg = table.concat(msg, "\n") or msg msg = table.concat(msg, "\n") or msg
end end
vim.notify( vim.notify(
msg, msg,
vim.log.levels.INFO, vim.log.levels.INFO,
vim.tbl_deep_extend("force", { vim.tbl_deep_extend("force", {
title = "lazy.nvim", title = "lazy.nvim",
on_open = function(win) on_open = function(win)
vim.wo[win].conceallevel = 3 vim.wo[win].conceallevel = 3
vim.wo[win].concealcursor = "n" vim.wo[win].concealcursor = "n"
vim.wo[win].spell = false vim.wo[win].spell = false
vim.treesitter.start(vim.api.nvim_win_get_buf(win), "markdown") vim.treesitter.start(vim.api.nvim_win_get_buf(win), "markdown")
end, end,
}, opts or {}) }, opts or {})
) )
end end
function M.error(msg) function M.error(msg)
vim.notify(msg, vim.log.levels.ERROR, { vim.notify(msg, vim.log.levels.ERROR, {
title = "lazy.nvim", title = "lazy.nvim",
}) })
end end
return M return M

View File

@ -14,123 +14,123 @@ local Text = require("lazy.view.text")
local M = setmetatable({}, { __index = Text }) local M = setmetatable({}, { __index = Text })
function M.render_plugins(buf, win, padding) function M.render_plugins(buf, win, padding)
local self = setmetatable({}, { __index = M }) local self = setmetatable({}, { __index = M })
self._lines = {} self._lines = {}
self.buf = buf self.buf = buf
self.win = win self.win = win
self.padding = padding self.padding = padding
Manager.check_clean() Manager.check_clean()
self.plugins = vim.tbl_values(Config.plugins) self.plugins = vim.tbl_values(Config.plugins)
table.sort(self.plugins, function(a, b) table.sort(self.plugins, function(a, b)
return a.name < b.name return a.name < b.name
end) end)
self.progress = { self.progress = {
total = 0, total = 0,
done = 0, done = 0,
} }
for _, plugin in ipairs(self.plugins) do for _, plugin in ipairs(self.plugins) do
if plugin.tasks then if plugin.tasks then
for _, task in ipairs(plugin.tasks) do for _, task in ipairs(plugin.tasks) do
self.progress.total = self.progress.total + 1 self.progress.total = self.progress.total + 1
if not task.running then if not task.running then
self.progress.done = self.progress.done + 1 self.progress.done = self.progress.done + 1
end end
end end
end end
end end
self:title() self:title()
for _, section in ipairs(Sections) do for _, section in ipairs(Sections) do
self:section(section) self:section(section)
end end
self:trim() self:trim()
self:render(buf, padding) self:render(buf, padding)
return self return self
end end
function M:title() function M:title()
self:append("Lazy", "LazyH1") self:append("Lazy", "LazyH1")
if self.progress.done < self.progress.total then if self.progress.done < self.progress.total then
self:append(" (" .. self.progress.done .. "/" .. self.progress.total .. ")", "LazyMuted"):nl() self:append(" (" .. self.progress.done .. "/" .. self.progress.total .. ")", "LazyMuted"):nl()
self:progressbar() self:progressbar()
else else
self:append(" (" .. #self.plugins .. ")", "LazyMuted"):nl() self:append(" (" .. #self.plugins .. ")", "LazyMuted"):nl()
end end
self:nl() self:nl()
end end
function M:progressbar() function M:progressbar()
local width = vim.api.nvim_win_get_width(self.win) - 2 * self.padding local width = vim.api.nvim_win_get_width(self.win) - 2 * self.padding
local done = math.floor((self.progress.done / self.progress.total) * width + 0.5) local done = math.floor((self.progress.done / self.progress.total) * width + 0.5)
self:append("", { self:append("", {
virt_text_win_col = self.padding, virt_text_win_col = self.padding,
virt_text = { { string.rep("", done), "LazyProgressDone" } }, virt_text = { { string.rep("", done), "LazyProgressDone" } },
}) })
self:append("", { self:append("", {
virt_text_win_col = done + self.padding, virt_text_win_col = done + self.padding,
virt_text = { { string.rep("", width - done), "LazyProgressTodo" } }, virt_text = { { string.rep("", width - done), "LazyProgressTodo" } },
}) })
end end
---@param section LazySection ---@param section LazySection
function M:section(section) function M:section(section)
---@type LazyPlugin[] ---@type LazyPlugin[]
local section_plugins = {} local section_plugins = {}
---@param plugin LazyPlugin ---@param plugin LazyPlugin
self.plugins = vim.tbl_filter(function(plugin) self.plugins = vim.tbl_filter(function(plugin)
if section.filter(plugin) then if section.filter(plugin) then
table.insert(section_plugins, plugin) table.insert(section_plugins, plugin)
return false return false
end end
return true return true
end, self.plugins) end, self.plugins)
local count = #section_plugins local count = #section_plugins
if count > 0 then if count > 0 then
self:append(section.title, "LazyH2"):append(" (" .. count .. ")", "LazyMuted"):nl() self:append(section.title, "LazyH2"):append(" (" .. count .. ")", "LazyMuted"):nl()
for _, plugin in ipairs(section_plugins) do for _, plugin in ipairs(section_plugins) do
self:plugin(plugin) self:plugin(plugin)
end end
self:nl() self:nl()
end end
end end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M:plugin(plugin) function M:plugin(plugin)
self:append(" - ", "LazySpecial"):append(plugin.name) self:append(" - ", "LazySpecial"):append(plugin.name)
if plugin.tasks then if plugin.tasks then
for _, task in ipairs(plugin.tasks) do for _, task in ipairs(plugin.tasks) do
if task.running then if task.running then
self:append(" [" .. task.type .. "] ", "Identifier") self:append(" [" .. task.type .. "] ", "Identifier")
self:append(task.status, "LazyMuted") self:append(task.status, "LazyMuted")
elseif task.error then elseif task.error then
local lines = vim.split(vim.trim(task.error), "\n") local lines = vim.split(vim.trim(task.error), "\n")
self:append(" [" .. task.type .. "] ", "Identifier") self:append(" [" .. task.type .. "] ", "Identifier")
for l, line in ipairs(lines) do for l, line in ipairs(lines) do
self:append(line, "LazyError") self:append(line, "LazyError")
if l ~= #lines then if l ~= #lines then
self:nl() self:nl()
end end
end end
end end
end end
end end
self:nl() self:nl()
-- self:details(plugin) -- self:details(plugin)
end end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M:details(plugin) function M:details(plugin)
local git = Util.git_info(plugin.dir) local git = Util.git_info(plugin.dir)
if git then if git then
self:append(git.branch) self:append(git.branch)
end end
self:nl() self:nl()
end end
return M return M

3
stylua.toml Normal file
View File

@ -0,0 +1,3 @@
indent_type = "Spaces"
indent_width = 2
column_width = 120