helix: Allow yank without a selection (#35612)

Related https://github.com/zed-industries/zed/issues/4642

Release Notes:
- Helix: without active selection, pressing `y` in helix mode will yank
a single character under cursor.

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
Romans Malinovskis 2025-08-14 18:04:01 +01:00 committed by GitHub
parent 528d56e807
commit 20be133713
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 100 additions and 1 deletions

View file

@ -15,6 +15,8 @@ actions!(
[
/// Switches to normal mode after the cursor (Helix-style).
HelixNormalAfter,
/// Yanks the current selection or character if no selection.
HelixYank,
/// Inserts at the beginning of the selection.
HelixInsert,
/// Appends at the end of the selection.
@ -26,6 +28,7 @@ pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
Vim::action(editor, cx, Vim::helix_normal_after);
Vim::action(editor, cx, Vim::helix_insert);
Vim::action(editor, cx, Vim::helix_append);
Vim::action(editor, cx, Vim::helix_yank);
}
impl Vim {
@ -310,6 +313,47 @@ impl Vim {
}
}
pub fn helix_yank(&mut self, _: &HelixYank, window: &mut Window, cx: &mut Context<Self>) {
self.update_editor(cx, |vim, editor, cx| {
let has_selection = editor
.selections
.all_adjusted(cx)
.iter()
.any(|selection| !selection.is_empty());
if !has_selection {
// If no selection, expand to current character (like 'v' does)
editor.change_selections(Default::default(), window, cx, |s| {
s.move_with(|map, selection| {
let head = selection.head();
let new_head = movement::saturating_right(map, head);
selection.set_tail(head, SelectionGoal::None);
selection.set_head(new_head, SelectionGoal::None);
});
});
vim.yank_selections_content(
editor,
crate::motion::MotionKind::Exclusive,
window,
cx,
);
editor.change_selections(Default::default(), window, cx, |s| {
s.move_with(|_map, selection| {
selection.collapse_to(selection.start, SelectionGoal::None);
});
});
} else {
// Yank the selection(s)
vim.yank_selections_content(
editor,
crate::motion::MotionKind::Exclusive,
window,
cx,
);
}
});
}
fn helix_insert(&mut self, _: &HelixInsert, window: &mut Window, cx: &mut Context<Self>) {
self.start_recording(cx);
self.update_editor(cx, |_, editor, cx| {
@ -703,4 +747,29 @@ mod test {
cx.assert_state("«xxˇ»", Mode::HelixNormal);
}
#[gpui::test]
async fn test_helix_yank(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.enable_helix();
// Test yanking current character with no selection
cx.set_state("hello ˇworld", Mode::HelixNormal);
cx.simulate_keystrokes("y");
// Test cursor remains at the same position after yanking single character
cx.assert_state("hello ˇworld", Mode::HelixNormal);
cx.shared_clipboard().assert_eq("w");
// Move cursor and yank another character
cx.simulate_keystrokes("l");
cx.simulate_keystrokes("y");
cx.shared_clipboard().assert_eq("o");
// Test yanking with existing selection
cx.set_state("hello «worlˇ»d", Mode::HelixNormal);
cx.simulate_keystrokes("y");
cx.shared_clipboard().assert_eq("worl");
cx.assert_state("hello «worlˇ»d", Mode::HelixNormal);
}
}