fix: correct file mention replacement in FloatingPromptInput

- Fix handleFileSelect to properly find @ position instead of assuming cursorPosition-1
- Replace entire @query text with selected file path instead of appending
- Add error handling for edge cases where @ symbol cannot be found
- Resolves issue where typing @READ and selecting README.md resulted in @READ@README.md

Fixes #114
This commit is contained in:
Vivek R
2025-07-06 16:52:11 +05:30
parent 4cd104b1dd
commit 985de02404

View File

@@ -439,10 +439,29 @@ const FloatingPromptInputInner = (
const handleFileSelect = (entry: FileEntry) => { const handleFileSelect = (entry: FileEntry) => {
if (textareaRef.current) { if (textareaRef.current) {
// Find the @ position before cursor
let atPosition = -1;
for (let i = cursorPosition - 1; i >= 0; i--) {
if (prompt[i] === '@') {
atPosition = i;
break;
}
// Stop if we hit whitespace (new word)
if (prompt[i] === ' ' || prompt[i] === '\n') {
break;
}
}
if (atPosition === -1) {
// @ not found, this shouldn't happen but handle gracefully
console.error('[FloatingPromptInput] @ position not found');
return;
}
// Replace the @ and partial query with the selected path (file or directory) // Replace the @ and partial query with the selected path (file or directory)
const textarea = textareaRef.current; const textarea = textareaRef.current;
const beforeAt = prompt.substring(0, cursorPosition - 1); const beforeAt = prompt.substring(0, atPosition);
const afterCursor = prompt.substring(cursorPosition + filePickerQuery.length); const afterCursor = prompt.substring(cursorPosition);
const relativePath = entry.path.startsWith(projectPath || '') const relativePath = entry.path.startsWith(projectPath || '')
? entry.path.slice((projectPath || '').length + 1) ? entry.path.slice((projectPath || '').length + 1)
: entry.path; : entry.path;