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

Method repair_diff_lines

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

497 /// each paired Added line pulled forward to appear immediately after
498 /// its matching Removed line.
499 fn repair_diff_lines(lines: Vec<HunkLine>) -> Vec<HunkLine> {
500 use std::collections::{HashMap, HashSet};
501
502 // Helper: compute character bigrams for Jaccard similarity
503 fn bigrams(s: &str) -> HashSet<(u8, u8)> {
504 let bytes = s.trim().as_bytes();
505 let mut set = HashSet::new();
506 if bytes.len() >= 2 {
507 for w in bytes.windows(2) {
508 set.insert((w[0], w[1]));
509 }
510 }
511 set
512 }
513
514 fn jaccard(a: &HashSet<(u8, u8)>, b: &HashSet<(u8, u8)>) -> f64 {
515 if a.is_empty() && b.is_empty() {
516 return 0.0;
517 }
518 let inter = a.intersection(b).count();
519 let union = a.union(b).count();
520 if union == 0 {
521 0.0
522 } else {
523 inter as f64 / union as f64
524 }
525 }
526
527 // 1. Collect all Removed and Added lines with their original indices
528 let mut rm_entries: Vec<(usize, &HunkLine)> = Vec::new();
529 let mut add_entries: Vec<(usize, &HunkLine)> = Vec::new();
530
531 for (idx, line) in lines.iter().enumerate() {
532 if line.is_removed() {
533 rm_entries.push((idx, line));
534 } else if line.is_added() {
535 add_entries.push((idx, line));
536 }
537 }
538
539 // Short-circuit: nothing to pair
540 if rm_entries.is_empty() || add_entries.is_empty() {
541 return lines;
542 }
543
544 // 2. Compute bigrams
545 let rm_bigrams: Vec<HashSet<(u8, u8)>> = rm_entries
546 .iter()
547 .map(|(_, l)| bigrams(&l.content))
548 .collect();
549 let add_bigrams: Vec<HashSet<(u8, u8)>> = add_entries
550 .iter()
551 .map(|(_, l)| bigrams(&l.content))
552 .collect();
553
554 // 3. Greedy best-match pairing across ALL removes and adds
555 let mut candidates: Vec<(usize, usize, f64)> = Vec::new();
556 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