From 8e2e00e00377600cd261bd1c43c9476991bd2a5f Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 7 Sep 2023 11:08:07 -0600 Subject: [PATCH] add vim-specific J (with repeatability) --- assets/keymaps/vim.json | 1 + crates/vim/src/normal.rs | 11 ++++- crates/vim/src/test.rs | 49 +++++++++++++++++++++++ crates/vim/test_data/test_join_lines.json | 13 ++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 crates/vim/test_data/test_join_lines.json diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.json index f7fdb57d9d..45891adee6 100644 --- a/assets/keymaps/vim.json +++ b/assets/keymaps/vim.json @@ -451,6 +451,7 @@ "~": "vim::ChangeCase", "shift-i": "vim::InsertBefore", "shift-a": "vim::InsertAfter", + "shift-j": "vim::JoinLines", "r": [ "vim::PushOperator", "Replace" diff --git a/crates/vim/src/normal.rs b/crates/vim/src/normal.rs index d328f663c5..63bfe9bd1e 100644 --- a/crates/vim/src/normal.rs +++ b/crates/vim/src/normal.rs @@ -110,11 +110,18 @@ pub fn init(cx: &mut AppContext) { cx.add_action(|_: &mut Workspace, _: &JoinLines, cx| { Vim::update(cx, |vim, cx| { vim.record_current_action(cx); - let times = vim.pop_number_operator(cx).unwrap_or(1); + let mut times = vim.pop_number_operator(cx).unwrap_or(1); + if vim.state().mode.is_visual() { + times = 1; + } else if times > 1 { + // 2J joins two lines together (same as J or 1J) + times -= 1; + } + vim.update_active_editor(cx, |editor, cx| { editor.transact(cx, |editor, cx| { for _ in 0..times { - editor.join_lines(editor::JoinLines, cx) + editor.join_lines(&Default::default(), cx) } }) }) diff --git a/crates/vim/src/test.rs b/crates/vim/src/test.rs index c6a212d77f..9d3a141a9e 100644 --- a/crates/vim/src/test.rs +++ b/crates/vim/src/test.rs @@ -286,6 +286,55 @@ async fn test_word_characters(cx: &mut gpui::TestAppContext) { ) } +#[gpui::test] +async fn test_join_lines(cx: &mut gpui::TestAppContext) { + let mut cx = NeovimBackedTestContext::new(cx).await; + + cx.set_shared_state(indoc! {" + ˇone + two + three + four + five + six + "}) + .await; + cx.simulate_shared_keystrokes(["shift-j"]).await; + cx.assert_shared_state(indoc! {" + oneˇ two + three + four + five + six + "}) + .await; + cx.simulate_shared_keystrokes(["3", "shift-j"]).await; + cx.assert_shared_state(indoc! {" + one two threeˇ four + five + six + "}) + .await; + + cx.set_shared_state(indoc! {" + ˇone + two + three + four + five + six + "}) + .await; + cx.simulate_shared_keystrokes(["j", "v", "3", "j", "shift-j"]) + .await; + cx.assert_shared_state(indoc! {" + one + two three fourˇ five + six + "}) + .await; +} + #[gpui::test] async fn test_wrapped_lines(cx: &mut gpui::TestAppContext) { let mut cx = NeovimBackedTestContext::new(cx).await; diff --git a/crates/vim/test_data/test_join_lines.json b/crates/vim/test_data/test_join_lines.json new file mode 100644 index 0000000000..b4bc5c30e1 --- /dev/null +++ b/crates/vim/test_data/test_join_lines.json @@ -0,0 +1,13 @@ +{"Put":{"state":"ˇone\ntwo\nthree\nfour\nfive\nsix\n"}} +{"Key":"shift-j"} +{"Get":{"state":"oneˇ two\nthree\nfour\nfive\nsix\n","mode":"Normal"}} +{"Key":"3"} +{"Key":"shift-j"} +{"Get":{"state":"one two threeˇ four\nfive\nsix\n","mode":"Normal"}} +{"Put":{"state":"ˇone\ntwo\nthree\nfour\nfive\nsix\n"}} +{"Key":"j"} +{"Key":"v"} +{"Key":"3"} +{"Key":"j"} +{"Key":"shift-j"} +{"Get":{"state":"one\ntwo three fourˇ five\nsix\n","mode":"Normal"}}