feat!: all plugins are now opt. Plugin.opt => Plugin.lazy

This commit is contained in:
Folke Lemaitre 2022-12-01 11:06:44 +01:00
parent 5e0662727d
commit 5134e797f3
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
7 changed files with 57 additions and 67 deletions

View file

@ -6,7 +6,7 @@ local M = {}
M.defaults = {
plugins = "config.plugins",
defaults = {
opt = false, -- should plugins default to "opt" or "start"
lazy = false, -- should plugins be loaded at startup?
version = nil,
-- version = "*", -- enable this to try installing the latest stable versions of plugins
},

View file

@ -6,6 +6,11 @@ local M = {}
---@type LazyPlugin[]
M.loading = {}
function M.setup()
local Handler = require("lazy.core.handler")
Handler.setup()
end
function M.init_plugins()
Util.track("plugin_init")
for _, plugin in pairs(Config.plugins) do
@ -14,7 +19,7 @@ function M.init_plugins()
Util.try(plugin.init, "Failed to run `init` for **" .. plugin.name .. "**")
Util.track()
end
if plugin.opt == false then
if plugin.lazy == false then
M.load(plugin, { start = "start" })
end
end
@ -24,8 +29,7 @@ end
---@class Loader
---@param plugins string|LazyPlugin|string[]|LazyPlugin[]
---@param reason {[string]:string}
---@param opts? {load_start: boolean}
function M.load(plugins, reason, opts)
function M.load(plugins, reason)
---@diagnostic disable-next-line: cast-local-type
plugins = type(plugins) == "string" or plugins.name and { plugins } or plugins
---@cast plugins (string|LazyPlugin)[]
@ -47,7 +51,7 @@ function M.load(plugins, reason, opts)
table.insert(M.loading, plugin)
Util.track({ plugin = plugin.name, start = reason.start })
M.packadd(plugin, opts and opts.load_start)
M.packadd(plugin)
if plugin.dependencies then
M.load(plugin.dependencies, {})
@ -67,15 +71,9 @@ function M.load(plugins, reason, opts)
end
---@param plugin LazyPlugin
function M.packadd(plugin, load_start)
if plugin.opt then
vim.cmd.packadd(plugin.name)
M.source_runtime(plugin, "/after/plugin")
elseif load_start then
vim.opt.runtimepath:append(plugin.dir)
M.source_runtime(plugin, "/plugin")
M.source_runtime(plugin, "/after/plugin")
end
function M.packadd(plugin)
vim.cmd.packadd(plugin.name)
M.source_runtime(plugin, "/after/plugin")
end
---@param plugin LazyPlugin

View file

@ -70,7 +70,6 @@ end
function M.save_cache()
local f = assert(uv.fs_open(cache_path, "w", 438))
vim.loop.fs_ftruncate(f, 0)
for modname, entry in pairs(M.cache) do
if entry.used then
entry.modname = modname

View file

@ -34,7 +34,7 @@ local M = {}
---@field dir string
---@field dep? boolean True if this plugin is only in the spec as a dependency
---@field enabled? boolean|(fun():boolean)
---@field opt? boolean
---@field lazy? boolean
---@field dependencies? string[]
---@field _ LazyPluginState
@ -145,52 +145,46 @@ function Spec:merge(old, new)
end
function M.update_state()
---@type table<"opt"|"start", table<string,FileType>>
local installed = { opt = {}, start = {} }
for opt, packs in pairs(installed) do
Util.ls(Config.options.packpath .. "/" .. opt, function(_, name, type)
if type == "directory" or type == "link" then
packs[name] = type
end
end)
end
---@type table<string,FileType>
local installed = {}
Util.ls(Config.options.packpath .. "/opt", function(_, name, type)
if type == "directory" or type == "link" then
installed[name] = type
end
end)
for _, plugin in pairs(Config.plugins) do
plugin._ = plugin._ or {}
if plugin.opt == nil then
local opt = plugin.dep
or Config.options.defaults.opt
if plugin.lazy == nil then
local lazy = plugin.dep
or Config.options.defaults.lazy
or plugin.module
or plugin.event
or plugin.keys
or plugin.ft
or plugin.cmd
plugin.opt = opt and true or false
plugin.lazy = lazy and true or false
end
local opt = plugin.opt and "opt" or "start"
plugin.dir = Config.options.packpath .. "/" .. opt .. "/" .. plugin.name
plugin.dir = Config.options.packpath .. "/opt/" .. plugin.name
plugin._.is_local = plugin.uri:sub(1, 4) ~= "http" and plugin.uri:sub(1, 3) ~= "git"
plugin._.is_symlink = installed[opt][plugin.name] == "link"
plugin._.installed = installed[opt][plugin.name] ~= nil
plugin._.is_symlink = installed[plugin.name] == "link"
plugin._.installed = installed[plugin.name] ~= nil
if plugin._.is_local == plugin._.is_symlink then
installed[opt][plugin.name] = nil
installed[plugin.name] = nil
end
end
Config.to_clean = {}
for opt, packs in pairs(installed) do
for pack, dir_type in pairs(packs) do
table.insert(Config.to_clean, {
name = pack,
dir = Config.options.packpath .. "/" .. opt .. "/" .. pack,
opt = opt == "opt",
_ = {
installed = true,
is_symlink = dir_type == "link",
is_local = dir_type == "link",
},
})
end
for pack, dir_type in pairs(installed) do
table.insert(Config.to_clean, {
name = pack,
dir = Config.options.packpath .. "/opt/" .. pack,
_ = {
installed = true,
is_symlink = dir_type == "link",
is_local = dir_type == "link",
},
})
end
end
@ -214,7 +208,7 @@ function M.load()
Util.track("spec")
local spec = M.spec()
if not spec.plugins["lazy.nvim"] then
spec:add({ "folke/lazy.nvim", opt = false })
spec:add({ "folke/lazy.nvim", lazy = false })
end
Config.plugins = spec.plugins
Util.track()

View file

@ -7,7 +7,6 @@ function M.setup(opts)
local Util = require("lazy.core.util")
local Config = require("lazy.core.config")
local Loader = require("lazy.core.loader")
local Handler = require("lazy.core.handler")
local Plugin = require("lazy.core.plugin")
Util.track("module", vim.loop.hrtime() - module_start)
@ -32,8 +31,8 @@ function M.setup(opts)
Util.track()
end
Util.track("handlers")
Handler.setup()
Util.track("loader")
Loader.setup()
Util.track()
local lazy_delta = vim.loop.hrtime() - module_start

View file

@ -6,10 +6,10 @@ local M = {}
M.build = {
skip = function(plugin)
return not (plugin._.dirty and (plugin.opt == false or plugin.build))
return not (plugin._.dirty and plugin.build)
end,
run = function(self)
Loader.load(self.plugin, { task = "run" }, { load_start = true })
Loader.load(self.plugin, { task = "build" })
local build = self.plugin.build
if build then