Demonstrate multi-line diff with word-level highlighting
()
| 120 | |
| 121 | /// Demonstrate multi-line diff with word-level highlighting |
| 122 | fn demo_multiline_diff() { |
| 123 | println!("┌─ Multi-Line Diff Example ─┐"); |
| 124 | println!("│"); |
| 125 | |
| 126 | let old_code = b"pub fn process(items: Vec<Item>) -> Result<(), Error> { |
| 127 | for item in items { |
| 128 | validate(item)?; |
| 129 | } |
| 130 | Ok(()) |
| 131 | }"; |
| 132 | |
| 133 | let new_code = b"pub fn process(items: &[Item]) -> Result<(), ProcessError> { |
| 134 | for item in items.iter() { |
| 135 | validate_item(item)?; |
| 136 | } |
| 137 | Ok(()) |
| 138 | }"; |
| 139 | |
| 140 | // Split into lines and diff |
| 141 | let old_lines: Vec<Line> = Line::from_bytes(old_code); |
| 142 | let new_lines: Vec<Line> = Line::from_bytes(new_code); |
| 143 | |
| 144 | let line_diff = atomic_core::diff::diff(&old_lines, &new_lines, Algorithm::Myers); |
| 145 | |
| 146 | let mut old_idx = 0; |
| 147 | let mut new_idx = 0; |
| 148 | |
| 149 | for op in line_diff.iter() { |
| 150 | match op { |
| 151 | DiffOp::Equal { len, .. } => { |
| 152 | // Show equal lines without highlighting |
| 153 | for _ in 0..*len { |
| 154 | let line_content = old_lines[old_idx].content(); |
| 155 | let text = String::from_utf8_lossy(line_content); |
| 156 | println!("│ {}", text.trim_end()); |
| 157 | old_idx += 1; |
| 158 | new_idx += 1; |
| 159 | } |
| 160 | } |
| 161 | DiffOp::Replace { |
| 162 | old_len, new_len, .. |
| 163 | } => { |
| 164 | // For replaced lines, show word-level diff |
| 165 | for i in 0..*old_len.max(new_len) { |
| 166 | let old_line = if i < *old_len { |
| 167 | Some(old_lines[old_idx + i].content()) |
| 168 | } else { |
| 169 | None |
| 170 | }; |
| 171 | let new_line = if i < *new_len { |
| 172 | Some(new_lines[new_idx + i].content()) |
| 173 | } else { |
| 174 | None |
| 175 | }; |
| 176 | |
| 177 | match (old_line, new_line) { |
| 178 | (Some(old), Some(new)) => { |
| 179 | // Both lines exist - show word diff |
no test coverage detected