* Find keyword positions, skipping occurrences that are clearly not a * launch directive: * * - Inside paired delimiters: backticks, double quotes, angle brackets * (tag-like only, so `n < 5 ultraplan n > 10` is not a phantom range), * curly braces, square brackets (innermost — preExpansion
( text: string, keyword: string, )
| 44 | * PromptInput treats both trigger types uniformly. |
| 45 | */ |
| 46 | function findKeywordTriggerPositions( |
| 47 | text: string, |
| 48 | keyword: string, |
| 49 | ): TriggerPosition[] { |
| 50 | const re = new RegExp(keyword, 'i') |
| 51 | if (!re.test(text)) return [] |
| 52 | if (text.startsWith('/')) return [] |
| 53 | const quotedRanges: Array<{ start: number; end: number }> = [] |
| 54 | let openQuote: string | null = null |
| 55 | let openAt = 0 |
| 56 | const isWord = (ch: string | undefined) => !!ch && /[\p{L}\p{N}_]/u.test(ch) |
| 57 | for (let i = 0; i < text.length; i++) { |
| 58 | const ch = text[i]! |
| 59 | if (openQuote) { |
| 60 | if (openQuote === '[' && ch === '[') { |
| 61 | openAt = i |
| 62 | continue |
| 63 | } |
| 64 | if (ch !== OPEN_TO_CLOSE[openQuote]) continue |
| 65 | if (openQuote === "'" && isWord(text[i + 1])) continue |
| 66 | quotedRanges.push({ start: openAt, end: i + 1 }) |
| 67 | openQuote = null |
| 68 | } else if ( |
| 69 | (ch === '<' && i + 1 < text.length && /[a-zA-Z/]/.test(text[i + 1]!)) || |
| 70 | (ch === "'" && !isWord(text[i - 1])) || |
| 71 | (ch !== '<' && ch !== "'" && ch in OPEN_TO_CLOSE) |
| 72 | ) { |
| 73 | openQuote = ch |
| 74 | openAt = i |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | const positions: TriggerPosition[] = [] |
| 79 | const wordRe = new RegExp(`\\b${keyword}\\b`, 'gi') |
| 80 | const matches = text.matchAll(wordRe) |
| 81 | for (const match of matches) { |
| 82 | if (match.index === undefined) continue |
| 83 | const start = match.index |
| 84 | const end = start + match[0].length |
| 85 | if (quotedRanges.some(r => start >= r.start && start < r.end)) continue |
| 86 | const before = text[start - 1] |
| 87 | const after = text[end] |
| 88 | if (before === '/' || before === '\\' || before === '-') continue |
| 89 | if (after === '/' || after === '\\' || after === '-' || after === '?') |
| 90 | continue |
| 91 | if (after === '.' && isWord(text[end + 1])) continue |
| 92 | positions.push({ word: match[0], start, end }) |
| 93 | } |
| 94 | return positions |
| 95 | } |
| 96 | |
| 97 | export function findUltraplanTriggerPositions(text: string): TriggerPosition[] { |
| 98 | return findKeywordTriggerPositions(text, 'ultraplan') |
no test coverage detected