diff options
Diffstat (limited to 'crates/libsyntax2')
-rw-r--r-- | crates/libsyntax2/src/algo/mod.rs | 13 | ||||
-rw-r--r-- | crates/libsyntax2/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/libsyntax2/src/text_utils.rs | 19 |
3 files changed, 24 insertions, 9 deletions
diff --git a/crates/libsyntax2/src/algo/mod.rs b/crates/libsyntax2/src/algo/mod.rs index 6efdff12f..2640d60ea 100644 --- a/crates/libsyntax2/src/algo/mod.rs +++ b/crates/libsyntax2/src/algo/mod.rs | |||
@@ -1,7 +1,10 @@ | |||
1 | pub mod walk; | 1 | pub mod walk; |
2 | pub mod visit; | 2 | pub mod visit; |
3 | 3 | ||
4 | use {SyntaxNodeRef, TextUnit, TextRange}; | 4 | use { |
5 | SyntaxNodeRef, TextUnit, TextRange, | ||
6 | text_utils::{contains_offset_nonstrict, is_subrange}, | ||
7 | }; | ||
5 | 8 | ||
6 | pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset { | 9 | pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset { |
7 | let range = node.range(); | 10 | let range = node.range(); |
@@ -116,14 +119,6 @@ fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNo | |||
116 | panic!("Can't find common ancestor of {:?} and {:?}", n1, n2) | 119 | panic!("Can't find common ancestor of {:?} and {:?}", n1, n2) |
117 | } | 120 | } |
118 | 121 | ||
119 | fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool { | ||
120 | range.start() <= offset && offset <= range.end() | ||
121 | } | ||
122 | |||
123 | fn is_subrange(range: TextRange, subrange: TextRange) -> bool { | ||
124 | range.start() <= subrange.start() && subrange.end() <= range.end() | ||
125 | } | ||
126 | |||
127 | fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item=T> { | 122 | fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item=T> { |
128 | ::itertools::unfold(seed, move |slot| { | 123 | ::itertools::unfold(seed, move |slot| { |
129 | slot.take().map(|curr| { | 124 | slot.take().map(|curr| { |
diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs index c078baa3a..53ae18988 100644 --- a/crates/libsyntax2/src/lib.rs +++ b/crates/libsyntax2/src/lib.rs | |||
@@ -39,6 +39,7 @@ mod syntax_kinds; | |||
39 | mod yellow; | 39 | mod yellow; |
40 | /// Utilities for simple uses of the parser. | 40 | /// Utilities for simple uses of the parser. |
41 | pub mod utils; | 41 | pub mod utils; |
42 | pub mod text_utils; | ||
42 | 43 | ||
43 | pub use { | 44 | pub use { |
44 | text_unit::{TextRange, TextUnit}, | 45 | text_unit::{TextRange, TextUnit}, |
diff --git a/crates/libsyntax2/src/text_utils.rs b/crates/libsyntax2/src/text_utils.rs new file mode 100644 index 000000000..e3d73888f --- /dev/null +++ b/crates/libsyntax2/src/text_utils.rs | |||
@@ -0,0 +1,19 @@ | |||
1 | use {TextRange, TextUnit}; | ||
2 | |||
3 | pub fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool { | ||
4 | range.start() <= offset && offset <= range.end() | ||
5 | } | ||
6 | |||
7 | pub fn is_subrange(range: TextRange, subrange: TextRange) -> bool { | ||
8 | range.start() <= subrange.start() && subrange.end() <= range.end() | ||
9 | } | ||
10 | |||
11 | pub fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> { | ||
12 | let start = r1.start().max(r2.start()); | ||
13 | let end = r1.end().min(r2.end()); | ||
14 | if start <= end { | ||
15 | Some(TextRange::from_to(start, end)) | ||
16 | } else { | ||
17 | None | ||
18 | } | ||
19 | } | ||