From 604857ed2e8a4b903e393dcfcfefb9d148b2ff8c Mon Sep 17 00:00:00 2001 From: Hendrik Sollich Date: Fri, 26 Apr 2024 03:47:52 +0200 Subject: [PATCH] vim: Increment search right (#10866) Hi there, nice editor! Here's my attempt at fixing #10865. Thanks Release Notes: -vim: Fix ctrl+a when cursor is on a decimal point ([#10865](https://github.com/zed-industries/zed/issues/10865)). --------- Co-authored-by: Conrad Irwin --- crates/vim/src/normal/increment.rs | 57 +++++++++++++++++-- .../test_data/test_increment_with_dot.json | 5 ++ .../test_increment_with_two_dots.json | 5 ++ 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 crates/vim/test_data/test_increment_with_dot.json create mode 100644 crates/vim/test_data/test_increment_with_two_dots.json diff --git a/crates/vim/src/normal/increment.rs b/crates/vim/src/normal/increment.rs index e70fce99e1..0ceb7c5a6d 100644 --- a/crates/vim/src/normal/increment.rs +++ b/crates/vim/src/normal/increment.rs @@ -117,13 +117,16 @@ fn find_number( ) -> Option<(Range, String, u32)> { let mut offset = start.to_offset(snapshot); - // go backwards to the start of any number the selection is within - for ch in snapshot.reversed_chars_at(offset) { - if ch.is_ascii_digit() || ch == '-' || ch == 'b' || ch == 'x' { - offset -= ch.len_utf8(); - continue; + let ch0 = snapshot.chars_at(offset).next(); + if ch0.as_ref().is_some_and(char::is_ascii_digit) || matches!(ch0, Some('-' | 'b' | 'x')) { + // go backwards to the start of any number the selection is within + for ch in snapshot.reversed_chars_at(offset) { + if ch.is_ascii_digit() || ch == '-' || ch == 'b' || ch == 'x' { + offset -= ch.len_utf8(); + continue; + } + break; } - break; } let mut begin = None; @@ -217,6 +220,48 @@ mod test { .await; } + #[gpui::test] + async fn test_increment_with_dot(cx: &mut gpui::TestAppContext) { + let mut cx = NeovimBackedTestContext::new(cx).await; + + cx.set_shared_state(indoc! {" + 1ˇ.2 + "}) + .await; + + cx.simulate_shared_keystrokes(["ctrl-a"]).await; + cx.assert_shared_state(indoc! {" + 1.ˇ3 + "}) + .await; + cx.simulate_shared_keystrokes(["ctrl-x"]).await; + cx.assert_shared_state(indoc! {" + 1.ˇ2 + "}) + .await; + } + + #[gpui::test] + async fn test_increment_with_two_dots(cx: &mut gpui::TestAppContext) { + let mut cx = NeovimBackedTestContext::new(cx).await; + + cx.set_shared_state(indoc! {" + 111.ˇ.2 + "}) + .await; + + cx.simulate_shared_keystrokes(["ctrl-a"]).await; + cx.assert_shared_state(indoc! {" + 111..ˇ3 + "}) + .await; + cx.simulate_shared_keystrokes(["ctrl-x"]).await; + cx.assert_shared_state(indoc! {" + 111..ˇ2 + "}) + .await; + } + #[gpui::test] async fn test_increment_radix(cx: &mut gpui::TestAppContext) { let mut cx = NeovimBackedTestContext::new(cx).await; diff --git a/crates/vim/test_data/test_increment_with_dot.json b/crates/vim/test_data/test_increment_with_dot.json new file mode 100644 index 0000000000..b5c5b0914e --- /dev/null +++ b/crates/vim/test_data/test_increment_with_dot.json @@ -0,0 +1,5 @@ +{"Put":{"state":"1ˇ.2\n"}} +{"Key":"ctrl-a"} +{"Get":{"state":"1.ˇ3\n","mode":"Normal"}} +{"Key":"ctrl-x"} +{"Get":{"state":"1.ˇ2\n","mode":"Normal"}} diff --git a/crates/vim/test_data/test_increment_with_two_dots.json b/crates/vim/test_data/test_increment_with_two_dots.json new file mode 100644 index 0000000000..38b38f1005 --- /dev/null +++ b/crates/vim/test_data/test_increment_with_two_dots.json @@ -0,0 +1,5 @@ +{"Put":{"state":"111.ˇ.2\n"}} +{"Key":"ctrl-a"} +{"Get":{"state":"111..ˇ3\n","mode":"Normal"}} +{"Key":"ctrl-x"} +{"Get":{"state":"111..ˇ2\n","mode":"Normal"}}