@@ -80,7 +80,7 @@ function M.startup()
80
80
end
81
81
Util .track ()
82
82
83
- -- 3. load plugins from rtp, excluding after
83
+ -- 3. load plugins from the original rtp, excluding after
84
84
Util .track ({ start = " rtp plugins" })
85
85
for _ , path in ipairs (rtp ) do
86
86
if not path :find (" after/?$" ) then
@@ -172,11 +172,7 @@ function M._load(plugin, reason)
172
172
Util .track ({ plugin = plugin .name , start = reason .start })
173
173
Handler .disable (plugin )
174
174
175
- vim .opt .runtimepath :prepend (plugin .dir )
176
- local after = plugin .dir .. " /after"
177
- if vim .loop .fs_stat (after ) then
178
- vim .opt .runtimepath :append (after )
179
- end
175
+ M .add_to_rtp (plugin )
180
176
181
177
if plugin .dependencies then
182
178
Util .try (function ()
@@ -271,6 +267,36 @@ function M.source_runtime(...)
271
267
end
272
268
end
273
269
270
+ -- This does the same as runtime.c:add_pack_dir_to_rtp
271
+ -- * find first after
272
+ -- * find lazy pack path
273
+ -- * insert right after lazy pack path or right before first after or at the end
274
+ -- * insert after dir right before first after or append to the end
275
+ --- @param plugin LazyPlugin
276
+ function M .add_to_rtp (plugin )
277
+ local rtp = vim .api .nvim_get_runtime_file (" " , true )
278
+ local idx_dir , idx_after
279
+
280
+ for i , path in ipairs (rtp ) do
281
+ if path == Config .me then
282
+ idx_dir = i + 1
283
+ elseif not idx_after and path :sub (- 6 , - 1 ) == " /after" then
284
+ idx_after = i + 1 -- +1 to offset the insert of the plugin dir
285
+ idx_dir = idx_dir or i
286
+ break
287
+ end
288
+ end
289
+
290
+ table.insert (rtp , idx_dir or (# rtp + 1 ), plugin .dir )
291
+
292
+ local after = plugin .dir .. " /after"
293
+ if vim .loop .fs_stat (after ) then
294
+ table.insert (rtp , idx_after or (# rtp + 1 ), after )
295
+ end
296
+
297
+ vim .opt .rtp = rtp
298
+ end
299
+
274
300
function M .source (path )
275
301
Util .track ({ runtime = path })
276
302
Util .try (function ()
0 commit comments