Improvements to suggestions & slash commands (#344)

Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
This commit is contained in:
Miguel Solorio
2025-05-14 16:01:29 -07:00
committed by GitHub
parent 89aa1cad41
commit 416813452e
5 changed files with 46 additions and 25 deletions

View File

@@ -119,20 +119,30 @@ export function useCompletion(
// --- Handle Slash Command Completion ---
if (trimmedQuery.startsWith('/')) {
const partialCommand = trimmedQuery.substring(1);
const commands = slashCommands
.map((cmd) => cmd.name)
.concat(
slashCommands
.map((cmd) => cmd.altName)
.filter((cmd) => cmd !== undefined),
);
const filteredSuggestions = commands
.filter((name) => name.startsWith(partialCommand))
// Filter out ? and any other single character commands
.filter((name) => name.length > 1)
.map((name) => ({ label: name, value: name }))
.sort();
const filteredSuggestions = slashCommands
.filter(
(cmd) =>
cmd.name.startsWith(partialCommand) ||
cmd.altName?.startsWith(partialCommand),
)
// Filter out ? and any other single character commands unless it's the only char
.filter((cmd) => {
const nameMatch = cmd.name.startsWith(partialCommand);
const altNameMatch = cmd.altName?.startsWith(partialCommand);
if (partialCommand.length === 1) {
return nameMatch || altNameMatch; // Allow single char match if query is single char
}
return (
(nameMatch && cmd.name.length > 1) ||
(altNameMatch && cmd.altName && cmd.altName.length > 1)
);
})
.map((cmd) => ({
label: cmd.name, // Always show the main name as label
value: cmd.name, // Value should be the main command name for execution
description: cmd.description,
}))
.sort((a, b) => a.label.localeCompare(b.label));
setSuggestions(filteredSuggestions);
setShowSuggestions(filteredSuggestions.length > 0);