From 768c991909afe93e1b7a540c3537b75d107c2c6d Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Wed, 27 Sep 2023 23:09:09 -0600 Subject: [PATCH] vim: Fix some dw edge cases --- crates/vim/src/motion.rs | 16 +++++ crates/vim/src/normal/change.rs | 6 -- crates/vim/src/normal/delete.rs | 64 +++++++++++++++---- .../src/test/neovim_backed_test_context.rs | 2 - .../vim/test_data/test_command_replace.json | 7 -- crates/vim/test_data/test_delete_w.json | 8 +++ 6 files changed, 75 insertions(+), 28 deletions(-) diff --git a/crates/vim/src/motion.rs b/crates/vim/src/motion.rs index fbec127843..a197121626 100644 --- a/crates/vim/src/motion.rs +++ b/crates/vim/src/motion.rs @@ -467,6 +467,22 @@ impl Motion { (_, selection.end) = map.next_line_boundary(selection.end.to_point(map)); } else { + // Another special case: When using the "w" motion in combination with an + // operator and the last word moved over is at the end of a line, the end of + // that word becomes the end of the operated text, not the first word in the + // next line. + if let Motion::NextWordStart { + ignore_punctuation: _, + } = self + { + let start_row = selection.start.to_point(&map).row; + if selection.end.to_point(&map).row > start_row { + selection.end = + Point::new(start_row, map.buffer_snapshot.line_len(start_row)) + .to_display_point(&map) + } + } + // If the motion is exclusive and the end of the motion is in column 1, the // end of the motion is moved to the end of the previous line and the motion // becomes inclusive. Example: "}" moves to the first line after a paragraph, diff --git a/crates/vim/src/normal/change.rs b/crates/vim/src/normal/change.rs index e9f3001392..6704fb7594 100644 --- a/crates/vim/src/normal/change.rs +++ b/crates/vim/src/normal/change.rs @@ -76,12 +76,6 @@ pub fn change_object(vim: &mut Vim, object: Object, around: bool, cx: &mut Windo // word does not include the following white space. {Vi: "cw" when on a blank // followed by other blanks changes only the first blank; this is probably a // bug, because "dw" deletes all the blanks} -// -// NOT HANDLED YET -// Another special case: When using the "w" motion in combination with an -// operator and the last word moved over is at the end of a line, the end of -// that word becomes the end of the operated text, not the first word in the -// next line. fn expand_changed_word_selection( map: &DisplaySnapshot, selection: &mut Selection, diff --git a/crates/vim/src/normal/delete.rs b/crates/vim/src/normal/delete.rs index 848e9f725d..517ece8033 100644 --- a/crates/vim/src/normal/delete.rs +++ b/crates/vim/src/normal/delete.rs @@ -2,6 +2,7 @@ use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim} use collections::{HashMap, HashSet}; use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias}; use gpui::WindowContext; +use language::Point; pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option, cx: &mut WindowContext) { vim.stop_recording(); @@ -14,6 +15,27 @@ pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option, cx: &m let original_head = selection.head(); original_columns.insert(selection.id, original_head.column()); motion.expand_selection(map, selection, times, true); + + // Motion::NextWordStart on an empty line should delete it. + if let Motion::NextWordStart { + ignore_punctuation: _, + } = motion + { + if selection.is_empty() + && map + .buffer_snapshot + .line_len(selection.start.to_point(&map).row) + == 0 + { + selection.end = map + .buffer_snapshot + .clip_point( + Point::new(selection.start.to_point(&map).row + 1, 0), + Bias::Left, + ) + .to_display_point(map) + } + } }); }); copy_selections_content(editor, motion.linewise(), cx); @@ -129,28 +151,44 @@ mod test { #[gpui::test] async fn test_delete_w(cx: &mut gpui::TestAppContext) { - let mut cx = NeovimBackedTestContext::new(cx).await.binding(["d", "w"]); - cx.assert("Teˇst").await; - cx.assert("Tˇest test").await; - cx.assert(indoc! {" - Test teˇst - test"}) - .await; - cx.assert(indoc! {" + let mut cx = NeovimBackedTestContext::new(cx).await; + cx.assert_neovim_compatible( + indoc! {" Test tesˇt - test"}) - .await; - cx.assert_exempted( + test"}, + ["d", "w"], + ) + .await; + + cx.assert_neovim_compatible("Teˇst", ["d", "w"]).await; + cx.assert_neovim_compatible("Tˇest test", ["d", "w"]).await; + cx.assert_neovim_compatible( + indoc! {" + Test teˇst + test"}, + ["d", "w"], + ) + .await; + cx.assert_neovim_compatible( + indoc! {" + Test tesˇt + test"}, + ["d", "w"], + ) + .await; + + cx.assert_neovim_compatible( indoc! {" Test test ˇ test"}, - ExemptionFeatures::DeleteWordOnEmptyLine, + ["d", "w"], ) .await; let mut cx = cx.binding(["d", "shift-w"]); - cx.assert("Test teˇst-test test").await; + cx.assert_neovim_compatible("Test teˇst-test test", ["d", "shift-w"]) + .await; } #[gpui::test] diff --git a/crates/vim/src/test/neovim_backed_test_context.rs b/crates/vim/src/test/neovim_backed_test_context.rs index 53aa536c2f..c6a77d13ea 100644 --- a/crates/vim/src/test/neovim_backed_test_context.rs +++ b/crates/vim/src/test/neovim_backed_test_context.rs @@ -23,8 +23,6 @@ pub enum ExemptionFeatures { // MOTIONS // When an operator completes at the end of the file, an extra newline is left OperatorLastNewlineRemains, - // Deleting a word on an empty line doesn't remove the newline - DeleteWordOnEmptyLine, // OBJECTS // Resulting position after the operation is slightly incorrect for unintuitive reasons. diff --git a/crates/vim/test_data/test_command_replace.json b/crates/vim/test_data/test_command_replace.json index 91827c0285..13928d5c7e 100644 --- a/crates/vim/test_data/test_command_replace.json +++ b/crates/vim/test_data/test_command_replace.json @@ -20,10 +20,3 @@ {"Key":"0"} {"Key":"enter"} {"Get":{"state":"aa\ndd\nˇcc","mode":"Normal"}} -{"Key":":"} -{"Key":"%"} -{"Key":"s"} -{"Key":"/"} -{"Key":"/"} -{"Key":"/"} -{"Key":"enter"} diff --git a/crates/vim/test_data/test_delete_w.json b/crates/vim/test_data/test_delete_w.json index c385ccb546..e5e48ca444 100644 --- a/crates/vim/test_data/test_delete_w.json +++ b/crates/vim/test_data/test_delete_w.json @@ -1,3 +1,7 @@ +{"Put":{"state":"Test tesˇt\n test"}} +{"Key":"d"} +{"Key":"w"} +{"Get":{"state":"Test teˇs\n test","mode":"Normal"}} {"Put":{"state":"Teˇst"}} {"Key":"d"} {"Key":"w"} @@ -14,6 +18,10 @@ {"Key":"d"} {"Key":"w"} {"Get":{"state":"Test teˇs\ntest","mode":"Normal"}} +{"Put":{"state":"Test test\nˇ\ntest"}} +{"Key":"d"} +{"Key":"w"} +{"Get":{"state":"Test test\nˇtest","mode":"Normal"}} {"Put":{"state":"Test teˇst-test test"}} {"Key":"d"} {"Key":"shift-w"}