//! `ep split-commit` implementation.//!//! This command generates a single evaluation example JSON object from a//! chronologically-ordered unified diff (a "commit").//!//! TODO: Port Python code to generate chronologically-ordered commitsuse crate::reorder_patch::{Patch, PatchLine, extract_edits, locate_edited_line};use anyhow::{Context as _, Result};use clap::Args;use rand::Rng;use rand::SeedableRng;use serde::{Deserialize, Serialize};use similar::{DiffTag, TextDiff};use std::collections::BTreeSet;use std::fs;use std::io::{self, Read}; /// `ep split-commit` CLI args.#[derive(Debug, Args)]pub struct SplitCommitArgs { /// Path to the commit file (use "-" for stdin) #[arg(long, short = 'c')] pub commit: String, /// Repository URL #[arg(long, short = 'r', default_value_t = String::new())] pub repository_url: String, /// Commit hash #[arg(long, default_value_t = String::new())] pub commit_hash: String, /// Split point (float 0.0-1.0 for fraction, or integer for index) #[arg(long, short = 's')] pub split_point: Option<String>, /// Random seed for reproducibility #[arg(long)] pub seed: Option<u64>, /// Pretty-print JSON output #[arg(long, short = 'p')] pub pretty: bool,} /// Cursor position in a file.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct CursorPosition { pub file: String, pub line: usize, pub column: usize,} impl std::fmt::Display for CursorPosition { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}:{}:{}", self.file, self.line, self.column) }} /// Represents a split commit with source and target patches.#[derive(Debug, Clone)]pub struct SplitCommit { pub source_patch: String, pub target_patch: String,} /// The evaluation case structure that will be serialized to JSON.#[derive(Debug, Clone, Serialize, Deserialize)]pub struct EvaluationCase { pub repository_url: String, pub commit: String, pub edit_history: Vec<String>, pub cursor_position: String, pub cursor_excerpt: String, pub expected_hunks: Vec<String>, pub expected_patch: String, pub allowed_patch: String, pub expected_context_excerpts: Vec<String>, pub extra: serde_json::Value,} /// Split point specification for evaluation generation.#[derive(Debug, Clone)]pub enum SplitPoint { /// Fraction of total edits (0.0 to 1.0) Fraction(f64), /// Absolute index Index(usize),} fn parse_split_point(value: &str) -> Option<SplitPoint> { if value.contains('.') { value.parse::<f64>().ok().map(SplitPoint::Fraction) } else { value.parse::<usize>().ok().map(SplitPoint::Index) }} /// Entry point for the `ep split-commit` subcommand.////// This runs synchronously and prints a single JSON object to stdout.pub fn run_split_commit(args: &SplitCommitArgs) -> Result<()> { let commit = if args.commit == "-" { let mut content = String::new(); io::stdin() .read_to_string(&mut content) .context("failed to read commit diff from stdin")?; content } else { fs::read_to_string(&args.commit) .with_context(|| format!("failed to read commit diff from {}", args.commit))? }; let split_point = args.split_point.as_deref().and_then(parse_split_point); let case = generate_evaluation_example_from_ordered_commit( &commit, &args.repository_url, &args.commit_hash, split_point, args.seed, ) .context("failed to generate evaluation example")?; let json = if args.pretty { serde_json::to_string_pretty(&case) } else { serde_json::to_string(&case) } .context("failed to serialize evaluation case as JSON")?; println!("{json}"); Ok(())} /// Main function to generate an evaluation example from an ordered commit.////// # Arguments/// * `commit` - Chronologically ordered unified diff of the commit/// * `repository_url` - URL of the repository/// * `commit_hash` - Hash of the commit/// * `split_point` - Point at which the commit will be split (None for random)/// * `seed` - Optional seed for randomnesspub fn generate_evaluation_example_from_ordered_commit( commit: &str, repository_url: &str, commit_hash: &str, split_point: Option<SplitPoint>, seed: Option<u64>,) -> Result<EvaluationCase> { let mut rng: Box<dyn rand::RngCore> = match seed { Some(seed) => Box::new(rand::rngs::StdRng::seed_from_u64(seed)), None => Box::new(rand::rngs::ThreadRng::default()), }; // Parse and normalize the commit let mut patch = Patch::parse_unified_diff(commit); // Filter header to only keep lines starting with "//" let header_lines: Vec<&str> = patch .header .lines() .filter(|line| line.starts_with("//")) .collect(); patch.header = if header_lines.is_empty() { String::new() } else { header_lines.join("\n") + "\n" }; let commit_normalized = patch.to_string(); // Compute the split point let stats = patch.stats(); let num_edits = stats.added + stats.removed; anyhow::ensure!(num_edits != 0, "no edits found in commit"); let split = match split_point { None => rng.random_range(1..=num_edits), Some(SplitPoint::Fraction(f)) => { let v = (f * num_edits as f64).floor() as usize; v.min(num_edits) } Some(SplitPoint::Index(i)) => i.min(num_edits), }; // Split the commit into source and target patches let (prefix, suffix) = split_ordered_commit(&commit_normalized, split); let mut split_commit = SplitCommit { source_patch: prefix, target_patch: suffix, }; // Imitate human edits let human_edit_seed = rng.random_range(1..=10000u64); let (src_patch, tgt_patch, cursor_opt) = imitate_human_edits( &split_commit.source_patch, &split_commit.target_patch, human_edit_seed, ); split_commit.source_patch = src_patch; split_commit.target_patch = tgt_patch; // Sample cursor position let cursor = match cursor_opt { Some(c) => c, None => sample_cursor_position(&patch, &split_commit) .context("failed to sample cursor position")?, }; // Get cursor excerpt let cursor_excerpt = get_cursor_excerpt( &cursor, &split_commit.source_patch, &split_commit.target_patch, ) .context("failed to generate cursor excerpt")?; // Handle edge case where split_point == 0 if split == 0 { split_commit.target_patch = String::new(); } Ok(EvaluationCase { repository_url: repository_url.to_string(), commit: format!("{}~1", commit_hash), edit_history: vec![split_commit.source_patch.clone()], cursor_position: cursor.to_string(), cursor_excerpt, expected_hunks: vec![split_commit.target_patch.clone()], expected_patch: split_commit.target_patch.clone(), allowed_patch: split_commit.target_patch, expected_context_excerpts: vec![], extra: serde_json::json!({}), })} /// Split an ordered commit into source and target commits.////// # Arguments/// * `commit` - Ordered commit string/// * `split_pos` - Position to split the commit (number of edited lines)////// # Returns/// A tuple of (source_diff, target_diff)pub fn split_ordered_commit(commit: &str, split_pos: usize) -> (String, String) { let patch = Patch::parse_unified_diff(commit); let source_edits: BTreeSet<usize> = (0..split_pos).collect(); let (source, target) = extract_edits(&patch, &source_edits); let mut source_str = source.to_string(); let target_str = target.to_string(); // Strip last group header from the source (lines starting with "//" at the end) let source_lines: Vec<&str> = source_str.lines().collect(); let mut end_idx = source_lines.len(); for i in (0..source_lines.len()).rev() { if source_lines[i].starts_with("//") { end_idx = i; } else { break; } } if end_idx < source_lines.len() { source_str = source_lines[..end_idx].join("\n"); if !source_str.is_empty() { source_str.push('\n'); } } (source_str, target_str)} /// Tokenize text into words and non-word characters.fn tokenize(text: &str) -> Vec<String> { let mut tokens = Vec::new(); let mut current = String::new(); for ch in text.chars() { if ch.is_alphanumeric() { current.push(ch); } else if ch == '_' { // Include underscore with the current word, then flush current.push(ch); if !current.is_empty() { tokens.push(std::mem::take(&mut current)); } } else { // Punctuation or whitespace - flush current word first if !current.is_empty() { tokens.push(std::mem::take(&mut current)); } // Each punctuation/whitespace is its own token tokens.push(ch.to_string()); } } if !current.is_empty() { tokens.push(current); } tokens} /// Calculate the weight for a split position based on the character at that position.////// Higher weights indicate more natural pause points (e.g., after punctuation,/// at identifier boundaries). Lower weights indicate less natural points/// (e.g., mid-identifier).fn position_weight(text: &str, pos: usize) -> u32 { if pos == 0 || pos > text.len() { return 1; } let chars: Vec<char> = text.chars().collect(); if pos > chars.len() { return 1; } // Get the character just before this position (what we just "typed") let prev_char = chars[pos - 1]; // High weight: natural pause points (end of statement/argument, opening brackets) if matches!(prev_char, ',' | ';' | ':' | '(' | '[' | '{') { return 10; } // High weight: closing brackets (finished a group) if matches!(prev_char, ')' | ']' | '}') { return 8; } // Medium weight: operators and method chains if matches!( prev_char, '.' | '+' | '-' | '*' | '/' | '=' | '<' | '>' | '&' | '|' | '!' ) { return 5; } // Check if we're at the end of an identifier (word char followed by non-word char) let is_prev_word_char = prev_char.is_alphanumeric() || prev_char == '_'; let is_next_word_char = pos < chars.len() && (chars[pos].is_alphanumeric() || chars[pos] == '_'); if is_prev_word_char && !is_next_word_char { // End of identifier - high weight return 8; } // Whitespace is a natural pause if prev_char.is_whitespace() { return 6; } // Mid-identifier: low weight (rare autocomplete scenarios) if is_prev_word_char && is_next_word_char { return 1; } // Default medium-low weight 3} /// Select a weighted random index from a list of weights.////// Returns an index based on the weights, using the provided seed for/// deterministic selection.fn weighted_select(weights: &[u32], seed: u64) -> usize { if weights.is_empty() { return 0; } let total_weight: u64 = weights.iter().map(|&w| w as u64).sum(); if total_weight == 0 { // Fallback to uniform selection if all weights are zero return seed as usize % weights.len(); } // Use seed to select a value in [0, total_weight) let target = seed % total_weight; let mut cumulative: u64 = 0; for (idx, &weight) in weights.iter().enumerate() { cumulative += weight as u64; if target < cumulative { return idx; } } // Fallback to last index weights.len() - 1} /// Calculate similarity ratio between two strings (0-100).fn fuzzy_ratio(s1: &str, s2: &str) -> u32 { if s1.is_empty() && s2.is_empty() { return 100; } if s1.is_empty() || s2.is_empty() { return 0; } let diff = TextDiff::from_chars(s1, s2); let matching: usize = diff .ops() .iter() .filter_map(|op| { if matches!(op.tag(), DiffTag::Equal) { Some(op.new_range().len()) } else { None } }) .sum(); let total = s1.len() + s2.len(); ((2 * matching * 100) / total) as u32} /// Imitate human edits by introducing partial line edits.////// This function simulates how a human might incrementally type code,/// rather than making complete line replacements.pub fn imitate_human_edits( source_patch: &str, target_patch: &str, seed: u64,) -> (String, String, Option<CursorPosition>) { let no_change = (source_patch.to_string(), target_patch.to_string(), None); let src_patch = Patch::parse_unified_diff(source_patch); let tgt_patch = Patch::parse_unified_diff(target_patch); if tgt_patch.hunks.is_empty() { return no_change; } // Try to locate the first edit in target let tgt_edit_loc = match locate_edited_line(&tgt_patch, 0) { Some(loc) => loc, None => return no_change, }; let tgt_is_addition = matches!(tgt_edit_loc.patch_line, PatchLine::Addition(_)); if !tgt_is_addition { return no_change; } let tgt_line = match &tgt_edit_loc.patch_line { PatchLine::Addition(s) => s.clone(), _ => return no_change, }; // Try to locate the last edit in source let src_edit_loc = locate_edited_line(&src_patch, -1); // Check if source has ANY edit at the same line as target's first edit // We need to iterate through all edits to check this let src_has_edit_at_target_line = { let mut found = false; let mut idx = 0isize; while let Some(loc) = locate_edited_line(&src_patch, idx) { if loc.filename == tgt_edit_loc.filename && loc.target_line_number == tgt_edit_loc.target_line_number { found = true; break; } idx += 1; } found }; // Check if this is a replacement (deletion followed by insertion on the same line) // or a pure insertion (no corresponding deletion in source) let is_replacement = src_edit_loc.as_ref().map_or(false, |loc| { matches!(loc.patch_line, PatchLine::Deletion(_)) && loc.filename == tgt_edit_loc.filename && loc.target_line_number == tgt_edit_loc.target_line_number }); // If source has an edit at the same line but it's not a replacement (i.e., it's an addition), // we shouldn't process this as a pure insertion either if !is_replacement && src_has_edit_at_target_line { return no_change; } let src_line = if is_replacement { match &src_edit_loc.as_ref().unwrap().patch_line { PatchLine::Deletion(s) => s.clone(), _ => return no_change, } } else { // Pure insertion: source line is empty String::new() }; // Don't process if source and target are the sameLines 1–500 of 1465. Load more
//! `ep split-commit` implementation. //! //! This command generates a single evaluation example JSON object from a //! chronologically-ordered unified diff (a "commit"). //! //! TODO: Port Python code to generate chronologically-ordered commits use crate::reorder_patch::{Patch, PatchLine, extract_edits, locate_edited_line}; use anyhow::{Context as _, Result}; use clap::Args; use rand::Rng; use rand::SeedableRng; use serde::{Deserialize, Serialize}; use similar::{DiffTag, TextDiff}; use std::collections::BTreeSet; use std::fs; use std::io::{self, Read}; /// `ep split-commit` CLI args. #[derive(Debug, Args)] pub struct SplitCommitArgs { /// Path to the commit file (use "-" for stdin) #[arg(long, short = 'c')] pub commit: String, /// Repository URL #[arg(long, short = 'r', default_value_t = String::new())] pub repository_url: String, /// Commit hash #[arg(long, default_value_t = String::new())] pub commit_hash: String, /// Split point (float 0.0-1.0 for fraction, or integer for index) #[arg(long, short = 's')] pub split_point: Option<String>, /// Random seed for reproducibility #[arg(long)] pub seed: Option<u64>, /// Pretty-print JSON output #[arg(long, short = 'p')] pub pretty: bool, } /// Cursor position in a file. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct CursorPosition { pub file: String, pub line: usize, pub column: usize, } impl std::fmt::Display for CursorPosition { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}:{}:{}", self.file, self.line, self.column) } } /// Represents a split commit with source and target patches. #[derive(Debug, Clone)] pub struct SplitCommit { pub source_patch: String, pub target_patch: String, } /// The evaluation case structure that will be serialized to JSON. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct EvaluationCase { pub repository_url: String, pub commit: String, pub edit_history: Vec<String>, pub cursor_position: String, pub cursor_excerpt: String, pub expected_hunks: Vec<String>, pub expected_patch: String, pub allowed_patch: String, pub expected_context_excerpts: Vec<String>, pub extra: serde_json::Value, } /// Split point specification for evaluation generation. #[derive(Debug, Clone)] pub enum SplitPoint { /// Fraction of total edits (0.0 to 1.0) Fraction(f64), /// Absolute index Index(usize), } fn parse_split_point(value: &str) -> Option<SplitPoint> { if value.contains('.') { value.parse::<f64>().ok().map(SplitPoint::Fraction) } else { value.parse::<usize>().ok().map(SplitPoint::Index) } } /// Entry point for the `ep split-commit` subcommand. /// /// This runs synchronously and prints a single JSON object to stdout. pub fn run_split_commit(args: &SplitCommitArgs) -> Result<()> { let commit = if args.commit == "-" { let mut content = String::new(); io::stdin() .read_to_string(&mut content) .context("failed to read commit diff from stdin")?; content } else { fs::read_to_string(&args.commit) .with_context(|| format!("failed to read commit diff from {}", args.commit))? }; let split_point = args.split_point.as_deref().and_then(parse_split_point); let case = generate_evaluation_example_from_ordered_commit( &commit, &args.repository_url, &args.commit_hash, split_point, args.seed, ) .context("failed to generate evaluation example")?; let json = if args.pretty { serde_json::to_string_pretty(&case) } else { serde_json::to_string(&case) } .context("failed to serialize evaluation case as JSON")?; println!("{json}"); Ok(()) } /// Main function to generate an evaluation example from an ordered commit. /// /// # Arguments /// * `commit` - Chronologically ordered unified diff of the commit /// * `repository_url` - URL of the repository /// * `commit_hash` - Hash of the commit /// * `split_point` - Point at which the commit will be split (None for random) /// * `seed` - Optional seed for randomness pub fn generate_evaluation_example_from_ordered_commit( commit: &str, repository_url: &str, commit_hash: &str, split_point: Option<SplitPoint>, seed: Option<u64>, ) -> Result<EvaluationCase> { let mut rng: Box<dyn rand::RngCore> = match seed { Some(seed) => Box::new(rand::rngs::StdRng::seed_from_u64(seed)), None => Box::new(rand::rngs::ThreadRng::default()), }; // Parse and normalize the commit let mut patch = Patch::parse_unified_diff(commit); // Filter header to only keep lines starting with "//" let header_lines: Vec<&str> = patch .header .lines() .filter(|line| line.starts_with("//")) .collect(); patch.header = if header_lines.is_empty() { String::new() } else { header_lines.join("\n") + "\n" }; let commit_normalized = patch.to_string(); // Compute the split point let stats = patch.stats(); let num_edits = stats.added + stats.removed; anyhow::ensure!(num_edits != 0, "no edits found in commit"); let split = match split_point { None => rng.random_range(1..=num_edits), Some(SplitPoint::Fraction(f)) => { let v = (f * num_edits as f64).floor() as usize; v.min(num_edits) } Some(SplitPoint::Index(i)) => i.min(num_edits), }; // Split the commit into source and target patches let (prefix, suffix) = split_ordered_commit(&commit_normalized, split); let mut split_commit = SplitCommit { source_patch: prefix, target_patch: suffix, }; // Imitate human edits let human_edit_seed = rng.random_range(1..=10000u64); let (src_patch, tgt_patch, cursor_opt) = imitate_human_edits( &split_commit.source_patch, &split_commit.target_patch, human_edit_seed, ); split_commit.source_patch = src_patch; split_commit.target_patch = tgt_patch; // Sample cursor position let cursor = match cursor_opt { Some(c) => c, None => sample_cursor_position(&patch, &split_commit) .context("failed to sample cursor position")?, }; // Get cursor excerpt let cursor_excerpt = get_cursor_excerpt( &cursor, &split_commit.source_patch, &split_commit.target_patch, ) .context("failed to generate cursor excerpt")?; // Handle edge case where split_point == 0 if split == 0 { split_commit.target_patch = String::new(); } Ok(EvaluationCase { repository_url: repository_url.to_string(), commit: format!("{}~1", commit_hash), edit_history: vec![split_commit.source_patch.clone()], cursor_position: cursor.to_string(), cursor_excerpt, expected_hunks: vec![split_commit.target_patch.clone()], expected_patch: split_commit.target_patch.clone(), allowed_patch: split_commit.target_patch, expected_context_excerpts: vec![], extra: serde_json::json!({}), }) } /// Split an ordered commit into source and target commits. /// /// # Arguments /// * `commit` - Ordered commit string /// * `split_pos` - Position to split the commit (number of edited lines) /// /// # Returns /// A tuple of (source_diff, target_diff) pub fn split_ordered_commit(commit: &str, split_pos: usize) -> (String, String) { let patch = Patch::parse_unified_diff(commit); let source_edits: BTreeSet<usize> = (0..split_pos).collect(); let (source, target) = extract_edits(&patch, &source_edits); let mut source_str = source.to_string(); let target_str = target.to_string(); // Strip last group header from the source (lines starting with "//" at the end) let source_lines: Vec<&str> = source_str.lines().collect(); let mut end_idx = source_lines.len(); for i in (0..source_lines.len()).rev() { if source_lines[i].starts_with("//") { end_idx = i; } else { break; } } if end_idx < source_lines.len() { source_str = source_lines[..end_idx].join("\n"); if !source_str.is_empty() { source_str.push('\n'); } } (source_str, target_str) } /// Tokenize text into words and non-word characters. fn tokenize(text: &str) -> Vec<String> { let mut tokens = Vec::new(); let mut current = String::new(); for ch in text.chars() { if ch.is_alphanumeric() { current.push(ch); } else if ch == '_' { // Include underscore with the current word, then flush current.push(ch); if !current.is_empty() { tokens.push(std::mem::take(&mut current)); } } else { // Punctuation or whitespace - flush current word first if !current.is_empty() { tokens.push(std::mem::take(&mut current)); } // Each punctuation/whitespace is its own token tokens.push(ch.to_string()); } } if !current.is_empty() { tokens.push(current); } tokens } /// Calculate the weight for a split position based on the character at that position. /// /// Higher weights indicate more natural pause points (e.g., after punctuation, /// at identifier boundaries). Lower weights indicate less natural points /// (e.g., mid-identifier). fn position_weight(text: &str, pos: usize) -> u32 { if pos == 0 || pos > text.len() { return 1; } let chars: Vec<char> = text.chars().collect(); if pos > chars.len() { return 1; } // Get the character just before this position (what we just "typed") let prev_char = chars[pos - 1]; // High weight: natural pause points (end of statement/argument, opening brackets) if matches!(prev_char, ',' | ';' | ':' | '(' | '[' | '{') { return 10; } // High weight: closing brackets (finished a group) if matches!(prev_char, ')' | ']' | '}') { return 8; } // Medium weight: operators and method chains if matches!( prev_char, '.' | '+' | '-' | '*' | '/' | '=' | '<' | '>' | '&' | '|' | '!' ) { return 5; } // Check if we're at the end of an identifier (word char followed by non-word char) let is_prev_word_char = prev_char.is_alphanumeric() || prev_char == '_'; let is_next_word_char = pos < chars.len() && (chars[pos].is_alphanumeric() || chars[pos] == '_'); if is_prev_word_char && !is_next_word_char { // End of identifier - high weight return 8; } // Whitespace is a natural pause if prev_char.is_whitespace() { return 6; } // Mid-identifier: low weight (rare autocomplete scenarios) if is_prev_word_char && is_next_word_char { return 1; } // Default medium-low weight 3 } /// Select a weighted random index from a list of weights. /// /// Returns an index based on the weights, using the provided seed for /// deterministic selection. fn weighted_select(weights: &[u32], seed: u64) -> usize { if weights.is_empty() { return 0; } let total_weight: u64 = weights.iter().map(|&w| w as u64).sum(); if total_weight == 0 { // Fallback to uniform selection if all weights are zero return seed as usize % weights.len(); } // Use seed to select a value in [0, total_weight) let target = seed % total_weight; let mut cumulative: u64 = 0; for (idx, &weight) in weights.iter().enumerate() { cumulative += weight as u64; if target < cumulative { return idx; } } // Fallback to last index weights.len() - 1 } /// Calculate similarity ratio between two strings (0-100). fn fuzzy_ratio(s1: &str, s2: &str) -> u32 { if s1.is_empty() && s2.is_empty() { return 100; } if s1.is_empty() || s2.is_empty() { return 0; } let diff = TextDiff::from_chars(s1, s2); let matching: usize = diff .ops() .iter() .filter_map(|op| { if matches!(op.tag(), DiffTag::Equal) { Some(op.new_range().len()) } else { None } }) .sum(); let total = s1.len() + s2.len(); ((2 * matching * 100) / total) as u32 } /// Imitate human edits by introducing partial line edits. /// /// This function simulates how a human might incrementally type code, /// rather than making complete line replacements. pub fn imitate_human_edits( source_patch: &str, target_patch: &str, seed: u64, ) -> (String, String, Option<CursorPosition>) { let no_change = (source_patch.to_string(), target_patch.to_string(), None); let src_patch = Patch::parse_unified_diff(source_patch); let tgt_patch = Patch::parse_unified_diff(target_patch); if tgt_patch.hunks.is_empty() { return no_change; } // Try to locate the first edit in target let tgt_edit_loc = match locate_edited_line(&tgt_patch, 0) { Some(loc) => loc, None => return no_change, }; let tgt_is_addition = matches!(tgt_edit_loc.patch_line, PatchLine::Addition(_)); if !tgt_is_addition { return no_change; } let tgt_line = match &tgt_edit_loc.patch_line { PatchLine::Addition(s) => s.clone(), _ => return no_change, }; // Try to locate the last edit in source let src_edit_loc = locate_edited_line(&src_patch, -1); // Check if source has ANY edit at the same line as target's first edit // We need to iterate through all edits to check this let src_has_edit_at_target_line = { let mut found = false; let mut idx = 0isize; while let Some(loc) = locate_edited_line(&src_patch, idx) { if loc.filename == tgt_edit_loc.filename && loc.target_line_number == tgt_edit_loc.target_line_number { found = true; break; } idx += 1; } found }; // Check if this is a replacement (deletion followed by insertion on the same line) // or a pure insertion (no corresponding deletion in source) let is_replacement = src_edit_loc.as_ref().map_or(false, |loc| { matches!(loc.patch_line, PatchLine::Deletion(_)) && loc.filename == tgt_edit_loc.filename && loc.target_line_number == tgt_edit_loc.target_line_number }); // If source has an edit at the same line but it's not a replacement (i.e., it's an addition), // we shouldn't process this as a pure insertion either if !is_replacement && src_has_edit_at_target_line { return no_change; } let src_line = if is_replacement { match &src_edit_loc.as_ref().unwrap().patch_line { PatchLine::Deletion(s) => s.clone(), _ => return no_change, } } else { // Pure insertion: source line is empty String::new() }; // Don't process if source and target are the same