From d0446857069293c45555b0102ea9d9ebe657cfb7 Mon Sep 17 00:00:00 2001 From: Hans Date: Fri, 19 Jul 2024 01:53:51 +0800 Subject: [PATCH] vim: Adjust surrounding_markers method (#14752) At present, when calculating some ranges, we take the `tuple_windows` to iterate forward, which will cause some problems when the cursor is being front, because `tuple_windows` iteration cannot iterate to the very beginning, so there will be some cases that cannot be calculated, adjust this method, and now it can calculate more perfectly, and the execution speed is about the same Release Notes: - N/A --- crates/vim/src/object.rs | 27 ++++++++++++++++--- ...gleline_surrounding_character_objects.json | 10 +++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/crates/vim/src/object.rs b/crates/vim/src/object.rs index 2392c4a025..fc011b0b91 100644 --- a/crates/vim/src/object.rs +++ b/crates/vim/src/object.rs @@ -838,13 +838,16 @@ fn surrounding_markers( } if opening.is_none() { - for ((ch, range), (before_ch, _)) in movement::chars_before(map, point).tuple_windows() { + let mut chars_before = movement::chars_before(map, point).peekable(); + while let Some((ch, range)) = chars_before.next() { if ch == '\n' && !search_across_lines { break; } - if before_ch == '\\' { - continue; + if let Some((before_ch, _)) = chars_before.peek() { + if *before_ch == '\\' { + continue; + } } if ch == open_marker { @@ -1209,6 +1212,24 @@ mod test { let mut cx = NeovimBackedTestContext::new(cx).await; cx.set_shared_wrap(12).await; + cx.set_shared_state(indoc! { + "\"ˇhello world\"!" + }) + .await; + cx.simulate_shared_keystrokes("v i \"").await; + cx.shared_state().await.assert_eq(indoc! { + "\"«hello worldˇ»\"!" + }); + + cx.set_shared_state(indoc! { + "\"hˇello world\"!" + }) + .await; + cx.simulate_shared_keystrokes("v i \"").await; + cx.shared_state().await.assert_eq(indoc! { + "\"«hello worldˇ»\"!" + }); + cx.set_shared_state(indoc! { "helˇlo \"world\"!" }) diff --git a/crates/vim/test_data/test_singleline_surrounding_character_objects.json b/crates/vim/test_data/test_singleline_surrounding_character_objects.json index f7f95ce697..2499786c72 100644 --- a/crates/vim/test_data/test_singleline_surrounding_character_objects.json +++ b/crates/vim/test_data/test_singleline_surrounding_character_objects.json @@ -1,5 +1,15 @@ {"SetOption":{"value":"wrap"}} {"SetOption":{"value":"columns=12"}} +{"Put":{"state":"\"ˇhello world\"!"}} +{"Key":"v"} +{"Key":"i"} +{"Key":"\""} +{"Get":{"state":"\"«hello worldˇ»\"!","mode":"Visual"}} +{"Put":{"state":"\"hˇello world\"!"}} +{"Key":"v"} +{"Key":"i"} +{"Key":"\""} +{"Get":{"state":"\"«hello worldˇ»\"!","mode":"Visual"}} {"Put":{"state":"helˇlo \"world\"!"}} {"Key":"v"} {"Key":"i"}