@@ -679,7 +679,7 @@ function Plugin:load(opts)
679679 self :call_setup ()
680680
681681 self .status = STATUS .LOADED
682- pcall (api .nvim_del_augroup_by_name , ' strive_ ' .. self .plugin_name )
682+ pcall (api .nvim_del_autocmd , self .group_id )
683683
684684 local deps_count = # self .dependencies
685685 -- Load dependencies in parallel
@@ -723,61 +723,39 @@ function Plugin:load(opts)
723723 return true
724724end
725725
726- local event_plugins = {}
727-
728726-- Set up lazy loading on specific events
729727function Plugin :on (events )
730728 self .is_lazy = true
731729 self .events = type (events ) ~= ' table' and { events } or events
732730
733- -- Create autocmds for each event
731+ -- Create a single augroup for this plugin
732+ self .group_id = api .nvim_create_augroup (' strive_' .. self .plugin_name , { clear = true })
733+
734+ -- Create autocmds for each event within this group
734735 for _ , event in ipairs (self .events ) do
735736 local pattern
736737 if event :find (' %s' ) then
737738 local t = vim .split (event , ' %s' )
738739 event , pattern = t [1 ], t [2 ]
739740 end
740741
741- -- Create key for event patterns
742- local key = event .. (pattern and (' _' .. pattern ) or ' ' )
743-
744- event_plugins [key ] = event_plugins [key ] or {
745- id = nil ,
746- plugins = {},
747- }
748- local t = event_plugins [key ]
749- table.insert (t .plugins , self )
750-
751- if not t .id then
752- t .id = api .nvim_create_augroup (' strive_' .. key , { clear = true })
753- api .nvim_create_autocmd (event , {
754- group = t .id ,
755- pattern = pattern ,
756- once = true ,
757- callback = function (args )
758- -- Load all plugins for this event at once
759- local any_loaded = false
760- for _ , plugin in ipairs (event_plugins [key ].plugins ) do
761- if not plugin .loaded and plugin :load () then
762- any_loaded = true
763- end
764- end
765- t = nil
766-
767- -- Only re-emit the event once after all plugins are loaded
768- if any_loaded then
769- -- Avoid deep nesting by scheduling
770- vim .schedule (function ()
771- api .nvim_exec_autocmds (event , {
772- buffer = args .buf ,
773- modeline = false ,
774- data = args .data ,
775- })
776- end )
777- end
778- end ,
779- })
780- end
742+ api .nvim_create_autocmd (event , {
743+ group = self .group_id ,
744+ pattern = pattern ,
745+ once = true ,
746+ callback = function (args )
747+ if not self .loaded and self :load () then
748+ -- Re-emit the event once after plugin is loaded
749+ vim .schedule (function ()
750+ api .nvim_exec_autocmds (event , {
751+ buffer = args .buf ,
752+ modeline = false ,
753+ data = args .data ,
754+ })
755+ end )
756+ end
757+ end ,
758+ })
781759 end
782760
783761 return self
0 commit comments