From be45f32753f982a242c983916805f5f22e958492 Mon Sep 17 00:00:00 2001 From: Vishal Bhavsar Date: Fri, 19 Jul 2024 00:34:40 -0400 Subject: [PATCH] vim: Fix 'Y' to yank to end of line (#14783) Instead of yanking the entire line. Release Notes: - vim: Updated `Y` to yank to end of line (like neovim) https://github.com/zed-industries/zed/issues/14771 --- assets/keymaps/vim.json | 2 +- crates/vim/src/normal.rs | 26 ++++++++++++++++++++++++++ crates/vim/test_data/test_shift_y.json | 4 ++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 crates/vim/test_data/test_shift_y.json diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.json index cb48d14d12c3f63ede9bd25fe5fd93aab345f902..a65e1e298bc31aae8eb10d114275def26c9841af 100644 --- a/assets/keymaps/vim.json +++ b/assets/keymaps/vim.json @@ -215,7 +215,7 @@ "shift-d": "vim::DeleteToEndOfLine", "shift-j": "vim::JoinLines", "y": ["vim::PushOperator", "Yank"], - "shift-y": "vim::YankLine", + "shift-y": "vim::YankToEndOfLine", "i": "vim::InsertBefore", "shift-i": "vim::InsertFirstNonWhitespace", "a": "vim::InsertAfter", diff --git a/crates/vim/src/normal.rs b/crates/vim/src/normal.rs index 781c6bb98a99ddd3ab3fe63330bdfbd9b1a68535..749428159e35c788e5f0f03755f4cd65707017dc 100644 --- a/crates/vim/src/normal.rs +++ b/crates/vim/src/normal.rs @@ -58,6 +58,7 @@ actions!( DeleteToEndOfLine, Yank, YankLine, + YankToEndOfLine, ChangeCase, ConvertToUpperCase, ConvertToLowerCase, @@ -82,6 +83,7 @@ pub(crate) fn register(workspace: &mut Workspace, cx: &mut ViewContext) { }) } +fn yank_to_end_of_line(_: &mut Workspace, _: &YankToEndOfLine, cx: &mut ViewContext) { + Vim::update(cx, |vim, cx| { + vim.record_current_action(cx); + let count = vim.take_count(cx); + yank_motion( + vim, + motion::Motion::EndOfLine { + display_lines: false, + }, + count, + cx, + ) + }) +} + fn toggle_comments(_: &mut Workspace, _: &ToggleComments, cx: &mut ViewContext) { Vim::update(cx, |vim, cx| { vim.record_current_action(cx); @@ -1434,6 +1451,15 @@ mod test { ); } + #[gpui::test] + async fn test_shift_y(cx: &mut gpui::TestAppContext) { + let mut cx = NeovimBackedTestContext::new(cx).await; + + cx.set_shared_state("helˇlo\n").await; + cx.simulate_shared_keystrokes("shift-y").await; + cx.shared_clipboard().await.assert_eq("lo"); + } + #[gpui::test] async fn test_r(cx: &mut gpui::TestAppContext) { let mut cx = NeovimBackedTestContext::new(cx).await; diff --git a/crates/vim/test_data/test_shift_y.json b/crates/vim/test_data/test_shift_y.json new file mode 100644 index 0000000000000000000000000000000000000000..53038a69e9fb7d26e4556d50f3ac9622e0cadd30 --- /dev/null +++ b/crates/vim/test_data/test_shift_y.json @@ -0,0 +1,4 @@ +{"Put":{"state":"helˇlo\n"}} +{"Key":"shift-y"} +{"Get":{"state":"helˇlo\n","mode":"Normal"}} +{"ReadRegister":{"name":"\"","value":"lo"}}