From 00cdde2c5218020b8f6ec751042a436aeef923c7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 17 Oct 2018 19:52:25 +0300 Subject: Update rowan --- crates/ra_syntax/src/algo/mod.rs | 2 +- crates/ra_syntax/src/algo/walk.rs | 28 ---------------------------- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/utils.rs | 7 +++---- crates/ra_syntax/src/yellow/mod.rs | 14 ++++++++++---- 5 files changed, 15 insertions(+), 38 deletions(-) delete mode 100644 crates/ra_syntax/src/algo/walk.rs (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/algo/mod.rs b/crates/ra_syntax/src/algo/mod.rs index b4896c482..9d2014bc7 100644 --- a/crates/ra_syntax/src/algo/mod.rs +++ b/crates/ra_syntax/src/algo/mod.rs @@ -1,5 +1,5 @@ pub mod visit; -pub mod walk; +// pub mod walk; use crate::{ text_utils::{contains_offset_nonstrict, is_subrange}, diff --git a/crates/ra_syntax/src/algo/walk.rs b/crates/ra_syntax/src/algo/walk.rs deleted file mode 100644 index 9afa86401..000000000 --- a/crates/ra_syntax/src/algo/walk.rs +++ /dev/null @@ -1,28 +0,0 @@ -use crate::{algo::generate, SyntaxNodeRef}; - -#[derive(Debug, Copy, Clone)] -pub enum WalkEvent<'a> { - Enter(SyntaxNodeRef<'a>), - Exit(SyntaxNodeRef<'a>), -} - -pub fn walk<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator> { - generate(Some(WalkEvent::Enter(root)), move |pos| { - let next = match *pos { - WalkEvent::Enter(node) => match node.first_child() { - Some(child) => WalkEvent::Enter(child), - None => WalkEvent::Exit(node), - }, - WalkEvent::Exit(node) => { - if node == root { - return None; - } - match node.next_sibling() { - Some(sibling) => WalkEvent::Enter(sibling), - None => WalkEvent::Exit(node.parent().unwrap()), - } - } - }; - Some(next) - }) -} diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 7a9718aad..3698eccd7 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -52,7 +52,7 @@ pub use crate::{ reparsing::AtomEdit, rowan::{SmolStr, TextRange, TextUnit}, syntax_kinds::SyntaxKind, - yellow::{Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot}, + yellow::{Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent}, }; use crate::yellow::GreenNode; diff --git a/crates/ra_syntax/src/utils.rs b/crates/ra_syntax/src/utils.rs index 27248ff32..7d0ef2fa2 100644 --- a/crates/ra_syntax/src/utils.rs +++ b/crates/ra_syntax/src/utils.rs @@ -1,6 +1,5 @@ use crate::{ - algo::walk::{walk, WalkEvent}, - File, SyntaxKind, SyntaxNodeRef, + File, SyntaxKind, SyntaxNodeRef, WalkEvent }; use std::fmt::Write; @@ -19,7 +18,7 @@ pub fn dump_tree(syntax: SyntaxNodeRef) -> String { }; } - for event in walk(syntax) { + for event in syntax.preorder() { match event { WalkEvent::Enter(node) => { indent!(); @@ -34,7 +33,7 @@ pub fn dump_tree(syntax: SyntaxNodeRef) -> String { } level += 1; } - WalkEvent::Exit(_) => level -= 1, + WalkEvent::Leave(_) => level -= 1, } } diff --git a/crates/ra_syntax/src/yellow/mod.rs b/crates/ra_syntax/src/yellow/mod.rs index b5c9da813..650917214 100644 --- a/crates/ra_syntax/src/yellow/mod.rs +++ b/crates/ra_syntax/src/yellow/mod.rs @@ -10,7 +10,7 @@ use std::{ }; pub(crate) use self::builder::GreenBuilder; -pub use rowan::TreeRoot; +pub use rowan::{TreeRoot, WalkEvent}; #[derive(Debug, Clone, Copy)] pub enum RaTypes {} @@ -71,9 +71,9 @@ impl<'a> SyntaxNodeRef<'a> { crate::algo::generate(Some(self), |&node| node.parent()) } pub fn descendants(self) -> impl Iterator> { - crate::algo::walk::walk(self).filter_map(|event| match event { - crate::algo::walk::WalkEvent::Enter(node) => Some(node), - crate::algo::walk::WalkEvent::Exit(_) => None, + self.preorder().filter_map(|event| match event { + WalkEvent::Enter(node) => Some(node), + WalkEvent::Leave(_) => None, }) } pub fn siblings(self, direction: Direction) -> impl Iterator> { @@ -82,6 +82,12 @@ impl<'a> SyntaxNodeRef<'a> { Direction::Prev => node.prev_sibling(), }) } + pub fn preorder(self) -> impl Iterator>> { + self.0.preorder().map(|event| match event { + WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode(n)), + WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode(n)), + }) + } } impl> SyntaxNode { -- cgit v1.2.3