aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-10-02 16:02:57 +0100
committerAleksey Kladov <[email protected]>2018-10-02 16:02:57 +0100
commitd323c81d5cc6a198239285abcede2166181d8f39 (patch)
tree02c64a01b14d893df439a9e90797db6d58c5a54d /crates/ra_syntax/src
parentdccaa5e45e9baaa2d286353a7499a89af1669e42 (diff)
make ancestors and descendants inherent
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/algo/mod.rs8
-rw-r--r--crates/ra_syntax/src/algo/walk.rs6
-rw-r--r--crates/ra_syntax/src/reparsing.rs2
-rw-r--r--crates/ra_syntax/src/utils.rs4
-rw-r--r--crates/ra_syntax/src/yellow/mod.rs9
-rw-r--r--crates/ra_syntax/src/yellow/syntax_text.rs4
6 files changed, 16 insertions, 17 deletions
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
94 common_ancestor(left, right) 94 common_ancestor(left, right)
95} 95}
96 96
97pub fn ancestors<'a>(node: SyntaxNodeRef<'a>) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
98 generate(Some(node), |&node| node.parent())
99}
100
101#[derive(Debug)] 97#[derive(Debug)]
102pub enum Direction { 98pub enum Direction {
103 Forward, 99 Forward,
@@ -115,8 +111,8 @@ pub fn siblings<'a>(
115} 111}
116 112
117fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> { 113fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> {
118 for p in ancestors(n1) { 114 for p in n1.ancestors() {
119 if ancestors(n2).any(|a| a == p) { 115 if n2.ancestors().any(|a| a == p) {
120 return p; 116 return p;
121 } 117 }
122 } 118 }
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 {
3 algo::generate, 3 algo::generate,
4}; 4};
5 5
6pub fn preorder<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator<Item = SyntaxNodeRef<'a>> {
7 walk(root).filter_map(|event| match event {
8 WalkEvent::Enter(node) => Some(node),
9 WalkEvent::Exit(_) => None,
10 })
11}
12 6
13#[derive(Debug, Copy, Clone)] 7#[derive(Debug, Copy, Clone)]
14pub enum WalkEvent<'a> { 8pub 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>(
112 range: TextRange, 112 range: TextRange,
113) -> Option<(SyntaxNodeRef<'node>, fn(&mut Parser))> { 113) -> Option<(SyntaxNodeRef<'node>, fn(&mut Parser))> {
114 let node = algo::find_covering_node(node, range); 114 let node = algo::find_covering_node(node, range);
115 return algo::ancestors(node) 115 return node.ancestors()
116 .filter_map(|node| reparser(node).map(|r| (node, r))) 116 .filter_map(|node| reparser(node).map(|r| (node, r)))
117 .next(); 117 .next();
118 118
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 @@
1use std::fmt::Write; 1use std::fmt::Write;
2use { 2use {
3 algo::walk::{preorder, walk, WalkEvent}, 3 algo::walk::{walk, WalkEvent},
4 SyntaxKind, File, SyntaxNodeRef 4 SyntaxKind, File, SyntaxNodeRef
5}; 5};
6 6
@@ -56,7 +56,7 @@ pub fn check_fuzz_invariants(text: &str) {
56 56
57pub(crate) fn validate_block_structure(root: SyntaxNodeRef) { 57pub(crate) fn validate_block_structure(root: SyntaxNodeRef) {
58 let mut stack = Vec::new(); 58 let mut stack = Vec::new();
59 for node in preorder(root) { 59 for node in root.descendants() {
60 match node.kind() { 60 match node.kind() {
61 SyntaxKind::L_CURLY => { 61 SyntaxKind::L_CURLY => {
62 stack.push(node) 62 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> {
62 pub fn leaf_text(self) -> Option<&'a SmolStr> { 62 pub fn leaf_text(self) -> Option<&'a SmolStr> {
63 self.0.leaf_text() 63 self.0.leaf_text()
64 } 64 }
65 pub fn ancestors(self) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
66 ::algo::generate(Some(self), |&node| node.parent())
67 }
68 pub fn descendants(self) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
69 ::algo::walk::walk(self).filter_map(|event| match event {
70 ::algo::walk::WalkEvent::Enter(node) => Some(node),
71 ::algo::walk::WalkEvent::Exit(_) => None,
72 })
73 }
65} 74}
66 75
67impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { 76impl<R: TreeRoot<RaTypes>> SyntaxNode<R> {
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::{
4 4
5use { 5use {
6 SyntaxNodeRef, TextRange, TextUnit, 6 SyntaxNodeRef, TextRange, TextUnit,
7 algo::walk::preorder,
8 text_utils::{intersect, contains_offset_nonstrict}, 7 text_utils::{intersect, contains_offset_nonstrict},
9}; 8};
10 9
@@ -23,7 +22,8 @@ impl<'a> SyntaxText<'a> {
23 } 22 }
24 pub fn chunks(&self) -> impl Iterator<Item=&'a str> { 23 pub fn chunks(&self) -> impl Iterator<Item=&'a str> {
25 let range = self.range; 24 let range = self.range;
26 preorder(self.node) 25 self.node
26 .descendants()
27 .filter_map(move |node| { 27 .filter_map(move |node| {
28 let text = node.leaf_text()?; 28 let text = node.leaf_text()?;
29 let range = intersect(range, node.range())?; 29 let range = intersect(range, node.range())?;