(text: string, cursorOffset: number)
| 50 | } |
| 51 | |
| 52 | function findActiveMention(text: string, cursorOffset: number): MentionMatch | null { |
| 53 | const safeOffset = Math.max(0, Math.min(cursorOffset, text.length)); |
| 54 | |
| 55 | let start = safeOffset; |
| 56 | while (start > 0 && !/\s/.test(text[start - 1]!)) { |
| 57 | start -= 1; |
| 58 | } |
| 59 | |
| 60 | let end = safeOffset; |
| 61 | while (end < text.length && !/\s/.test(text[end]!)) { |
| 62 | end += 1; |
| 63 | } |
| 64 | |
| 65 | const token = text.slice(start, end); |
| 66 | const relativeCursor = safeOffset - start; |
| 67 | const mentionStart = token.lastIndexOf("@", relativeCursor); |
| 68 | |
| 69 | if (mentionStart === -1) { |
| 70 | return null; |
| 71 | } |
| 72 | |
| 73 | const previousCharacter = token[mentionStart - 1]; |
| 74 | if (previousCharacter && isMentionQueryCharacter(previousCharacter)) { |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | let mentionEnd = mentionStart + 1; |
| 79 | while (mentionEnd < token.length && isMentionQueryCharacter(token[mentionEnd]!)) { |
| 80 | mentionEnd += 1; |
| 81 | } |
| 82 | |
| 83 | if (relativeCursor < mentionStart || relativeCursor > mentionEnd) { |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | return { |
| 88 | start: start + mentionStart, |
| 89 | end: start + mentionEnd, |
| 90 | query: token.slice(mentionStart + 1, mentionEnd), |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | async function getMentionCandidates(query: string): Promise<MentionCandidate[]> { |
| 95 | const normalizedQuery = query.startsWith("./") ? query.slice(2) : query; |
no test coverage detected