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

Function print_word_diff_patch

atomic-cli/src/commands/rpc.rs:2758–2839  ·  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

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