MCPcopy Index your code
hub / github.com/forloopcodes/contextplus / walkTree

Function walkTree

src/core/tree-sitter.ts:181–217  ·  view source on GitHub ↗
(rootNode: TSNode, defTypes: Record<string, string>, maxDepth: number = 3)

Source from the content-addressed store, hash-verified

179}
180
181function walkTree(rootNode: TSNode, defTypes: Record<string, string>, maxDepth: number = 3): CodeSymbol[] {
182 const symbols: CodeSymbol[] = [];
183
184 function visit(node: TSNode, depth: number, parent: CodeSymbol | null): void {
185 if (depth > maxDepth) return;
186
187 const kindStr = defTypes[node.type];
188 if (kindStr) {
189 const sym: CodeSymbol = {
190 name: extractName(node, kindStr),
191 kind: mapKind(kindStr),
192 line: node.startPosition.row + 1,
193 endLine: node.endPosition.row + 1,
194 signature: extractSignature(node),
195 children: [],
196 };
197
198 if (parent && depth > 0) {
199 parent.children.push(sym);
200 } else {
201 symbols.push(sym);
202 }
203
204 for (const child of node.namedChildren ?? []) {
205 visit(child, depth + 1, sym);
206 }
207 return;
208 }
209
210 for (const child of node.namedChildren ?? []) {
211 visit(child, depth, parent);
212 }
213 }
214
215 visit(rootNode, 0, null);
216 return symbols;
217}
218
219export function getGrammarName(ext: string): string | null {
220 return EXT_TO_GRAMMAR[ext.toLowerCase()] ?? null;

Callers 1

parseWithTreeSitterFunction · 0.85

Calls 1

visitFunction · 0.85

Tested by

no test coverage detected