Changesets · Branches · Tags · Compare
| Changeset | 7283be5297d6a0e76b1ed98b78632ce3d7570ffaa2391ae72bee0f93f081491f |
|---|---|
| Author | uinstinct <61635505+uinstinct@users.noreply.github.com> · 2025-08-08 11:21 +05:30 |
| Committer | uinstinct <61635505+uinstinct@users.noreply.github.com> · 2025-08-08 11:21 +05:30 |
| Parents | 76e6219ef348 |
1 file changed
modified core/indexing/chunk/code.ts +36 −15
| @@ -128,25 +128,46 @@ | ||
| 128 | 128 | maxChunkSize: number, |
| 129 | 129 | ): Promise<string> { |
| 130 | 130 | const bodyNode = node.children[node.children.length - 1]; |
| 131 | - const funcText = | |
| 132 | - code.slice(node.startIndex, bodyNode.startIndex) + | |
| 133 | - collapsedReplacement(bodyNode); | |
| 134 | - | |
| 135 | - if ( | |
| 131 | + const collapsedBody = collapsedReplacement(bodyNode); | |
| 132 | + const signature = code.slice(node.startIndex, bodyNode.startIndex); | |
| 133 | + const funcText = signature + collapsedBody; | |
| 134 | + | |
| 135 | + const isInClass = | |
| 136 | 136 | node.parent && |
| 137 | 137 | ["block", "declaration_list"].includes(node.parent.type) && |
| 138 | 138 | node.parent.parent && |
| 139 | - ["class_definition", "impl_item"].includes(node.parent.parent.type) | |
| 140 | - ) { | |
| 141 | - // If inside a class, include the class header | |
| 142 | - const classNode = node.parent.parent; | |
| 143 | - const classBlock = node.parent; | |
| 144 | - return `${code.slice( | |
| 145 | - classNode.startIndex, | |
| 146 | - classBlock.startIndex, | |
| 147 | - )}...\n\n${" ".repeat(node.startPosition.column)}${funcText}`; | |
| 139 | + ["class_definition", "impl_item"].includes(node.parent.parent.type); | |
| 140 | + | |
| 141 | + if (isInClass) { | |
| 142 | + const classNode = node.parent!.parent!; | |
| 143 | + const classBlock = node.parent!; | |
| 144 | + const classHeader = code.slice(classNode.startIndex, classBlock.startIndex); | |
| 145 | + const indent = " ".repeat(node.startPosition.column); | |
| 146 | + const combined = `${classHeader}...\n\n${indent}${funcText}`; | |
| 147 | + | |
| 148 | + if ((await countTokensAsync(combined)) <= maxChunkSize) { | |
| 149 | + return combined; | |
| 150 | + } | |
| 151 | + if ((await countTokensAsync(funcText)) <= maxChunkSize) { | |
| 152 | + return funcText; | |
| 153 | + } | |
| 154 | + const firstLine = signature.split("\n")[0] ?? ""; | |
| 155 | + const minimal = `${firstLine} ${collapsedBody}`; | |
| 156 | + if ((await countTokensAsync(minimal)) <= maxChunkSize) { | |
| 157 | + return minimal; | |
| 158 | + } | |
| 159 | + return collapsedBody; | |
| 160 | + } | |
| 161 | + | |
| 162 | + if ((await countTokensAsync(funcText)) <= maxChunkSize) { | |
| 163 | + return funcText; | |
| 164 | + } | |
| 165 | + const firstLine = signature.split("\n")[0] ?? ""; | |
| 166 | + const minimal = `${firstLine} ${collapsedBody}`; | |
| 167 | + if ((await countTokensAsync(minimal)) <= maxChunkSize) { | |
| 168 | + return minimal; | |
| 148 | 169 | } |
| 149 | - return funcText; | |
| 170 | + return collapsedBody; | |
| 150 | 171 | } |
| 151 | 172 | |
| 152 | 173 | const collapsedNodeConstructors: { |