MCPcopy Index your code
hub / github.com/anus-dev/ANUS / parseAllAtCommands

Function parseAllAtCommands

packages/cli/src/ui/hooks/atCommandProcessor.ts:48–116  ·  view source on GitHub ↗

* Parses a query string to find all '@ ' commands and text segments. * Handles \ escaped spaces within paths.

(query: string)

Source from the content-addressed store, hash-verified

46 * Handles \ escaped spaces within paths.
47 */
48function 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 }

Callers 1

handleAtCommandFunction · 0.85

Calls 1

unescapePathFunction · 0.85

Tested by

no test coverage detected