feat: lazy setup with either a plugins module, or a plugins spec

This commit is contained in:
Folke Lemaitre 2022-12-01 13:33:55 +01:00
parent f0894be69d
commit af8b8e128e
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
8 changed files with 54 additions and 53 deletions

View File

@ -26,7 +26,8 @@
- [ ] health checks: check merge conflicts async - [ ] health checks: check merge conflicts async
- [ ] unsupported props or props from other managers - [ ] unsupported props or props from other managers
- [x] rename `run` to `build` - [x] rename `run` to `build`
- [ ] allow setting up plugins through config - [ ] delete lazy keymaps when a plugin loads
- [x] allow setting up plugins through config
- [x] task timeout - [x] task timeout
- [ ] log file - [ ] log file
- [ ] deal with re-sourcing init.lua. Check a global? - [ ] deal with re-sourcing init.lua. Check a global?
@ -67,6 +68,7 @@
- `requires` => `dependencies` - `requires` => `dependencies`
- `as` => `name` - `as` => `name`
- `opt` => `lazy` - `opt` => `lazy`
- `run` => `build`
## 📦 Other Neovim Plugin Managers in Lua ## 📦 Other Neovim Plugin Managers in Lua

View File

@ -4,7 +4,6 @@ local M = {}
---@class LazyConfig ---@class LazyConfig
M.defaults = { M.defaults = {
plugins = "config.plugins",
defaults = { defaults = {
lazy = false, -- should plugins be loaded at startup? lazy = false, -- should plugins be loaded at startup?
version = nil, version = nil,
@ -52,14 +51,11 @@ M.defaults = {
M.ns = vim.api.nvim_create_namespace("lazy") M.ns = vim.api.nvim_create_namespace("lazy")
M.paths = { ---@type string|LazySpec Should be either a string pointing to a module, or a spec
---@type string M.spec = nil
opt = nil,
---@type string ---@type string Opt directory where plugins will be installed
main = nil, M.root = nil
---@type string
plugins = nil,
}
---@type table<string, LazyPlugin> ---@type table<string, LazyPlugin>
M.plugins = {} M.plugins = {}
@ -70,12 +66,12 @@ M.to_clean = {}
---@type LazyConfig ---@type LazyConfig
M.options = {} M.options = {}
---@param spec LazySpec
---@param opts? LazyConfig ---@param opts? LazyConfig
function M.setup(opts) function M.setup(spec, opts)
M.spec = spec
M.options = vim.tbl_deep_extend("force", M.defaults, opts or {}) M.options = vim.tbl_deep_extend("force", M.defaults, opts or {})
M.paths.plugins = vim.fn.stdpath("config") .. "/lua/" .. M.options.plugins:gsub("%.", "/") M.root = M.options.package.path .. "/pack/" .. M.options.package.name .. "/opt"
M.paths.main = M.paths.plugins .. (vim.loop.fs_stat(M.paths.plugins .. ".lua") and ".lua" or "/init.lua")
M.paths.opt = M.options.package.path .. "/pack/" .. M.options.package.name .. "/opt"
if M.options.package.reset then if M.options.package.reset then
vim.go.packpath = M.options.package.path vim.go.packpath = M.options.package.path

View File

@ -127,6 +127,7 @@ function M.handlers.module(grouped)
if plugins then if plugins then
grouped[name] = nil grouped[name] = nil
local reason = { require = modname } local reason = { require = modname }
-- almost never happens, so this does not decrease performance
if #Loader.loading == 0 then if #Loader.loading == 0 then
local f = 3 local f = 3
while not reason.source do while not reason.source do

View File

@ -7,8 +7,24 @@ local M = {}
M.loading = {} M.loading = {}
function M.setup() function M.setup()
-- install missing plugins
if Config.options.install_missing then
Util.track("install")
for _, plugin in pairs(Config.plugins) do
if not plugin._.installed then
vim.cmd("do User LazyInstallPre")
require("lazy.manage").install({ wait = true })
break
end
end
Util.track()
end
-- setup handlers
Util.track("handlers")
local Handler = require("lazy.core.handler") local Handler = require("lazy.core.handler")
Handler.setup() Handler.setup()
Util.track()
end end
function M.init_plugins() function M.init_plugins()

View File

@ -147,7 +147,7 @@ end
function M.update_state() function M.update_state()
---@type table<string,FileType> ---@type table<string,FileType>
local installed = {} local installed = {}
Util.ls(Config.paths.opt, function(_, name, type) Util.ls(Config.root, function(_, name, type)
if type == "directory" or type == "link" then if type == "directory" or type == "link" then
installed[name] = type installed[name] = type
end end
@ -165,7 +165,7 @@ function M.update_state()
or plugin.cmd or plugin.cmd
plugin.lazy = lazy and true or false plugin.lazy = lazy and true or false
end end
plugin.dir = Config.paths.opt .. "/" .. plugin.name plugin.dir = Config.root .. "/" .. plugin.name
plugin._.is_local = plugin.uri:sub(1, 4) ~= "http" and plugin.uri:sub(1, 3) ~= "git" plugin._.is_local = plugin.uri:sub(1, 4) ~= "http" and plugin.uri:sub(1, 3) ~= "git"
plugin._.is_symlink = installed[plugin.name] == "link" plugin._.is_symlink = installed[plugin.name] == "link"
plugin._.installed = installed[plugin.name] ~= nil plugin._.installed = installed[plugin.name] ~= nil
@ -178,7 +178,7 @@ function M.update_state()
for pack, dir_type in pairs(installed) do for pack, dir_type in pairs(installed) do
table.insert(Config.to_clean, { table.insert(Config.to_clean, {
name = pack, name = pack,
dir = Config.paths.opt .. "/" .. pack, dir = Config.root .. "/" .. pack,
_ = { _ = {
installed = true, installed = true,
is_symlink = dir_type == "link", is_symlink = dir_type == "link",
@ -191,15 +191,23 @@ end
function M.spec() function M.spec()
local spec = Spec.new() local spec = Spec.new()
local function _load(name, modpath) if type(Config.spec) == "string" then
local modname = Config.options.plugins .. (name and ("." .. name) or "") -- spec is a module
Util.try(function() local function _load(name, modpath)
spec:normalize(Module.load(modname, modpath)) local modname = Config.spec .. (name and ("." .. name) or "")
end, "Failed to load **" .. modname .. "**") Util.try(function()
end spec:normalize(Module.load(modname, modpath))
end, "Failed to load **" .. modname .. "**")
end
local path_plugins = vim.fn.stdpath("config") .. "/lua/" .. Config.spec:gsub("%.", "/")
local path_main = path_plugins .. (vim.loop.fs_stat(path_plugins .. ".lua") and ".lua" or "/init.lua")
_load(nil, Config.paths.main) _load(nil, path_main)
Util.lsmod(Config.paths.plugins, _load) Util.lsmod(path_plugins, _load)
else
-- spec is a spec
spec:normalize(Config.spec)
end
return spec return spec
end end

View File

@ -40,8 +40,8 @@ function M.try(fn, msg)
end end
if info.what == "Lua" and not info.source:find("lazy.nvim") then if info.what == "Lua" and not info.source:find("lazy.nvim") then
local source = info.source:sub(2) local source = info.source:sub(2)
if source:find(Config.paths.opt, 1, true) == 1 then if source:find(Config.root, 1, true) == 1 then
source = source:sub(#Config.paths.opt + 1) source = source:sub(#Config.root + 1)
end end
source = vim.fn.fnamemodify(source, ":p:~:.") source = vim.fn.fnamemodify(source, ":p:~:.")
local line = " - " .. source .. ":" .. info.currentline local line = " - " .. source .. ":" .. info.currentline

View File

@ -1,7 +1,8 @@
local M = {} local M = {}
---@param spec LazySpec Should be a module name to load, or a plugin spec
---@param opts? LazyConfig ---@param opts? LazyConfig
function M.setup(opts) function M.setup(spec, opts)
local module_start = vim.loop.hrtime() local module_start = vim.loop.hrtime()
require("lazy.core.module").setup() require("lazy.core.module").setup()
local Util = require("lazy.core.util") local Util = require("lazy.core.util")
@ -14,23 +15,11 @@ function M.setup(opts)
Util.track("setup") Util.track("setup")
Util.track("config") Util.track("config")
Config.setup(opts) Config.setup(spec, opts)
Util.track() Util.track()
Plugin.load() Plugin.load()
if Config.options.install_missing then
Util.track("install")
for _, plugin in pairs(Config.plugins) do
if not plugin._.installed then
vim.cmd("do User LazyInstallPre")
require("lazy.manage").install({ wait = true })
break
end
end
Util.track()
end
Util.track("loader") Util.track("loader")
Loader.setup() Loader.setup()
Util.track() Util.track()

View File

@ -193,25 +193,14 @@ function M:reason(reason, opts)
---@type string? ---@type string?
local source = reason.source local source = reason.source
if source then if source then
---@type string?
local modname = source:match("/lua/(.*)%.lua$")
if modname then
modname = modname:gsub("/", ".")
end
local name = source:match("/([^/]-)/lua") local name = source:match("/([^/]-)/lua")
for _, other in pairs(Config.plugins) do for _, other in pairs(Config.plugins) do
if (modname and other.modname == modname) or (name and other.name == name) then if name and other.name == name then
reason.plugin = other.name reason.plugin = other.name
reason.source = nil reason.source = nil
break break
end end
end end
if reason.source then
reason.source = modname or reason.source
if reason.source == "lua" then
reason.source = Config.options.plugins
end
end
end end
local time = " " .. math.floor((reason.time or 0) / 1e6 * 100) / 100 .. "ms" local time = " " .. math.floor((reason.time or 0) / 1e6 * 100) / 100 .. "ms"
if not opts.time_right then if not opts.time_right then