( filepath: string, contents: string, maxChunkSize: number, )
| 244 | } |
| 245 | |
| 246 | export async function* codeChunker( |
| 247 | filepath: string, |
| 248 | contents: string, |
| 249 | maxChunkSize: number, |
| 250 | ): AsyncGenerator<ChunkWithoutID> { |
| 251 | if (contents.trim().length === 0) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | const parser = await getParserForFile(filepath); |
| 256 | if (parser === undefined) { |
| 257 | throw new Error(`Failed to load parser for file ${filepath}: `); |
| 258 | } |
| 259 | |
| 260 | const tree = parser.parse(contents); |
| 261 | |
| 262 | yield* getSmartCollapsedChunks(tree.rootNode, contents, maxChunkSize); |
| 263 | } |