Extracts code blocks for the entry-point nodes.
(
&self,
entry_points: &[Node],
options: &BuildContextOptions,
file_cache: &mut HashMap<String, Option<String>>,
)
| 377 | |
| 378 | /// Extracts code blocks for the entry-point nodes. |
| 379 | fn extract_code_blocks( |
| 380 | &self, |
| 381 | entry_points: &[Node], |
| 382 | options: &BuildContextOptions, |
| 383 | file_cache: &mut HashMap<String, Option<String>>, |
| 384 | ) -> Vec<CodeBlock> { |
| 385 | debug_assert!( |
| 386 | options.max_code_blocks > 0, |
| 387 | "max_code_blocks must be positive" |
| 388 | ); |
| 389 | debug_assert!( |
| 390 | options.max_code_block_size > 0, |
| 391 | "max_code_block_size must be positive" |
| 392 | ); |
| 393 | let mut blocks: Vec<CodeBlock> = Vec::new(); |
| 394 | |
| 395 | for node in entry_points { |
| 396 | if blocks.len() >= options.max_code_blocks { |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | if let Some(code) = self.get_code_cached(node, file_cache) { |
| 401 | let truncated = if code.len() > options.max_code_block_size { |
| 402 | let prefix = |
| 403 | crate::text::utf8_prefix_at_or_before(&code, options.max_code_block_size); |
| 404 | // Prefer a line boundary within the truncated prefix. |
| 405 | let end = prefix.rfind('\n').unwrap_or(prefix.len()); |
| 406 | format!("{}...", &prefix[..end]) |
| 407 | } else { |
| 408 | code |
| 409 | }; |
| 410 | |
| 411 | blocks.push(CodeBlock { |
| 412 | content: truncated, |
| 413 | file_path: node.file_path.clone(), |
| 414 | start_line: node.start_line, |
| 415 | end_line: node.end_line, |
| 416 | node_id: Some(node.id.clone()), |
| 417 | }); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | blocks |
| 422 | } |
| 423 | |
| 424 | /// Merges code blocks from the same file that are adjacent or overlapping. |
| 425 | /// Two blocks are "adjacent" if the gap between them is <= 5 lines. |
no test coverage detected