Compute a semantic diff with custom configuration. # Arguments `old` - The original content `new` - The modified content `config` - Configuration for the diff operation # Returns A [`SemanticDiff`] with line and token level changes.
(
old: &'a [u8],
new: &'a [u8],
config: &SemanticDiffConfig,
)
| 45 | /// |
| 46 | /// A [`SemanticDiff`] with line and token level changes. |
| 47 | pub fn semantic_diff_with_config<'a>( |
| 48 | old: &'a [u8], |
| 49 | new: &'a [u8], |
| 50 | config: &SemanticDiffConfig, |
| 51 | ) -> SemanticDiff<'a> { |
| 52 | // Parse into semantic lines |
| 53 | let old_lines = SemanticLine::from_bytes(old); |
| 54 | let new_lines = SemanticLine::from_bytes(new); |
| 55 | |
| 56 | // Compute line-level diff |
| 57 | let old_raw: Vec<Line> = old_lines.iter().map(|sl| sl.line.clone()).collect(); |
| 58 | let new_raw: Vec<Line> = new_lines.iter().map(|sl| sl.line.clone()).collect(); |
| 59 | let line_diff = diff(&old_raw, &new_raw, config.algorithm); |
| 60 | |
| 61 | // Process the diff operations into semantic changes |
| 62 | let mut changes = Vec::new(); |
| 63 | let mut stats = SemanticDiffStats::default(); |
| 64 | |
| 65 | for op in line_diff.iter() { |
| 66 | match op { |
| 67 | DiffOp::Equal { .. } => { |
| 68 | // Skip unchanged lines (unless including context) |
| 69 | } |
| 70 | |
| 71 | DiffOp::Insert { |
| 72 | old_pos: _, |
| 73 | new_pos, |
| 74 | len, |
| 75 | } => { |
| 76 | // Lines were added |
| 77 | for i in 0..*len { |
| 78 | let line_idx = new_pos + i; |
| 79 | if line_idx < new_lines.len() { |
| 80 | let line = new_lines[line_idx].clone(); |
| 81 | |
| 82 | // Skip blank lines if configured |
| 83 | if config.ignore_blank_lines && line.is_blank() { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | // All tokens are insertions |
| 88 | let tokens = create_insertion_tokens(&line); |
| 89 | stats.tokens_inserted += tokens.len(); |
| 90 | |
| 91 | changes.push(LineChange::Added { |
| 92 | line_num: line_idx + 1, |
| 93 | line, |
| 94 | tokens, |
| 95 | }); |
| 96 | stats.lines_added += 1; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | DiffOp::Delete { |
| 102 | old_pos, |
| 103 | new_pos: _, |
| 104 | len, |
no test coverage detected