aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/algo
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-10-02 16:51:39 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-10-02 16:51:39 +0100
commit248ee0c3fe333f9180f8c9d9dfd4efcc6886b5bc (patch)
treef94bb3a8bd47e3612ef29e815532c07d7871fa0f /crates/ra_syntax/src/algo
parent7ffc114dab6d1e25ead195a5937cd4f9ca51ef2c (diff)
parent1a2a8dec14ec04ea8eeccae99fd885e7a280e45b (diff)
Merge #90
90: Inherent traversal r=matklad a=matklad bors r+ Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/algo')
-rw-r--r--crates/ra_syntax/src/algo/mod.rs24
-rw-r--r--crates/ra_syntax/src/algo/walk.rs6
2 files changed, 2 insertions, 28 deletions
diff --git a/crates/ra_syntax/src/algo/mod.rs b/crates/ra_syntax/src/algo/mod.rs
index 8de44c586..a6678093d 100644
--- a/crates/ra_syntax/src/algo/mod.rs
+++ b/crates/ra_syntax/src/algo/mod.rs
@@ -94,29 +94,9 @@ 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)]
102pub enum Direction {
103 Forward,
104 Backward,
105}
106
107pub fn siblings<'a>(
108 node: SyntaxNodeRef<'a>,
109 direction: Direction
110) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
111 generate(Some(node), move |&node| match direction {
112 Direction::Forward => node.next_sibling(),
113 Direction::Backward => node.prev_sibling(),
114 })
115}
116
117fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> { 97fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> {
118 for p in ancestors(n1) { 98 for p in n1.ancestors() {
119 if ancestors(n2).any(|a| a == p) { 99 if n2.ancestors().any(|a| a == p) {
120 return p; 100 return p;
121 } 101 }
122 } 102 }
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> {