( text: string, position: number, value: string, isSlashCommand: boolean = false, )
| 25 | } |
| 26 | |
| 27 | export function insertMention( |
| 28 | text: string, |
| 29 | position: number, |
| 30 | value: string, |
| 31 | isSlashCommand: boolean = false, |
| 32 | ): { newValue: string; mentionIndex: number } { |
| 33 | // Handle slash command selection (only when explicitly selecting a slash command) |
| 34 | if (isSlashCommand) { |
| 35 | return { |
| 36 | newValue: value, |
| 37 | mentionIndex: 0, |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | const beforeCursor = text.slice(0, position) |
| 42 | const afterCursor = text.slice(position) |
| 43 | |
| 44 | // Find the position of the last '@' symbol before the cursor |
| 45 | const lastAtIndex = beforeCursor.lastIndexOf("@") |
| 46 | |
| 47 | // Process the value - escape spaces if it's a file path |
| 48 | let processedValue = value |
| 49 | if (value && value.startsWith("/")) { |
| 50 | // Only escape if the path contains spaces that aren't already escaped |
| 51 | if (value.includes(" ") && !value.includes("\\ ")) { |
| 52 | processedValue = escapeSpaces(value) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | let newValue: string |
| 57 | let mentionIndex: number |
| 58 | |
| 59 | if (lastAtIndex !== -1) { |
| 60 | // If there's an '@' symbol, replace everything after it with the new mention |
| 61 | const beforeMention = text.slice(0, lastAtIndex) |
| 62 | // Only replace if afterCursor is all alphanumerical |
| 63 | // This is required to handle languages that don't use space as a word separator (chinese, japanese, korean, etc) |
| 64 | const afterCursorContent = /^[a-zA-Z0-9\s]*$/.test(afterCursor) |
| 65 | ? afterCursor.replace(/^[^\s]*/, "") |
| 66 | : afterCursor |
| 67 | newValue = beforeMention + "@" + processedValue + " " + afterCursorContent |
| 68 | mentionIndex = lastAtIndex |
| 69 | } else { |
| 70 | // If there's no '@' symbol, insert the mention at the cursor position |
| 71 | newValue = beforeCursor + "@" + processedValue + " " + afterCursor |
| 72 | mentionIndex = position |
| 73 | } |
| 74 | |
| 75 | return { newValue, mentionIndex } |
| 76 | } |
| 77 | |
| 78 | export function removeMention(text: string, position: number): { newText: string; newPosition: number } { |
| 79 | const beforeCursor = text.slice(0, position) |
no test coverage detected