Skip to content

Commit

Permalink
feat(spec): added import to import other plugin modules
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jan 2, 2023
1 parent 313015f commit 919b7f5
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lua/lazy/core/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ function M.find(modname, opts)
---@type LazyCoreConfig
local Config = package.loaded["lazy.core.config"]
if Config then
for _, plugin in pairs(Config.plugins) do
for _, plugin in pairs(Config.spec.plugins) do
if not (M.indexed[plugin.dir] or plugin._.loaded or plugin.module == false) then
updated = M._index(plugin.dir) or updated
end
Expand Down
24 changes: 13 additions & 11 deletions lua/lazy/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ M.defaults = {
version = nil,
-- version = "*", -- enable this to try installing the latest stable versions of plugins
},
-- leave nil when passing the spec as the first argument to setup()
spec = nil, ---@type LazySpec
lockfile = vim.fn.stdpath("config") .. "/lazy-lock.json", -- lockfile generated after running update.
concurrency = nil, ---@type number limit the maximum amount of concurrent tasks
git = {
Expand Down Expand Up @@ -38,20 +40,21 @@ M.defaults = {
-- The border to use for the UI window. Accepts same border values as |nvim_open_win()|.
border = "none",
icons = {
loaded = "",
not_loaded = "",
cmd = "",
config = "",
event = "",
ft = "",
init = "",
import = "",
keys = "",
lazy = "",
loaded = "",
not_loaded = "",
plugin = "",
runtime = "",
source = "",
start = "",
task = "",
lazy = "",
list = {
"",
"",
Expand Down Expand Up @@ -144,11 +147,8 @@ M.defaults = {

M.ns = vim.api.nvim_create_namespace("lazy")

---@type LazySpec
M.spec = nil

---@type LazySpecLoader
M.parsed = nil
M.spec = nil

---@type table<string, LazyPlugin>
M.plugins = {}
Expand All @@ -167,12 +167,14 @@ M.mapleader = nil

M.headless = #vim.api.nvim_list_uis() == 0

---@param spec LazySpec
---@param opts? LazyConfig
function M.setup(spec, opts)
M.spec = type(spec) == "string" and { import = spec } or spec
function M.setup(opts)
M.options = vim.tbl_deep_extend("force", M.defaults, opts or {})
M.options.performance.cache = require("lazy.core.cache")

if type(M.options.spec) == "string" then
M.options.spec = { import = M.options.spec }
end
M.options.performance.cache = require("lazy.core.cache").config
table.insert(M.options.install.colorscheme, "habamax")

M.options.root = Util.norm(M.options.root)
Expand Down
59 changes: 36 additions & 23 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@ local M = {}
---@class LazySpecLoader
---@field plugins table<string, LazyPlugin>
---@field modules string[]
---@field errors string[]
---@field opts LazySpecOptions
---@field notifs {msg:string, level:number, file?:string}[]
---@field importing? string
local Spec = {}
M.Spec = Spec

---@alias LazySpecOptions {show_errors: boolean}

---@param spec? LazySpec
---@param opts? LazySpecOptions
function Spec.new(spec, opts)
function Spec.new(spec)
local self = setmetatable({}, { __index = Spec })
self.opts = opts or {}
self.plugins = {}
self.modules = {}
self.errors = {}
self.notifs = {}
if spec then
self:normalize(spec)
end
Expand Down Expand Up @@ -83,11 +79,19 @@ function Spec:add(plugin, is_dep)
return self.plugins[plugin.name]
end

function Spec:error(error)
self.errors[#self.errors + 1] = error
if self.opts.show_errors ~= false then
Util.error(error)
end
function Spec:error(msg)
self:notify(msg, vim.log.levels.ERROR)
end

function Spec:warn(msg)
self:notify(msg, vim.log.levels.WARN)
end

---@param msg string
---@param level number
function Spec:notify(msg, level)
self.notifs[#self.notifs + 1] = { msg = msg, level = level, file = self.importing }
Util.notify(msg, level)
end

---@param spec LazySpec|LazySpecImport
Expand Down Expand Up @@ -134,20 +138,34 @@ function Spec:import(spec)
if spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) then
return
end

Cache.indexed_unloaded = false

local imported = 0
Util.lsmod(spec.import, function(modname)
imported = imported + 1
Util.track({ import = modname })
self.importing = modname
-- unload the module so we get a clean slate
---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = nil
Util.try(function()
self:normalize(Cache.require(modname))
self.modules[#self.modules + 1] = modname
self.importing = nil
Util.track()
end, {
msg = "Failed to load `" .. modname .. "`",
on_error = function(msg)
self:error(msg)
self.importing = nil
Util.track()
end,
})
end)
if imported == 0 then
self:error("No specs found for module " .. spec.import)
end
end

---@param old LazyPlugin
Expand Down Expand Up @@ -230,21 +248,16 @@ function M.update_state()
end
end

---@param opts? LazySpecOptions
function M.spec(opts)
return Spec.new(vim.deepcopy(Config.spec), opts)
end

function M.load()
-- load specs
Util.track("spec")
local spec = M.spec()
Config.parsed = spec
Config.spec = Spec.new()
Config.spec:normalize(vim.deepcopy(Config.options.spec))

-- add ourselves
spec:add({ "folke/lazy.nvim" })
Config.spec:add({ "folke/lazy.nvim" })
-- override some lazy props
local lazy = spec.plugins["lazy.nvim"]
local lazy = Config.spec.plugins["lazy.nvim"]
lazy.lazy = true
lazy.dir = Config.me
lazy.config = function()
Expand All @@ -253,7 +266,7 @@ function M.load()
lazy._.loaded = {}

local existing = Config.plugins
Config.plugins = spec.plugins
Config.plugins = Config.spec.plugins
-- copy state. This wont do anything during startup
for name, plugin in pairs(existing) do
if Config.plugins[name] then
Expand Down
16 changes: 10 additions & 6 deletions lua/lazy/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function M.check()
"cond",
"_",
}
local spec = Config.parsed
local spec = Config.spec
for _, plugin in pairs(spec.plugins) do
for key in pairs(plugin) do
if not vim.tbl_contains(valid, key) then
Expand All @@ -58,12 +58,16 @@ function M.check()
end
end
end
if #spec.errors > 0 then
vim.health.report_error("Errors were reported when loading your specs:")
for _, error in ipairs(spec.errors) do
local lines = vim.split(error, "\n")
if #spec.notifs > 0 then
vim.health.report_error("Issues were reported when loading your specs:")
for _, notif in ipairs(spec.notifs) do
local lines = vim.split(notif.msg, "\n")
for _, line in ipairs(lines) do
vim.health.report_error(line)
if notif.level == vim.log.levels.ERROR then
vim.health.report_error(line)
else
vim.health.report_warn(line)
end
end
end
end
Expand Down
14 changes: 11 additions & 3 deletions lua/lazy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
local M = {}
M._start = 0

---@param spec LazySpec Should be a module name to load, or a plugin spec
---@param opts? LazyConfig
---@overload fun(opts: LazyConfig)
---@overload fun(spec:LazySpec, opts: LazyConfig)
function M.setup(spec, opts)
if type(spec) == "table" and spec.spec then
---@cast spec LazyConfig
opts = spec
else
opts = opts or {}
opts.spec = spec
end

M._start = M._start == 0 and vim.loop.hrtime() or M._start
if vim.g.lazy_did_setup then
return vim.notify(
Expand Down Expand Up @@ -38,7 +46,7 @@ function M.setup(spec, opts)

-- load config
Util.track("config")
Config.setup(spec, opts)
Config.setup(opts)
Util.track()

-- load the plugins
Expand Down
4 changes: 2 additions & 2 deletions lua/lazy/manage/reloader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function M.enable()
if M.timer then
M.timer:stop()
end
if #Config.parsed.modules > 0 then
if #Config.spec.modules > 0 then
M.timer = vim.loop.new_timer()
M.root = vim.fn.stdpath("config") .. "/lua"
M.check(true)
Expand Down Expand Up @@ -55,7 +55,7 @@ function M.check(start)
end
end

for _, modname in ipairs(Config.parsed.modules) do
for _, modname in ipairs(Config.spec.modules) do
Util.lsmod(modname, check)
end

Expand Down
1 change: 1 addition & 0 deletions lua/lazy/view/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ M.colors = {
ReasonSource = "Character",
ReasonFt = "Character",
ReasonCmd = "Operator",
ReasonImport = "Identifier",
Button = "CursorLine",
ButtonActive = "Visual",
TaskOutput = "MsgArea", -- task output
Expand Down

0 comments on commit 919b7f5

Please sign in to comment.