aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/split_import.rs5
-rw-r--r--crates/ra_fmt/src/lib.rs4
-rw-r--r--crates/ra_hir/src/ty/autoderef.rs4
-rw-r--r--crates/ra_syntax/src/algo.rs10
-rw-r--r--crates/ra_syntax/src/ptr.rs9
-rw-r--r--crates/ra_syntax/src/syntax_node.rs9
6 files changed, 17 insertions, 24 deletions
diff --git a/crates/ra_assists/src/split_import.rs b/crates/ra_assists/src/split_import.rs
index 4bf1852db..57e0efaf2 100644
--- a/crates/ra_assists/src/split_import.rs
+++ b/crates/ra_assists/src/split_import.rs
@@ -1,8 +1,9 @@
1use std::iter::successors;
2
1use hir::db::HirDatabase; 3use hir::db::HirDatabase;
2use ra_syntax::{ 4use ra_syntax::{
3 TextUnit, AstNode, SyntaxKind::COLONCOLON, 5 TextUnit, AstNode, SyntaxKind::COLONCOLON,
4 ast, 6 ast,
5 algo::generate,
6}; 7};
7 8
8use crate::{AssistCtx, Assist, AssistId}; 9use crate::{AssistCtx, Assist, AssistId};
@@ -10,7 +11,7 @@ use crate::{AssistCtx, Assist, AssistId};
10pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 11pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?; 12 let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?;
12 let path = ast::Path::cast(colon_colon.parent())?; 13 let path = ast::Path::cast(colon_colon.parent())?;
13 let top_path = generate(Some(path), |it| it.parent_path()).last()?; 14 let top_path = successors(Some(path), |it| it.parent_path()).last()?;
14 15
15 let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast); 16 let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast);
16 if use_tree.is_none() { 17 if use_tree.is_none() {
diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs
index 85b7ce250..603be1854 100644
--- a/crates/ra_fmt/src/lib.rs
+++ b/crates/ra_fmt/src/lib.rs
@@ -1,10 +1,10 @@
1//! This crate provides some utilities for indenting rust code. 1//! This crate provides some utilities for indenting rust code.
2//! 2//!
3use std::iter::successors;
3use itertools::Itertools; 4use itertools::Itertools;
4use ra_syntax::{ 5use ra_syntax::{
5 SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind, 6 SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind,
6 ast::{self, AstNode, AstToken}, 7 ast::{self, AstNode, AstToken},
7 algo::generate,
8}; 8};
9 9
10pub fn reindent(text: &str, indent: &str) -> String { 10pub fn reindent(text: &str, indent: &str) -> String {
@@ -29,7 +29,7 @@ pub fn leading_indent(node: &SyntaxNode) -> Option<&str> {
29} 29}
30 30
31fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> { 31fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
32 generate(token.prev_token(), |&token| token.prev_token()) 32 successors(token.prev_token(), |&token| token.prev_token())
33} 33}
34 34
35pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { 35pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs
index ab5f008ef..a442a856c 100644
--- a/crates/ra_hir/src/ty/autoderef.rs
+++ b/crates/ra_hir/src/ty/autoderef.rs
@@ -3,7 +3,7 @@
3//! reference to a type with the field `bar`. This is an approximation of the 3//! reference to a type with the field `bar`. This is an approximation of the
4//! logic in rustc (which lives in librustc_typeck/check/autoderef.rs). 4//! logic in rustc (which lives in librustc_typeck/check/autoderef.rs).
5 5
6use ra_syntax::algo::generate; 6use std::iter::successors;
7 7
8use crate::HirDatabase; 8use crate::HirDatabase;
9use super::Ty; 9use super::Ty;
@@ -11,7 +11,7 @@ use super::Ty;
11impl Ty { 11impl Ty {
12 /// Iterates over the possible derefs of `ty`. 12 /// Iterates over the possible derefs of `ty`.
13 pub fn autoderef<'a>(self, db: &'a impl HirDatabase) -> impl Iterator<Item = Ty> + 'a { 13 pub fn autoderef<'a>(self, db: &'a impl HirDatabase) -> impl Iterator<Item = Ty> + 'a {
14 generate(Some(self), move |ty| ty.autoderef_step(db)) 14 successors(Some(self), move |ty| ty.autoderef_step(db))
15 } 15 }
16 16
17 fn autoderef_step(&self, _db: &impl HirDatabase) -> Option<Ty> { 17 fn autoderef_step(&self, _db: &impl HirDatabase) -> Option<Ty> {
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
46pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement { 46pub 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`
51pub 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 @@
1use std::marker::PhantomData; 1use std::{
2 2 marker::PhantomData,
3 iter::successors,
4};
3use crate::{ 5use 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
15use ra_parser::ParseError; 16use 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 })