diff options
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/algo.rs | 10 | ||||
-rw-r--r-- | crates/ra_syntax/src/ptr.rs | 9 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_node.rs | 9 |
3 files changed, 10 insertions, 18 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 06b45135c..1f68fe467 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs | |||
@@ -46,13 +46,3 @@ pub fn non_trivia_sibling(element: SyntaxElement, direction: Direction) -> Optio | |||
46 | pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement { | 46 | pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement { |
47 | root.0.covering_node(range).into() | 47 | root.0.covering_node(range).into() |
48 | } | 48 | } |
49 | |||
50 | // Replace with `std::iter::successors` in `1.34.0` | ||
51 | pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item = T> { | ||
52 | ::itertools::unfold(seed, move |slot| { | ||
53 | slot.take().map(|curr| { | ||
54 | *slot = step(&curr); | ||
55 | curr | ||
56 | }) | ||
57 | }) | ||
58 | } | ||
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs index d8de1c4c1..15a8b94cd 100644 --- a/crates/ra_syntax/src/ptr.rs +++ b/crates/ra_syntax/src/ptr.rs | |||
@@ -1,8 +1,9 @@ | |||
1 | use std::marker::PhantomData; | 1 | use std::{ |
2 | 2 | marker::PhantomData, | |
3 | iter::successors, | ||
4 | }; | ||
3 | use crate::{ | 5 | use crate::{ |
4 | AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, | 6 | AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, |
5 | algo::generate, | ||
6 | }; | 7 | }; |
7 | 8 | ||
8 | /// A pointer to a syntax node inside a file. It can be used to remember a | 9 | /// A pointer to a syntax node inside a file. It can be used to remember a |
@@ -19,7 +20,7 @@ impl SyntaxNodePtr { | |||
19 | } | 20 | } |
20 | 21 | ||
21 | pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode { | 22 | pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode { |
22 | generate(Some(source_file.syntax()), |&node| { | 23 | successors(Some(source_file.syntax()), |&node| { |
23 | node.children().find(|it| self.range.is_subrange(&it.range())) | 24 | node.children().find(|it| self.range.is_subrange(&it.range())) |
24 | }) | 25 | }) |
25 | .find(|it| it.range() == self.range && it.kind() == self.kind) | 26 | .find(|it| it.range() == self.range && it.kind() == self.kind) |
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index 64d884287..dc2352c76 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs | |||
@@ -10,6 +10,7 @@ use std::{ | |||
10 | fmt::{self, Write}, | 10 | fmt::{self, Write}, |
11 | any::Any, | 11 | any::Any, |
12 | borrow::Borrow, | 12 | borrow::Borrow, |
13 | iter::successors, | ||
13 | }; | 14 | }; |
14 | 15 | ||
15 | use ra_parser::ParseError; | 16 | use ra_parser::ParseError; |
@@ -195,7 +196,7 @@ impl SyntaxNode { | |||
195 | } | 196 | } |
196 | 197 | ||
197 | pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> { | 198 | pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> { |
198 | crate::algo::generate(Some(self), |&node| node.parent()) | 199 | successors(Some(self), |&node| node.parent()) |
199 | } | 200 | } |
200 | 201 | ||
201 | pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> { | 202 | pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> { |
@@ -213,7 +214,7 @@ impl SyntaxNode { | |||
213 | } | 214 | } |
214 | 215 | ||
215 | pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> { | 216 | pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> { |
216 | crate::algo::generate(Some(self), move |&node| match direction { | 217 | successors(Some(self), move |&node| match direction { |
217 | Direction::Next => node.next_sibling(), | 218 | Direction::Next => node.next_sibling(), |
218 | Direction::Prev => node.prev_sibling(), | 219 | Direction::Prev => node.prev_sibling(), |
219 | }) | 220 | }) |
@@ -224,7 +225,7 @@ impl SyntaxNode { | |||
224 | direction: Direction, | 225 | direction: Direction, |
225 | ) -> impl Iterator<Item = SyntaxElement> { | 226 | ) -> impl Iterator<Item = SyntaxElement> { |
226 | let me: SyntaxElement = self.into(); | 227 | let me: SyntaxElement = self.into(); |
227 | crate::algo::generate(Some(me), move |el| match direction { | 228 | successors(Some(me), move |el| match direction { |
228 | Direction::Next => el.next_sibling_or_token(), | 229 | Direction::Next => el.next_sibling_or_token(), |
229 | Direction::Prev => el.prev_sibling_or_token(), | 230 | Direction::Prev => el.prev_sibling_or_token(), |
230 | }) | 231 | }) |
@@ -373,7 +374,7 @@ impl<'a> SyntaxToken<'a> { | |||
373 | direction: Direction, | 374 | direction: Direction, |
374 | ) -> impl Iterator<Item = SyntaxElement<'a>> { | 375 | ) -> impl Iterator<Item = SyntaxElement<'a>> { |
375 | let me: SyntaxElement = (*self).into(); | 376 | let me: SyntaxElement = (*self).into(); |
376 | crate::algo::generate(Some(me), move |el| match direction { | 377 | successors(Some(me), move |el| match direction { |
377 | Direction::Next => el.next_sibling_or_token(), | 378 | Direction::Next => el.next_sibling_or_token(), |
378 | Direction::Prev => el.prev_sibling_or_token(), | 379 | Direction::Prev => el.prev_sibling_or_token(), |
379 | }) | 380 | }) |