From 5b9d48d723eff66eb99afc72208ab90ba2015bb1 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 21 Aug 2023 15:53:43 +0200 Subject: [PATCH] Avoid diffing when the length is too small --- crates/ai/src/refactor.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/ai/src/refactor.rs b/crates/ai/src/refactor.rs index fc6cbdb8c4..1a1d02cf1f 100644 --- a/crates/ai/src/refactor.rs +++ b/crates/ai/src/refactor.rs @@ -80,13 +80,17 @@ impl RefactoringAssistant { let mut last_old_word_end_ix = 0; 'outer: loop { + const MIN_DIFF_LEN: usize = 50; + let start = new_word_search_start_ix; let mut words = words(&new_text[start..]); while let Some((range, new_word)) = words.next() { // We found a word in the new text that was unique in the old text. We can use // it as a diff boundary, and start applying edits. - if let Some(old_word_end_ix) = unique_old_words.remove(new_word) { - if old_word_end_ix > last_old_word_end_ix { + if let Some(old_word_end_ix) = unique_old_words.get(new_word).copied() { + if old_word_end_ix.saturating_sub(last_old_word_end_ix) + > MIN_DIFF_LEN + { drop(words); let remainder = new_text.split_off(start + range.end);