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

Method repair_diff_lines

atomic-cli/src/commands/diff/helpers.rs:711–842  ·  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

709 /// each paired Added line pulled forward to appear immediately after
710 /// its matching Removed line.
711 fn repair_diff_lines(lines: Vec<HunkLine>) -> Vec<HunkLine> {
712 use std::collections::{HashMap, HashSet};
713
714 // Helper: compute character bigrams for Jaccard similarity
715 fn bigrams(s: &str) -> HashSet<(u8, u8)> {
716 let bytes = s.trim().as_bytes();
717 let mut set = HashSet::new();
718 if bytes.len() >= 2 {
719 for w in bytes.windows(2) {
720 set.insert((w[0], w[1]));
721 }
722 }
723 set
724 }
725
726 fn jaccard(a: &HashSet<(u8, u8)>, b: &HashSet<(u8, u8)>) -> f64 {
727 if a.is_empty() && b.is_empty() {
728 return 0.0;
729 }
730 let inter = a.intersection(b).count();
731 let union = a.union(b).count();
732 if union == 0 {
733 0.0
734 } else {
735 inter as f64 / union as f64
736 }
737 }
738
739 // 1. Collect all Removed and Added lines with their original indices
740 let mut rm_entries: Vec<(usize, &HunkLine)> = Vec::new();
741 let mut add_entries: Vec<(usize, &HunkLine)> = Vec::new();
742
743 for (idx, line) in lines.iter().enumerate() {
744 if line.is_removed() {
745 rm_entries.push((idx, line));
746 } else if line.is_added() {
747 add_entries.push((idx, line));
748 }
749 }
750
751 // Short-circuit: nothing to pair
752 if rm_entries.is_empty() || add_entries.is_empty() {
753 return lines;
754 }
755
756 // 2. Compute bigrams
757 let rm_bigrams: Vec<HashSet<(u8, u8)>> = rm_entries
758 .iter()
759 .map(|(_, l)| bigrams(&l.content))
760 .collect();
761 let add_bigrams: Vec<HashSet<(u8, u8)>> = add_entries
762 .iter()
763 .map(|(_, l)| bigrams(&l.content))
764 .collect();
765
766 // 3. Greedy best-match pairing across ALL removes and adds
767 let mut candidates: Vec<(usize, usize, f64)> = Vec::new();
768 for (ri, rb) in rm_bigrams.iter().enumerate() {

Callers

nothing calls this directly

Calls 11

is_removedMethod · 0.80
partial_cmpMethod · 0.80
getMethod · 0.65
iterMethod · 0.45
pushMethod · 0.45
is_addedMethod · 0.45
is_emptyMethod · 0.45
containsMethod · 0.45
insertMethod · 0.45
lenMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected