Add autocomplete for slash commands

This commit is contained in:
Seth Troisi
2025-05-01 00:52:01 +00:00
parent f237082c37
commit cc838fad44
4 changed files with 62 additions and 17 deletions

View File

@@ -47,25 +47,37 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
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
}, [