| 1 | use regex::Regex; |
| 2 | |
| 3 | use unicode_segmentation::UnicodeSegmentation; |
| 4 | use unicode_width::UnicodeWidthStr; |
| 5 | |
| 6 | use crate::align; |
| 7 | use crate::minusplus::MinusPlus; |
| 8 | |
| 9 | /// Infer the edit operations responsible for the differences between a collection of old and new |
| 10 | /// lines. A "line" is a string. An annotated line is a Vec of (op, &str) pairs, where the &str |
| 11 | /// slices are slices of the line, and their concatenation equals the line. Return the input minus |
| 12 | /// and plus lines, in annotated form. |
| 13 | /// |
| 14 | /// Also return a specification of the inferred alignment of minus and plus lines: a paired minus |
| 15 | /// and plus line is represented in this alignment specification as |
| 16 | /// (Some(minus_line_index),Some(plus_line_index)), whereas an unpaired minus line is |
| 17 | /// (Some(minus_line_index), None). |
| 18 | /// |
| 19 | /// `noop_deletions[i]` is the appropriate deletion operation tag to be used for `minus_lines[i]`; |
| 20 | /// `noop_deletions` is guaranteed to be the same length as `minus_lines`. The equivalent statements |
| 21 | /// hold for `plus_insertions` and `plus_lines`. |
| 22 | #[allow(clippy::too_many_arguments)] |
| 23 | #[allow(clippy::type_complexity)] |
| 24 | pub fn infer_edits<'a, EditOperation>( |
| 25 | minus_lines: Vec<&'a str>, |
| 26 | plus_lines: Vec<&'a str>, |
| 27 | noop_deletions: Vec<EditOperation>, |
| 28 | deletion: EditOperation, |
| 29 | noop_insertions: Vec<EditOperation>, |
| 30 | insertion: EditOperation, |
| 31 | tokenization_regex: &Regex, |
| 32 | max_line_distance: f64, |
| 33 | max_line_distance_for_naively_paired_lines: f64, |
| 34 | ) -> ( |
| 35 | Vec<Vec<(EditOperation, &'a str)>>, // annotated minus lines |
| 36 | Vec<Vec<(EditOperation, &'a str)>>, // annotated plus lines |
| 37 | Vec<(Option<usize>, Option<usize>)>, // line alignment |
| 38 | ) |
| 39 | where |
| 40 | EditOperation: Copy + PartialEq + std::fmt::Debug, |
| 41 | { |
| 42 | let mut annotated_minus_lines = Vec::<Vec<(EditOperation, &str)>>::new(); |
| 43 | let mut annotated_plus_lines = Vec::<Vec<(EditOperation, &str)>>::new(); |
| 44 | let mut line_alignment = Vec::<(Option<usize>, Option<usize>)>::new(); |
| 45 | |
| 46 | let mut plus_index = 0; // plus lines emitted so far |
| 47 | |
| 48 | 'minus_lines_loop: for (minus_index, minus_line) in minus_lines.iter().enumerate() { |
| 49 | let mut considered = 0; // plus lines considered so far as match for minus_line |
| 50 | for plus_line in &plus_lines[plus_index..] { |
| 51 | let alignment = align::Alignment::new( |
| 52 | tokenize(minus_line, tokenization_regex), |
| 53 | tokenize(plus_line, tokenization_regex), |
| 54 | ); |
| 55 | let (annotated_minus_line, annotated_plus_line, distance) = annotate( |
| 56 | alignment, |
| 57 | noop_deletions[minus_index], |
| 58 | deletion, |
| 59 | noop_insertions[plus_index], |
| 60 | insertion, |
| 61 | minus_line, |
| 62 | plus_line, |
| 63 | ); |
| 64 | if minus_lines.len() == plus_lines.len() |
| 65 | && distance <= max_line_distance_for_naively_paired_lines |
| 66 | || distance <= max_line_distance |
| 67 | { |
| 68 | // minus_line and plus_line are inferred to be a homologous pair. |
| 69 | |
| 70 | // Emit as unpaired the plus lines already considered and rejected |
| 71 | for plus_line in &plus_lines[plus_index..(plus_index + considered)] { |
| 72 | annotated_plus_lines.push(vec![(noop_insertions[plus_index], plus_line)]); |
| 73 | line_alignment.push((None, Some(plus_index))); |
| 74 | plus_index += 1; |
| 75 | } |
| 76 | annotated_minus_lines.push(annotated_minus_line); |
| 77 | annotated_plus_lines.push(annotated_plus_line); |
| 78 | line_alignment.push((Some(minus_index), Some(plus_index))); |
| 79 | plus_index += 1; |
| 80 | |
| 81 | // Greedy: move on to the next minus line. |
| 82 | continue 'minus_lines_loop; |
| 83 | } else { |
| 84 | considered += 1; |
| 85 | } |
| 86 | } |
| 87 | // No homolog was found for minus i; emit as unpaired. |
| 88 | annotated_minus_lines.push(vec![(noop_deletions[minus_index], minus_line)]); |
| 89 | line_alignment.push((Some(minus_index), None)); |
| 90 | } |
| 91 | // Emit any remaining plus lines |
| 92 | for plus_line in &plus_lines[plus_index..] { |
| 93 | if let Some(content) = get_contents_before_trailing_whitespace(plus_line) { |
| 94 | annotated_plus_lines.push(vec![ |
| 95 | (noop_insertions[plus_index], content), |
| 96 | (noop_insertions[plus_index], &plus_line[content.len()..]), |
| 97 | ]); |
| 98 | } else { |
| 99 | annotated_plus_lines.push(vec![(noop_insertions[plus_index], plus_line)]); |
| 100 | } |
| 101 | line_alignment.push((None, Some(plus_index))); |
| 102 | plus_index += 1; |
| 103 | } |
| 104 | |
| 105 | (annotated_minus_lines, annotated_plus_lines, line_alignment) |
| 106 | } |
| 107 | |
| 108 | // Return `None` if there is no trailing whitespace. |
| 109 | // Return `Some(content)` where content is trimmed if there was some trailing whitespace |
| 110 | fn get_contents_before_trailing_whitespace(line: &str) -> Option<&str> { |
| 111 | let content = line.trim_end(); |
| 112 | // if line has a trailing newline, do not consider it as a 'trailing whitespace' |
| 113 | if !content.is_empty() && content != line.trim_end_matches('\n') { |
| 114 | Some(content) |
| 115 | } else { |
| 116 | None |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Return boolean arrays indicating whether each line has a homolog (is "paired"). |
| 121 | pub fn make_lines_have_homolog( |
| 122 | line_alignment: &[(Option<usize>, Option<usize>)], |
| 123 | ) -> MinusPlus<Vec<bool>> { |
| 124 | MinusPlus::new( |
| 125 | line_alignment |
| 126 | .iter() |
| 127 | .filter(|(m, _)| m.is_some()) |
| 128 | .map(|(_, p)| p.is_some()) |
| 129 | .collect(), |
| 130 | line_alignment |
| 131 | .iter() |
| 132 | .filter(|(_, p)| p.is_some()) |
| 133 | .map(|(m, _)| m.is_some()) |
| 134 | .collect(), |
| 135 | ) |
| 136 | } |
| 137 | |
| 138 | /// Split line into tokens for alignment. The alignment algorithm aligns sequences of substrings; |
| 139 | /// not individual characters. |
| 140 | fn tokenize<'a>(line: &'a str, regex: &Regex) -> Vec<&'a str> { |
| 141 | // Starting with "", see comment in Alignment::new(). Historical note: Replacing the '+/-' |
| 142 | // prefix with a space implicitly generated this. |
| 143 | let mut tokens = vec![""]; |
| 144 | let mut offset = 0; |
| 145 | for m in regex.find_iter(line) { |
| 146 | if offset == 0 && m.start() > 0 { |
| 147 | tokens.push(""); |
| 148 | } |
| 149 | // Align separating text as multiple single-character tokens. |
| 150 | for t in line[offset..m.start()].graphemes(true) { |
| 151 | tokens.push(t); |
| 152 | } |
| 153 | tokens.push(&line[m.start()..m.end()]); |
| 154 | offset = m.end(); |
| 155 | } |
| 156 | if offset < line.len() { |
| 157 | if offset == 0 { |
| 158 | tokens.push(""); |
| 159 | } |
| 160 | for t in line[offset..line.len()].graphemes(true) { |
| 161 | tokens.push(t); |
| 162 | } |
| 163 | } |
| 164 | tokens |
| 165 | } |
| 166 | |
| 167 | /// Use alignment to "annotate" minus and plus lines. An "annotated" line is a sequence of |
| 168 | /// (a: Annotation, s: &str) pairs, where the &strs reference the memory |
| 169 | /// of the original line and their concatenation equals the line. |
| 170 | // This function doesn't return "coalesced" annotations: i.e. they're often are runs of consecutive |
| 171 | // occurrences of the same operation. Since it is returning &strs pointing into the memory of the |
| 172 | // original line, it's not possible to coalesce them in this function. |
| 173 | #[allow(clippy::type_complexity)] |
| 174 | fn annotate<'a, Annotation>( |
| 175 | alignment: align::Alignment<'a>, |
| 176 | noop_deletion: Annotation, |
| 177 | deletion: Annotation, |
| 178 | noop_insertion: Annotation, |
| 179 | insertion: Annotation, |
| 180 | minus_line: &'a str, |
| 181 | plus_line: &'a str, |
| 182 | ) -> (Vec<(Annotation, &'a str)>, Vec<(Annotation, &'a str)>, f64) |
| 183 | where |
| 184 | Annotation: Copy + PartialEq + std::fmt::Debug, |
| 185 | { |
| 186 | let mut annotated_minus_line = Vec::new(); |
| 187 | let mut annotated_plus_line = Vec::new(); |
| 188 | |
| 189 | let (mut x_offset, mut y_offset) = (0, 0); |
| 190 | let (mut minus_line_offset, mut plus_line_offset) = (0, 0); |
| 191 | let (mut d_numer, mut d_denom) = (0, 0); |
| 192 | |
| 193 | // Note that the inputs to align::Alignment are not the original strings themselves, but |
| 194 | // sequences of substrings derived from the tokenization process. We have just applied |
| 195 | // run_length_encoding to "coalesce" runs of the same edit operation into a single |
| 196 | // operation. We now need to form a &str, pointing into the memory of the original line, |
| 197 | // identifying a "section" which is the concatenation of the substrings involved in this |
| 198 | // coalesced operation. That's what the following closures do. Note that they must be called |
| 199 | // once only since they advance offset pointers. |
| 200 | let get_section = |n: usize, |
| 201 | line_offset: &mut usize, |
| 202 | substrings_offset: &mut usize, |
| 203 | substrings: &[&str], |
| 204 | line: &'a str| { |
| 205 | let section_length = substrings[*substrings_offset..*substrings_offset + n] |
| 206 | .iter() |
| 207 | .fold(0, |n, s| n + s.len()); |
| 208 | let old_offset = *line_offset; |
| 209 | *line_offset += section_length; |
| 210 | *substrings_offset += n; |
| 211 | &line[old_offset..*line_offset] |
| 212 | }; |
| 213 | let mut minus_section = |n: usize, offset: &mut usize| { |
| 214 | get_section(n, &mut minus_line_offset, offset, &alignment.x, minus_line) |
| 215 | }; |
| 216 | let mut plus_section = |n: usize, offset: &mut usize| { |
| 217 | get_section(n, &mut plus_line_offset, offset, &alignment.y, plus_line) |
| 218 | }; |
| 219 | let distance_contribution = |section: &str| UnicodeWidthStr::width(section.trim()); |
| 220 | |
| 221 | let (mut minus_op_prev, mut plus_op_prev) = (noop_deletion, noop_insertion); |
| 222 | for (op, n) in alignment.coalesced_operations() { |
| 223 | match op { |
| 224 | align::Operation::Deletion => { |
| 225 | let minus_section = minus_section(n, &mut x_offset); |
| 226 | let n_d = distance_contribution(minus_section); |
| 227 | d_denom += n_d; |
| 228 | d_numer += n_d; |
| 229 | annotated_minus_line.push((deletion, minus_section)); |
| 230 | minus_op_prev = deletion; |
| 231 | } |
| 232 | align::Operation::NoOp => { |
| 233 | let minus_section = minus_section(n, &mut x_offset); |
| 234 | let n_d = distance_contribution(minus_section); |
| 235 | d_denom += 2 * n_d; |
| 236 | let is_space = minus_section.trim().is_empty(); |
| 237 | let coalesce_space_with_previous = is_space |
| 238 | && ((minus_op_prev == deletion |
| 239 | && plus_op_prev == insertion |
| 240 | && (x_offset < alignment.x.len() - 1 || y_offset < alignment.y.len() - 1)) |
| 241 | || (minus_op_prev == noop_deletion && plus_op_prev == noop_insertion)); |
| 242 | annotated_minus_line.push(( |
| 243 | if coalesce_space_with_previous { |
| 244 | minus_op_prev |
| 245 | } else { |
| 246 | noop_deletion |
| 247 | }, |
| 248 | minus_section, |
| 249 | )); |
| 250 | let op = if coalesce_space_with_previous { |
| 251 | plus_op_prev |
| 252 | } else { |
| 253 | noop_insertion |
| 254 | }; |
| 255 | let plus_section = plus_section(n, &mut y_offset); |
| 256 | if let Some(non_whitespace) = get_contents_before_trailing_whitespace(plus_section) |
| 257 | { |
| 258 | annotated_plus_line.push((op, non_whitespace)); |
| 259 | annotated_plus_line.push((op, &plus_section[non_whitespace.len()..])); |
| 260 | } else { |
| 261 | annotated_plus_line.push((op, plus_section)); |
| 262 | } |
| 263 | minus_op_prev = noop_deletion; |
| 264 | plus_op_prev = noop_insertion; |
| 265 | } |
| 266 | align::Operation::Insertion => { |
| 267 | let plus_section = plus_section(n, &mut y_offset); |
| 268 | let n_d = distance_contribution(plus_section); |
| 269 | d_denom += n_d; |
| 270 | d_numer += n_d; |
| 271 | annotated_plus_line.push((insertion, plus_section)); |
| 272 | plus_op_prev = insertion; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | ( |
| 277 | annotated_minus_line, |
| 278 | annotated_plus_line, |
| 279 | compute_distance(d_numer as f64, d_denom as f64), |
| 280 | ) |
| 281 | } |
| 282 | |
| 283 | fn compute_distance(d_numer: f64, d_denom: f64) -> f64 { |
| 284 | if d_denom > 0.0 { |
| 285 | d_numer / d_denom |
| 286 | } else { |
| 287 | 0.0 |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | #[cfg(test)] |
| 292 | mod tests { |
| 293 | use super::*; |
| 294 | use itertools::Itertools; |
| 295 | use lazy_static::lazy_static; |
| 296 | use unicode_segmentation::UnicodeSegmentation; |
| 297 | |
| 298 | lazy_static! { |
| 299 | static ref DEFAULT_TOKENIZATION_REGEXP: Regex = Regex::new(r#"\w+"#).unwrap(); |
| 300 | } |
| 301 | |
| 302 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 303 | enum EditOperation { |
| 304 | MinusNoop, |
| 305 | PlusNoop, |
| 306 | Deletion, |
| 307 | Insertion, |
| 308 | } |
| 309 | |
| 310 | type Annotation<'a> = (EditOperation, &'a str); |
| 311 | type AnnotatedLine<'a> = Vec<Annotation<'a>>; |
| 312 | type AnnotatedLines<'a> = Vec<AnnotatedLine<'a>>; |
| 313 | type Edits<'a> = (AnnotatedLines<'a>, AnnotatedLines<'a>); |
| 314 | |
| 315 | use EditOperation::*; |
| 316 | |
| 317 | #[test] |
| 318 | fn test_tokenize_0() { |
| 319 | assert_tokenize("", &[]); |
| 320 | assert_tokenize(";", &["", ";"]); |
| 321 | assert_tokenize(";;", &["", ";", ";"]); |
| 322 | assert_tokenize(";;a", &["", ";", ";", "a"]); |
| 323 | assert_tokenize(";;ab", &["", ";", ";", "ab"]); |
| 324 | assert_tokenize(";;ab;", &["", ";", ";", "ab", ";"]); |
| 325 | assert_tokenize(";;ab;;", &["", ";", ";", "ab", ";", ";"]); |
| 326 | } |
| 327 | |
| 328 | #[test] |
| 329 | fn test_tokenize_1() { |
| 330 | assert_tokenize("aaa bbb", &["aaa", " ", "bbb"]) |
| 331 | } |
| 332 | |
| 333 | #[test] |
| 334 | fn test_tokenize_2() { |
| 335 | assert_tokenize( |
| 336 | "fn coalesce_edits<'a, EditOperation>(", |
| 337 | &[ |
| 338 | "fn", |
| 339 | " ", |
| 340 | "coalesce_edits", |
| 341 | "<", |
| 342 | "'", |
| 343 | "a", |
| 344 | ",", |
| 345 | " ", |
| 346 | "EditOperation", |
| 347 | ">", |
| 348 | "(", |
| 349 | ], |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | #[test] |
| 354 | fn test_tokenize_3() { |
| 355 | assert_tokenize( |
| 356 | "fn coalesce_edits<'a, 'b, EditOperation>(", |
| 357 | &[ |
| 358 | "fn", |
| 359 | " ", |
| 360 | "coalesce_edits", |
| 361 | "<", |
| 362 | "'", |
| 363 | "a", |
| 364 | ",", |
| 365 | " ", |
| 366 | "'", |
| 367 | "b", |
| 368 | ",", |
| 369 | " ", |
| 370 | "EditOperation", |
| 371 | ">", |
| 372 | "(", |
| 373 | ], |
| 374 | ); |
| 375 | } |
| 376 | |
| 377 | #[test] |
| 378 | fn test_tokenize_4() { |
| 379 | assert_tokenize( |
| 380 | "annotated_plus_lines.push(vec![(noop_insertion, plus_line)]);", |
| 381 | &[ |
| 382 | "annotated_plus_lines", |
| 383 | ".", |
| 384 | "push", |
| 385 | "(", |
| 386 | "vec", |
| 387 | "!", |
| 388 | "[", |
| 389 | "(", |
| 390 | "noop_insertion", |
| 391 | ",", |
| 392 | " ", |
| 393 | "plus_line", |
| 394 | ")", |
| 395 | "]", |
| 396 | ")", |
| 397 | ";", |
| 398 | ], |
| 399 | ); |
| 400 | } |
| 401 | |
| 402 | #[test] |
| 403 | fn test_tokenize_5() { |
| 404 | assert_tokenize( |
| 405 | " let col = Color::from_str(s).unwrap_or_else(|_| die());", |
| 406 | &[ |
| 407 | "", |
| 408 | " ", |
| 409 | " ", |
| 410 | " ", |
| 411 | " ", |
| 412 | " ", |
| 413 | " ", |
| 414 | " ", |
| 415 | " ", |
| 416 | " ", |
| 417 | "let", |
| 418 | " ", |
| 419 | "col", |
| 420 | " ", |
| 421 | "=", |
| 422 | " ", |
| 423 | "Color", |
| 424 | ":", |
| 425 | ":", |
| 426 | "from_str", |
| 427 | "(", |
| 428 | "s", |
| 429 | ")", |
| 430 | ".", |
| 431 | "unwrap_or_else", |
| 432 | "(", |
| 433 | "|", |
| 434 | "_", |
| 435 | "|", |
| 436 | " ", |
| 437 | "die", |
| 438 | "(", |
| 439 | ")", |
| 440 | ")", |
| 441 | ";", |
| 442 | ], |
| 443 | ) |
| 444 | } |
| 445 | |
| 446 | #[test] |
| 447 | fn test_tokenize_6() { |
| 448 | assert_tokenize( |
| 449 | " (minus_file, plus_file) => format!(\"renamed: {} ⟶ {}\", minus_file, plus_file),", |
| 450 | &["", |
| 451 | " ", |
| 452 | " ", |
| 453 | " ", |
| 454 | " ", |
| 455 | " ", |
| 456 | " ", |
| 457 | " ", |
| 458 | " ", |
| 459 | " ", |
| 460 | "(", |
| 461 | "minus_file", |
| 462 | ",", |
| 463 | " ", |
| 464 | "plus_file", |
| 465 | ")", |
| 466 | " ", |
| 467 | "=", |
| 468 | ">", |
| 469 | " ", |
| 470 | "format", |
| 471 | "!", |
| 472 | "(", |
| 473 | "\"", |
| 474 | "renamed", |
| 475 | ":", |
| 476 | " ", |
| 477 | "{", |
| 478 | "}", |
| 479 | " ", |
| 480 | "⟶", |
| 481 | " ", |
| 482 | " ", |
| 483 | "{", |
| 484 | "}", |
| 485 | "\"", |
| 486 | ",", |
| 487 | " ", |
| 488 | "minus_file", |
| 489 | ",", |
| 490 | " ", |
| 491 | "plus_file", |
| 492 | ")", |
| 493 | ","]) |
| 494 | } |
| 495 | |
| 496 | fn assert_tokenize(text: &str, expected_tokens: &[&str]) { |
| 497 | let actual_tokens = tokenize(text, &DEFAULT_TOKENIZATION_REGEXP); |
| 498 | assert_eq!(text, expected_tokens.iter().join("")); |
| 499 | // tokenize() guarantees that the first element of the token stream is "". |
| 500 | // See comment in Alignment::new() |
| 501 | assert_eq!(actual_tokens[0], ""); |
| 502 | assert_eq!(&actual_tokens[1..], expected_tokens); |
| 503 | } |
| 504 | |
| 505 | #[test] |
| 506 | fn test_infer_edits_1() { |
| 507 | assert_paired_edits( |
| 508 | vec!["aaa"], |
| 509 | vec!["aba"], |
| 510 | ( |
| 511 | vec![vec![(MinusNoop, ""), (Deletion, "aaa")]], |
| 512 | vec![vec![(PlusNoop, ""), (Insertion, "aba")]], |
| 513 | ), |
| 514 | ) |
| 515 | } |
| 516 | |
| 517 | #[test] |
| 518 | fn test_infer_edits_1_2() { |
| 519 | assert_paired_edits( |
| 520 | vec!["aaa ccc"], |
| 521 | vec!["aba ccc"], |
| 522 | ( |
| 523 | vec![vec![ |
| 524 | (MinusNoop, ""), |
| 525 | (Deletion, "aaa"), |
| 526 | (MinusNoop, " ccc"), |
| 527 | ]], |
| 528 | vec![vec![(PlusNoop, ""), (Insertion, "aba"), (PlusNoop, " ccc")]], |
| 529 | ), |
| 530 | ) |
| 531 | } |
| 532 | |
| 533 | #[test] |
| 534 | fn test_infer_edits_2() { |
| 535 | assert_paired_edits( |
| 536 | vec!["áaa"], |
| 537 | vec!["ááb"], |
| 538 | ( |
| 539 | vec![vec![(MinusNoop, ""), (Deletion, "áaa")]], |
| 540 | vec![vec![(PlusNoop, ""), (Insertion, "ááb")]], |
| 541 | ), |
| 542 | ) |
| 543 | } |
| 544 | |
| 545 | #[test] |
| 546 | fn test_infer_edits_3() { |
| 547 | assert_paired_edits( |
| 548 | vec!["d.iteritems()"], |
| 549 | vec!["d.items()"], |
| 550 | ( |
| 551 | vec![vec![ |
| 552 | (MinusNoop, "d."), |
| 553 | (Deletion, "iteritems"), |
| 554 | (MinusNoop, "()"), |
| 555 | ]], |
| 556 | vec![vec![ |
| 557 | (PlusNoop, "d."), |
| 558 | (Insertion, "items"), |
| 559 | (PlusNoop, "()"), |
| 560 | ]], |
| 561 | ), |
| 562 | ) |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn test_infer_edits_4() { |
| 567 | assert_edits( |
| 568 | vec!["á a a á a a á a a", "á á b á á b á á b"], |
| 569 | vec!["á á b á á c á á b"], |
| 570 | ( |
| 571 | vec![ |
| 572 | vec![(MinusNoop, "á a a á a a á a a")], |
| 573 | vec![ |
| 574 | (MinusNoop, "á á b á á "), |
| 575 | (Deletion, "b"), |
| 576 | (MinusNoop, " á á b"), |
| 577 | ], |
| 578 | ], |
| 579 | vec![vec![ |
| 580 | (PlusNoop, "á á b á á"), |
| 581 | (PlusNoop, " "), |
| 582 | (Insertion, "c"), |
| 583 | (PlusNoop, " á á b"), |
| 584 | ]], |
| 585 | ), |
| 586 | 0.66, |
| 587 | ) |
| 588 | } |
| 589 | |
| 590 | #[test] |
| 591 | fn test_infer_edits_5() { |
| 592 | assert_edits( |
| 593 | vec!["aaaa a aaa", "bbbb b bbb", "cccc c ccc"], |
| 594 | vec!["bbbb ! bbb", "dddd d ddd", "cccc ! ccc"], |
| 595 | ( |
| 596 | vec![ |
| 597 | vec![(MinusNoop, "aaaa a aaa")], |
| 598 | vec![(MinusNoop, "bbbb "), (Deletion, "b"), (MinusNoop, " bbb")], |
| 599 | vec![(MinusNoop, "cccc "), (Deletion, "c"), (MinusNoop, " ccc")], |
| 600 | ], |
| 601 | vec![ |
| 602 | vec![ |
| 603 | (PlusNoop, "bbbb"), |
| 604 | (PlusNoop, " "), |
| 605 | (Insertion, "!"), |
| 606 | (PlusNoop, " bbb"), |
| 607 | ], |
| 608 | vec![(PlusNoop, "dddd d ddd")], |
| 609 | vec![ |
| 610 | (PlusNoop, "cccc"), |
| 611 | (PlusNoop, " "), |
| 612 | (Insertion, "!"), |
| 613 | (PlusNoop, " ccc"), |
| 614 | ], |
| 615 | ], |
| 616 | ), |
| 617 | 0.66, |
| 618 | ) |
| 619 | } |
| 620 | |
| 621 | #[test] |
| 622 | fn test_infer_edits_6() { |
| 623 | assert_no_edits( |
| 624 | vec![ |
| 625 | " let mut i = 0;", |
| 626 | " for ((_, c0), (_, c1)) in s0.zip(s1) {", |
| 627 | " if c0 != c1 {", |
| 628 | " break;", |
| 629 | " } else {", |
| 630 | " i += c0.len();", |
| 631 | " }", |
| 632 | " }", |
| 633 | " i", |
| 634 | ], |
| 635 | vec![ |
| 636 | " s0.zip(s1)", |
| 637 | " .take_while(|((_, c0), (_, c1))| c0 == c1) // TODO: Don't consume one-past-the-end!", |
| 638 | " .fold(0, |offset, ((_, c0), (_, _))| offset + c0.len())" |
| 639 | ], 0.5) |
| 640 | } |
| 641 | |
| 642 | #[test] |
| 643 | fn test_infer_edits_7() { |
| 644 | assert_edits( |
| 645 | vec!["fn coalesce_edits<'a, EditOperation>("], |
| 646 | vec!["fn coalesce_edits<'a, 'b, EditOperation>("], |
| 647 | ( |
| 648 | vec![vec![ |
| 649 | (MinusNoop, "fn coalesce_edits<'a, "), |
| 650 | (MinusNoop, "EditOperation>("), |
| 651 | ]], |
| 652 | vec![vec![ |
| 653 | (PlusNoop, "fn coalesce_edits<'a,"), |
| 654 | (PlusNoop, " "), |
| 655 | (Insertion, "'b, "), |
| 656 | (PlusNoop, "EditOperation>("), |
| 657 | ]], |
| 658 | ), |
| 659 | 0.66, |
| 660 | ) |
| 661 | } |
| 662 | |
| 663 | #[test] |
| 664 | fn test_infer_edits_8() { |
| 665 | assert_edits( |
| 666 | vec!["for _ in range(0, options[\"count\"]):"], |
| 667 | vec!["for _ in range(0, int(options[\"count\"])):"], |
| 668 | ( |
| 669 | vec![vec![ |
| 670 | (MinusNoop, "for _ in range(0, "), |
| 671 | (MinusNoop, "options[\"count\"])"), |
| 672 | (MinusNoop, ":"), |
| 673 | ]], |
| 674 | vec![vec![ |
| 675 | (PlusNoop, "for _ in range(0,"), |
| 676 | (PlusNoop, " "), |
| 677 | (Insertion, "int("), |
| 678 | (PlusNoop, "options[\"count\"])"), |
| 679 | (Insertion, ")"), |
| 680 | (PlusNoop, ":"), |
| 681 | ]], |
| 682 | ), |
| 683 | 0.3, |
| 684 | ) |
| 685 | } |
| 686 | |
| 687 | #[test] |
| 688 | fn test_infer_edits_9() { |
| 689 | assert_edits( |
| 690 | vec!["a a"], |
| 691 | vec!["a b a"], |
| 692 | ( |
| 693 | vec![vec![(MinusNoop, "a "), (MinusNoop, "a")]], |
| 694 | vec![vec![ |
| 695 | (PlusNoop, "a"), |
| 696 | (PlusNoop, " "), |
| 697 | (Insertion, "b "), |
| 698 | (PlusNoop, "a"), |
| 699 | ]], |
| 700 | ), |
| 701 | 1.0, |
| 702 | ); |
| 703 | assert_edits( |
| 704 | vec!["a a"], |
| 705 | vec!["a b b a"], |
| 706 | ( |
| 707 | vec![vec![(MinusNoop, "a "), (MinusNoop, "a")]], |
| 708 | vec![vec![ |
| 709 | (PlusNoop, "a"), |
| 710 | (PlusNoop, " "), |
| 711 | (Insertion, "b b "), |
| 712 | (PlusNoop, "a"), |
| 713 | ]], |
| 714 | ), |
| 715 | 1.0, |
| 716 | ); |
| 717 | } |
| 718 | |
| 719 | #[test] |
| 720 | fn test_infer_edits_10() { |
| 721 | assert_edits( |
| 722 | vec!["so it is safe to read the commit number from any one of them."], |
| 723 | vec!["so it is safe to read build info from any one of them."], |
| 724 | ( |
| 725 | // TODO: Coalesce runs of the same operation. |
| 726 | vec![vec![ |
| 727 | (MinusNoop, "so it is safe to read "), |
| 728 | (Deletion, "the commit"), |
| 729 | (Deletion, " "), |
| 730 | (Deletion, "number"), |
| 731 | (MinusNoop, " from any one of them."), |
| 732 | ]], |
| 733 | vec![vec![ |
| 734 | (PlusNoop, "so it is safe to read"), |
| 735 | (PlusNoop, " "), |
| 736 | (Insertion, "build"), |
| 737 | (Insertion, " "), |
| 738 | (Insertion, "info"), |
| 739 | (PlusNoop, " from any one of them."), |
| 740 | ]], |
| 741 | ), |
| 742 | 1.0, |
| 743 | ); |
| 744 | } |
| 745 | |
| 746 | #[test] |
| 747 | fn test_infer_edits_11() { |
| 748 | assert_edits( |
| 749 | vec![" self.table[index] ="], |
| 750 | vec![" self.table[index] = candidates"], |
| 751 | ( |
| 752 | vec![vec![(MinusNoop, " self.table[index] =")]], |
| 753 | vec![vec![ |
| 754 | (PlusNoop, " self.table[index] ="), |
| 755 | (Insertion, " candidates"), |
| 756 | ]], |
| 757 | ), |
| 758 | 1.0, |
| 759 | ); |
| 760 | } |
| 761 | |
| 762 | #[test] |
| 763 | fn test_infer_edits_12() { |
| 764 | assert_edits( |
| 765 | vec![" (xxxxxxxxx, \"build info\"),"], |
| 766 | vec![" (xxxxxxxxx, \"build\"),"], |
| 767 | ( |
| 768 | vec![vec![ |
| 769 | (MinusNoop, " (xxxxxxxxx, \"build"), |
| 770 | (Deletion, " info"), |
| 771 | (MinusNoop, "\"),"), |
| 772 | ]], |
| 773 | vec![vec![ |
| 774 | (PlusNoop, " (xxxxxxxxx, \"build"), |
| 775 | (PlusNoop, "\"),"), |
| 776 | ]], |
| 777 | ), |
| 778 | 1.0, |
| 779 | ); |
| 780 | } |
| 781 | |
| 782 | #[test] |
| 783 | fn test_infer_edits_13() { |
| 784 | assert_paired_edits( |
| 785 | vec!["'b '", "[element,]"], |
| 786 | vec!["' b'", "[element],"], |
| 787 | ( |
| 788 | vec![ |
| 789 | vec![ |
| 790 | (MinusNoop, "'"), |
| 791 | (Deletion, "b"), |
| 792 | (MinusNoop, " "), |
| 793 | (MinusNoop, "'"), |
| 794 | ], |
| 795 | vec![(MinusNoop, "[element"), (Deletion, ","), (MinusNoop, "]")], |
| 796 | ], |
| 797 | vec![ |
| 798 | vec![ |
| 799 | (PlusNoop, "'"), |
| 800 | (PlusNoop, " "), |
| 801 | (Insertion, "b"), |
| 802 | (PlusNoop, "'"), |
| 803 | ], |
| 804 | vec![(PlusNoop, "[element"), (PlusNoop, "]"), (Insertion, ",")], |
| 805 | ], |
| 806 | ), |
| 807 | ); |
| 808 | } |
| 809 | |
| 810 | #[test] |
| 811 | fn test_infer_edits_14() { |
| 812 | assert_edits( |
| 813 | vec!["a b c d ", "p "], |
| 814 | vec!["x y c z ", "q r"], |
| 815 | ( |
| 816 | vec![ |
| 817 | vec![ |
| 818 | (MinusNoop, ""), |
| 819 | (Deletion, "a"), |
| 820 | (Deletion, " "), |
| 821 | (Deletion, "b"), |
| 822 | (MinusNoop, " c "), |
| 823 | (Deletion, "d"), |
| 824 | (MinusNoop, " "), |
| 825 | ], |
| 826 | vec![(MinusNoop, ""), (Deletion, "p"), (Deletion, " ")], |
| 827 | ], |
| 828 | vec![ |
| 829 | vec![ |
| 830 | (PlusNoop, ""), |
| 831 | (Insertion, "x"), |
| 832 | (Insertion, " "), |
| 833 | (Insertion, "y"), |
| 834 | (PlusNoop, " c"), |
| 835 | (PlusNoop, " "), |
| 836 | (Insertion, "z"), |
| 837 | (PlusNoop, " "), |
| 838 | ], |
| 839 | vec![ |
| 840 | (PlusNoop, ""), |
| 841 | (Insertion, "q"), |
| 842 | (Insertion, " "), |
| 843 | (Insertion, "r"), |
| 844 | ], |
| 845 | ], |
| 846 | ), |
| 847 | 1.0, |
| 848 | ); |
| 849 | } |
| 850 | |
| 851 | #[test] |
| 852 | fn test_infer_edits_15() { |
| 853 | assert_paired_edits( |
| 854 | vec![r#"printf "%s\n" s y y | git add -p &&"#], |
| 855 | vec!["test_write_lines s y y | git add -p &&"], |
| 856 | ( |
| 857 | vec![vec![ |
| 858 | (MinusNoop, ""), |
| 859 | (Deletion, r#"printf "%s\n""#), |
| 860 | (MinusNoop, " s y y | git add -p &&"), |
| 861 | ]], |
| 862 | vec![vec![ |
| 863 | (PlusNoop, ""), |
| 864 | (Insertion, "test_write_lines"), |
| 865 | (PlusNoop, " s y y | git add -p &&"), |
| 866 | ]], |
| 867 | ), |
| 868 | ); |
| 869 | } |
| 870 | |
| 871 | #[test] |
| 872 | fn test_infer_edits_16() { |
| 873 | assert_edits( |
| 874 | vec!["a a a a a a b b b"], |
| 875 | vec!["c a a a a a a c c"], |
| 876 | ( |
| 877 | vec![vec![ |
| 878 | (MinusNoop, ""), |
| 879 | (MinusNoop, "a a a a a a "), |
| 880 | (Deletion, "b b"), |
| 881 | (Deletion, " "), |
| 882 | (Deletion, "b"), |
| 883 | ]], |
| 884 | vec![vec![ |
| 885 | (PlusNoop, ""), |
| 886 | (Insertion, "c "), |
| 887 | (PlusNoop, "a a a a a a"), |
| 888 | (PlusNoop, " "), |
| 889 | (Insertion, "c"), |
| 890 | (Insertion, " "), |
| 891 | (Insertion, "c"), |
| 892 | ]], |
| 893 | ), |
| 894 | 1.0, |
| 895 | ); |
| 896 | } |
| 897 | |
| 898 | fn assert_edits( |
| 899 | minus_lines: Vec<&str>, |
| 900 | plus_lines: Vec<&str>, |
| 901 | expected_edits: Edits, |
| 902 | max_line_distance: f64, |
| 903 | ) { |
| 904 | let (minus_lines, noop_deletions): (Vec<&str>, Vec<EditOperation>) = |
| 905 | minus_lines.into_iter().map(|s| (s, MinusNoop)).unzip(); |
| 906 | let (plus_lines, noop_insertions): (Vec<&str>, Vec<EditOperation>) = |
| 907 | plus_lines.into_iter().map(|s| (s, PlusNoop)).unzip(); |
| 908 | let actual_edits = infer_edits( |
| 909 | minus_lines, |
| 910 | plus_lines, |
| 911 | noop_deletions, |
| 912 | Deletion, |
| 913 | noop_insertions, |
| 914 | Insertion, |
| 915 | &DEFAULT_TOKENIZATION_REGEXP, |
| 916 | max_line_distance, |
| 917 | 0.0, |
| 918 | ); |
| 919 | // compare_annotated_lines(actual_edits, expected_edits); |
| 920 | // TODO: test line alignment |
| 921 | assert_eq!((actual_edits.0, actual_edits.1), expected_edits); |
| 922 | } |
| 923 | |
| 924 | // Assert that no edits are inferred for the supplied minus and plus lines. |
| 925 | fn assert_no_edits(minus_lines: Vec<&str>, plus_lines: Vec<&str>, max_line_distance: f64) { |
| 926 | let expected_edits = ( |
| 927 | minus_lines.iter().map(|s| vec![(MinusNoop, *s)]).collect(), |
| 928 | plus_lines.iter().map(|s| vec![(PlusNoop, *s)]).collect(), |
| 929 | ); |
| 930 | assert_edits(minus_lines, plus_lines, expected_edits, max_line_distance) |
| 931 | } |
| 932 | |
| 933 | // Assertions for a single pair of lines, considered as a homologous pair. We set |
| 934 | // max_line_distance = 1.0 in order that the pair will be inferred to be homologous. |
| 935 | fn assert_paired_edits(minus_lines: Vec<&str>, plus_lines: Vec<&str>, expected_edits: Edits) { |
| 936 | assert_consistent_pairs(&expected_edits); |
| 937 | assert_edits(minus_lines, plus_lines, expected_edits, 1.0); |
| 938 | } |
| 939 | |
| 940 | fn assert_consistent_pairs(edits: &Edits) { |
| 941 | let (minus_annotated_lines, plus_annotated_lines) = edits; |
| 942 | |
| 943 | for (minus_annotated_line, plus_annotated_line) in |
| 944 | minus_annotated_lines.iter().zip(plus_annotated_lines) |
| 945 | { |
| 946 | let (minus_total, minus_delta) = summarize_annotated_line(minus_annotated_line); |
| 947 | let (plus_total, plus_delta) = summarize_annotated_line(plus_annotated_line); |
| 948 | assert_eq!( |
| 949 | minus_total - minus_delta, |
| 950 | plus_total - plus_delta, |
| 951 | "\nInconsistent edits:\n \ |
| 952 | {:?}\n \ |
| 953 | \tminus_total - minus_delta = {} - {} = {}\n \ |
| 954 | {:?}\n \ |
| 955 | \tplus_total - plus_delta = {} - {} = {}\n", |
| 956 | minus_annotated_line, |
| 957 | minus_total, |
| 958 | minus_delta, |
| 959 | minus_total - minus_delta, |
| 960 | plus_annotated_line, |
| 961 | plus_total, |
| 962 | plus_delta, |
| 963 | plus_total - plus_delta |
| 964 | ); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | fn summarize_annotated_line(sections: &AnnotatedLine) -> (usize, usize) { |
| 969 | let mut total = 0; |
| 970 | let mut delta = 0; |
| 971 | for (edit, s) in sections { |
| 972 | let length = s.graphemes(true).count(); |
| 973 | total += length; |
| 974 | if is_edit(edit) { |
| 975 | delta += length; |
| 976 | } |
| 977 | } |
| 978 | (total, delta) |
| 979 | } |
| 980 | |
| 981 | // For debugging test failures: |
| 982 | |
| 983 | #[allow(dead_code)] |
| 984 | fn compare_annotated_lines(actual: Edits, expected: Edits) { |
| 985 | let (minus, plus) = actual; |
| 986 | println!("\n\nactual minus:"); |
| 987 | print_annotated_lines(minus); |
| 988 | println!("\nactual plus:"); |
| 989 | print_annotated_lines(plus); |
| 990 | |
| 991 | let (minus, plus) = expected; |
| 992 | println!("\n\nexpected minus:"); |
| 993 | print_annotated_lines(minus); |
| 994 | println!("\nexpected plus:"); |
| 995 | print_annotated_lines(plus); |
| 996 | } |
| 997 | |
| 998 | #[allow(dead_code)] |
| 999 | fn print_annotated_lines(annotated_lines: AnnotatedLines) { |
| 1000 | for annotated_line in annotated_lines { |
| 1001 | print_annotated_line(annotated_line); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | #[allow(dead_code)] |
| 1006 | fn print_annotated_line(annotated_line: AnnotatedLine) { |
| 1007 | for (edit, s) in annotated_line { |
| 1008 | print!("({} {}), ", fmt_edit(edit), s.trim_end()); |
| 1009 | } |
| 1010 | println!(); |
| 1011 | } |
| 1012 | |
| 1013 | #[allow(dead_code)] |
| 1014 | fn fmt_edit(edit: EditOperation) -> &'static str { |
| 1015 | match edit { |
| 1016 | MinusNoop => "MinusNoop", |
| 1017 | Deletion => "Deletion", |
| 1018 | PlusNoop => "PlusNoop", |
| 1019 | Insertion => "Insertion", |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | fn is_edit(edit: &EditOperation) -> bool { |
| 1024 | *edit == Deletion || *edit == Insertion |
| 1025 | } |
| 1026 | } |