MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / repair_diff_lines

Method repair_diff_lines

atomic-cli/src/commands/diff/helpers.rs:642–773  ·  view source on GitHub ↗

Re-pair Delete+Insert lines by content similarity for display. The CRDT builder emits all Deletes before all Inserts within each Replace block (to preserve BRANCH_AFTER chain ordering). When the Myers diff creates multiple Replace blocks for one file, a deleted line and its matching insertion may land in different blocks. This post-pass collects ALL Removed and Added lines across the entire hun

(lines: Vec<HunkLine>)

Source from the content-addressed store, hash-verified

640 /// each paired Added line pulled forward to appear immediately after
641 /// its matching Removed line.
642 fn repair_diff_lines(lines: Vec<HunkLine>) -> Vec<HunkLine> {
643 use std::collections::{HashMap, HashSet};
644
645 // Helper: compute character bigrams for Jaccard similarity
646 fn bigrams(s: &str) -> HashSet<(u8, u8)> {
647 let bytes = s.trim().as_bytes();
648 let mut set = HashSet::new();
649 if bytes.len() >= 2 {
650 for w in bytes.windows(2) {
651 set.insert((w[0], w[1]));
652 }
653 }
654 set
655 }
656
657 fn jaccard(a: &HashSet<(u8, u8)>, b: &HashSet<(u8, u8)>) -> f64 {
658 if a.is_empty() && b.is_empty() {
659 return 0.0;
660 }
661 let inter = a.intersection(b).count();
662 let union = a.union(b).count();
663 if union == 0 {
664 0.0
665 } else {
666 inter as f64 / union as f64
667 }
668 }
669
670 // 1. Collect all Removed and Added lines with their original indices
671 let mut rm_entries: Vec<(usize, &HunkLine)> = Vec::new();
672 let mut add_entries: Vec<(usize, &HunkLine)> = Vec::new();
673
674 for (idx, line) in lines.iter().enumerate() {
675 if line.is_removed() {
676 rm_entries.push((idx, line));
677 } else if line.is_added() {
678 add_entries.push((idx, line));
679 }
680 }
681
682 // Short-circuit: nothing to pair
683 if rm_entries.is_empty() || add_entries.is_empty() {
684 return lines;
685 }
686
687 // 2. Compute bigrams
688 let rm_bigrams: Vec<HashSet<(u8, u8)>> = rm_entries
689 .iter()
690 .map(|(_, l)| bigrams(&l.content))
691 .collect();
692 let add_bigrams: Vec<HashSet<(u8, u8)>> = add_entries
693 .iter()
694 .map(|(_, l)| bigrams(&l.content))
695 .collect();
696
697 // 3. Greedy best-match pairing across ALL removes and adds
698 let mut candidates: Vec<(usize, usize, f64)> = Vec::new();
699 for (ri, rb) in rm_bigrams.iter().enumerate() {

Callers

nothing calls this directly

Calls 14

bigramsFunction · 0.85
jaccardFunction · 0.85
getMethod · 0.65
iterMethod · 0.45
is_removedMethod · 0.45
pushMethod · 0.45
is_addedMethod · 0.45
is_emptyMethod · 0.45
collectMethod · 0.45
partial_cmpMethod · 0.45
containsMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected