Skip to content

Commit bce0c6e

Browse files
committedJan 4, 2023
perf(spec): more efficient merging of specs and added Plugin._.super
1 parent 6d46a30 commit bce0c6e

File tree

3 files changed

+76
-58
lines changed

3 files changed

+76
-58
lines changed
 

Diff for: ‎lua/lazy/core/plugin.lua

+22-25
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ local Cache = require("lazy.core.cache")
66
---@class LazyCorePlugin
77
local M = {}
88

9+
local list_merge = { "dependencies" }
10+
vim.list_extend(list_merge, vim.tbl_values(Handler.types))
11+
912
---@class LazySpecLoader
1013
---@field plugins table<string, LazyPlugin>
1114
---@field disabled table<string, LazyPlugin>
@@ -99,6 +102,7 @@ function Spec:add(plugin, results, is_dep)
99102
self.plugins[plugin.name] = plugin
100103
return results and table.insert(results, plugin.name)
101104
else
105+
-- FIXME: we should somehow merge when needed
102106
plugin._.kind = "disabled"
103107
self.disabled[plugin.name] = plugin
104108
self.plugins[plugin.name] = nil
@@ -198,36 +202,29 @@ end
198202
---@param new LazyPlugin
199203
---@return LazyPlugin
200204
function Spec:merge(old, new)
201-
local is_dep = old._.dep and new._.dep
202-
203-
---@diagnostic disable-next-line: no-unknown
204-
for k, v in pairs(new) do
205-
if k == "_" then
206-
elseif old[k] ~= nil and old[k] ~= v then
207-
if Handler.types[k] then
208-
local values = type(v) == "string" and { v } or v
209-
vim.list_extend(values, type(old[k]) == "string" and { old[k] } or old[k])
210-
---@diagnostic disable-next-line: no-unknown
211-
old[k] = values
212-
elseif k == "config" or k == "priority" then
213-
old[k] = v
214-
elseif k == "dependencies" then
215-
for _, dep in ipairs(v) do
216-
if not vim.tbl_contains(old[k], dep) then
217-
table.insert(old[k], dep)
218-
end
205+
new._.dep = old._.dep and new._.dep
206+
207+
for _, prop in ipairs(list_merge) do
208+
if new[prop] and old[prop] then
209+
---@type any[]
210+
local props = {}
211+
---@diagnostic disable-next-line: no-unknown
212+
for _, value in ipairs(old[prop]) do
213+
props[#props + 1] = value
214+
end
215+
---@diagnostic disable-next-line: no-unknown
216+
for _, value in ipairs(new[prop]) do
217+
if not vim.tbl_contains(props, value) then
218+
props[#props + 1] = value
219219
end
220-
else
221-
old[k] = v
222-
self:warn("Overwriting key `" .. k .. "`\n" .. vim.inspect({ old = old, new = new }))
223220
end
224-
else
225221
---@diagnostic disable-next-line: no-unknown
226-
old[k] = v
222+
new[prop] = props
227223
end
228224
end
229-
old._.dep = is_dep
230-
return old
225+
new._.super = old
226+
setmetatable(new, { __index = old })
227+
return new
231228
end
232229

233230
function M.update_state()

Diff for: ‎lua/lazy/health.lua

+53-33
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,10 @@ function M.check()
3131
vim.health.report_ok("packer_compiled.lua not found")
3232
end
3333

34-
local valid = {
35-
1,
36-
"name",
37-
"url",
38-
"enabled",
39-
"lazy",
40-
"dev",
41-
"dependencies",
42-
"init",
43-
"config",
44-
"build",
45-
"branch",
46-
"tag",
47-
"commit",
48-
"version",
49-
"module",
50-
"pin",
51-
"cmd",
52-
"event",
53-
"keys",
54-
"ft",
55-
"dir",
56-
"priority",
57-
"cond",
58-
"_",
59-
}
6034
local spec = Config.spec
6135
for _, plugin in pairs(spec.plugins) do
62-
for key in pairs(plugin) do
63-
if not vim.tbl_contains(valid, key) then
64-
if key ~= "module" or type(plugin.module) ~= "boolean" then
65-
vim.health.report_warn("{" .. plugin.name .. "}: unknown key <" .. key .. ">")
66-
end
67-
end
68-
end
36+
M.check_valid(plugin)
37+
M.check_override(plugin)
6938
end
7039
if #spec.notifs > 0 then
7140
vim.health.report_error("Issues were reported when loading your specs:")
@@ -82,4 +51,55 @@ function M.check()
8251
end
8352
end
8453

54+
---@param plugin LazyPlugin
55+
function M.check_valid(plugin)
56+
for key in pairs(plugin) do
57+
if not vim.tbl_contains(M.valid, key) then
58+
if key ~= "module" or type(plugin.module) ~= "boolean" then
59+
vim.health.report_warn("{" .. plugin.name .. "}: unknown key <" .. key .. ">")
60+
end
61+
end
62+
end
63+
end
64+
65+
---@param plugin LazyPlugin
66+
function M.check_override(plugin)
67+
if not plugin._.super then
68+
return
69+
end
70+
71+
for key, value in pairs(plugin._.super) do
72+
if key ~= "_" and plugin[key] and plugin[key] ~= value then
73+
vim.health.report_warn("{" .. plugin.name .. "}: overriding <" .. key .. ">")
74+
end
75+
end
76+
end
77+
78+
M.valid = {
79+
1,
80+
"name",
81+
"url",
82+
"enabled",
83+
"lazy",
84+
"dev",
85+
"dependencies",
86+
"init",
87+
"config",
88+
"build",
89+
"branch",
90+
"tag",
91+
"commit",
92+
"version",
93+
"module",
94+
"pin",
95+
"cmd",
96+
"event",
97+
"keys",
98+
"ft",
99+
"dir",
100+
"priority",
101+
"cond",
102+
"_",
103+
}
104+
85105
return M

Diff for: ‎lua/lazy/types.lua

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
---@field kind? LazyPluginKind
1414
---@field dep? boolean True if this plugin is only in the spec as a dependency
1515
---@field cond? boolean
16+
---@field super? LazyPlugin
1617

1718
---@class LazyPluginHooks
1819
---@field init? fun(LazyPlugin) Will always be run

0 commit comments

Comments
 (0)
Please sign in to comment.