Skip to content

Commit

Permalink
fix: don't fall into holes
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed Apr 23, 2024
1 parent 34487aa commit 6267c4d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/tree/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ impl Location {
}

/// Returns the location at the beginning of the child sequence of the given node.
/// (Returns `None` for a texty node, or a fixed node with no children.)
pub fn before_children(s: &Storage, node: Node) -> Option<Location> {
if node.is_texty(s) {
if !node.can_have_children(s) {
return None;
}
if let Some(first_child) = node.first_child(s) {
Expand All @@ -61,8 +62,9 @@ impl Location {
}

/// Returns the location at the end of the child sequence of the given node.
/// (Returns `None` for a texty node, or a fixed node with no children.)
pub fn after_children(s: &Storage, node: Node) -> Option<Location> {
if node.is_texty(s) {
if !node.can_have_children(s) {
return None;
}
if let Some(last_child) = node.last_child(s) {
Expand Down
14 changes: 11 additions & 3 deletions src/tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ impl Node {
s.forest().data(self.0).text.is_some()
}

pub fn can_have_children(self, s: &Storage) -> bool {
match self.arity(s) {
Arity::Texty => false,
Arity::Fixed(sorts) => sorts.len(s) > 0,
Arity::Listy(_) => true,
}
}

/// Borrow the text of a texty node. `None` if it's not texty.
pub fn text(self, s: &Storage) -> Option<&Text> {
s.forest().data(self.0).text.as_ref()
Expand Down Expand Up @@ -218,9 +226,9 @@ impl Node {
s.forest().sibling_index(self.0)
}

/// Return the number of children this node has. For a Fixed node, this is
/// its arity. For a Listy node, this is its current number of children.
/// For text, this is None. Requires iterating over all the children.
/// Return the number of children this node has. For a Fixed node, this is the length of its
/// arity. For a Listy node, this is its current number of children. For text, this is None.
/// Requires iterating over all the children.
pub fn num_children(self, s: &Storage) -> Option<usize> {
if self.is_texty(s) {
None
Expand Down

0 comments on commit 6267c4d

Please sign in to comment.