From cc838fad44057a42561215fe3daed2834e6e0442 Mon Sep 17 00:00:00 2001 From: Seth Troisi Date: Thu, 1 May 2025 00:52:01 +0000 Subject: Add autocomplete for slash commands --- packages/cli/src/ui/components/InputPrompt.tsx | 42 +++++++++++++++++--------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'packages/cli/src/ui/components/InputPrompt.tsx') diff --git a/packages/cli/src/ui/components/InputPrompt.tsx b/packages/cli/src/ui/components/InputPrompt.tsx index fbf84766..1c8cff4d 100644 --- a/packages/cli/src/ui/components/InputPrompt.tsx +++ b/packages/cli/src/ui/components/InputPrompt.tsx @@ -47,25 +47,37 @@ export const InputPrompt: React.FC = ({ return; } const selectedSuggestion = suggestions[activeSuggestionIndex]; - const atIndex = query.lastIndexOf('@'); - if (atIndex === -1) return; + const trimmedQuery = query.trimStart(); - // Find the part of the query after the '@' - const pathPart = query.substring(atIndex + 1); - // Find the last slash within that part - const lastSlashIndexInPath = pathPart.lastIndexOf('/'); - - let base = ''; - if (lastSlashIndexInPath === -1) { - // No slash after '@', replace everything after '@' - base = query.substring(0, atIndex + 1); + if (trimmedQuery.startsWith('/')) { + // Handle / command completion + const slashIndex = query.indexOf('/'); + const base = query.substring(0, slashIndex + 1); + const newValue = base + selectedSuggestion.value; + setQuery(newValue); } else { - // Slash found, keep everything up to and including the last slash - base = query.substring(0, atIndex + 1 + lastSlashIndexInPath + 1); + // Handle @ command completion + const atIndex = query.lastIndexOf('@'); + if (atIndex === -1) return; + + // Find the part of the query after the '@' + const pathPart = query.substring(atIndex + 1); + // Find the last slash within that part + const lastSlashIndexInPath = pathPart.lastIndexOf('/'); + + let base = ''; + if (lastSlashIndexInPath === -1) { + // No slash after '@', replace everything after '@' + base = query.substring(0, atIndex + 1); + } else { + // Slash found, keep everything up to and including the last slash + base = query.substring(0, atIndex + 1 + lastSlashIndexInPath + 1); + } + + const newValue = base + selectedSuggestion.value; + setQuery(newValue); } - const newValue = base + selectedSuggestion.value; - setQuery(newValue); resetCompletion(); // Hide suggestions after selection setInputKey((k) => k + 1); // Increment key to force re-render and cursor reset }, [ -- cgit v1.2.3