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
+98
View File
@@ -15138,6 +15138,104 @@ impl Editor {
});
}
pub fn select_next_syntax_node(
&mut self,
_: &SelectNextSyntaxNode,
window: &mut Window,
cx: &mut Context<Self>,
) {
let old_selections: Box<[_]> = self.selections.all::<usize>(cx).into();
if old_selections.is_empty() {
return;
}
self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
let buffer = self.buffer.read(cx).snapshot(cx);
let mut selected_sibling = false;
let new_selections = old_selections
.iter()
.map(|selection| {
let old_range = selection.start..selection.end;
if let Some(node) = buffer.syntax_next_sibling(old_range) {
let new_range = node.byte_range();
selected_sibling = true;
Selection {
id: selection.id,
start: new_range.start,
end: new_range.end,
goal: SelectionGoal::None,
reversed: selection.reversed,
}
} else {
selection.clone()
}
})
.collect::<Vec<_>>();
if selected_sibling {
self.change_selections(
SelectionEffects::scroll(Autoscroll::fit()),
window,
cx,
|s| {
s.select(new_selections);
},
);
}
}
pub fn select_prev_syntax_node(
&mut self,
_: &SelectPreviousSyntaxNode,
window: &mut Window,
cx: &mut Context<Self>,
) {
let old_selections: Box<[_]> = self.selections.all::<usize>(cx).into();
if old_selections.is_empty() {
return;
}
self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
let buffer = self.buffer.read(cx).snapshot(cx);
let mut selected_sibling = false;
let new_selections = old_selections
.iter()
.map(|selection| {
let old_range = selection.start..selection.end;
if let Some(node) = buffer.syntax_prev_sibling(old_range) {
let new_range = node.byte_range();
selected_sibling = true;
Selection {
id: selection.id,
start: new_range.start,
end: new_range.end,
goal: SelectionGoal::None,
reversed: selection.reversed,
}
} else {
selection.clone()
}
})
.collect::<Vec<_>>();
if selected_sibling {
self.change_selections(
SelectionEffects::scroll(Autoscroll::fit()),
window,
cx,
|s| {
s.select(new_selections);
},
);
}
}
fn refresh_runnables(&mut self, window: &mut Window, cx: &mut Context<Self>) -> Task<()> {
if !EditorSettings::get_global(cx).gutter.runnables {
self.clear_tasks();