From d323c81d5cc6a198239285abcede2166181d8f39 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 2 Oct 2018 18:02:57 +0300 Subject: make ancestors and descendants inherent --- crates/ra_syntax/src/algo/mod.rs | 8 ++------ crates/ra_syntax/src/algo/walk.rs | 6 ------ crates/ra_syntax/src/reparsing.rs | 2 +- crates/ra_syntax/src/utils.rs | 4 ++-- crates/ra_syntax/src/yellow/mod.rs | 9 +++++++++ crates/ra_syntax/src/yellow/syntax_text.rs | 4 ++-- 6 files changed, 16 insertions(+), 17 deletions(-) (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 8de44c586..3716a6000 100644 --- a/crates/ra_syntax/src/algo/mod.rs +++ b/crates/ra_syntax/src/algo/mod.rs @@ -94,10 +94,6 @@ pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRe common_ancestor(left, right) } -pub fn ancestors<'a>(node: SyntaxNodeRef<'a>) -> impl Iterator> { - generate(Some(node), |&node| node.parent()) -} - #[derive(Debug)] pub enum Direction { Forward, @@ -115,8 +111,8 @@ pub fn siblings<'a>( } fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> { - for p in ancestors(n1) { - if ancestors(n2).any(|a| a == p) { + for p in n1.ancestors() { + if n2.ancestors().any(|a| a == p) { return p; } } diff --git a/crates/ra_syntax/src/algo/walk.rs b/crates/ra_syntax/src/algo/walk.rs index 536ee705f..8e294d965 100644 --- a/crates/ra_syntax/src/algo/walk.rs +++ b/crates/ra_syntax/src/algo/walk.rs @@ -3,12 +3,6 @@ use { algo::generate, }; -pub fn preorder<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator> { - walk(root).filter_map(|event| match event { - WalkEvent::Enter(node) => Some(node), - WalkEvent::Exit(_) => None, - }) -} #[derive(Debug, Copy, Clone)] pub enum WalkEvent<'a> { diff --git a/crates/ra_syntax/src/reparsing.rs b/crates/ra_syntax/src/reparsing.rs index e3c200d1e..dcafd2c40 100644 --- a/crates/ra_syntax/src/reparsing.rs +++ b/crates/ra_syntax/src/reparsing.rs @@ -112,7 +112,7 @@ fn find_reparsable_node<'node>( range: TextRange, ) -> Option<(SyntaxNodeRef<'node>, fn(&mut Parser))> { let node = algo::find_covering_node(node, range); - return algo::ancestors(node) + return node.ancestors() .filter_map(|node| reparser(node).map(|r| (node, r))) .next(); diff --git a/crates/ra_syntax/src/utils.rs b/crates/ra_syntax/src/utils.rs index 8bc5f0e24..e274f7471 100644 --- a/crates/ra_syntax/src/utils.rs +++ b/crates/ra_syntax/src/utils.rs @@ -1,6 +1,6 @@ use std::fmt::Write; use { - algo::walk::{preorder, walk, WalkEvent}, + algo::walk::{walk, WalkEvent}, SyntaxKind, File, SyntaxNodeRef }; @@ -56,7 +56,7 @@ pub fn check_fuzz_invariants(text: &str) { pub(crate) fn validate_block_structure(root: SyntaxNodeRef) { let mut stack = Vec::new(); - for node in preorder(root) { + for node in root.descendants() { match node.kind() { SyntaxKind::L_CURLY => { stack.push(node) diff --git a/crates/ra_syntax/src/yellow/mod.rs b/crates/ra_syntax/src/yellow/mod.rs index 2c57d8b41..95d277a2f 100644 --- a/crates/ra_syntax/src/yellow/mod.rs +++ b/crates/ra_syntax/src/yellow/mod.rs @@ -62,6 +62,15 @@ impl<'a> SyntaxNodeRef<'a> { pub fn leaf_text(self) -> Option<&'a SmolStr> { self.0.leaf_text() } + pub fn ancestors(self) -> impl Iterator> { + ::algo::generate(Some(self), |&node| node.parent()) + } + pub fn descendants(self) -> impl Iterator> { + ::algo::walk::walk(self).filter_map(|event| match event { + ::algo::walk::WalkEvent::Enter(node) => Some(node), + ::algo::walk::WalkEvent::Exit(_) => None, + }) + } } impl> SyntaxNode { diff --git a/crates/ra_syntax/src/yellow/syntax_text.rs b/crates/ra_syntax/src/yellow/syntax_text.rs index affd7f9c7..0db1049de 100644 --- a/crates/ra_syntax/src/yellow/syntax_text.rs +++ b/crates/ra_syntax/src/yellow/syntax_text.rs @@ -4,7 +4,6 @@ use std::{ use { SyntaxNodeRef, TextRange, TextUnit, - algo::walk::preorder, text_utils::{intersect, contains_offset_nonstrict}, }; @@ -23,7 +22,8 @@ impl<'a> SyntaxText<'a> { } pub fn chunks(&self) -> impl Iterator { let range = self.range; - preorder(self.node) + self.node + .descendants() .filter_map(move |node| { let text = node.leaf_text()?; let range = intersect(range, node.range())?; -- cgit v1.2.3