replace usages of algo::generate with iter::successors from std

This commit is contained in:
Robin Freyler 2019-04-13 16:43:49 +02:00
parent 8887782c4a
commit 6aae0cf7fa
No known key found for this signature in database
GPG key ID: 1949800ACC8CE89B
6 changed files with 17 additions and 24 deletions

View file

@ -1,8 +1,9 @@
use std::iter::successors;
use hir::db::HirDatabase; use hir::db::HirDatabase;
use ra_syntax::{ use ra_syntax::{
TextUnit, AstNode, SyntaxKind::COLONCOLON, TextUnit, AstNode, SyntaxKind::COLONCOLON,
ast, ast,
algo::generate,
}; };
use crate::{AssistCtx, Assist, AssistId}; use crate::{AssistCtx, Assist, AssistId};
@ -10,7 +11,7 @@ use crate::{AssistCtx, Assist, AssistId};
pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?; let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?;
let path = ast::Path::cast(colon_colon.parent())?; 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); let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast);
if use_tree.is_none() { if use_tree.is_none() {

View file

@ -1,10 +1,10 @@
//! This crate provides some utilities for indenting rust code. //! This crate provides some utilities for indenting rust code.
//! //!
use std::iter::successors;
use itertools::Itertools; use itertools::Itertools;
use ra_syntax::{ use ra_syntax::{
SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind, SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind,
ast::{self, AstNode, AstToken}, ast::{self, AstNode, AstToken},
algo::generate,
}; };
pub fn reindent(text: &str, indent: &str) -> String { 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<Item = SyntaxToken> { fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
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> { pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {

View file

@ -3,7 +3,7 @@
//! reference to a type with the field `bar`. This is an approximation of the //! 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). //! logic in rustc (which lives in librustc_typeck/check/autoderef.rs).
use ra_syntax::algo::generate; use std::iter::successors;
use crate::HirDatabase; use crate::HirDatabase;
use super::Ty; use super::Ty;
@ -11,7 +11,7 @@ use super::Ty;
impl Ty { impl Ty {
/// Iterates over the possible derefs of `ty`. /// Iterates over the possible derefs of `ty`.
pub fn autoderef<'a>(self, db: &'a impl HirDatabase) -> impl Iterator<Item = Ty> + 'a { pub fn autoderef<'a>(self, db: &'a impl HirDatabase) -> impl Iterator<Item = Ty> + '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<Ty> { fn autoderef_step(&self, _db: &impl HirDatabase) -> Option<Ty> {

View file

@ -46,13 +46,3 @@ pub fn non_trivia_sibling(element: SyntaxElement, direction: Direction) -> Optio
pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement { pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement {
root.0.covering_node(range).into() root.0.covering_node(range).into()
} }
// Replace with `std::iter::successors` in `1.34.0`
pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item = T> {
::itertools::unfold(seed, move |slot| {
slot.take().map(|curr| {
*slot = step(&curr);
curr
})
})
}

View file

@ -1,8 +1,9 @@
use std::marker::PhantomData; use std::{
marker::PhantomData,
iter::successors,
};
use crate::{ use crate::{
AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange,
algo::generate,
}; };
/// A pointer to a syntax node inside a file. It can be used to remember a /// 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 { 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())) node.children().find(|it| self.range.is_subrange(&it.range()))
}) })
.find(|it| it.range() == self.range && it.kind() == self.kind) .find(|it| it.range() == self.range && it.kind() == self.kind)

View file

@ -10,6 +10,7 @@ use std::{
fmt::{self, Write}, fmt::{self, Write},
any::Any, any::Any,
borrow::Borrow, borrow::Borrow,
iter::successors,
}; };
use ra_parser::ParseError; use ra_parser::ParseError;
@ -195,7 +196,7 @@ impl SyntaxNode {
} }
pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> { pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> {
crate::algo::generate(Some(self), |&node| node.parent()) successors(Some(self), |&node| node.parent())
} }
pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> { pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> {
@ -213,7 +214,7 @@ impl SyntaxNode {
} }
pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> { pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> {
crate::algo::generate(Some(self), move |&node| match direction { successors(Some(self), move |&node| match direction {
Direction::Next => node.next_sibling(), Direction::Next => node.next_sibling(),
Direction::Prev => node.prev_sibling(), Direction::Prev => node.prev_sibling(),
}) })
@ -224,7 +225,7 @@ impl SyntaxNode {
direction: Direction, direction: Direction,
) -> impl Iterator<Item = SyntaxElement> { ) -> impl Iterator<Item = SyntaxElement> {
let me: SyntaxElement = self.into(); 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::Next => el.next_sibling_or_token(),
Direction::Prev => el.prev_sibling_or_token(), Direction::Prev => el.prev_sibling_or_token(),
}) })
@ -373,7 +374,7 @@ impl<'a> SyntaxToken<'a> {
direction: Direction, direction: Direction,
) -> impl Iterator<Item = SyntaxElement<'a>> { ) -> impl Iterator<Item = SyntaxElement<'a>> {
let me: SyntaxElement = (*self).into(); 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::Next => el.next_sibling_or_token(),
Direction::Prev => el.prev_sibling_or_token(), Direction::Prev => el.prev_sibling_or_token(),
}) })