MCPcopy Create free account
hub / github.com/anus-dev/ANUS / findCodeRegions

Function findCodeRegions

packages/core/src/utils/memoryImportProcessor.ts:154–193  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

152}
153
154function findCodeRegions(content: string): Array<[number, number]> {
155 const regions: Array<[number, number]> = [];
156 const tokens = marked.lexer(content);
157
158 // Map from raw content to a queue of its start indices in the original content.
159 const rawContentIndices = new Map<string, number[]>();
160
161 function walk(token: { type: string; raw: string; tokens?: unknown[] }) {
162 if (token.type === 'code' || token.type === 'codespan') {
163 if (!rawContentIndices.has(token.raw)) {
164 const indices: number[] = [];
165 let lastIndex = -1;
166 while ((lastIndex = content.indexOf(token.raw, lastIndex + 1)) !== -1) {
167 indices.push(lastIndex);
168 }
169 rawContentIndices.set(token.raw, indices);
170 }
171
172 const indices = rawContentIndices.get(token.raw);
173 if (indices && indices.length > 0) {
174 // Assume tokens are processed in order of appearance.
175 // Dequeue the next available index for this raw content.
176 const idx = indices.shift()!;
177 regions.push([idx, idx + token.raw.length]);
178 }
179 }
180
181 if ('tokens' in token && token.tokens) {
182 for (const child of token.tokens) {
183 walk(child as { type: string; raw: string; tokens?: unknown[] });
184 }
185 }
186 }
187
188 for (const token of tokens) {
189 walk(token);
190 }
191
192 return regions;
193}
194
195/**
196 * Processes import statements in ANUS.md content

Callers 2

processFlatFunction · 0.85
processImportsFunction · 0.85

Calls 1

walkFunction · 0.85

Tested by

no test coverage detected