@@ -654,7 +654,7 @@ local function load_opts(opt)
654
654
end
655
655
656
656
-- Load a plugin and its dependencies
657
- function Plugin :load ()
657
+ function Plugin :load (opts )
658
658
if self .loaded then
659
659
return true
660
660
end
@@ -683,7 +683,7 @@ function Plugin:load()
683
683
if isdir (after_path ) then
684
684
vim .opt .rtp :append (after_path )
685
685
end
686
- self :load_scripts ()
686
+ self :load_scripts (( opts and opts . script_cb ) and opts . script_cb or nil )
687
687
elseif self .is_lazy then
688
688
-- For non-local lazy plugins, use packadd
689
689
vim .cmd .packadd (self .plugin_name )
@@ -841,37 +841,51 @@ end
841
841
-- Set up lazy loading for specific commands
842
842
function Plugin :cmd (commands )
843
843
self .is_lazy = true
844
- self .commands = type (commands ) ~= ' table' and { commands } or commands
845
-
846
- for _ , cmd_name in ipairs (self .commands ) do
847
- api .nvim_create_user_command (cmd_name , function (cmd_args )
848
- pcall (api .nvim_del_user_command , cmd_name )
849
- local args = cmd_args .args ~= ' ' and ' ' .. cmd_args .args or ' '
850
- local bang = cmd_args .bang and ' !' or ' '
851
- self :load ()
852
-
853
- if vim .fn .exists (' :' .. cmd_name ) == 2 then
854
- --- @diagnostic disable-next-line : param-type-mismatch
855
- local ok , err = pcall (vim .cmd , cmd_name .. bang .. args )
856
- if not ok then
857
- vim .notify (string.format (' execute %s wrong: %s' , cmd_name , err ), vim .log .levels .ERROR )
858
- end
844
+ self .commands = type (commands ) == ' table' and commands or { commands }
845
+
846
+ -- Helper to execute a given command string
847
+ local function execute (name , bang , args )
848
+ if vim .fn .exists (' :' .. name ) ~= 2 then
849
+ return
850
+ end
851
+ local cmd_str = name .. (bang and ' !' or ' ' ) .. (args or ' ' )
852
+ --- @diagnostic disable-next-line : param-type-mismatch
853
+ local ok , err = pcall (vim .cmd , cmd_str )
854
+ if not ok then
855
+ vim .notify (string.format (' execute %s wrong: %s' , name , err ), vim .log .levels .ERROR )
856
+ end
857
+ end
858
+
859
+ for _ , name in ipairs (self .commands ) do
860
+ api .nvim_create_user_command (name , function (opts )
861
+ -- Remove this command to avoid recursion
862
+ pcall (api .nvim_del_user_command , name )
863
+
864
+ local args = opts .args ~= ' ' and (' ' .. opts .args ) or ' '
865
+ local bang = opts .bang
866
+
867
+ if self .is_local then
868
+ self :load ({
869
+ script_cb = function ()
870
+ execute (name , bang , args )
871
+ end ,
872
+ })
873
+ else
874
+ execute (name , bang , args )
859
875
end
860
876
end , {
861
877
nargs = ' *' ,
862
878
bang = true ,
863
- complete = function (_ , cmd_line , _ )
879
+ complete = function (_ , cmd_line )
864
880
if not self .loaded then
865
881
self :load ()
866
882
end
867
- local ok , result = pcall (function ()
868
- return vim .fn .getcompletion (cmd_line , ' cmdline' )
869
- end )
883
+ local ok , result = pcall (vim .fn .getcompletion , cmd_line , ' cmdline' )
870
884
return ok and result or {}
871
885
end ,
872
886
})
873
887
874
- table.insert (self .user_commands , cmd_name )
888
+ table.insert (self .user_commands , name )
875
889
end
876
890
877
891
return self
0 commit comments