Skip to content

Commit

Permalink
[Spelunker] Add breadcrumbs and fix recursive chunking (#648)
Browse files Browse the repository at this point in the history
I really need to start working on a test suite...
  • Loading branch information
gvanrossum-ms authored Feb 1, 2025
1 parent 4de3798 commit 605b4db
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions ts/packages/agents/spelunker/src/typescriptChunker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ export async function chunkifyTypeScriptFiles(
// ts.SyntaxKind[childNode.kind],
// tsCode.getStatementName(childNode),
// );
const chunk: Chunk = {
const treeName = ts.SyntaxKind[childNode.kind];
const codeName = tsCode.getStatementName(childNode) ?? "";
const childChunk: Chunk = {
chunkId: generate_id(),
treeName: ts.SyntaxKind[childNode.kind],
codeName: tsCode.getStatementName(childNode) ?? "",
treeName,
codeName,
blobs: makeBlobs(
sourceFile,
childNode.getFullStart(),
Expand All @@ -85,11 +87,11 @@ export async function chunkifyTypeScriptFiles(
children: [],
fileName,
};
spliceBlobs(parentChunk, chunk);
chunks.push(chunk);
recursivelyChunkify(childNode, chunk);
spliceBlobs(parentChunk, childChunk);
chunks.push(childChunk);
chunks.push(...recursivelyChunkify(childNode, childChunk));
} else {
recursivelyChunkify(childNode, parentChunk);
chunks.push(...recursivelyChunkify(childNode, parentChunk));
}
}
return chunks;
Expand Down Expand Up @@ -142,12 +144,34 @@ function spliceBlobs(parentChunk: Chunk, childChunk: Chunk): void {
if (linesBefore.length) {
blobs.push({ start: startBefore, lines: linesBefore });
}
const sig: string = signature(childChunk);
// console.log("signature", sig);
if (sig) {
blobs.push({ start: childBlob.start, lines: [sig], breadcrumb: true });
}
if (linesAfter.length) {
blobs.push({ start: startAfter, lines: linesAfter });
}
parentChunk.blobs.splice(-1, 1, ...blobs);
}

function signature(chunk: Chunk): string {
const firstLine = chunk.blobs[0]?.lines[0] ?? "";
const indent = firstLine.match(/^(\s*)/)?.[0] || "";

switch (chunk.treeName) {
case "InterfaceDeclaration":
return `${indent}interface ${chunk.codeName} ...`;
case "TypeAliasDeclaration":
return `${indent}type ${chunk.codeName} ...`;
case "FunctionDeclaration":
return `${indent}function ${chunk.codeName} ...`;
case "ClassDeclaration":
return `${indent}class ${chunk.codeName} ...`;
}
return "";
}

function makeBlobs(
sourceFile: ts.SourceFile,
startPos: number,
Expand Down

0 comments on commit 605b4db

Please sign in to comment.