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_syntax/src/algo.rs | 10 ---------- crates/ra_syntax/src/ptr.rs | 9 +++++---- crates/ra_syntax/src/syntax_node.rs | 9 +++++---- 3 files changed, 10 insertions(+), 18 deletions(-) (limited to 'crates/ra_syntax/src') 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