Skip to content

Commit 83dfb1d

Browse files
committed
fix local cmd load
1 parent 4203cd8 commit 83dfb1d

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

lua/strive/init.lua

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ local function load_opts(opt)
654654
end
655655

656656
-- Load a plugin and its dependencies
657-
function Plugin:load()
657+
function Plugin:load(opts)
658658
if self.loaded then
659659
return true
660660
end
@@ -683,7 +683,7 @@ function Plugin:load()
683683
if isdir(after_path) then
684684
vim.opt.rtp:append(after_path)
685685
end
686-
self:load_scripts()
686+
self:load_scripts((opts and opts.script_cb) and opts.script_cb or nil)
687687
elseif self.is_lazy then
688688
-- For non-local lazy plugins, use packadd
689689
vim.cmd.packadd(self.plugin_name)
@@ -841,37 +841,51 @@ end
841841
-- Set up lazy loading for specific commands
842842
function Plugin:cmd(commands)
843843
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)
859875
end
860876
end, {
861877
nargs = '*',
862878
bang = true,
863-
complete = function(_, cmd_line, _)
879+
complete = function(_, cmd_line)
864880
if not self.loaded then
865881
self:load()
866882
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')
870884
return ok and result or {}
871885
end,
872886
})
873887

874-
table.insert(self.user_commands, cmd_name)
888+
table.insert(self.user_commands, name)
875889
end
876890

877891
return self

0 commit comments

Comments
 (0)