rust-analyzer/crates/ra_syntax/src/algo.rs

27 lines
847 B
Rust
Raw Normal View History

2018-08-11 09:28:59 +00:00
pub mod visit;
2018-08-07 15:28:30 +00:00
use crate::{SyntaxNode, SyntaxNodeRef, TextRange, TextUnit};
2018-08-07 15:28:30 +00:00
pub use rowan::LeafAtOffset;
2018-08-07 15:28:30 +00:00
pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset<SyntaxNodeRef> {
match node.0.leaf_at_offset(offset) {
LeafAtOffset::None => LeafAtOffset::None,
LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode(n)),
LeafAtOffset::Between(l, r) => LeafAtOffset::Between(SyntaxNode(l), SyntaxNode(r)),
2018-08-07 15:28:30 +00:00
}
}
pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef {
SyntaxNode(root.0.covering_node(range))
2018-08-07 15:28:30 +00:00
}
pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item = T> {
2018-08-12 15:50:16 +00:00
::itertools::unfold(seed, move |slot| {
slot.take().map(|curr| {
*slot = step(&curr);
curr
})
})
}