Skip to content

Commit

Permalink
feat!: lazy now handles the full startup sequence (`vim.go.loadplugin…
Browse files Browse the repository at this point in the history
…s=false`)
  • Loading branch information
folke committed Dec 14, 2022
1 parent ad0b4ca commit ec2f432
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 49 deletions.
53 changes: 31 additions & 22 deletions lua/lazy/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,24 @@ M.defaults = {
-- version = "*", -- enable this to try installing the latest stable versions of plugins
},
lockfile = vim.fn.stdpath("config") .. "/lazy-lock.json", -- lockfile generated after running update.
concurrency = nil, -- set to a number to limit the maximum amount of concurrent tasks
concurrency = nil, ---@type number limit the maximum amount of concurrent tasks
git = {
-- defaults for `Lazy log`
-- log = { "-10" }, -- last 10 commits
log = { "--since=1 days ago" }, -- commits from the last 3 days
timeout = 120, -- processes taking over 2 minutes will be killed
-- defaults for the `Lazy log` command
-- log = { "-10" }, -- show the last 10 commits
log = { "--since=1 days ago" }, -- show commits from the last 3 days
timeout = 120, -- kill processes that take more than 2 minutes
url_format = "https://github.com/%s.git",
},
-- Any plugin spec that contains one of the patterns will use your
-- local repo in the projects folder instead of fetchig it from github
-- Mostly useful for plugin developers.
dev = {
path = vim.fn.expand("~/projects"), -- the path where you store your projects
---@type string[]
-- directory where you store your local plugin projects
path = vim.fn.expand("~/projects"),
---@type string[] plugins that match these patterns will use your local versions instead of being fetched from GitHub
patterns = {}, -- For example {"folke"}
},
install = {
-- install missing plugins on startup. This doesn't increase startup time.
missing = true,
-- try to load one of the colorschemes in this list when starting an install during startup
-- the first colorscheme that is found will be loaded
-- try to load one of these colorschemes when starting an installation during startup
colorscheme = { "habamax" },
},
ui = {
Expand All @@ -53,17 +50,30 @@ M.defaults = {
throttle = 20, -- how frequently should the ui process render events
},
checker = {
-- lazy can automatically check for updates
-- automcatilly check for plugin updates
enabled = false,
concurrency = nil, ---@type number? set to 1 to very slowly check for updates
notify = true, -- get a notification if new updates are found
frequency = 3600, -- every hour
concurrency = nil, ---@type number? set to 1 to check for updates very slowly
notify = true, -- get a notification when new updates are found
frequency = 3600, -- check for updates every hour
},
performance = {
---@type LazyCacheConfig
cache = nil,
reset_packpath = true, -- packpath will be reset to nothing. This will improver startup time.
reset_rtp = true, -- the runtime path will be reset to $VIMRUNTIME and your config directory
reset_packpath = true, -- reset the package path to improve startup time
rtp = {
reset = true, -- reset the runtime path to $VIMRUNTIME and your config directory
---@type string[] list any plugins you want to disable here
disabled_plugins = {
-- "gzip",
-- "matchit",
-- "matchparen",
-- "netrwPlugin",
-- "tarPlugin",
-- "tohtml",
-- "tutor",
-- "zipPlugin",
},
},
},
debug = false,
}
Expand Down Expand Up @@ -93,14 +103,13 @@ function M.setup(spec, opts)
if M.options.performance.reset_packpath then
vim.go.packpath = ""
end
if M.options.performance.reset_rtp then
if M.options.performance.rtp.reset then
local me = debug.getinfo(1, "S").source:sub(2)
me = vim.fn.fnamemodify(me, ":p:h:h:h:h")
vim.opt.rtp = {
"$VIMRUNTIME",
vim.fn.stdpath("config"),
me,
vim.fn.stdpath("config") .. "/after",
vim.env.VIMRUNTIME,
vim.fn.stdpath("config"),
}
end

Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/core/handler/ft.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end
---@param plugin LazyPlugin
---@param value string
function M:_add(plugin, value)
Loader.ftdetect(plugin)
Loader.ftdetect(plugin.dir)
Event._add(self, plugin, value)
end

Expand Down
79 changes: 54 additions & 25 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ local M = {}
---@type LazyPlugin[]
M.loading = {}
M.init_done = false
---@type table<string,true>
M.disabled_rtp_plugins = {}

