pub mod visit; use rowan::TransparentNewType; use crate::{SyntaxNode, TextRange, TextUnit, AstNode}; pub use rowan::LeafAtOffset; pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<&SyntaxNode> { match node.0.leaf_at_offset(offset) { LeafAtOffset::None => LeafAtOffset::None, LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode::from_repr(n)), LeafAtOffset::Between(l, r) => { LeafAtOffset::Between(SyntaxNode::from_repr(l), SyntaxNode::from_repr(r)) } } } /// Finds a node of specific Ast type at offset. Note that this is slightly /// impercise: if the cursor is strictly betwen two nodes of the desired type, /// as in /// /// ```no-run /// struct Foo {}|struct Bar; /// ``` /// /// then the left node will be silently prefered. pub fn find_node_at_offset(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> { find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast)) } pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode { SyntaxNode::from_repr(root.0.covering_node(range)) } pub fn generate(seed: Option, step: impl Fn(&T) -> Option) -> impl Iterator { ::itertools::unfold(seed, move |slot| { slot.take().map(|curr| { *slot = step(&curr); curr }) }) }