From 6aae0cf7fa042d51e97c7606cdf3a338f172f7d2 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sat, 13 Apr 2019 16:43:49 +0200 Subject: replace usages of `algo::generate` with `iter::successors` from std --- crates/ra_assists/src/split_import.rs | 5 +++-- crates/ra_fmt/src/lib.rs | 4 ++-- crates/ra_hir/src/ty/autoderef.rs | 4 ++-- crates/ra_syntax/src/algo.rs | 10 ---------- crates/ra_syntax/src/ptr.rs | 9 +++++---- crates/ra_syntax/src/syntax_node.rs | 9 +++++---- 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 @@ +use std::iter::successors; + use hir::db::HirDatabase; use ra_syntax::{ TextUnit, AstNode, SyntaxKind::COLONCOLON, ast, - algo::generate, }; use crate::{AssistCtx, Assist, AssistId}; @@ -10,7 +11,7 @@ use crate::{AssistCtx, Assist, AssistId}; pub(crate) fn split_import(mut ctx: AssistCtx) -> Option { let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?; let path = ast::Path::cast(colon_colon.parent())?; - let top_path = generate(Some(path), |it| it.parent_path()).last()?; + let top_path = successors(Some(path), |it| it.parent_path()).last()?; let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast); 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 @@ //! This crate provides some utilities for indenting rust code. //! +use std::iter::successors; use itertools::Itertools; use ra_syntax::{ SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind, ast::{self, AstNode, AstToken}, - algo::generate, }; pub fn reindent(text: &str, indent: &str) -> String { @@ -29,7 +29,7 @@ pub fn leading_indent(node: &SyntaxNode) -> Option<&str> { } fn prev_tokens(token: SyntaxToken) -> impl Iterator { - generate(token.prev_token(), |&token| token.prev_token()) + successors(token.prev_token(), |&token| token.prev_token()) } pub 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 @@ //! reference to a type with the field `bar`. This is an approximation of the //! logic in rustc (which lives in librustc_typeck/check/autoderef.rs). -use ra_syntax::algo::generate; +use std::iter::successors; use crate::HirDatabase; use super::Ty; @@ -11,7 +11,7 @@ use super::Ty; impl Ty { /// Iterates over the possible derefs of `ty`. pub fn autoderef<'a>(self, db: &'a impl HirDatabase) -> impl Iterator + 'a { - generate(Some(self), move |ty| ty.autoderef_step(db)) + successors(Some(self), move |ty| ty.autoderef_step(db)) } fn autoderef_step(&self, _db: &impl HirDatabase) -> Option { 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 pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement { root.0.covering_node(range).into() } - -// Replace with `std::iter::successors` in `1.34.0` -pub fn generate(seed: Option, step: impl Fn(&T) -> Option) -> impl Iterator { - ::itertools::unfold(seed, move |slot| { - slot.take().map(|curr| { - *slot = step(&curr); - curr - }) - }) -} 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 @@ -use std::marker::PhantomData; - +use std::{ + marker::PhantomData, + iter::successors, +}; use crate::{ AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, - algo::generate, }; /// A pointer to a syntax node inside a file. It can be used to remember a @@ -19,7 +20,7 @@ impl SyntaxNodePtr { } pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode { - generate(Some(source_file.syntax()), |&node| { + successors(Some(source_file.syntax()), |&node| { node.children().find(|it| self.range.is_subrange(&it.range())) }) .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::{ fmt::{self, Write}, any::Any, borrow::Borrow, + iter::successors, }; use ra_parser::ParseError; @@ -195,7 +196,7 @@ impl SyntaxNode { } pub fn ancestors(&self) -> impl Iterator { - crate::algo::generate(Some(self), |&node| node.parent()) + successors(Some(self), |&node| node.parent()) } pub fn descendants(&self) -> impl Iterator { @@ -213,7 +214,7 @@ impl SyntaxNode { } pub fn siblings(&self, direction: Direction) -> impl Iterator { - crate::algo::generate(Some(self), move |&node| match direction { + successors(Some(self), move |&node| match direction { Direction::Next => node.next_sibling(), Direction::Prev => node.prev_sibling(), }) @@ -224,7 +225,7 @@ impl SyntaxNode { direction: Direction, ) -> impl Iterator { let me: SyntaxElement = self.into(); - crate::algo::generate(Some(me), move |el| match direction { + successors(Some(me), move |el| match direction { Direction::Next => el.next_sibling_or_token(), Direction::Prev => el.prev_sibling_or_token(), }) @@ -373,7 +374,7 @@ impl<'a> SyntaxToken<'a> { direction: Direction, ) -> impl Iterator> { let me: SyntaxElement = (*self).into(); - crate::algo::generate(Some(me), move |el| match direction { + successors(Some(me), move |el| match direction { Direction::Next => el.next_sibling_or_token(), Direction::Prev => el.prev_sibling_or_token(), }) -- cgit v1.2.3