feat(util): inject module

This commit is contained in:
Folke Lemaitre 2023-10-11 22:38:10 +02:00
parent 7bbd48caa0
commit d6bc320f20
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
4 changed files with 33 additions and 14 deletions

View file

@ -210,9 +210,7 @@ function M.init()
-- after installing missing plugins -- after installing missing plugins
M.load("options") M.load("options")
Util.plugin.fix_imports() Util.plugin.setup()
Util.plugin.fix_renames()
Util.plugin.lazy_file()
M.json.load() M.json.load()
end end

View file

@ -10,6 +10,7 @@ local LazyUtil = require("lazy.core.util")
---@field format lazyvim.util.format ---@field format lazyvim.util.format
---@field plugin lazyvim.util.plugin ---@field plugin lazyvim.util.plugin
---@field extras lazyvim.util.extras ---@field extras lazyvim.util.extras
---@field inject lazyvim.util.inject
local M = {} local M = {}
---@type table<string, string|string[]> ---@type table<string, string|string[]>

View file

@ -0,0 +1,20 @@
---@class lazyvim.util.inject
local M = {}
---@generic A: any
---@generic B: any
---@generic C: any
---@generic F: function
---@param fn F|fun(a:A, b:B, c:C)
---@param wrapper fun(a:A, b:B, c:C): boolean?
---@return F
function M.args(fn, wrapper)
return function(...)
if wrapper(...) == false then
return
end
return fn(...)
end
end
return M

View file

@ -21,6 +21,12 @@ M.renames = {
["null-ls.nvim"] = "none-ls.nvim", ["null-ls.nvim"] = "none-ls.nvim",
} }
function M.setup()
M.fix_imports()
M.fix_renames()
M.lazy_file()
end
-- Properly load file based plugins without blocking the UI -- Properly load file based plugins without blocking the UI
function M.lazy_file() function M.lazy_file()
M.use_lazy_file = M.use_lazy_file and vim.fn.argc(-1) > 0 M.use_lazy_file = M.use_lazy_file and vim.fn.argc(-1) > 0
@ -86,23 +92,18 @@ function M.lazy_file()
end end
function M.fix_imports() function M.fix_imports()
local import = Plugin.Spec.import Plugin.Spec.import = Util.inject.args(Plugin.Spec.import, function(_, spec)
---@diagnostic disable-next-line: duplicate-set-field
function Plugin.Spec:import(spec)
local dep = M.deprecated_extras[spec and spec.import] local dep = M.deprecated_extras[spec and spec.import]
if dep then if dep then
dep = dep .. "\n" .. "Please remove the extra to hide this warning." dep = dep .. "\n" .. "Please remove the extra to hide this warning."
Util.warn(dep, { title = "LazyVim", once = true, stacktrace = true, stacklevel = 6 }) Util.warn(dep, { title = "LazyVim", once = true, stacktrace = true, stacklevel = 6 })
return return false
end end
return import(self, spec) end)
end
end end
function M.fix_renames() function M.fix_renames()
local add = Plugin.Spec.add Plugin.Spec.add = Util.inject.args(Plugin.Spec.add, function(self, plugin)
---@diagnostic disable-next-line: duplicate-set-field
Plugin.Spec.add = function(self, plugin, ...)
if type(plugin) == "table" then if type(plugin) == "table" then
if M.renames[plugin[1]] then if M.renames[plugin[1]] then
Util.warn( Util.warn(
@ -116,8 +117,7 @@ function M.fix_renames()
plugin[1] = M.renames[plugin[1]] plugin[1] = M.renames[plugin[1]]
end end
end end
return add(self, plugin, ...) end)
end
end end
return M return M