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

Function print_word_diff_patch

atomic-cli/src/commands/rpc.rs:2759–2840  ·  view source on GitHub ↗

Print a wire patch with word-level highlighting — the same pairing and token-level highlighting the local unified renderer (`diff/format.rs`) applies to its computed hunks: consecutive removed/added lines pair up and diff at token level (the semantic engine first, the inline engine as fallback); unpaired and context lines render plain.

(patch: &str)

Source from the content-addressed store, hash-verified

2757/// and diff at token level (the semantic engine first, the inline engine
2758/// as fallback); unpaired and context lines render plain.
2759fn print_word_diff_patch(patch: &str) {
2760 use crate::output::{added, deleted};
2761 use atomic_core::diff::compute_inline_diff;
2762 use atomic_core::diff::{semantic_diff, LineChange};
2763
2764 for hunk in parse_patch_hunks(patch) {
2765 println!(
2766 "@@ -{},{} +{},{} @@",
2767 hunk.old_start, hunk.old_count, hunk.new_start, hunk.new_count
2768 );
2769 let lines = hunk.lines;
2770 let mut index = 0;
2771 while index < lines.len() {
2772 if lines[index].0 != '-' {
2773 let (marker, content, ..) = &lines[index];
2774 match marker {
2775 '+' => println!("{}", added(&format!("{marker}{content}"))),
2776 _ => println!("{marker}{content}"),
2777 }
2778 index += 1;
2779 continue;
2780 }
2781 // Collect the consecutive removed block, then the added block
2782 // that follows — the local renderer's pairing window.
2783 let removed: Vec<&(char, String, Option<usize>, Option<usize>)> = lines[index..]
2784 .iter()
2785 .take_while(|line| line.0 == '-')
2786 .collect();
2787 let mut cursor = index + removed.len();
2788 let added_lines: Vec<&(char, String, Option<usize>, Option<usize>)> = lines[cursor..]
2789 .iter()
2790 .take_while(|line| line.0 == '+')
2791 .collect();
2792 cursor += added_lines.len();
2793 if added_lines.is_empty() {
2794 for (marker, content, ..) in &removed {
2795 println!("{}", deleted(&format!("{marker}{content}")));
2796 }
2797 index += removed.len();
2798 continue;
2799 }
2800 let pairs = removed.len().min(added_lines.len());
2801 for pair in 0..pairs {
2802 let (removed_line, added_line) =
2803 (removed[pair].1.as_str(), added_lines[pair].1.as_str());
2804 let old_content = removed_line.as_bytes();
2805 let new_content = added_line.as_bytes();
2806 // Semantic token diff first; the inline diff is the fallback.
2807 let sem_diff = semantic_diff(old_content, new_content);
2808 let mut used_semantic = false;
2809 #[allow(clippy::collapsible_match)]
2810 if let Some(change) = sem_diff.changes().first() {
2811 if let LineChange::Modified { token_changes, .. } = change {
2812 print!("{}", deleted(&format!("-{removed_line}")));
2813 super::diff::print_semantic_word_diff_line(token_changes, true);
2814 println!();
2815 print!("{}", added(&format!("+{added_line}")));
2816 super::diff::print_semantic_word_diff_line(token_changes, false);

Callers

nothing calls this directly

Calls 15

parse_patch_hunksFunction · 0.85
semantic_diffFunction · 0.85
compute_inline_diffFunction · 0.85
print_word_diff_lineFunction · 0.85
old_hunksMethod · 0.80
new_hunksMethod · 0.80
skipMethod · 0.80
lenMethod · 0.45
collectMethod · 0.45
iterMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected