diff options
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/algo/mod.rs | 24 | ||||
-rw-r--r-- | crates/ra_syntax/src/algo/walk.rs | 6 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/reparsing.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/utils.rs | 4 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/mod.rs | 26 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/syntax_text.rs | 4 |
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 | ||
97 | pub fn ancestors<'a>(node: SyntaxNodeRef<'a>) -> impl Iterator<Item=SyntaxNodeRef<'a>> { | ||
98 | generate(Some(node), |&node| node.parent()) | ||
99 | } | ||
100 | |||
101 | #[derive(Debug)] | ||
102 | pub enum Direction { | ||
103 | Forward, | ||
104 | Backward, | ||
105 | } | ||
106 | |||
107 | pub 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 | |||
117 | fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> { | 97 | fn 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 | ||
6 | pub 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)] |
14 | pub enum WalkEvent<'a> { | 8 | pub 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 @@ | |||
1 | use std::fmt::Write; | 1 | use std::fmt::Write; |
2 | use { | 2 | use { |
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 | ||
57 | pub(crate) fn validate_block_structure(root: SyntaxNodeRef) { | 57 | pub(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 | ||
56 | impl SyntaxNode<OwnedRoot> { | 56 | impl 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 | } |
61 | impl<'a> SyntaxNode<RefRoot<'a>> { | 61 | |
62 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
63 | pub enum Direction { | ||
64 | Next, | ||
65 | Prev, | ||
66 | } | ||
67 | |||
68 | impl<'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 | ||
67 | impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { | 89 | impl<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 | ||
5 | use { | 5 | use { |
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())?; |