Skip to content

Commit 2357f86

Browse files
committed
feat: support arguments for commands after completion
1 parent 13b9e88 commit 2357f86

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

lua/opencode/api.lua

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,16 @@ function M.run_user_command(name, args)
380380
M.open_input()
381381

382382
ui.render_output(true)
383-
state.api_client:send_command(state.active_session.id, {
384-
command = name,
385-
arguments = table.concat(args or {}, ' '),
386-
})
383+
state.api_client
384+
:send_command(state.active_session.id, {
385+
command = name,
386+
arguments = table.concat(args or {}, ' '),
387+
})
388+
:and_then(function()
389+
vim.schedule(function()
390+
require('opencode.history').write('/' .. name .. ' ' .. table.concat(args or {}, ' '))
391+
end)
392+
end)
387393
end
388394

389395
--- Compacts the current session by removing unnecessary data.
@@ -986,10 +992,11 @@ function M.get_slash_commands()
986992

987993
local user_commands = require('opencode.config_file').get_user_commands()
988994
if user_commands then
989-
for name, _ in pairs(user_commands) do
995+
for name, cfg in pairs(user_commands) do
990996
table.insert(commands, {
991997
slash_cmd = '/' .. name,
992998
desc = 'Run user command: ' .. name,
999+
args = cfg.template and cfg.template:match('$ARGUMENTS') ~= nil,
9931000
fn = function(args)
9941001
M.run_user_command(name, args)
9951002
end,

lua/opencode/types.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,5 @@
479479
---@class OpencodeSlashCommand
480480
---@field slash_cmd string The command trigger (e.g., "/help")
481481
---@field desc string|nil Description of the command
482-
---@field fn fun(...:string):nil Function to execute the command
482+
---@field fn fun(args:string[]):nil Function to execute the command
483+
---@field args boolean Whether the command accepts arguments

lua/opencode/ui/completion/commands.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local function get_available_commands()
1111
description = cmd_info.desc,
1212
documentation = 'Opencode command: ' .. cmd_info.slash_cmd,
1313
command_key = key,
14+
args = cmd_info.args,
1415
fn = cmd_info.fn,
1516
})
1617
end
@@ -44,15 +45,16 @@ local command_source = {
4445

4546
if context.input == '' or name_lower:find(input_lower, 1, true) or desc_lower:find(input_lower, 1, true) then
4647
local item = {
47-
label = command.name,
48+
label = command.name .. (command.args and ' *' or ''),
4849
kind = 'command',
4950
detail = command.description,
50-
documentation = command.documentation,
51+
documentation = command.documentation .. (command.args and '\n\n* This command takes arguments.' or ''),
5152
insert_text = command.name,
5253
source_name = 'commands',
5354
data = {
5455
name = command.name,
5556
fn = command.fn,
57+
args = command.args,
5658
},
5759
}
5860

@@ -68,6 +70,11 @@ local command_source = {
6870
on_complete = function(item)
6971
if item.kind == 'command' then
7072
if item.data.fn then
73+
if item.data.args then
74+
require('opencode.ui.input_window').set_content(item.insert_text .. ' ')
75+
vim.api.nvim_win_set_cursor(0, { 1, #item.insert_text + 1 })
76+
return
77+
end
7178
item.data.fn()
7279
require('opencode.ui.input_window').set_content('')
7380
else

lua/opencode/ui/input_window.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,34 @@ function M.handle_submit()
5858
modeline = false,
5959
})
6060

61+
if input_content == '' then
62+
return
63+
end
64+
65+
if input_content:match('^/') then
66+
M._execute_slash_command(input_content)
67+
return
68+
end
69+
6170
require('opencode.core').send_message(input_content)
6271
end
6372

73+
M._execute_slash_command = function(command)
74+
local slash_commands = require('opencode.config_file').get_user_commands()
75+
local cmd = command:sub(2):match('^%s*(.-)%s*$')
76+
if cmd == '' then
77+
return
78+
end
79+
local parts = vim.split(cmd, ' ')
80+
local command_cfg = slash_commands[parts[1]]
81+
82+
if command_cfg then
83+
require('opencode.api').run_user_command(parts[1], vim.list_slice(parts, 2))
84+
else
85+
vim.notify('Unknown command: ' .. cmd, vim.log.levels.WARN)
86+
end
87+
end
88+
6489
function M.setup(windows)
6590
vim.api.nvim_set_option_value('winhighlight', config.ui.window_highlight, { win = windows.input_win })
6691
vim.api.nvim_set_option_value('wrap', config.ui.input.text.wrap, { win = windows.input_win })

0 commit comments

Comments
 (0)