aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
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
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')
-rw-r--r--crates/ra_syntax/src/algo/mod.rs24
-rw-r--r--crates/ra_syntax/src/algo/walk.rs6
-rw-r--r--crates/ra_syntax/src/lib.rs2
-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.rs26
-rw-r--r--crates/ra_syntax/src/yellow/syntax_text.rs4
7 files changed, 32 insertions, 36 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> {
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index c7eda4563..738664afd 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -51,7 +51,7 @@ pub use {
51 ast::AstNode, 51 ast::AstNode,
52 lexer::{tokenize, Token}, 52 lexer::{tokenize, Token},
53 syntax_kinds::SyntaxKind, 53 syntax_kinds::SyntaxKind,
54 yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError}, 54 yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError, Direction},
55 reparsing::AtomEdit, 55 reparsing::AtomEdit,
56}; 56};
57 57
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 c621b1d6a..710320f47 100644
--- a/crates/ra_syntax/src/yellow/mod.rs
+++ b/crates/ra_syntax/src/yellow/mod.rs
@@ -53,15 +53,37 @@ impl<R: TreeRoot<RaTypes>> Hash for SyntaxNode<R> {
53 } 53 }
54} 54}
55 55
56impl SyntaxNode<OwnedRoot> { 56impl SyntaxNode {
57 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxNode { 57 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxNode {
58 SyntaxNode(::rowan::SyntaxNode::new(green, errors)) 58 SyntaxNode(::rowan::SyntaxNode::new(green, errors))
59 } 59 }
60} 60}
61impl<'a> SyntaxNode<RefRoot<'a>> { 61
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub enum Direction {
64 Next,
65 Prev,
66}
67
68impl<'a> SyntaxNodeRef<'a> {
62 pub fn leaf_text(self) -> Option<&'a SmolStr> { 69 pub fn leaf_text(self) -> Option<&'a SmolStr> {
63 self.0.leaf_text() 70 self.0.leaf_text()
64 } 71 }
72 pub fn ancestors(self) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
73 ::algo::generate(Some(self), |&node| node.parent())
74 }
75 pub fn descendants(self) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
76 ::algo::walk::walk(self).filter_map(|event| match event {
77 ::algo::walk::WalkEvent::Enter(node) => Some(node),
78 ::algo::walk::WalkEvent::Exit(_) => None,
79 })
80 }
81 pub fn siblings(self, direction: Direction) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
82 ::algo::generate(Some(self), move |&node| match direction {
83 Direction::Next => node.next_sibling(),
84 Direction::Prev => node.prev_sibling(),
85 })
86 }
65} 87}
66 88
67impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { 89impl<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())?;