diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-08 17:55:17 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-08 17:55:17 +0000 |
commit | 51f669606cfbd29f3e9a3695810ead4124f49e84 (patch) | |
tree | 1f23b16e096c0eec8daacc31443905c987bbf979 /crates/ra_syntax/src/algo.rs | |
parent | 5ad7547ce2f469905992acff5f95e55d54eda714 (diff) | |
parent | 7a79cde107ec71abbc7c715e933f29f7a1fb2b95 (diff) |
Merge #263
263: New modules r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/algo.rs')
-rw-r--r-- | crates/ra_syntax/src/algo.rs | 26 |
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 @@ | |||
1 | pub mod visit; | ||
2 | |||
3 | use crate::{SyntaxNode, SyntaxNodeRef, TextRange, TextUnit}; | ||
4 | |||
5 | pub use rowan::LeafAtOffset; | ||
6 | |||
7 | pub 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 | |||
15 | pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef { | ||
16 | SyntaxNode(root.0.covering_node(range)) | ||
17 | } | ||
18 | |||
19 | pub 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 | } | ||