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

50 lines
1.7 KiB
Rust
Raw Normal View History

2018-08-11 09:28:59 +00:00
pub mod visit;
2018-08-07 15:28:30 +00:00
2019-01-07 13:15:47 +00:00
use rowan::TransparentNewType;
use crate::{SyntaxNode, TextRange, TextUnit, AstNode, Direction};
2018-08-07 15:28:30 +00:00
pub use rowan::LeafAtOffset;
2018-08-07 15:28:30 +00:00
2019-01-07 13:15:47 +00:00
pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<&SyntaxNode> {
match node.0.leaf_at_offset(offset) {
LeafAtOffset::None => LeafAtOffset::None,
2019-01-07 13:15:47 +00: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 15:28:30 +00:00
}
}
2019-01-08 17:47:37 +00:00
/// Finds a node of specific Ast type at offset. Note that this is slightly
2019-01-27 13:49:02 +00:00
/// imprecise: if the cursor is strictly between two nodes of the desired type,
2019-01-08 17:47:37 +00:00
/// as in
///
/// ```no-run
/// struct Foo {}|struct Bar;
/// ```
///
2019-01-27 13:49:02 +00:00
/// then the left node will be silently preferred.
2019-01-08 17:44:31 +00:00
pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> {
find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast))
}
/// Finds the first sibling in the given direction which is not `trivia`
pub fn non_trivia_sibling(node: &SyntaxNode, direction: Direction) -> Option<&SyntaxNode> {
node.siblings(direction).skip(1).find(|node| !node.kind().is_trivia())
}
2019-01-07 13:15:47 +00:00
pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode {
SyntaxNode::from_repr(root.0.covering_node(range))
2018-08-07 15:28:30 +00:00
}
2019-02-21 12:24:42 +00:00
// Replace with `std::iter::successors` in `1.34.0`
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
})
})
}