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
+95
View File
@@ -25330,6 +25330,101 @@ async fn test_non_utf_8_opens(cx: &mut TestAppContext) {
);
}
#[gpui::test]
async fn test_select_next_prev_syntax_node(cx: &mut TestAppContext) {
init_test(cx, |_| {});
let language = Arc::new(Language::new(
LanguageConfig::default(),
Some(tree_sitter_rust::LANGUAGE.into()),
));
// Test hierarchical sibling navigation
let text = r#"
fn outer() {
if condition {
let a = 1;
}
let b = 2;
}
fn another() {
let c = 3;
}
"#;
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(language, cx));
let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, cx) = cx.add_window_view(|window, cx| build_editor(buffer, window, cx));
// Wait for parsing to complete
editor
.condition::<crate::EditorEvent>(cx, |editor, cx| !editor.buffer.read(cx).is_parsing(cx))
.await;
editor.update_in(cx, |editor, window, cx| {
// Start by selecting "let a = 1;" inside the if block
editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
s.select_display_ranges([
DisplayPoint::new(DisplayRow(3), 16)..DisplayPoint::new(DisplayRow(3), 26)
]);
});
let initial_selection = editor.selections.display_ranges(cx);
assert_eq!(initial_selection.len(), 1, "Should have one selection");
// Test select next sibling - should move up levels to find the next sibling
// Since "let a = 1;" has no siblings in the if block, it should move up
// to find "let b = 2;" which is a sibling of the if block
editor.select_next_syntax_node(&SelectNextSyntaxNode, window, cx);
let next_selection = editor.selections.display_ranges(cx);
// Should have a selection and it should be different from the initial
assert_eq!(
next_selection.len(),
1,
"Should have one selection after next"
);
assert_ne!(
next_selection[0], initial_selection[0],
"Next sibling selection should be different"
);
// Test hierarchical navigation by going to the end of the current function
// and trying to navigate to the next function
editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
s.select_display_ranges([
DisplayPoint::new(DisplayRow(5), 12)..DisplayPoint::new(DisplayRow(5), 22)
]);
});
editor.select_next_syntax_node(&SelectNextSyntaxNode, window, cx);
let function_next_selection = editor.selections.display_ranges(cx);
// Should move to the next function
assert_eq!(
function_next_selection.len(),
1,
"Should have one selection after function next"
);
// Test select previous sibling navigation
editor.select_prev_syntax_node(&SelectPreviousSyntaxNode, window, cx);
let prev_selection = editor.selections.display_ranges(cx);
// Should have a selection and it should be different
assert_eq!(
prev_selection.len(),
1,
"Should have one selection after prev"
);
assert_ne!(
prev_selection[0], function_next_selection[0],
"Previous sibling selection should be different from next"
);
});
}
#[track_caller]
fn extract_color_inlays(editor: &Editor, cx: &App) -> Vec<Rgba> {
editor