Skip to content

Commit 4df73f1

Browse files
committed
feat: keep track what loaded a plugin
1 parent e59dc37 commit 4df73f1

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

lua/lazy/core/loader.lua

+33-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ M.types = {
1515

1616
---@type table<LoaderType, table<string, string[]>>|{init: string[]}
1717
M.loaders = nil
18+
---@type LazyPlugin[]
19+
M.loading = {}
1820

1921
---@param plugin LazyPlugin
2022
function M.add(plugin)
@@ -59,7 +61,7 @@ function M.setup()
5961
Util.track("loader_events")
6062
for event, plugins in pairs(M.loaders.event) do
6163
if event == "VimEnter" and vim.v.vim_did_enter == 1 then
62-
M.load(plugins)
64+
M.load(plugins, { event = event })
6365
else
6466
local user_event = event:match("User (.*)")
6567
vim.api.nvim_create_autocmd(user_event and "User" or event, {
@@ -68,7 +70,7 @@ function M.setup()
6870
pattern = user_event,
6971
callback = function()
7072
Util.track("event: " .. (user_event or event))
71-
M.load(plugins)
73+
M.load(plugins, { event = event })
7274
Util.track()
7375
end,
7476
})
@@ -85,7 +87,7 @@ function M.setup()
8587
group = group,
8688
callback = function()
8789
Util.track("filetype: " .. ft)
88-
M.load(plugins)
90+
M.load(plugins, { ft = ft })
8991
Util.track()
9092
end,
9193
})
@@ -98,7 +100,7 @@ function M.setup()
98100
vim.keymap.set("n", keys, function()
99101
vim.keymap.del("n", keys)
100102
Util.track("keys: " .. keys)
101-
M.load(plugins)
103+
M.load(plugins, { keys = keys })
102104
vim.api.nvim_input(keys)
103105
Util.track()
104106
end)
@@ -140,7 +142,7 @@ function M.init_plugins()
140142
Util.track()
141143
end
142144
if plugin.opt == false then
143-
M.load(plugin)
145+
M.load(plugin, { package = "start" })
144146
end
145147
end
146148
Util.track()
@@ -154,7 +156,21 @@ function M.module(modname)
154156
local name = modname:sub(1, idx - 1)
155157
local plugins = M.loaders.module[name]
156158
if plugins then
157-
M.load(plugins)
159+
local reason = { require = modname }
160+
if #M.loading == 0 then
161+
local f = 3
162+
while not reason.source do
163+
local info = debug.getinfo(f, "S")
164+
f = f + 1
165+
if not info then
166+
break
167+
end
168+
if info.what ~= "C" then
169+
reason.source = info.source:sub(2)
170+
end
171+
end
172+
end
173+
M.load(plugins, reason)
158174
-- M.loaders.module[name] = nil
159175
end
160176
idx = modname:find(".", idx + 1, true)
@@ -170,7 +186,8 @@ function M.module(modname)
170186
end
171187

172188
---@param plugins string|LazyPlugin|string[]|LazyPlugin[]
173-
function M.load(plugins)
189+
---@param reason {[string]:string}
190+
function M.load(plugins, reason)
174191
if type(plugins) == "string" or plugins.name then
175192
---@diagnostic disable-next-line: assign-type-mismatch
176193
plugins = { plugins }
@@ -183,20 +200,26 @@ function M.load(plugins)
183200
end
184201

185202
if not plugin.loaded then
186-
plugin.loaded = true
203+
plugin.loaded = vim.deepcopy(reason or {})
204+
if #M.loading > 0 then
205+
plugin.loaded.plugin = M.loading[#M.loading].name
206+
end
207+
208+
table.insert(M.loading, plugin)
187209

188210
Util.track(plugin.name)
189211
M.packadd(plugin)
190212

191213
if plugin.requires then
192-
M.load(plugin.requires)
214+
M.load(plugin.requires, {})
193215
end
194216

195217
if plugin.config then
196218
plugin.config()
197219
end
198220

199-
Util.track()
221+
plugin.loaded.time = Util.track().time
222+
table.remove(M.loading)
200223
vim.schedule(function()
201224
vim.cmd("do User LazyRender")
202225
end)

lua/lazy/plugin.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ local M = {}
2121
---@field module? string|string[]
2222
---@field keys? string|string[]
2323
---@field requires? string[]
24-
---@field loaded? boolean
24+
---@field loaded? {[string]:string, time:number}
2525
---@field installed? boolean
2626
---@field run? string|fun()
2727
---@field tasks? LazyTask[]
2828
---@field dirty? boolean
29+
---@field updated? {from:string, to:string}
2930

3031
---@param plugin LazyPlugin
3132
function M.plugin(plugin)

0 commit comments

Comments
 (0)