| 654 | } |
| 655 | |
| 656 | pub fn trim_diff(diff: &str) -> String { |
| 657 | let lines: Vec<&str> = diff.split('\n').collect(); |
| 658 | |
| 659 | let content_lines: Vec<&&str> = lines |
| 660 | .iter() |
| 661 | .filter(|line| { |
| 662 | let line = line.to_string(); |
| 663 | (line.starts_with('+') || line.starts_with('-') || line.starts_with(' ')) |
| 664 | && !line.starts_with("---") |
| 665 | && !line.starts_with("+++") |
| 666 | }) |
| 667 | .collect(); |
| 668 | |
| 669 | if content_lines.is_empty() { |
| 670 | return diff.to_string(); |
| 671 | } |
| 672 | |
| 673 | let min_indent = content_lines |
| 674 | .iter() |
| 675 | .filter_map(|line| { |
| 676 | let content = &line[1..]; |
| 677 | if content.trim().is_empty() { |
| 678 | return None; |
| 679 | } |
| 680 | let indent_len = content.chars().take_while(|c| c.is_whitespace()).count(); |
| 681 | Some(indent_len) |
| 682 | }) |
| 683 | .min() |
| 684 | .unwrap_or(0); |
| 685 | |
| 686 | if min_indent == 0 { |
| 687 | return diff.to_string(); |
| 688 | } |
| 689 | |
| 690 | let trimmed_lines: Vec<String> = lines |
| 691 | .iter() |
| 692 | .map(|line| { |
| 693 | if (line.starts_with('+') || line.starts_with('-') || line.starts_with(' ')) |
| 694 | && !line.starts_with("---") |
| 695 | && !line.starts_with("+++") |
| 696 | { |
| 697 | let prefix = &line[0..1]; |
| 698 | let content = &line[1..]; |
| 699 | if content.len() >= min_indent { |
| 700 | format!("{}{}", prefix, &content[min_indent..]) |
| 701 | } else { |
| 702 | line.to_string() |
| 703 | } |
| 704 | } else { |
| 705 | line.to_string() |
| 706 | } |
| 707 | }) |
| 708 | .collect(); |
| 709 | |
| 710 | trimmed_lines.join("\n") |
| 711 | } |
| 712 | |
| 713 | pub fn generate_unified_diff(file_path: &str, old_content: &str, new_content: &str) -> String { |