2018-08-11 12:28:59 +03:00
|
|
|
pub mod visit;
|
2018-08-07 18:28:30 +03:00
|
|
|
|
2019-01-07 16:15:47 +03:00
|
|
|
use rowan::TransparentNewType;
|
|
|
|
|
|
|
|
|
|
use crate::{SyntaxNode, TextRange, TextUnit};
|
2018-08-07 18:28:30 +03:00
|
|
|
|
2018-11-06 20:56:32 +03:00
|
|
|
pub use rowan::LeafAtOffset;
|
2018-08-07 18:28:30 +03:00
|
|
|
|
2019-01-07 16:15:47 +03:00
|
|
|
pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<&SyntaxNode> {
|
2018-11-06 20:56:32 +03:00
|
|
|
match node.0.leaf_at_offset(offset) {
|
|
|
|
|
LeafAtOffset::None => LeafAtOffset::None,
|
2019-01-07 16:15:47 +03:00
|
|
|
LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode::from_repr(n)),
|
|
|
|
|
LeafAtOffset::Between(l, r) => {
|
|
|
|
|
LeafAtOffset::Between(SyntaxNode::from_repr(l), SyntaxNode::from_repr(r))
|
|
|
|
|
}
|
2018-08-07 18:28:30 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-07 16:15:47 +03:00
|
|
|
pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode {
|
|
|
|
|
SyntaxNode::from_repr(root.0.covering_node(range))
|
2018-08-07 18:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
2018-10-15 17:44:23 -04:00
|
|
|
pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item = T> {
|
2018-08-12 18:50:16 +03:00
|
|
|
::itertools::unfold(seed, move |slot| {
|
|
|
|
|
slot.take().map(|curr| {
|
|
|
|
|
*slot = step(&curr);
|
|
|
|
|
curr
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|