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:
co-authored by
Joseph T. Lyons
parent
4a0a7d1d27
commit
ae54a4e1b8
+153
-27
@@ -3459,46 +3459,66 @@ impl BufferSnapshot {
|
||||
}
|
||||
|
||||
/// Returns the closest syntax node enclosing the given range.
|
||||
/// Positions a tree cursor at the leaf node that contains or touches the given range.
|
||||
/// This is shared logic used by syntax navigation methods.
|
||||
fn position_cursor_at_range(cursor: &mut tree_sitter::TreeCursor, range: &Range<usize>) {
|
||||
// Descend to the first leaf that touches the start of the range.
|
||||
//
|
||||
// If the range is non-empty and the current node ends exactly at the start,
|
||||
// move to the next sibling to find a node that extends beyond the start.
|
||||
//
|
||||
// If the range is empty and the current node starts after the range position,
|
||||
// move to the previous sibling to find the node that contains the position.
|
||||
while cursor.goto_first_child_for_byte(range.start).is_some() {
|
||||
if !range.is_empty() && cursor.node().end_byte() == range.start {
|
||||
cursor.goto_next_sibling();
|
||||
}
|
||||
if range.is_empty() && cursor.node().start_byte() > range.start {
|
||||
cursor.goto_previous_sibling();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves the cursor to find a node that contains the given range.
|
||||
/// Returns true if such a node is found, false otherwise.
|
||||
/// This is shared logic used by syntax navigation methods.
|
||||
fn find_containing_node(
|
||||
cursor: &mut tree_sitter::TreeCursor,
|
||||
range: &Range<usize>,
|
||||
strict: bool,
|
||||
) -> bool {
|
||||
loop {
|
||||
let node_range = cursor.node().byte_range();
|
||||
|
||||
if node_range.start <= range.start
|
||||
&& node_range.end >= range.end
|
||||
&& (!strict || node_range.len() > range.len())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if !cursor.goto_parent() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn syntax_ancestor<'a, T: ToOffset>(
|
||||
&'a self,
|
||||
range: Range<T>,
|
||||
) -> Option<tree_sitter::Node<'a>> {
|
||||
let range = range.start.to_offset(self)..range.end.to_offset(self);
|
||||
let mut result: Option<tree_sitter::Node<'a>> = None;
|
||||
'outer: for layer in self
|
||||
for layer in self
|
||||
.syntax
|
||||
.layers_for_range(range.clone(), &self.text, true)
|
||||
{
|
||||
let mut cursor = layer.node().walk();
|
||||
|
||||
// Descend to the first leaf that touches the start of the range.
|
||||
//
|
||||
// If the range is non-empty and the current node ends exactly at the start,
|
||||
// move to the next sibling to find a node that extends beyond the start.
|
||||
//
|
||||
// If the range is empty and the current node starts after the range position,
|
||||
// move to the previous sibling to find the node that contains the position.
|
||||
while cursor.goto_first_child_for_byte(range.start).is_some() {
|
||||
if !range.is_empty() && cursor.node().end_byte() == range.start {
|
||||
cursor.goto_next_sibling();
|
||||
}
|
||||
if range.is_empty() && cursor.node().start_byte() > range.start {
|
||||
cursor.goto_previous_sibling();
|
||||
}
|
||||
}
|
||||
Self::position_cursor_at_range(&mut cursor, &range);
|
||||
|
||||
// Ascend to the smallest ancestor that strictly contains the range.
|
||||
loop {
|
||||
let node_range = cursor.node().byte_range();
|
||||
if node_range.start <= range.start
|
||||
&& node_range.end >= range.end
|
||||
&& node_range.len() > range.len()
|
||||
{
|
||||
break;
|
||||
}
|
||||
if !cursor.goto_parent() {
|
||||
continue 'outer;
|
||||
}
|
||||
if !Self::find_containing_node(&mut cursor, &range, true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let left_node = cursor.node();
|
||||
@@ -3541,6 +3561,112 @@ impl BufferSnapshot {
|
||||
result
|
||||
}
|
||||
|
||||
/// Find the previous sibling syntax node at the given range.
|
||||
///
|
||||
/// This function locates the syntax node that precedes the node containing
|
||||
/// the given range. It searches hierarchically by:
|
||||
/// 1. Finding the node that contains the given range
|
||||
/// 2. Looking for the previous sibling at the same tree level
|
||||
/// 3. If no sibling is found, moving up to parent levels and searching for siblings
|
||||
///
|
||||
/// Returns `None` if there is no previous sibling at any ancestor level.
|
||||
pub fn syntax_prev_sibling<'a, T: ToOffset>(
|
||||
&'a self,
|
||||
range: Range<T>,
|
||||
) -> Option<tree_sitter::Node<'a>> {
|
||||
let range = range.start.to_offset(self)..range.end.to_offset(self);
|
||||
let mut result: Option<tree_sitter::Node<'a>> = None;
|
||||
|
||||
for layer in self
|
||||
.syntax
|
||||
.layers_for_range(range.clone(), &self.text, true)
|
||||
{
|
||||
let mut cursor = layer.node().walk();
|
||||
|
||||
Self::position_cursor_at_range(&mut cursor, &range);
|
||||
|
||||
// Find the node that contains the range
|
||||
if !Self::find_containing_node(&mut cursor, &range, false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Look for the previous sibling, moving up ancestor levels if needed
|
||||
loop {
|
||||
if cursor.goto_previous_sibling() {
|
||||
let layer_result = cursor.node();
|
||||
|
||||
if let Some(previous_result) = &result {
|
||||
if previous_result.byte_range().end < layer_result.byte_range().end {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
result = Some(layer_result);
|
||||
break;
|
||||
}
|
||||
|
||||
// No sibling found at this level, try moving up to parent
|
||||
if !cursor.goto_parent() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Find the next sibling syntax node at the given range.
|
||||
///
|
||||
/// This function locates the syntax node that follows the node containing
|
||||
/// the given range. It searches hierarchically by:
|
||||
/// 1. Finding the node that contains the given range
|
||||
/// 2. Looking for the next sibling at the same tree level
|
||||
/// 3. If no sibling is found, moving up to parent levels and searching for siblings
|
||||
///
|
||||
/// Returns `None` if there is no next sibling at any ancestor level.
|
||||
pub fn syntax_next_sibling<'a, T: ToOffset>(
|
||||
&'a self,
|
||||
range: Range<T>,
|
||||
) -> Option<tree_sitter::Node<'a>> {
|
||||
let range = range.start.to_offset(self)..range.end.to_offset(self);
|
||||
let mut result: Option<tree_sitter::Node<'a>> = None;
|
||||
|
||||
for layer in self
|
||||
.syntax
|
||||
.layers_for_range(range.clone(), &self.text, true)
|
||||
{
|
||||
let mut cursor = layer.node().walk();
|
||||
|
||||
Self::position_cursor_at_range(&mut cursor, &range);
|
||||
|
||||
// Find the node that contains the range
|
||||
if !Self::find_containing_node(&mut cursor, &range, false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Look for the next sibling, moving up ancestor levels if needed
|
||||
loop {
|
||||
if cursor.goto_next_sibling() {
|
||||
let layer_result = cursor.node();
|
||||
|
||||
if let Some(previous_result) = &result {
|
||||
if previous_result.byte_range().start > layer_result.byte_range().start {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
result = Some(layer_result);
|
||||
break;
|
||||
}
|
||||
|
||||
// No sibling found at this level, try moving up to parent
|
||||
if !cursor.goto_parent() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Returns the root syntax node within the given row
|
||||
pub fn syntax_root_ancestor(&self, position: Anchor) -> Option<tree_sitter::Node<'_>> {
|
||||
let start_offset = position.to_offset(self);
|
||||
|
||||
Reference in New Issue
Block a user