Skip to content

Commit

Permalink
vim: Fix VisualYankLine (zed-industries#22416)
Browse files Browse the repository at this point in the history
Closes zed-industries#22388

Release Notes:

- Fixed Visual Mode Use `Y` Yank Line

---------

Co-authored-by: Conrad Irwin <[email protected]>
  • Loading branch information
0x2CA and ConradIrwin authored Jan 7, 2025
1 parent 7d0c571 commit e7ca39d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion assets/keymaps/vim.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
"shift-d": "vim::VisualDeleteLine",
"shift-x": "vim::VisualDeleteLine",
"y": "vim::VisualYank",
"shift-y": "vim::VisualYank",
"shift-y": "vim::VisualYankLine",
"p": "vim::Paste",
"shift-p": ["vim::Paste", { "preserveClipboard": true }],
"s": "vim::Substitute",
Expand Down
28 changes: 24 additions & 4 deletions crates/vim/src/visual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
vim.record_current_action(cx);
vim.visual_delete(true, cx);
});
Vim::action(editor, cx, |vim, _: &VisualYank, cx| vim.visual_yank(cx));
Vim::action(editor, cx, |vim, _: &VisualYank, cx| {
vim.visual_yank(false, cx)
});
Vim::action(editor, cx, |vim, _: &VisualYankLine, cx| {
vim.visual_yank(true, cx)
});

Vim::action(editor, cx, Vim::select_next);
Vim::action(editor, cx, Vim::select_previous);
Expand Down Expand Up @@ -506,10 +511,11 @@ impl Vim {
self.switch_mode(Mode::Normal, true, cx);
}

pub fn visual_yank(&mut self, cx: &mut ViewContext<Self>) {
pub fn visual_yank(&mut self, line_mode: bool, cx: &mut ViewContext<Self>) {
self.store_visual_marks(cx);
self.update_editor(cx, |vim, editor, cx| {
let line_mode = editor.selections.line_mode;
let line_mode = line_mode || editor.selections.line_mode;
editor.selections.line_mode = line_mode;
vim.yank_selections_content(editor, line_mode, cx);
editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| {
Expand Down Expand Up @@ -670,7 +676,7 @@ impl Vim {
self.stop_recording(cx);
self.visual_delete(false, cx)
}
Some(Operator::Yank) => self.visual_yank(cx),
Some(Operator::Yank) => self.visual_yank(false, cx),
_ => {} // Ignoring other operators
}
}
Expand Down Expand Up @@ -1386,6 +1392,20 @@ mod test {
});
}

#[gpui::test]
async fn test_shift_y(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;

cx.set_shared_state(indoc! {
"The ˇquick brown\n"
})
.await;
cx.simulate_shared_keystrokes("v i w shift-y").await;
cx.shared_clipboard().await.assert_eq(indoc! {
"The quick brown\n"
});
}

#[gpui::test]
async fn test_gv(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
Expand Down
7 changes: 7 additions & 0 deletions crates/vim/test_data/test_shift_y.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{"Put":{"state":"The ˇquick brown\n"}}
{"Key":"v"}
{"Key":"i"}
{"Key":"w"}
{"Key":"shift-y"}
{"Get":{"state":"ˇThe quick brown\n","mode":"Normal"}}
{"ReadRegister":{"name":"\"","value":"The quick brown\n"}}

0 comments on commit e7ca39d

Please sign in to comment.