MCPcopy Create free account
hub / github.com/ScriptedAlchemy/tracedecay / merge_adjacent_blocks

Method merge_adjacent_blocks

src/context/builder.rs:426–497  ·  view source on GitHub ↗

Merges code blocks from the same file that are adjacent or overlapping. Two blocks are "adjacent" if the gap between them is <= 5 lines.

(
        &self,
        blocks: Vec<CodeBlock>,
        file_cache: &mut HashMap<String, Option<String>>,
    )

Source from the content-addressed store, hash-verified

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.
426 fn merge_adjacent_blocks(
427 &self,
428 blocks: Vec<CodeBlock>,
429 file_cache: &mut HashMap<String, Option<String>>,
430 ) -> Vec<CodeBlock> {
431 if blocks.len() <= 1 {
432 return blocks;
433 }
434
435 // Group by file_path
436 let mut by_file: std::collections::HashMap<String, Vec<CodeBlock>> =
437 std::collections::HashMap::new();
438 for block in blocks {
439 by_file
440 .entry(block.file_path.clone())
441 .or_default()
442 .push(block);
443 }
444
445 let mut merged: Vec<CodeBlock> = Vec::new();
446 for (_file, mut file_blocks) in by_file {
447 file_blocks.sort_by_key(|b| b.start_line);
448 let mut current = file_blocks.remove(0);
449 for next in file_blocks {
450 // Merge if overlapping or gap <= 5 lines
451 if next.start_line <= current.end_line + 5 {
452 let new_end = current.end_line.max(next.end_line);
453 // Re-read the merged range from the file
454 let merged_node = Node {
455 id: current.node_id.clone().unwrap_or_default(),
456 kind: NodeKind::Function,
457 name: String::new(),
458 qualified_name: String::new(),
459 file_path: current.file_path.clone(),
460 start_line: current.start_line,
461 attrs_start_line: current.start_line,
462 end_line: new_end,
463 start_column: 0,
464 end_column: 0,
465 signature: None,
466 docstring: None,
467 visibility: Visibility::default(),
468 is_async: false,
469 branches: 0,
470 loops: 0,
471 returns: 0,
472 max_nesting: 0,
473 unsafe_blocks: 0,
474 unchecked_calls: 0,
475 assertions: 0,
476 updated_at: 0,
477 parent_id: None,
478 };
479 if let Some(code) = self.get_code_cached(&merged_node, file_cache) {
480 current.content = code;
481 current.end_line = new_end;
482 } else {
483 // Can't re-read; just concatenate

Callers 1

build_contextMethod · 0.80

Calls 4

pushMethod · 0.80
entryMethod · 0.80
get_code_cachedMethod · 0.80
cmpMethod · 0.80

Tested by

no test coverage detected