( suggestion: string | SuggestionItem, shouldExecute: boolean, commands: Command[], onInputChange: (value: string) => void, setCursorOffset: (offset: number) => void, onSubmit: (value: string, isSubmittingSlashCommand?: boolean) => void, )
| 501 | * Apply selected command to input |
| 502 | */ |
| 503 | export function applyCommandSuggestion( |
| 504 | suggestion: string | SuggestionItem, |
| 505 | shouldExecute: boolean, |
| 506 | commands: Command[], |
| 507 | onInputChange: (value: string) => void, |
| 508 | setCursorOffset: (offset: number) => void, |
| 509 | onSubmit: (value: string, isSubmittingSlashCommand?: boolean) => void, |
| 510 | ): void { |
| 511 | // Extract command name and object from string or SuggestionItem metadata |
| 512 | let commandName: string |
| 513 | let commandObj: Command | undefined |
| 514 | if (typeof suggestion === 'string') { |
| 515 | commandName = suggestion |
| 516 | commandObj = shouldExecute ? getCommand(commandName, commands) : undefined |
| 517 | } else { |
| 518 | if (!isCommandMetadata(suggestion.metadata)) { |
| 519 | return // Invalid suggestion, nothing to apply |
| 520 | } |
| 521 | commandName = getCommandName(suggestion.metadata) |
| 522 | commandObj = suggestion.metadata |
| 523 | } |
| 524 | |
| 525 | // Format the command input with trailing space |
| 526 | const newInput = formatCommand(commandName) |
| 527 | onInputChange(newInput) |
| 528 | setCursorOffset(newInput.length) |
| 529 | |
| 530 | // Execute command if requested and it takes no arguments |
| 531 | if (shouldExecute && commandObj) { |
| 532 | if ( |
| 533 | commandObj.type !== 'prompt' || |
| 534 | (commandObj.argNames ?? []).length === 0 |
| 535 | ) { |
| 536 | onSubmit(newInput, /* isSubmittingSlashCommand */ true) |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | // Helper function at bottom of file per CLAUDE.md |
| 542 | function cleanWord(word: string) { |
no test coverage detected