59 lines
2.0 KiB
Rust
Raw Normal View History

2018-08-11 12:28:59 +03:00
pub mod visit;
2018-08-07 18:28:30 +03:00
2019-03-30 13:25:53 +03:00
use crate::{SyntaxNode, TextRange, TextUnit, AstNode, Direction, SyntaxToken, SyntaxElement};
2019-01-07 16:15:47 +03:00
2019-03-30 13:25:53 +03:00
pub use rowan::TokenAtOffset;
2018-08-07 18:28:30 +03:00
2019-03-30 13:25:53 +03:00
pub fn find_token_at_offset(node: &SyntaxNode, offset: TextUnit) -> TokenAtOffset<SyntaxToken> {
match node.0.token_at_offset(offset) {
TokenAtOffset::None => TokenAtOffset::None,
TokenAtOffset::Single(n) => TokenAtOffset::Single(n.into()),
TokenAtOffset::Between(l, r) => TokenAtOffset::Between(l.into(), r.into()),
2018-08-07 18:28:30 +03:00
}
}
2019-01-08 20:47:37 +03:00
/// Finds a node of specific Ast type at offset. Note that this is slightly
2019-01-27 14:49:02 +01:00
/// imprecise: if the cursor is strictly between two nodes of the desired type,
2019-01-08 20:47:37 +03:00
/// as in
///
/// ```no-run
/// struct Foo {}|struct Bar;
/// ```
///
2019-01-27 14:49:02 +01:00
/// then the left node will be silently preferred.
2019-01-08 20:44:31 +03:00
pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> {
2019-03-30 13:25:53 +03:00
find_token_at_offset(syntax, offset)
.find_map(|leaf| leaf.parent().ancestors().find_map(N::cast))
2019-01-08 20:44:31 +03:00
}
/// Finds the first sibling in the given direction which is not `trivia`
2019-03-30 13:25:53 +03:00
pub fn non_trivia_sibling(element: SyntaxElement, direction: Direction) -> Option<SyntaxElement> {
return match element {
SyntaxElement::Node(node) => node.siblings_with_tokens(direction).skip(1).find(not_trivia),
SyntaxElement::Token(token) => {
token.siblings_with_tokens(direction).skip(1).find(not_trivia)
}
};
fn not_trivia(element: &SyntaxElement) -> bool {
match element {
SyntaxElement::Node(_) => true,
SyntaxElement::Token(token) => !token.kind().is_trivia(),
}
}
}
2019-03-30 13:25:53 +03:00
pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement {
root.0.covering_node(range).into()
2018-08-07 18:28:30 +03:00
}
2019-02-21 15:24:42 +03: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 18:50:16 +03:00
::itertools::unfold(seed, move |slot| {
slot.take().map(|curr| {
*slot = step(&curr);
curr
})
})
}