Add commands to select next/previous siblings in the syntax tree (#35053)

Closes #5133 and discussion
https://github.com/zed-industries/zed/discussions/33493

This PR adds two new commands to select next/previous siblings in the
syntax tree. These commands were modelled after the existing ones about
expand/shrink selection. With this PR I've added new key bindings
inspired by `helix` for previous / next / expand / shrink selections.



https://github.com/user-attachments/assets/4ef7fadb-0b82-4897-95c7-1737827bf4ac


Release Notes:

- Add commands to select next/previous siblings in the syntax tree

---------

Co-authored-by: Joseph T. Lyons <JosephTLyons@gmail.com>
This commit is contained in:
Ivan Danov
2025-09-08 23:11:53 +00:00
committed by GitHub
co-authored by Joseph T. Lyons
parent 4a0a7d1d27
commit ae54a4e1b8
8 changed files with 440 additions and 27 deletions
+61
View File
@@ -53,6 +53,10 @@ actions!(
SelectSmallerSyntaxNode,
/// Selects the next larger syntax node.
SelectLargerSyntaxNode,
/// Selects the next syntax node sibling.
SelectNextSyntaxNode,
/// Selects the previous syntax node sibling.
SelectPreviousSyntaxNode,
/// Restores the previous visual selection.
RestoreVisualSelection,
/// Inserts at the end of each line in visual selection.
@@ -110,6 +114,30 @@ pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
}
});
Vim::action(editor, cx, |vim, _: &SelectNextSyntaxNode, window, cx| {
let count = Vim::take_count(cx).unwrap_or(1);
Vim::take_forced_motion(cx);
for _ in 0..count {
vim.update_editor(cx, |_, editor, cx| {
editor.select_next_syntax_node(&Default::default(), window, cx);
});
}
});
Vim::action(
editor,
cx,
|vim, _: &SelectPreviousSyntaxNode, window, cx| {
let count = Vim::take_count(cx).unwrap_or(1);
Vim::take_forced_motion(cx);
for _ in 0..count {
vim.update_editor(cx, |_, editor, cx| {
editor.select_prev_syntax_node(&Default::default(), window, cx);
});
}
},
);
Vim::action(
editor,
cx,
@@ -1839,4 +1867,37 @@ mod test {
fˇ»ox"
});
}
#[gpui::test]
async fn test_visual_syntax_sibling_selection(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.set_state(
indoc! {"
fn test() {
let ˇa = 1;
let b = 2;
let c = 3;
}
"},
Mode::Normal,
);
// Enter visual mode and select the statement
cx.simulate_keystrokes("v w w w");
cx.assert_state(
indoc! {"
fn test() {
let «a = 1;ˇ»
let b = 2;
let c = 3;
}
"},
Mode::Visual,
);
// The specific behavior of syntax sibling selection in vim mode
// would depend on the key bindings configured, but the actions
// are now available for use
}
}