From b9256dd469c093266a4e1f4368ad1039ba4c5be1 Mon Sep 17 00:00:00 2001 From: Gilles Peiffer Date: Tue, 3 Jun 2025 23:07:14 +0200 Subject: [PATCH] editor: Apply `common_prefix_len` refactor suggestion (#31957) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds João's nice suggestion from https://github.com/zed-industries/zed/pull/31818#discussion_r2118582616. Release Notes: - N/A --------- Co-authored-by: João Marcos --- crates/editor/src/editor.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index b0336c8140b3f2f6f1062078b27f8e38c1b42df3..06cdc68ce6ba8e75e680dc91a118e640a2cffb4a 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -5550,14 +5550,12 @@ impl Editor { } } - let mut common_prefix_len = 0; - for (a, b) in old_text.chars().zip(new_text.chars()) { - if a == b { - common_prefix_len += a.len_utf8(); - } else { - break; - } - } + let common_prefix_len = old_text + .chars() + .zip(new_text.chars()) + .take_while(|(a, b)| a == b) + .map(|(a, _)| a.len_utf8()) + .sum::(); cx.emit(EditorEvent::InputHandled { utf16_range_to_replace: None,