Skip to content

Commit

Permalink
feat!: all plugins are now opt. Plugin.opt => Plugin.lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 1, 2022
1 parent 5e06627 commit 5134e79
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 67 deletions.
2 changes: 1 addition & 1 deletion lua/lazy/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
24 changes: 11 additions & 13 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)[]
Expand All @@ -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, {})
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion lua/lazy/core/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 27 additions & 33 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions lua/lazy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/lazy/manage/task/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions tests/core/plugin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end)

describe("plugin spec opt", function()
it("handles dependencies", function()
Config.options.defaults.opt = false
Config.options.defaults.lazy = false
local tests = {
{ "foo/bar", dependencies = { "foo/dep1", "foo/dep2" } },
{ "foo/bar", dependencies = { { "foo/dep1" }, "foo/dep2" } },
Expand All @@ -43,53 +43,53 @@ describe("plugin spec opt", function()
assert(vim.tbl_count(spec.plugins) == 3)
assert(#spec.plugins.bar.dependencies == 2)
assert(spec.plugins.bar.dep ~= true)
assert(spec.plugins.bar.opt == false)
assert(spec.plugins.bar.lazy == false)
assert(spec.plugins.dep1.dep == true)
assert(spec.plugins.dep1.opt == true)
assert(spec.plugins.dep1.lazy == true)
assert(spec.plugins.dep2.dep == true)
assert(spec.plugins.dep2.opt == true)
assert(spec.plugins.dep2.lazy == true)
end
end)

it("handles opt from dep", function()
Config.options.defaults.opt = false
Config.options.defaults.lazy = false
local spec = Plugin.Spec.new({ "foo/dep1", { "foo/bar", dependencies = { "foo/dep1", "foo/dep2" } } })
Config.plugins = spec.plugins
Plugin.update_state()
assert.same(3, vim.tbl_count(spec.plugins))
assert(spec.plugins.bar.dep ~= true)
assert(spec.plugins.bar.opt == false)
assert(spec.plugins.bar.lazy == false)
assert(spec.plugins.dep2.dep == true)
assert(spec.plugins.dep2.opt == true)
assert(spec.plugins.dep2.lazy == true)
assert(spec.plugins.dep1.dep ~= true)
assert(spec.plugins.dep1.opt == false)
assert(spec.plugins.dep1.lazy == false)
end)

it("handles defaults opt", function()
do
Config.options.defaults.opt = true
Config.options.defaults.lazy = true
local spec = Plugin.Spec.new({ "foo/bar" })
Config.plugins = spec.plugins
Plugin.update_state()
assert(spec.plugins.bar.opt == true)
assert(spec.plugins.bar.lazy == true)
end
do
Config.options.defaults.opt = false
Config.options.defaults.lazy = false
local spec = Plugin.Spec.new({ "foo/bar" })
Config.plugins = spec.plugins
Plugin.update_state()
assert(spec.plugins.bar.opt == false)
assert(spec.plugins.bar.lazy == false)
end
end)

it("handles opt from dep", function()
Config.options.defaults.opt = false
Config.options.defaults.lazy = false
local spec = Plugin.Spec.new({ "foo/bar", module = "foo" })
Config.plugins = spec.plugins
Plugin.update_state()
assert.same(1, vim.tbl_count(spec.plugins))
assert(spec.plugins.bar.dep ~= true)
assert(spec.plugins.bar.opt == true)
assert(spec.plugins.bar.lazy == true)
end)

it("merges lazy loaders", function()
Expand Down

0 comments on commit 5134e79

Please sign in to comment.