fix: function definition to respect max chunk size
This commit is contained in:
parent
10401503b6
commit
58f4fffae2
1 changed files with 35 additions and 14 deletions
|
|
@ -128,25 +128,46 @@ async function constructFunctionDefinitionChunk(
|
|||
maxChunkSize: number,
|
||||
): Promise<string> {
|
||||
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: {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue