+use std::iter::successors;
+
use hir::db::HirDatabase;
use ra_syntax::{
TextUnit, AstNode, SyntaxKind::COLONCOLON,
ast,
- algo::generate,
};
use crate::{AssistCtx, Assist, AssistId};
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 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() {
//! 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 {
}
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> {
//! 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;
impl Ty {
/// Iterates over the possible derefs of `ty`.
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> {
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<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
- })
- })
-}
-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
}
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)
fmt::{self, Write},
any::Any,
borrow::Borrow,
+ iter::successors,
};
use ra_parser::ParseError;
}
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 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::Prev => node.prev_sibling(),
})
direction: Direction,
) -> impl Iterator<Item = SyntaxElement> {
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(),
})
direction: Direction,
) -> impl Iterator<Item = SyntaxElement<'a>> {
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(),
})