aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/algo.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/algo.rs')
-rw-r--r--crates/ra_syntax/src/algo.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
new file mode 100644
index 000000000..4b3548ea9
--- /dev/null
+++ b/crates/ra_syntax/src/algo.rs
@@ -0,0 +1,26 @@
1pub mod visit;
2
3use crate::{SyntaxNode, SyntaxNodeRef, TextRange, TextUnit};
4
5pub use rowan::LeafAtOffset;
6
7pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset<SyntaxNodeRef> {
8 match node.0.leaf_at_offset(offset) {
9 LeafAtOffset::None => LeafAtOffset::None,
10 LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode(n)),
11 LeafAtOffset::Between(l, r) => LeafAtOffset::Between(SyntaxNode(l), SyntaxNode(r)),
12 }
13}
14
15pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef {
16 SyntaxNode(root.0.covering_node(range))
17}
18
19pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item = T> {
20 ::itertools::unfold(seed, move |slot| {
21 slot.take().map(|curr| {
22 *slot = step(&curr);
23 curr
24 })
25 })
26}