* Parses a query string to find all '@ ' commands and text segments. * Handles \ escaped spaces within paths.
(query: string)
| 46 | * Handles \ escaped spaces within paths. |
| 47 | */ |
| 48 | function parseAllAtCommands(query: string): AtCommandPart[] { |
| 49 | const parts: AtCommandPart[] = []; |
| 50 | let currentIndex = 0; |
| 51 | |
| 52 | while (currentIndex < query.length) { |
| 53 | let atIndex = -1; |
| 54 | let nextSearchIndex = currentIndex; |
| 55 | // Find next unescaped '@' |
| 56 | while (nextSearchIndex < query.length) { |
| 57 | if ( |
| 58 | query[nextSearchIndex] === '@' && |
| 59 | (nextSearchIndex === 0 || query[nextSearchIndex - 1] !== '\\') |
| 60 | ) { |
| 61 | atIndex = nextSearchIndex; |
| 62 | break; |
| 63 | } |
| 64 | nextSearchIndex++; |
| 65 | } |
| 66 | |
| 67 | if (atIndex === -1) { |
| 68 | // No more @ |
| 69 | if (currentIndex < query.length) { |
| 70 | parts.push({ type: 'text', content: query.substring(currentIndex) }); |
| 71 | } |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | // Add text before @ |
| 76 | if (atIndex > currentIndex) { |
| 77 | parts.push({ |
| 78 | type: 'text', |
| 79 | content: query.substring(currentIndex, atIndex), |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | // Parse @path |
| 84 | let pathEndIndex = atIndex + 1; |
| 85 | let inEscape = false; |
| 86 | while (pathEndIndex < query.length) { |
| 87 | const char = query[pathEndIndex]; |
| 88 | if (inEscape) { |
| 89 | inEscape = false; |
| 90 | } else if (char === '\\') { |
| 91 | inEscape = true; |
| 92 | } else if (/[,\s;!?()[\]{}]/.test(char)) { |
| 93 | // Path ends at first whitespace or punctuation not escaped |
| 94 | break; |
| 95 | } else if (char === '.') { |
| 96 | // For . we need to be more careful - only terminate if followed by whitespace or end of string |
| 97 | // This allows file extensions like .txt, .js but terminates at sentence endings like "file.txt. Next sentence" |
| 98 | const nextChar = |
| 99 | pathEndIndex + 1 < query.length ? query[pathEndIndex + 1] : ''; |
| 100 | if (nextChar === '' || /\s/.test(nextChar)) { |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | pathEndIndex++; |
| 105 | } |
no test coverage detected