Skip to content

Commit 13b9e88

Browse files
committed
feat: allow arguments passing to user commands
1 parent 4eab392 commit 13b9e88

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lua/opencode/api.lua

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,16 @@ function M.mcp()
373373
ui.render_lines(msg)
374374
end
375375

376-
function M.run_user_command(name)
376+
--- Runs a user-defined command by name.
377+
--- @param name string The name of the user command to run.
378+
--- @param args? string[] Additional arguments to pass to the command.
379+
function M.run_user_command(name, args)
377380
M.open_input()
378381

379382
ui.render_output(true)
380383
state.api_client:send_command(state.active_session.id, {
381384
command = name,
382-
arguments = '',
385+
arguments = table.concat(args or {}, ' '),
383386
})
384387
end
385388

@@ -874,12 +877,13 @@ M.commands = {
874877
name = 'OpencodeRunUserCommand',
875878
desc = 'Run a user-defined Opencode command by name',
876879
fn = function(opts)
877-
local name = opts.args and opts.args:match('^%s*(%S+)')
880+
local parts = vim.split(opts.args or '', '%s+')
881+
local name = parts[1]
878882
if not name or name == '' then
879883
vim.notify('User command name required. Usage: :OpencodeRunUserCommand <name>', vim.log.levels.ERROR)
880884
return
881885
end
882-
M.run_user_command(name)
886+
M.run_user_command(name, vim.list_slice(parts, 2))
883887
end,
884888
args = true,
885889
},
@@ -974,6 +978,7 @@ M.commands = {
974978
},
975979
}
976980

981+
---@return OpencodeSlashCommand[]
977982
function M.get_slash_commands()
978983
local commands = vim.tbl_filter(function(cmd)
979984
return cmd.slash_cmd and cmd.slash_cmd ~= ''
@@ -985,8 +990,8 @@ function M.get_slash_commands()
985990
table.insert(commands, {
986991
slash_cmd = '/' .. name,
987992
desc = 'Run user command: ' .. name,
988-
fn = function()
989-
M.commands.run_user_command.fn({ args = name })
993+
fn = function(args)
994+
M.run_user_command(name, args)
990995
end,
991996
})
992997
end

lua/opencode/types.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,8 @@
475475
---@field model OpencodeAgentModel|nil Optional model configuration
476476
---@field prompt string|nil Optional custom prompt for the agent
477477
---@field temperature number|nil Optional temperature setting
478+
479+
---@class OpencodeSlashCommand
480+
---@field slash_cmd string The command trigger (e.g., "/help")
481+
---@field desc string|nil Description of the command
482+
---@field fn fun(...:string):nil Function to execute the command

0 commit comments

Comments
 (0)