diff --git a/core/indexing/chunk/code.ts b/core/indexing/chunk/code.ts index 45487d551..13f1e870b 100644 --- a/core/indexing/chunk/code.ts +++ b/core/indexing/chunk/code.ts @@ -128,25 +128,46 @@ async function constructFunctionDefinitionChunk( maxChunkSize: number, ): Promise { const bodyNode = node.children[node.children.length - 1]; - const funcText = - code.slice(node.startIndex, bodyNode.startIndex) + - collapsedReplacement(bodyNode); + const collapsedBody = collapsedReplacement(bodyNode); + const signature = code.slice(node.startIndex, bodyNode.startIndex); + const funcText = signature + collapsedBody; - if ( + const isInClass = node.parent && ["block", "declaration_list"].includes(node.parent.type) && node.parent.parent && - ["class_definition", "impl_item"].includes(node.parent.parent.type) - ) { - // If inside a class, include the class header - const classNode = node.parent.parent; - const classBlock = node.parent; - return `${code.slice( - classNode.startIndex, - classBlock.startIndex, - )}...\n\n${" ".repeat(node.startPosition.column)}${funcText}`; + ["class_definition", "impl_item"].includes(node.parent.parent.type); + + if (isInClass) { + const classNode = node.parent!.parent!; + const classBlock = node.parent!; + const classHeader = code.slice(classNode.startIndex, classBlock.startIndex); + const indent = " ".repeat(node.startPosition.column); + const combined = `${classHeader}...\n\n${indent}${funcText}`; + + if ((await countTokensAsync(combined)) <= maxChunkSize) { + return combined; + } + if ((await countTokensAsync(funcText)) <= maxChunkSize) { + return funcText; + } + const firstLine = signature.split("\n")[0] ?? ""; + const minimal = `${firstLine} ${collapsedBody}`; + if ((await countTokensAsync(minimal)) <= maxChunkSize) { + return minimal; + } + return collapsedBody; } - return funcText; + + if ((await countTokensAsync(funcText)) <= maxChunkSize) { + return funcText; + } + const firstLine = signature.split("\n")[0] ?? ""; + const minimal = `${firstLine} ${collapsedBody}`; + if ((await countTokensAsync(minimal)) <= maxChunkSize) { + return minimal; + } + return collapsedBody; } const collapsedNodeConstructors: {