Skip to content

Commit

Permalink
fix(loader): implemented correct adding to rtp. fix #230, fix #226
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 29, 2022
1 parent c7122d6 commit 3a1a10c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lua/lazy/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ function M.setup(spec, opts)
M.me = Util.norm(vim.fn.fnamemodify(M.me, ":p:h:h:h:h"))
if M.options.performance.rtp.reset then
vim.opt.rtp = {
M.me,
vim.fn.stdpath("config"),
M.me,
vim.env.VIMRUNTIME,
vim.fn.fnamemodify(vim.v.progpath, ":p:h:h") .. "/lib/nvim",
vim.fn.stdpath("config") .. "/after",
Expand Down
38 changes: 32 additions & 6 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function M.startup()
end
Util.track()

-- 3. load plugins from rtp, excluding after
-- 3. load plugins from the original rtp, excluding after
Util.track({ start = "rtp plugins" })
for _, path in ipairs(rtp) do
if not path:find("after/?$") then
Expand Down Expand Up @@ -172,11 +172,7 @@ function M._load(plugin, reason)
Util.track({ plugin = plugin.name, start = reason.start })
Handler.disable(plugin)

vim.opt.runtimepath:prepend(plugin.dir)
local after = plugin.dir .. "/after"
if vim.loop.fs_stat(after) then
vim.opt.runtimepath:append(after)
end
M.add_to_rtp(plugin)

if plugin.dependencies then
Util.try(function()
Expand Down Expand Up @@ -271,6 +267,36 @@ function M.source_runtime(...)
end
end

-- This does the same as runtime.c:add_pack_dir_to_rtp
-- * find first after
-- * find lazy pack path
-- * insert right after lazy pack path or right before first after or at the end
-- * insert after dir right before first after or append to the end
---@param plugin LazyPlugin
function M.add_to_rtp(plugin)
local rtp = vim.api.nvim_get_runtime_file("", true)
local idx_dir, idx_after

for i, path in ipairs(rtp) do
if path == Config.me then
idx_dir = i + 1
elseif not idx_after and path:sub(-6, -1) == "/after" then
idx_after = i + 1 -- +1 to offset the insert of the plugin dir
idx_dir = idx_dir or i
break
end
end

table.insert(rtp, idx_dir or (#rtp + 1), plugin.dir)

local after = plugin.dir .. "/after"
if vim.loop.fs_stat(after) then
table.insert(rtp, idx_after or (#rtp + 1), after)
end

vim.opt.rtp = rtp
end

function M.source(path)
Util.track({ runtime = path })
Util.try(function()
Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function M.bootstrap()
lazypath,
})
end
vim.opt.runtimepath:prepend(lazypath)
vim.opt.rtp:prepend(lazypath)
end

---@return LazyPlugin[]
Expand Down

0 comments on commit 3a1a10c

Please sign in to comment.