function M.setup()
-- install missing plugins
Expand All @@ -31,11 +33,38 @@ function M.setup()
Handler.setup()
Util.track()

for _, file in ipairs(Config.options.performance.rtp.disabled_plugins) do
M.disabled_rtp_plugins[file] = true
end

-- autoload opt plugins
table.insert(package.loaders, M.autoload)
end

function M.init_plugins()
-- Startup sequence
-- 1. load any start plugins and do init
function M.startup()
Util.track({ start = "startup" })

-- load plugins from rtp
Util.track({ start = "rtp plugins" })
for _, path in ipairs(vim.opt.rtp:get()) do
if not path:find("after/?$") then
M.source_runtime(path, "plugin")
M.ftdetect(path)
end
end
Util.track()

Util.track({ start = "start" })
for _, plugin in pairs(Config.plugins) do
-- load start plugin
if plugin.lazy == false then
M.load(plugin, { start = "start" })
end
end
Util.track()

Util.track({ start = "init" })
for _, plugin in pairs(Config.plugins) do
-- run plugin init
Expand All @@ -44,14 +73,22 @@ function M.init_plugins()
Util.try(plugin.init, "Failed to run `init` for **" .. plugin.name .. "**")
Util.track()
end
end
Util.track()

-- load start plugin
if plugin.lazy == false then
M.load(plugin, { start = "startup" })
-- load after files
Util.track({ start = "after" })
for _, path in ipairs(vim.opt.rtp:get()) do
if path:find("after/?$") then
M.source_runtime(path, "plugin")
else
M.source_runtime(path, "after/plugin")
end
end
Util.track()

M.init_done = true
Util.track()
end

---@class Loader
Expand Down Expand Up @@ -84,21 +121,12 @@ function M.load(plugins, reason)
Handler.disable(plugin)

vim.opt.runtimepath:prepend(plugin.dir)
if not M.init_done then
local after = plugin.dir .. "/after"
-- only add the after directories during startup
-- afterwards we only source the runtime files in after
-- Check if it exists here, to prevent further rtp file checks during startup
if vim.loop.fs_stat(after) then
vim.opt.runtimepath:append(after)
end
end

if plugin.dependencies then
M.load(plugin.dependencies, {})
end

M.packadd(plugin)
M.packadd(plugin.dir)
if plugin.config then
Util.try(plugin.config, "Failed to run `config` for " .. plugin.name)
end
Expand All @@ -112,28 +140,29 @@ function M.load(plugins, reason)
end
end

---@param plugin LazyPlugin
function M.packadd(plugin)
---@param path string
function M.packadd(path)
M.source_runtime(path, "plugin")
M.ftdetect(path)
if M.init_done then
M.source_runtime(plugin.dir, "plugin")
M.ftdetect(plugin)
M.source_runtime(plugin.dir, "after/plugin")
M.source_runtime(path, "after/plugin")
end
end

---@param plugin LazyPlugin
function M.ftdetect(plugin)
---@param path string
function M.ftdetect(path)
vim.cmd("augroup filetypedetect")
M.source_runtime(plugin.dir, "ftdetect")
M.source_runtime(path, "ftdetect")
vim.cmd("augroup END")
end

---@param ... string
function M.source_runtime(...)
local dir = table.concat({ ... }, "/")
Util.walk(dir, function(path, _, t)
local ext = path:sub(-3)
if t == "file" and (ext == "lua" or ext == "vim") then
Util.walk(dir, function(path, name, t)
local ext = name:sub(-3)
name = name:sub(1, -5)
if t == "file" and (ext == "lua" or ext == "vim") and not M.disabled_rtp_plugins[name] then
Util.track({ runtime = path })
vim.cmd("silent source " .. path)
Util.track()
Expand Down
4 changes: 3 additions & 1 deletion lua/lazy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ function M.setup(spec, opts)
vim.notify("Re-sourcing your config is not supported with lazy.nvim", vim.log.levels.WARN, { title = "lazy.nvim" })
return
end
vim.go.loadplugins = false

vim.g.lazy_did_setup = true
local start = vim.loop.hrtime()

Expand Down Expand Up @@ -42,7 +44,7 @@ function M.setup(spec, opts)
end

-- load plugins with lazy=false or Plugin.init
Loader.init_plugins()
Loader.startup()

-- all done!
vim.cmd("do User LazyDone")
Expand Down

0 comments on commit ec2f432

Please sign in to comment.