From 2a7b0047dd25f543b147b692fe100e1b2d88ffb2 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Thu, 22 Dec 2022 22:41:44 +0100 Subject: [PATCH] feat(spec): `config` can be `true` or a `table` that will be passed to `require("plugin").setup(config)` --- README.md | 16 +++++++++++++++- lua/lazy/core/loader.lua | 36 +++++++++++++++++++++++++++++++++++- lua/lazy/core/util.lua | 5 +++++ lua/lazy/example.lua | 14 ++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a8201de..491b48b 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ require("lazy").setup({ | **enabled** | `boolean?` or `fun():boolean` | When `false`, or if the `function` returns false, then this plugin will not be used | | **dependencies** | `LazySpec[]` | A list of plugin specs that should be loaded when the plugin loads. Dependencies are always lazy-loaded unless specified otherwise | | **init** | `fun(LazyPlugin)` | `init` functions are always executed during startup | -| **config** | `fun(LazyPlugin)` | `config` is executed when the plugin loads | +| **config** | `fun(LazyPlugin)` or `true` or `table` | `config` is executed when the plugin loads. You can also set to `true` or pass a `table`, that will be passed to `require("plugin").setup(opts)` | | **build** | `fun(LazyPlugin)` | `build` is executed when a plugin is installed or updated | | **branch** | `string?` | Branch of the repository | | **tag** | `string?` | Tag of the repository | @@ -202,6 +202,20 @@ return { end, }, + -- the above could also be written as + { + "nvim-neorg/neorg", + ft = "norg", + config = true, -- run require("norg").setup() + }, + + -- or set custom config + { + "nvim-neorg/neorg", + ft = "norg", + config = { foo = "bar" }, -- run require("norg").setup({foo = "bar"}) + }, + { "dstein64/vim-startuptime", -- lazy-load on a command diff --git a/lua/lazy/core/loader.lua b/lua/lazy/core/loader.lua index 2da7f97..481c02b 100644 --- a/lua/lazy/core/loader.lua +++ b/lua/lazy/core/loader.lua @@ -142,7 +142,7 @@ function M.load(plugins, reason) M.packadd(plugin.dir) if plugin.config then - Util.try(plugin.config, "Failed to run `config` for " .. plugin.name) + M.config(plugin) end plugin._.loaded.time = Util.track().time @@ -154,6 +154,40 @@ function M.load(plugins, reason) end end +--- runs plugin config +---@param plugin LazyPlugin +function M.config(plugin) + local fn + if type(plugin.config) == "function" then + fn = plugin.config + else + local normname = Util.normname(plugin.name) + ---@type table + local mods = {} + Util.ls(plugin.dir .. "/lua", function(_, modname) + modname = modname:gsub("%.lua$", "") + mods[modname] = modname + local modnorm = Util.normname(modname) + -- if we found an exact match, then use that + if modnorm == normname then + mods = { modname } + return false + end + end) + mods = vim.tbl_values(mods) + if #mods == 1 then + fn = function() + require(mods[1]).setup(plugin.config == true and {} or plugin.config) + end + else + return Util.error( + "Lua module not found for config of " .. plugin.name .. ". Please use a `config()` function instead" + ) + end + end + Util.try(fn, "Failed to run `config` for " .. plugin.name) +end + ---@param path string function M.packadd(path) M.source_runtime(path, "plugin") diff --git a/lua/lazy/core/util.lua b/lua/lazy/core/util.lua index 4f79800..3fe7f91 100644 --- a/lua/lazy/core/util.lua +++ b/lua/lazy/core/util.lua @@ -27,6 +27,11 @@ function M.track(data, time) end end +---@param name string +function M.normname(name) + return name:lower():gsub("^n?vim%-", ""):gsub("%.n?vim$", ""):gsub("%.lua", ""):gsub("[^a-z]+", "") +end + function M.norm(path) if path:sub(1, 1) == "~" then local home = vim.loop.os_homedir() diff --git a/lua/lazy/example.lua b/lua/lazy/example.lua index cd99e16..e9c2c74 100644 --- a/lua/lazy/example.lua +++ b/lua/lazy/example.lua @@ -16,6 +16,20 @@ return { end, }, + -- the above could also be written as + { + "nvim-neorg/neorg", + ft = "norg", + config = true, -- run require("norg").setup() + }, + + -- or set custom config + { + "nvim-neorg/neorg", + ft = "norg", + config = { foo = "bar" }, -- run require("norg").setup({foo = "bar"}) + }, + { "dstein64/vim-startuptime", -- lazy-load on a command