diff --git a/src/myers/preprocess.rs b/src/myers/preprocess.rs index 9985c2a..af2bf6e 100644 --- a/src/myers/preprocess.rs +++ b/src/myers/preprocess.rs @@ -135,7 +135,7 @@ fn should_prune_common_line(token_status: &[Occurrences], pos: usize) -> bool { let mut unmatched_before = 0; let mut common_before = 0; - let start = if pos > WINDOW_SIZE { WINDOW_SIZE } else { 0 }; + let start = pos.saturating_sub(WINDOW_SIZE); for status in token_status[start..pos].iter().rev() { match status { Occurrences::None => { @@ -176,3 +176,23 @@ fn should_prune_common_line(token_status: &[Occurrences], pos: usize) -> bool { unmatched > 3 * common } + +#[cfg(test)] +mod tests { + use super::{should_prune_common_line, Occurrences}; + + #[test] + fn common_line_pruning_ignores_distant_context() { + let mut token_status = vec![Occurrences::Some; 700]; + token_status[100..400].fill(Occurrences::None); + token_status[400..450].fill(Occurrences::None); + token_status[450..500].fill(Occurrences::Common); + token_status[500..550].fill(Occurrences::Common); + token_status[550..600].fill(Occurrences::None); + + assert!( + !should_prune_common_line(&token_status, 500), + "only the last 100 items before the current line should contribute to the backward scan" + ); + } +} diff --git a/src/myers/slice.rs b/src/myers/slice.rs index fce3e97..526b615 100644 --- a/src/myers/slice.rs +++ b/src/myers/slice.rs @@ -27,7 +27,7 @@ impl<'a> FileSlice<'a> { } } - pub fn borrow(&'_ mut self) -> FileSlice<'_> { + pub fn borrow(&mut self) -> FileSlice<'_> { FileSlice { tokens: self.tokens, changed: self.changed,