rust/crates/ra_syntax/src/algo.rs

64 lines
2.3 KiB
Rust
Raw Normal View History

2018-08-11 04:28:59 -05:00
pub mod visit;
2018-08-07 10:28:30 -05:00
use itertools::Itertools;
use crate::{AstNode, Direction, SyntaxElement, SyntaxNode, SyntaxToken, TextRange, TextUnit};
2019-01-07 07:15:47 -06:00
2019-03-30 05:25:53 -05:00
pub use rowan::TokenAtOffset;
2018-08-07 10:28:30 -05:00
2019-03-30 05:25:53 -05:00
pub fn find_token_at_offset(node: &SyntaxNode, offset: TextUnit) -> TokenAtOffset<SyntaxToken> {
match node.0.token_at_offset(offset) {
TokenAtOffset::None => TokenAtOffset::None,
2019-07-18 11:23:05 -05:00
TokenAtOffset::Single(n) => TokenAtOffset::Single(SyntaxToken(n)),
TokenAtOffset::Between(l, r) => TokenAtOffset::Between(SyntaxToken(l), SyntaxToken(r)),
2018-08-07 10:28:30 -05:00
}
}
/// Returns ancestors of the node at the offset, sorted by length. This should
/// do the right thing at an edge, e.g. when searching for expressions at `{
/// <|>foo }` we will get the name reference instead of the whole block, which
/// we would get if we just did `find_token_at_offset(...).flat_map(|t|
/// t.parent().ancestors())`.
pub fn ancestors_at_offset(
node: &SyntaxNode,
offset: TextUnit,
2019-07-18 11:23:05 -05:00
) -> impl Iterator<Item = SyntaxNode> {
find_token_at_offset(node, offset)
.map(|token| token.parent().ancestors())
.kmerge_by(|node1, node2| node1.range().len() < node2.range().len())
}
2019-01-08 11:47:37 -06:00
/// Finds a node of specific Ast type at offset. Note that this is slightly
2019-01-27 07:49:02 -06:00
/// imprecise: if the cursor is strictly between two nodes of the desired type,
2019-01-08 11:47:37 -06:00
/// as in
///
/// ```no-run
/// struct Foo {}|struct Bar;
/// ```
///
/// then the shorter node will be silently preferred.
2019-07-18 11:23:05 -05:00
pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<N> {
ancestors_at_offset(syntax, offset).find_map(N::cast)
2019-01-08 11:44:31 -06:00
}
/// Finds the first sibling in the given direction which is not `trivia`
2019-03-30 05:25:53 -05: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 05:25:53 -05:00
pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement {
2019-07-18 11:23:05 -05:00
SyntaxElement::new(root.0.covering_node(range))
2018-08-07 10:28:30 -05:00
}