diff options
author | Aleksey Kladov <[email protected]> | 2019-01-07 13:15:47 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-08 08:20:15 +0000 |
commit | d91a98ec843ec8562c58ccb01a1e29d00cc744dc (patch) | |
tree | c628b6afbbe2e3a31ea88e6c16f556309dd0dde9 /crates/ra_syntax/src | |
parent | 55272f2023e69f3eff727ecbf77078e1b45e7f86 (diff) |
switch ra_syntax to new rowan API
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/algo.rs | 16 | ||||
-rw-r--r-- | crates/ra_syntax/src/algo/visit.rs | 30 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 199 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs.tera | 93 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 38 | ||||
-rw-r--r-- | crates/ra_syntax/src/reparsing.rs | 35 | ||||
-rw-r--r-- | crates/ra_syntax/src/utils.rs | 17 | ||||
-rw-r--r-- | crates/ra_syntax/src/validation.rs | 15 | ||||
-rw-r--r-- | crates/ra_syntax/src/validation/byte.rs | 8 | ||||
-rw-r--r-- | crates/ra_syntax/src/validation/byte_string.rs | 8 | ||||
-rw-r--r-- | crates/ra_syntax/src/validation/char.rs | 8 | ||||
-rw-r--r-- | crates/ra_syntax/src/validation/string.rs | 8 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow.rs | 104 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/syntax_text.rs | 6 |
14 files changed, 262 insertions, 323 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 4b3548ea9..13f50d2ef 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs | |||
@@ -1,19 +1,23 @@ | |||
1 | pub mod visit; | 1 | pub mod visit; |
2 | 2 | ||
3 | use crate::{SyntaxNode, SyntaxNodeRef, TextRange, TextUnit}; | 3 | use rowan::TransparentNewType; |
4 | |||
5 | use crate::{SyntaxNode, TextRange, TextUnit}; | ||
4 | 6 | ||
5 | pub use rowan::LeafAtOffset; | 7 | pub use rowan::LeafAtOffset; |
6 | 8 | ||
7 | pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset<SyntaxNodeRef> { | 9 | pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<&SyntaxNode> { |
8 | match node.0.leaf_at_offset(offset) { | 10 | match node.0.leaf_at_offset(offset) { |
9 | LeafAtOffset::None => LeafAtOffset::None, | 11 | LeafAtOffset::None => LeafAtOffset::None, |
10 | LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode(n)), | 12 | LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode::from_repr(n)), |
11 | LeafAtOffset::Between(l, r) => LeafAtOffset::Between(SyntaxNode(l), SyntaxNode(r)), | 13 | LeafAtOffset::Between(l, r) => { |
14 | LeafAtOffset::Between(SyntaxNode::from_repr(l), SyntaxNode::from_repr(r)) | ||
15 | } | ||
12 | } | 16 | } |
13 | } | 17 | } |
14 | 18 | ||
15 | pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef { | 19 | pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode { |
16 | SyntaxNode(root.0.covering_node(range)) | 20 | SyntaxNode::from_repr(root.0.covering_node(range)) |
17 | } | 21 | } |
18 | 22 | ||
19 | pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item = T> { | 23 | pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item = T> { |
diff --git a/crates/ra_syntax/src/algo/visit.rs b/crates/ra_syntax/src/algo/visit.rs index c021f464c..38f21594c 100644 --- a/crates/ra_syntax/src/algo/visit.rs +++ b/crates/ra_syntax/src/algo/visit.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use crate::{AstNode, SyntaxNodeRef}; | 1 | use crate::{AstNode, SyntaxNode}; |
2 | 2 | ||
3 | use std::marker::PhantomData; | 3 | use std::marker::PhantomData; |
4 | 4 | ||
@@ -15,11 +15,11 @@ pub fn visitor_ctx<'a, T, C>(ctx: C) -> impl VisitorCtx<'a, Output = T, Ctx = C> | |||
15 | 15 | ||
16 | pub trait Visitor<'a>: Sized { | 16 | pub trait Visitor<'a>: Sized { |
17 | type Output; | 17 | type Output; |
18 | fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>; | 18 | fn accept(self, node: &'a SyntaxNode) -> Option<Self::Output>; |
19 | fn visit<N, F>(self, f: F) -> Vis<Self, N, F> | 19 | fn visit<N, F>(self, f: F) -> Vis<Self, N, F> |
20 | where | 20 | where |
21 | N: AstNode<'a>, | 21 | N: AstNode + 'a, |
22 | F: FnOnce(N) -> Self::Output, | 22 | F: FnOnce(&'a N) -> Self::Output, |
23 | { | 23 | { |
24 | Vis { | 24 | Vis { |
25 | inner: self, | 25 | inner: self, |
@@ -32,11 +32,11 @@ pub trait Visitor<'a>: Sized { | |||
32 | pub trait VisitorCtx<'a>: Sized { | 32 | pub trait VisitorCtx<'a>: Sized { |
33 | type Output; | 33 | type Output; |
34 | type Ctx; | 34 | type Ctx; |
35 | fn accept(self, node: SyntaxNodeRef<'a>) -> Result<Self::Output, Self::Ctx>; | 35 | fn accept(self, node: &'a SyntaxNode) -> Result<Self::Output, Self::Ctx>; |
36 | fn visit<N, F>(self, f: F) -> VisCtx<Self, N, F> | 36 | fn visit<N, F>(self, f: F) -> VisCtx<Self, N, F> |
37 | where | 37 | where |
38 | N: AstNode<'a>, | 38 | N: AstNode + 'a, |
39 | F: FnOnce(N, Self::Ctx) -> Self::Output, | 39 | F: FnOnce(&'a N, Self::Ctx) -> Self::Output, |
40 | { | 40 | { |
41 | VisCtx { | 41 | VisCtx { |
42 | inner: self, | 42 | inner: self, |
@@ -54,7 +54,7 @@ struct EmptyVisitor<T> { | |||
54 | impl<'a, T> Visitor<'a> for EmptyVisitor<T> { | 54 | impl<'a, T> Visitor<'a> for EmptyVisitor<T> { |
55 | type Output = T; | 55 | type Output = T; |
56 | 56 | ||
57 | fn accept(self, _node: SyntaxNodeRef<'a>) -> Option<T> { | 57 | fn accept(self, _node: &'a SyntaxNode) -> Option<T> { |
58 | None | 58 | None |
59 | } | 59 | } |
60 | } | 60 | } |
@@ -69,7 +69,7 @@ impl<'a, T, C> VisitorCtx<'a> for EmptyVisitorCtx<T, C> { | |||
69 | type Output = T; | 69 | type Output = T; |
70 | type Ctx = C; | 70 | type Ctx = C; |
71 | 71 | ||
72 | fn accept(self, _node: SyntaxNodeRef<'a>) -> Result<T, C> { | 72 | fn accept(self, _node: &'a SyntaxNode) -> Result<T, C> { |
73 | Err(self.ctx) | 73 | Err(self.ctx) |
74 | } | 74 | } |
75 | } | 75 | } |
@@ -84,12 +84,12 @@ pub struct Vis<V, N, F> { | |||
84 | impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F> | 84 | impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F> |
85 | where | 85 | where |
86 | V: Visitor<'a>, | 86 | V: Visitor<'a>, |
87 | N: AstNode<'a>, | 87 | N: AstNode + 'a, |
88 | F: FnOnce(N) -> <V as Visitor<'a>>::Output, | 88 | F: FnOnce(&'a N) -> <V as Visitor<'a>>::Output, |
89 | { | 89 | { |
90 | type Output = <V as Visitor<'a>>::Output; | 90 | type Output = <V as Visitor<'a>>::Output; |
91 | 91 | ||
92 | fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output> { | 92 | fn accept(self, node: &'a SyntaxNode) -> Option<Self::Output> { |
93 | let Vis { inner, f, .. } = self; | 93 | let Vis { inner, f, .. } = self; |
94 | inner.accept(node).or_else(|| N::cast(node).map(f)) | 94 | inner.accept(node).or_else(|| N::cast(node).map(f)) |
95 | } | 95 | } |
@@ -105,13 +105,13 @@ pub struct VisCtx<V, N, F> { | |||
105 | impl<'a, V, N, F> VisitorCtx<'a> for VisCtx<V, N, F> | 105 | impl<'a, V, N, F> VisitorCtx<'a> for VisCtx<V, N, F> |
106 | where | 106 | where |
107 | V: VisitorCtx<'a>, | 107 | V: VisitorCtx<'a>, |
108 | N: AstNode<'a>, | 108 | N: AstNode + 'a, |
109 | F: FnOnce(N, <V as VisitorCtx<'a>>::Ctx) -> <V as VisitorCtx<'a>>::Output, | 109 | F: FnOnce(&'a N, <V as VisitorCtx<'a>>::Ctx) -> <V as VisitorCtx<'a>>::Output, |
110 | { | 110 | { |
111 | type Output = <V as VisitorCtx<'a>>::Output; | 111 | type Output = <V as VisitorCtx<'a>>::Output; |
112 | type Ctx = <V as VisitorCtx<'a>>::Ctx; | 112 | type Ctx = <V as VisitorCtx<'a>>::Ctx; |
113 | 113 | ||
114 | fn accept(self, node: SyntaxNodeRef<'a>) -> Result<Self::Output, Self::Ctx> { | 114 | fn accept(self, node: &'a SyntaxNode) -> Result<Self::Output, Self::Ctx> { |
115 | let VisCtx { inner, f, .. } = self; | 115 | let VisCtx { inner, f, .. } = self; |
116 | inner.accept(node).or_else(|ctx| match N::cast(node) { | 116 | inner.accept(node).or_else(|ctx| match N::cast(node) { |
117 | None => Err(ctx), | 117 | None => Err(ctx), |
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 9ab59738f..285dda1e0 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -1,119 +1,88 @@ | |||
1 | mod generated; | 1 | mod generated; |
2 | 2 | ||
3 | use std::marker::PhantomData; | 3 | use std::marker::PhantomData; |
4 | use std::string::String as RustString; | ||
5 | 4 | ||
6 | use itertools::Itertools; | 5 | use itertools::Itertools; |
7 | 6 | ||
8 | pub use self::generated::*; | 7 | pub use self::generated::*; |
9 | use crate::{ | 8 | use crate::{ |
10 | yellow::{RefRoot, SyntaxNodeChildren}, | 9 | yellow::{SyntaxNode, SyntaxNodeChildren, TreePtr, RaTypes}, |
11 | SmolStr, | 10 | SmolStr, |
12 | SyntaxKind::*, | 11 | SyntaxKind::*, |
13 | SyntaxNodeRef, | ||
14 | }; | 12 | }; |
15 | 13 | ||
16 | /// The main trait to go from untyped `SyntaxNode` to a typed ast. The | 14 | /// The main trait to go from untyped `SyntaxNode` to a typed ast. The |
17 | /// conversion itself has zero runtime cost: ast and syntax nodes have exactly | 15 | /// conversion itself has zero runtime cost: ast and syntax nodes have exactly |
18 | /// the same representation: a pointer to the tree root and a pointer to the | 16 | /// the same representation: a pointer to the tree root and a pointer to the |
19 | /// node itself. | 17 | /// node itself. |
20 | pub trait AstNode<'a>: Clone + Copy + 'a { | 18 | pub trait AstNode: rowan::TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>> { |
21 | fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> | 19 | fn cast(syntax: &SyntaxNode) -> Option<&Self> |
22 | where | 20 | where |
23 | Self: Sized; | 21 | Self: Sized; |
24 | fn syntax(self) -> SyntaxNodeRef<'a>; | 22 | fn syntax(&self) -> &SyntaxNode; |
23 | fn to_owned(&self) -> TreePtr<Self>; | ||
25 | } | 24 | } |
26 | 25 | ||
27 | pub trait NameOwner<'a>: AstNode<'a> { | 26 | pub trait NameOwner: AstNode { |
28 | fn name(self) -> Option<Name<'a>> { | 27 | fn name(&self) -> Option<&Name> { |
29 | child_opt(self) | 28 | child_opt(self) |
30 | } | 29 | } |
31 | } | 30 | } |
32 | 31 | ||
33 | pub trait VisibilityOwner<'a>: AstNode<'a> { | 32 | pub trait VisibilityOwner: AstNode { |
34 | fn visibility(self) -> Option<Visibility<'a>> { | 33 | fn visibility(&self) -> Option<&Visibility> { |
35 | child_opt(self) | 34 | child_opt(self) |
36 | } | 35 | } |
37 | } | 36 | } |
38 | 37 | ||
39 | pub trait LoopBodyOwner<'a>: AstNode<'a> { | 38 | pub trait LoopBodyOwner: AstNode { |
40 | fn loop_body(self) -> Option<Block<'a>> { | 39 | fn loop_body(&self) -> Option<&Block> { |
41 | child_opt(self) | 40 | child_opt(self) |
42 | } | 41 | } |
43 | } | 42 | } |
44 | 43 | ||
45 | pub trait ArgListOwner<'a>: AstNode<'a> { | 44 | pub trait ArgListOwner: AstNode { |
46 | fn arg_list(self) -> Option<ArgList<'a>> { | 45 | fn arg_list(&self) -> Option<&ArgList> { |
47 | child_opt(self) | 46 | child_opt(self) |
48 | } | 47 | } |
49 | } | 48 | } |
50 | 49 | ||
51 | pub trait FnDefOwner<'a>: AstNode<'a> { | 50 | pub trait FnDefOwner: AstNode { |
52 | fn functions(self) -> AstChildren<'a, FnDef<'a>> { | 51 | fn functions(&self) -> AstChildren<FnDef> { |
53 | children(self) | 52 | children(self) |
54 | } | 53 | } |
55 | } | 54 | } |
56 | 55 | ||
57 | // ModuleItem | 56 | pub trait ModuleItemOwner: AstNode { |
58 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 57 | fn items(&self) -> AstChildren<ModuleItem> { |
59 | pub enum ItemOrMacro<'a> { | ||
60 | Item(ModuleItem<'a>), | ||
61 | Macro(MacroCall<'a>), | ||
62 | } | ||
63 | |||
64 | impl<'a> AstNode<'a> for ItemOrMacro<'a> { | ||
65 | fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { | ||
66 | let res = if let Some(item) = ModuleItem::cast(syntax) { | ||
67 | ItemOrMacro::Item(item) | ||
68 | } else if let Some(macro_call) = MacroCall::cast(syntax) { | ||
69 | ItemOrMacro::Macro(macro_call) | ||
70 | } else { | ||
71 | return None; | ||
72 | }; | ||
73 | Some(res) | ||
74 | } | ||
75 | fn syntax(self) -> SyntaxNodeRef<'a> { | ||
76 | match self { | ||
77 | ItemOrMacro::Item(it) => it.syntax(), | ||
78 | ItemOrMacro::Macro(it) => it.syntax(), | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | pub trait ModuleItemOwner<'a>: AstNode<'a> { | ||
84 | fn items(self) -> AstChildren<'a, ModuleItem<'a>> { | ||
85 | children(self) | ||
86 | } | ||
87 | |||
88 | fn items_with_macros(self) -> AstChildren<'a, ItemOrMacro<'a>> { | ||
89 | children(self) | 58 | children(self) |
90 | } | 59 | } |
91 | } | 60 | } |
92 | 61 | ||
93 | pub trait TypeParamsOwner<'a>: AstNode<'a> { | 62 | pub trait TypeParamsOwner: AstNode { |
94 | fn type_param_list(self) -> Option<TypeParamList<'a>> { | 63 | fn type_param_list(&self) -> Option<&TypeParamList> { |
95 | child_opt(self) | 64 | child_opt(self) |
96 | } | 65 | } |
97 | 66 | ||
98 | fn where_clause(self) -> Option<WhereClause<'a>> { | 67 | fn where_clause(&self) -> Option<&WhereClause> { |
99 | child_opt(self) | 68 | child_opt(self) |
100 | } | 69 | } |
101 | } | 70 | } |
102 | 71 | ||
103 | pub trait AttrsOwner<'a>: AstNode<'a> { | 72 | pub trait AttrsOwner: AstNode { |
104 | fn attrs(self) -> AstChildren<'a, Attr<'a>> { | 73 | fn attrs(&self) -> AstChildren<Attr> { |
105 | children(self) | 74 | children(self) |
106 | } | 75 | } |
107 | } | 76 | } |
108 | 77 | ||
109 | pub trait DocCommentsOwner<'a>: AstNode<'a> { | 78 | pub trait DocCommentsOwner: AstNode { |
110 | fn doc_comments(self) -> AstChildren<'a, Comment<'a>> { | 79 | fn doc_comments(&self) -> AstChildren<Comment> { |
111 | children(self) | 80 | children(self) |
112 | } | 81 | } |
113 | 82 | ||
114 | /// Returns the textual content of a doc comment block as a single string. | 83 | /// Returns the textual content of a doc comment block as a single string. |
115 | /// That is, strips leading `///` and joins lines | 84 | /// That is, strips leading `///` and joins lines |
116 | fn doc_comment_text(self) -> RustString { | 85 | fn doc_comment_text(&self) -> std::string::String { |
117 | self.doc_comments() | 86 | self.doc_comments() |
118 | .filter(|comment| comment.is_doc_comment()) | 87 | .filter(|comment| comment.is_doc_comment()) |
119 | .map(|comment| { | 88 | .map(|comment| { |
@@ -130,13 +99,13 @@ pub trait DocCommentsOwner<'a>: AstNode<'a> { | |||
130 | } | 99 | } |
131 | } | 100 | } |
132 | 101 | ||
133 | impl<'a> FnDef<'a> { | 102 | impl FnDef { |
134 | pub fn has_atom_attr(&self, atom: &str) -> bool { | 103 | pub fn has_atom_attr(&self, atom: &str) -> bool { |
135 | self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) | 104 | self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) |
136 | } | 105 | } |
137 | } | 106 | } |
138 | 107 | ||
139 | impl<'a> Attr<'a> { | 108 | impl Attr { |
140 | pub fn as_atom(&self) -> Option<SmolStr> { | 109 | pub fn as_atom(&self) -> Option<SmolStr> { |
141 | let tt = self.value()?; | 110 | let tt = self.value()?; |
142 | let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; | 111 | let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; |
@@ -147,7 +116,7 @@ impl<'a> Attr<'a> { | |||
147 | } | 116 | } |
148 | } | 117 | } |
149 | 118 | ||
150 | pub fn as_call(&self) -> Option<(SmolStr, TokenTree<'a>)> { | 119 | pub fn as_call(&self) -> Option<(SmolStr, &TokenTree)> { |
151 | let tt = self.value()?; | 120 | let tt = self.value()?; |
152 | let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; | 121 | let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; |
153 | let args = TokenTree::cast(args)?; | 122 | let args = TokenTree::cast(args)?; |
@@ -159,37 +128,37 @@ impl<'a> Attr<'a> { | |||
159 | } | 128 | } |
160 | } | 129 | } |
161 | 130 | ||
162 | impl<'a> Lifetime<'a> { | 131 | impl Lifetime { |
163 | pub fn text(&self) -> SmolStr { | 132 | pub fn text(&self) -> SmolStr { |
164 | self.syntax().leaf_text().unwrap().clone() | 133 | self.syntax().leaf_text().unwrap().clone() |
165 | } | 134 | } |
166 | } | 135 | } |
167 | 136 | ||
168 | impl<'a> Char<'a> { | 137 | impl Char { |
169 | pub fn text(&self) -> &SmolStr { | 138 | pub fn text(&self) -> &SmolStr { |
170 | &self.syntax().leaf_text().unwrap() | 139 | &self.syntax().leaf_text().unwrap() |
171 | } | 140 | } |
172 | } | 141 | } |
173 | 142 | ||
174 | impl<'a> Byte<'a> { | 143 | impl Byte { |
175 | pub fn text(&self) -> &SmolStr { | 144 | pub fn text(&self) -> &SmolStr { |
176 | &self.syntax().leaf_text().unwrap() | 145 | &self.syntax().leaf_text().unwrap() |
177 | } | 146 | } |
178 | } | 147 | } |
179 | 148 | ||
180 | impl<'a> ByteString<'a> { | 149 | impl ByteString { |
181 | pub fn text(&self) -> &SmolStr { | 150 | pub fn text(&self) -> &SmolStr { |
182 | &self.syntax().leaf_text().unwrap() | 151 | &self.syntax().leaf_text().unwrap() |
183 | } | 152 | } |
184 | } | 153 | } |
185 | 154 | ||
186 | impl<'a> String<'a> { | 155 | impl String { |
187 | pub fn text(&self) -> &SmolStr { | 156 | pub fn text(&self) -> &SmolStr { |
188 | &self.syntax().leaf_text().unwrap() | 157 | &self.syntax().leaf_text().unwrap() |
189 | } | 158 | } |
190 | } | 159 | } |
191 | 160 | ||
192 | impl<'a> Comment<'a> { | 161 | impl Comment { |
193 | pub fn text(&self) -> &SmolStr { | 162 | pub fn text(&self) -> &SmolStr { |
194 | self.syntax().leaf_text().unwrap() | 163 | self.syntax().leaf_text().unwrap() |
195 | } | 164 | } |
@@ -251,7 +220,7 @@ impl CommentFlavor { | |||
251 | } | 220 | } |
252 | } | 221 | } |
253 | 222 | ||
254 | impl<'a> Whitespace<'a> { | 223 | impl Whitespace { |
255 | pub fn text(&self) -> &SmolStr { | 224 | pub fn text(&self) -> &SmolStr { |
256 | &self.syntax().leaf_text().unwrap() | 225 | &self.syntax().leaf_text().unwrap() |
257 | } | 226 | } |
@@ -265,36 +234,36 @@ impl<'a> Whitespace<'a> { | |||
265 | } | 234 | } |
266 | } | 235 | } |
267 | 236 | ||
268 | impl<'a> Name<'a> { | 237 | impl Name { |
269 | pub fn text(&self) -> SmolStr { | 238 | pub fn text(&self) -> SmolStr { |
270 | let ident = self.syntax().first_child().unwrap(); | 239 | let ident = self.syntax().first_child().unwrap(); |
271 | ident.leaf_text().unwrap().clone() | 240 | ident.leaf_text().unwrap().clone() |
272 | } | 241 | } |
273 | } | 242 | } |
274 | 243 | ||
275 | impl<'a> NameRef<'a> { | 244 | impl NameRef { |
276 | pub fn text(&self) -> SmolStr { | 245 | pub fn text(&self) -> SmolStr { |
277 | let ident = self.syntax().first_child().unwrap(); | 246 | let ident = self.syntax().first_child().unwrap(); |
278 | ident.leaf_text().unwrap().clone() | 247 | ident.leaf_text().unwrap().clone() |
279 | } | 248 | } |
280 | } | 249 | } |
281 | 250 | ||
282 | impl<'a> ImplBlock<'a> { | 251 | impl ImplBlock { |
283 | pub fn target_type(self) -> Option<TypeRef<'a>> { | 252 | pub fn target_type(&self) -> Option<&TypeRef> { |
284 | match self.target() { | 253 | match self.target() { |
285 | (Some(t), None) | (_, Some(t)) => Some(t), | 254 | (Some(t), None) | (_, Some(t)) => Some(t), |
286 | _ => None, | 255 | _ => None, |
287 | } | 256 | } |
288 | } | 257 | } |
289 | 258 | ||
290 | pub fn target_trait(self) -> Option<TypeRef<'a>> { | 259 | pub fn target_trait(&self) -> Option<&TypeRef> { |
291 | match self.target() { | 260 | match self.target() { |
292 | (Some(t), Some(_)) => Some(t), | 261 | (Some(t), Some(_)) => Some(t), |
293 | _ => None, | 262 | _ => None, |
294 | } | 263 | } |
295 | } | 264 | } |
296 | 265 | ||
297 | fn target(self) -> (Option<TypeRef<'a>>, Option<TypeRef<'a>>) { | 266 | fn target(&self) -> (Option<&TypeRef>, Option<&TypeRef>) { |
298 | let mut types = children(self); | 267 | let mut types = children(self); |
299 | let first = types.next(); | 268 | let first = types.next(); |
300 | let second = types.next(); | 269 | let second = types.next(); |
@@ -302,8 +271,8 @@ impl<'a> ImplBlock<'a> { | |||
302 | } | 271 | } |
303 | } | 272 | } |
304 | 273 | ||
305 | impl<'a> Module<'a> { | 274 | impl Module { |
306 | pub fn has_semi(self) -> bool { | 275 | pub fn has_semi(&self) -> bool { |
307 | match self.syntax().last_child() { | 276 | match self.syntax().last_child() { |
308 | None => false, | 277 | None => false, |
309 | Some(node) => node.kind() == SEMI, | 278 | Some(node) => node.kind() == SEMI, |
@@ -311,8 +280,8 @@ impl<'a> Module<'a> { | |||
311 | } | 280 | } |
312 | } | 281 | } |
313 | 282 | ||
314 | impl<'a> LetStmt<'a> { | 283 | impl LetStmt { |
315 | pub fn has_semi(self) -> bool { | 284 | pub fn has_semi(&self) -> bool { |
316 | match self.syntax().last_child() { | 285 | match self.syntax().last_child() { |
317 | None => false, | 286 | None => false, |
318 | Some(node) => node.kind() == SEMI, | 287 | Some(node) => node.kind() == SEMI, |
@@ -320,35 +289,35 @@ impl<'a> LetStmt<'a> { | |||
320 | } | 289 | } |
321 | } | 290 | } |
322 | 291 | ||
323 | impl<'a> IfExpr<'a> { | 292 | impl IfExpr { |
324 | pub fn then_branch(self) -> Option<Block<'a>> { | 293 | pub fn then_branch(&self) -> Option<&Block> { |
325 | self.blocks().nth(0) | 294 | self.blocks().nth(0) |
326 | } | 295 | } |
327 | pub fn else_branch(self) -> Option<Block<'a>> { | 296 | pub fn else_branch(&self) -> Option<&Block> { |
328 | self.blocks().nth(1) | 297 | self.blocks().nth(1) |
329 | } | 298 | } |
330 | fn blocks(self) -> AstChildren<'a, Block<'a>> { | 299 | fn blocks(&self) -> AstChildren<Block> { |
331 | children(self) | 300 | children(self) |
332 | } | 301 | } |
333 | } | 302 | } |
334 | 303 | ||
335 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 304 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
336 | pub enum PathSegmentKind<'a> { | 305 | pub enum PathSegmentKind<'a> { |
337 | Name(NameRef<'a>), | 306 | Name(&'a NameRef), |
338 | SelfKw, | 307 | SelfKw, |
339 | SuperKw, | 308 | SuperKw, |
340 | CrateKw, | 309 | CrateKw, |
341 | } | 310 | } |
342 | 311 | ||
343 | impl<'a> PathSegment<'a> { | 312 | impl PathSegment { |
344 | pub fn parent_path(self) -> Path<'a> { | 313 | pub fn parent_path(&self) -> &Path { |
345 | self.syntax() | 314 | self.syntax() |
346 | .parent() | 315 | .parent() |
347 | .and_then(Path::cast) | 316 | .and_then(Path::cast) |
348 | .expect("segments are always nested in paths") | 317 | .expect("segments are always nested in paths") |
349 | } | 318 | } |
350 | 319 | ||
351 | pub fn kind(self) -> Option<PathSegmentKind<'a>> { | 320 | pub fn kind(&self) -> Option<PathSegmentKind> { |
352 | let res = if let Some(name_ref) = self.name_ref() { | 321 | let res = if let Some(name_ref) = self.name_ref() { |
353 | PathSegmentKind::Name(name_ref) | 322 | PathSegmentKind::Name(name_ref) |
354 | } else { | 323 | } else { |
@@ -363,20 +332,20 @@ impl<'a> PathSegment<'a> { | |||
363 | } | 332 | } |
364 | } | 333 | } |
365 | 334 | ||
366 | impl<'a> Path<'a> { | 335 | impl Path { |
367 | pub fn parent_path(self) -> Option<Path<'a>> { | 336 | pub fn parent_path(&self) -> Option<&Path> { |
368 | self.syntax().parent().and_then(Path::cast) | 337 | self.syntax().parent().and_then(Path::cast) |
369 | } | 338 | } |
370 | } | 339 | } |
371 | 340 | ||
372 | impl<'a> UseTree<'a> { | 341 | impl UseTree { |
373 | pub fn has_star(self) -> bool { | 342 | pub fn has_star(&self) -> bool { |
374 | self.syntax().children().any(|it| it.kind() == STAR) | 343 | self.syntax().children().any(|it| it.kind() == STAR) |
375 | } | 344 | } |
376 | } | 345 | } |
377 | 346 | ||
378 | impl<'a> UseTreeList<'a> { | 347 | impl UseTreeList { |
379 | pub fn parent_use_tree(self) -> UseTree<'a> { | 348 | pub fn parent_use_tree(&self) -> &UseTree { |
380 | self.syntax() | 349 | self.syntax() |
381 | .parent() | 350 | .parent() |
382 | .and_then(UseTree::cast) | 351 | .and_then(UseTree::cast) |
@@ -384,22 +353,22 @@ impl<'a> UseTreeList<'a> { | |||
384 | } | 353 | } |
385 | } | 354 | } |
386 | 355 | ||
387 | fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> { | 356 | fn child_opt<P: AstNode, C: AstNode>(parent: &P) -> Option<&C> { |
388 | children(parent).next() | 357 | children(parent).next() |
389 | } | 358 | } |
390 | 359 | ||
391 | fn children<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> AstChildren<'a, C> { | 360 | fn children<P: AstNode, C: AstNode>(parent: &P) -> AstChildren<C> { |
392 | AstChildren::new(parent.syntax()) | 361 | AstChildren::new(parent.syntax()) |
393 | } | 362 | } |
394 | 363 | ||
395 | #[derive(Debug)] | 364 | #[derive(Debug)] |
396 | pub struct AstChildren<'a, N> { | 365 | pub struct AstChildren<'a, N> { |
397 | inner: SyntaxNodeChildren<RefRoot<'a>>, | 366 | inner: SyntaxNodeChildren<'a>, |
398 | ph: PhantomData<N>, | 367 | ph: PhantomData<N>, |
399 | } | 368 | } |
400 | 369 | ||
401 | impl<'a, N> AstChildren<'a, N> { | 370 | impl<'a, N> AstChildren<'a, N> { |
402 | fn new(parent: SyntaxNodeRef<'a>) -> Self { | 371 | fn new(parent: &'a SyntaxNode) -> Self { |
403 | AstChildren { | 372 | AstChildren { |
404 | inner: parent.children(), | 373 | inner: parent.children(), |
405 | ph: PhantomData, | 374 | ph: PhantomData, |
@@ -407,9 +376,9 @@ impl<'a, N> AstChildren<'a, N> { | |||
407 | } | 376 | } |
408 | } | 377 | } |
409 | 378 | ||
410 | impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> { | 379 | impl<'a, N: AstNode + 'a> Iterator for AstChildren<'a, N> { |
411 | type Item = N; | 380 | type Item = &'a N; |
412 | fn next(&mut self) -> Option<N> { | 381 | fn next(&mut self) -> Option<&'a N> { |
413 | loop { | 382 | loop { |
414 | if let Some(n) = N::cast(self.inner.next()?) { | 383 | if let Some(n) = N::cast(self.inner.next()?) { |
415 | return Some(n); | 384 | return Some(n); |
@@ -420,13 +389,13 @@ impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> { | |||
420 | 389 | ||
421 | #[derive(Debug, Clone, PartialEq, Eq)] | 390 | #[derive(Debug, Clone, PartialEq, Eq)] |
422 | pub enum StructFlavor<'a> { | 391 | pub enum StructFlavor<'a> { |
423 | Tuple(PosFieldList<'a>), | 392 | Tuple(&'a PosFieldList), |
424 | Named(NamedFieldDefList<'a>), | 393 | Named(&'a NamedFieldDefList), |
425 | Unit, | 394 | Unit, |
426 | } | 395 | } |
427 | 396 | ||
428 | impl<'a> StructFlavor<'a> { | 397 | impl StructFlavor<'_> { |
429 | fn from_node<N: AstNode<'a>>(node: N) -> StructFlavor<'a> { | 398 | fn from_node<N: AstNode>(node: &N) -> StructFlavor { |
430 | if let Some(nfdl) = child_opt::<_, NamedFieldDefList>(node) { | 399 | if let Some(nfdl) = child_opt::<_, NamedFieldDefList>(node) { |
431 | StructFlavor::Named(nfdl) | 400 | StructFlavor::Named(nfdl) |
432 | } else if let Some(pfl) = child_opt::<_, PosFieldList>(node) { | 401 | } else if let Some(pfl) = child_opt::<_, PosFieldList>(node) { |
@@ -437,31 +406,31 @@ impl<'a> StructFlavor<'a> { | |||
437 | } | 406 | } |
438 | } | 407 | } |
439 | 408 | ||
440 | impl<'a> StructDef<'a> { | 409 | impl StructDef { |
441 | pub fn flavor(self) -> StructFlavor<'a> { | 410 | pub fn flavor(&self) -> StructFlavor { |
442 | StructFlavor::from_node(self) | 411 | StructFlavor::from_node(self) |
443 | } | 412 | } |
444 | } | 413 | } |
445 | 414 | ||
446 | impl<'a> EnumVariant<'a> { | 415 | impl EnumVariant { |
447 | pub fn flavor(self) -> StructFlavor<'a> { | 416 | pub fn flavor(&self) -> StructFlavor { |
448 | StructFlavor::from_node(self) | 417 | StructFlavor::from_node(self) |
449 | } | 418 | } |
450 | } | 419 | } |
451 | 420 | ||
452 | impl<'a> PointerType<'a> { | 421 | impl PointerType { |
453 | pub fn is_mut(&self) -> bool { | 422 | pub fn is_mut(&self) -> bool { |
454 | self.syntax().children().any(|n| n.kind() == MUT_KW) | 423 | self.syntax().children().any(|n| n.kind() == MUT_KW) |
455 | } | 424 | } |
456 | } | 425 | } |
457 | 426 | ||
458 | impl<'a> ReferenceType<'a> { | 427 | impl ReferenceType { |
459 | pub fn is_mut(&self) -> bool { | 428 | pub fn is_mut(&self) -> bool { |
460 | self.syntax().children().any(|n| n.kind() == MUT_KW) | 429 | self.syntax().children().any(|n| n.kind() == MUT_KW) |
461 | } | 430 | } |
462 | } | 431 | } |
463 | 432 | ||
464 | impl<'a> RefExpr<'a> { | 433 | impl RefExpr { |
465 | pub fn is_mut(&self) -> bool { | 434 | pub fn is_mut(&self) -> bool { |
466 | self.syntax().children().any(|n| n.kind() == MUT_KW) | 435 | self.syntax().children().any(|n| n.kind() == MUT_KW) |
467 | } | 436 | } |
@@ -477,7 +446,7 @@ pub enum PrefixOp { | |||
477 | Neg, | 446 | Neg, |
478 | } | 447 | } |
479 | 448 | ||
480 | impl<'a> PrefixExpr<'a> { | 449 | impl PrefixExpr { |
481 | pub fn op(&self) -> Option<PrefixOp> { | 450 | pub fn op(&self) -> Option<PrefixOp> { |
482 | match self.syntax().first_child()?.kind() { | 451 | match self.syntax().first_child()?.kind() { |
483 | STAR => Some(PrefixOp::Deref), | 452 | STAR => Some(PrefixOp::Deref), |
@@ -552,7 +521,7 @@ pub enum BinOp { | |||
552 | BitXorAssign, | 521 | BitXorAssign, |
553 | } | 522 | } |
554 | 523 | ||
555 | impl<'a> BinExpr<'a> { | 524 | impl BinExpr { |
556 | pub fn op(&self) -> Option<BinOp> { | 525 | pub fn op(&self) -> Option<BinOp> { |
557 | self.syntax() | 526 | self.syntax() |
558 | .children() | 527 | .children() |
@@ -592,15 +561,15 @@ impl<'a> BinExpr<'a> { | |||
592 | .next() | 561 | .next() |
593 | } | 562 | } |
594 | 563 | ||
595 | pub fn lhs(self) -> Option<Expr<'a>> { | 564 | pub fn lhs(&self) -> Option<&Expr> { |
596 | children(self).nth(0) | 565 | children(self).nth(0) |
597 | } | 566 | } |
598 | 567 | ||
599 | pub fn rhs(self) -> Option<Expr<'a>> { | 568 | pub fn rhs(&self) -> Option<&Expr> { |
600 | children(self).nth(1) | 569 | children(self).nth(1) |
601 | } | 570 | } |
602 | 571 | ||
603 | pub fn sub_exprs(self) -> (Option<Expr<'a>>, Option<Expr<'a>>) { | 572 | pub fn sub_exprs(&self) -> (Option<&Expr>, Option<&Expr>) { |
604 | let mut children = children(self); | 573 | let mut children = children(self); |
605 | let first = children.next(); | 574 | let first = children.next(); |
606 | let second = children.next(); | 575 | let second = children.next(); |
@@ -618,7 +587,7 @@ pub enum SelfParamFlavor { | |||
618 | MutRef, | 587 | MutRef, |
619 | } | 588 | } |
620 | 589 | ||
621 | impl<'a> SelfParam<'a> { | 590 | impl SelfParam { |
622 | pub fn flavor(&self) -> SelfParamFlavor { | 591 | pub fn flavor(&self) -> SelfParamFlavor { |
623 | let borrowed = self.syntax().children().any(|n| n.kind() == AMP); | 592 | let borrowed = self.syntax().children().any(|n| n.kind() == AMP); |
624 | if borrowed { | 593 | if borrowed { |
@@ -641,7 +610,7 @@ impl<'a> SelfParam<'a> { | |||
641 | 610 | ||
642 | #[test] | 611 | #[test] |
643 | fn test_doc_comment_of_items() { | 612 | fn test_doc_comment_of_items() { |
644 | let file = SourceFileNode::parse( | 613 | let file = SourceFile::parse( |
645 | r#" | 614 | r#" |
646 | //! doc | 615 | //! doc |
647 | // non-doc | 616 | // non-doc |
diff --git a/crates/ra_syntax/src/ast/generated.rs.tera b/crates/ra_syntax/src/ast/generated.rs.tera index 131ee09ec..0a20fc78e 100644 --- a/crates/ra_syntax/src/ast/generated.rs.tera +++ b/crates/ra_syntax/src/ast/generated.rs.tera | |||
@@ -11,89 +11,92 @@ the below applies to the result of this template | |||
11 | 11 | ||
12 | #![cfg_attr(rustfmt, rustfmt_skip)] | 12 | #![cfg_attr(rustfmt, rustfmt_skip)] |
13 | 13 | ||
14 | use std::hash::{Hash, Hasher}; | 14 | use rowan::TransparentNewType; |
15 | 15 | ||
16 | use crate::{ | 16 | use crate::{ |
17 | ast, | 17 | SyntaxNode, SyntaxKind::*, |
18 | SyntaxNode, SyntaxNodeRef, AstNode, | 18 | yellow::{RaTypes, TreePtr}, |
19 | yellow::{TreeRoot, RaTypes, OwnedRoot, RefRoot}, | 19 | ast::{self, AstNode}, |
20 | SyntaxKind::*, | ||
21 | }; | 20 | }; |
22 | {% for node, methods in ast %} | 21 | {% for node, methods in ast %} |
23 | // {{ node }} | 22 | // {{ node }} |
24 | 23 | ||
25 | {%- if methods.enum %} | 24 | {%- if methods.enum %} |
25 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
26 | #[repr(transparent)] | ||
27 | pub struct {{ node }} { | ||
28 | pub(crate) syntax: SyntaxNode, | ||
29 | } | ||
30 | unsafe impl TransparentNewType for {{ node }} { | ||
31 | type Repr = rowan::SyntaxNode<RaTypes>; | ||
32 | } | ||
33 | |||
26 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 34 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
27 | pub enum {{ node }}<'a> { | 35 | pub enum {{ node }}Kind<'a> { |
28 | {%- for kind in methods.enum %} | 36 | {%- for kind in methods.enum %} |
29 | {{ kind }}({{ kind }}<'a>), | 37 | {{ kind }}(&'a {{ kind }}), |
30 | {%- endfor %} | 38 | {%- endfor %} |
31 | } | 39 | } |
32 | 40 | ||
33 | impl<'a> AstNode<'a> for {{ node }}<'a> { | 41 | impl AstNode for {{ node }} { |
34 | fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { | 42 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { |
35 | match syntax.kind() { | 43 | match syntax.kind() { |
36 | {%- for kind in methods.enum %} | 44 | {%- for kind in methods.enum %} |
37 | {{ kind | SCREAM }} => Some({{ node }}::{{ kind }}({{ kind }} { syntax })), | 45 | | {{ kind | SCREAM }} |
38 | {%- endfor %} | 46 | {%- endfor %} => Some({{ node }}::from_repr(syntax.into_repr())), |
39 | _ => None, | 47 | _ => None, |
40 | } | 48 | } |
41 | } | 49 | } |
42 | fn syntax(self) -> SyntaxNodeRef<'a> { | 50 | fn syntax(&self) -> &SyntaxNode { &self.syntax } |
43 | match self { | 51 | fn to_owned(&self) -> TreePtr<{{ node }}> { TreePtr::cast(self.syntax.to_owned()) } |
44 | {%- for kind in methods.enum %} | 52 | } |
45 | {{ node }}::{{ kind }}(inner) => inner.syntax(), | 53 | |
46 | {%- endfor %} | 54 | impl {{ node }} { |
55 | pub fn kind(&self) -> {{ node }}Kind { | ||
56 | match self.syntax.kind() { | ||
57 | {%- for kind in methods.enum %} | ||
58 | {{ kind | SCREAM }} => {{ node }}Kind::{{ kind }}({{ kind }}::cast(&self.syntax).unwrap()), | ||
59 | {%- endfor %} | ||
60 | _ => unreachable!(), | ||
47 | } | 61 | } |
48 | } | 62 | } |
49 | } | 63 | } |
50 | {% else %} | 64 | {% else %} |
51 | #[derive(Debug, Clone, Copy,)] | 65 | #[derive(Debug, PartialEq, Eq, Hash)] |
52 | pub struct {{ node }}Node<R: TreeRoot<RaTypes> = OwnedRoot> { | 66 | #[repr(transparent)] |
53 | pub(crate) syntax: SyntaxNode<R>, | 67 | pub struct {{ node }} { |
68 | pub(crate) syntax: SyntaxNode, | ||
54 | } | 69 | } |
55 | pub type {{ node }}<'a> = {{ node }}Node<RefRoot<'a>>; | 70 | unsafe impl TransparentNewType for {{ node }} { |
56 | 71 | type Repr = rowan::SyntaxNode<RaTypes>; | |
57 | impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<{{node}}Node<R1>> for {{node}}Node<R2> { | ||
58 | fn eq(&self, other: &{{node}}Node<R1>) -> bool { self.syntax == other.syntax } | ||
59 | } | ||
60 | impl<R: TreeRoot<RaTypes>> Eq for {{node}}Node<R> {} | ||
61 | impl<R: TreeRoot<RaTypes>> Hash for {{node}}Node<R> { | ||
62 | fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) } | ||
63 | } | 72 | } |
64 | 73 | ||
65 | impl<'a> AstNode<'a> for {{ node }}<'a> { | 74 | impl AstNode for {{ node }} { |
66 | fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { | 75 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { |
67 | match syntax.kind() { | 76 | match syntax.kind() { |
68 | {{ node | SCREAM }} => Some({{ node }} { syntax }), | 77 | {{ node | SCREAM }} => Some({{ node }}::from_repr(syntax.into_repr())), |
69 | _ => None, | 78 | _ => None, |
70 | } | 79 | } |
71 | } | 80 | } |
72 | fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } | 81 | fn syntax(&self) -> &SyntaxNode { &self.syntax } |
73 | } | 82 | fn to_owned(&self) -> TreePtr<{{ node }}> { TreePtr::cast(self.syntax.to_owned()) } |
74 | |||
75 | impl<R: TreeRoot<RaTypes>> {{ node }}Node<R> { | ||
76 | pub fn borrowed(&self) -> {{ node }} { | ||
77 | {{ node }}Node { syntax: self.syntax.borrowed() } | ||
78 | } | ||
79 | pub fn owned(&self) -> {{ node }}Node { | ||
80 | {{ node }}Node { syntax: self.syntax.owned() } | ||
81 | } | ||
82 | } | 83 | } |
83 | 84 | ||
84 | {% endif %} | 85 | {% endif %} |
85 | {% if methods.traits -%} | 86 | {% if methods.traits -%} |
87 | |||
86 | {%- for t in methods.traits -%} | 88 | {%- for t in methods.traits -%} |
87 | impl<'a> ast::{{ t }}<'a> for {{ node }}<'a> {} | 89 | impl ast::{{ t }} for {{ node }} {} |
88 | {% endfor -%} | 90 | {% endfor -%} |
91 | |||
89 | {%- endif -%} | 92 | {%- endif -%} |
90 | 93 | ||
91 | impl<'a> {{ node }}<'a> { | 94 | impl {{ node }} { |
92 | {%- if methods.collections -%} | 95 | {%- if methods.collections -%} |
93 | {%- for m in methods.collections -%} | 96 | {%- for m in methods.collections -%} |
94 | {%- set method_name = m.0 -%} | 97 | {%- set method_name = m.0 -%} |
95 | {%- set ChildName = m.1 %} | 98 | {%- set ChildName = m.1 %} |
96 | pub fn {{ method_name }}(self) -> impl Iterator<Item = {{ ChildName }}<'a>> + 'a { | 99 | pub fn {{ method_name }}(&self) -> impl Iterator<Item = &{{ ChildName }}> { |
97 | super::children(self) | 100 | super::children(self) |
98 | } | 101 | } |
99 | {% endfor -%} | 102 | {% endfor -%} |
@@ -109,7 +112,7 @@ impl<'a> {{ node }}<'a> { | |||
109 | {%- set method_name = m.0 -%} | 112 | {%- set method_name = m.0 -%} |
110 | {%- set ChildName = m.1 %} | 113 | {%- set ChildName = m.1 %} |
111 | {%- endif %} | 114 | {%- endif %} |
112 | pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> { | 115 | pub fn {{ method_name }}(&self) -> Option<&{{ ChildName }}> { |
113 | super::child_opt(self) | 116 | super::child_opt(self) |
114 | } | 117 | } |
115 | {% endfor -%} | 118 | {% endfor -%} |
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 6753c513f..a75e641ea 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs | |||
@@ -42,52 +42,42 @@ pub use crate::{ | |||
42 | ast::AstNode, | 42 | ast::AstNode, |
43 | lexer::{tokenize, Token}, | 43 | lexer::{tokenize, Token}, |
44 | syntax_kinds::SyntaxKind, | 44 | syntax_kinds::SyntaxKind, |
45 | yellow::{ | 45 | yellow::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreePtr}, |
46 | Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent, Location, | ||
47 | }, | ||
48 | }; | 46 | }; |
49 | 47 | ||
50 | use ra_text_edit::AtomTextEdit; | 48 | use ra_text_edit::AtomTextEdit; |
51 | use crate::yellow::GreenNode; | 49 | use crate::yellow::GreenNode; |
52 | 50 | ||
53 | /// `SourceFileNode` represents a parse tree for a single Rust file. | 51 | /// `SourceFile` represents a parse tree for a single Rust file. |
54 | pub use crate::ast::{SourceFile, SourceFileNode}; | 52 | pub use crate::ast::SourceFile; |
55 | 53 | ||
56 | impl SourceFileNode { | 54 | impl SourceFile { |
57 | fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SourceFileNode { | 55 | fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreePtr<SourceFile> { |
58 | let root = SyntaxNode::new(green, errors); | 56 | let root = SyntaxNode::new(green, errors); |
59 | if cfg!(debug_assertions) { | 57 | if cfg!(debug_assertions) { |
60 | utils::validate_block_structure(root.borrowed()); | 58 | utils::validate_block_structure(&root); |
61 | } | 59 | } |
62 | assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); | 60 | assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); |
63 | ast::SourceFileNode { syntax: root } | 61 | TreePtr::cast(root) |
64 | } | 62 | } |
65 | pub fn parse(text: &str) -> SourceFileNode { | 63 | pub fn parse(text: &str) -> TreePtr<SourceFile> { |
66 | let tokens = tokenize(&text); | 64 | let tokens = tokenize(&text); |
67 | let (green, errors) = | 65 | let (green, errors) = |
68 | parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root); | 66 | parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root); |
69 | SourceFileNode::new(green, errors) | 67 | SourceFile::new(green, errors) |
70 | } | 68 | } |
71 | pub fn reparse(&self, edit: &AtomTextEdit) -> SourceFileNode { | 69 | pub fn reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> { |
72 | self.incremental_reparse(edit) | 70 | self.incremental_reparse(edit) |
73 | .unwrap_or_else(|| self.full_reparse(edit)) | 71 | .unwrap_or_else(|| self.full_reparse(edit)) |
74 | } | 72 | } |
75 | pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<SourceFileNode> { | 73 | pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<TreePtr<SourceFile>> { |
76 | reparsing::incremental_reparse(self.syntax(), edit, self.errors()) | 74 | reparsing::incremental_reparse(self.syntax(), edit, self.errors()) |
77 | .map(|(green_node, errors)| SourceFileNode::new(green_node, errors)) | 75 | .map(|(green_node, errors)| SourceFile::new(green_node, errors)) |
78 | } | 76 | } |
79 | fn full_reparse(&self, edit: &AtomTextEdit) -> SourceFileNode { | 77 | fn full_reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> { |
80 | let text = | 78 | let text = |
81 | text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); | 79 | text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); |
82 | SourceFileNode::parse(&text) | 80 | SourceFile::parse(&text) |
83 | } | ||
84 | /// Typed AST representation of the parse tree. | ||
85 | pub fn ast(&self) -> ast::SourceFile { | ||
86 | self.borrowed() | ||
87 | } | ||
88 | /// Untyped homogeneous representation of the parse tree. | ||
89 | pub fn syntax(&self) -> SyntaxNodeRef { | ||
90 | self.syntax.borrowed() | ||
91 | } | 81 | } |
92 | pub fn errors(&self) -> Vec<SyntaxError> { | 82 | pub fn errors(&self) -> Vec<SyntaxError> { |
93 | let mut errors = self.syntax.root_data().clone(); | 83 | let mut errors = self.syntax.root_data().clone(); |
diff --git a/crates/ra_syntax/src/reparsing.rs b/crates/ra_syntax/src/reparsing.rs index 7ee71a1b6..d5d72e1f8 100644 --- a/crates/ra_syntax/src/reparsing.rs +++ b/crates/ra_syntax/src/reparsing.rs | |||
@@ -4,12 +4,12 @@ use crate::lexer::{tokenize, Token}; | |||
4 | use crate::parser_api::Parser; | 4 | use crate::parser_api::Parser; |
5 | use crate::parser_impl; | 5 | use crate::parser_impl; |
6 | use crate::text_utils::replace_range; | 6 | use crate::text_utils::replace_range; |
7 | use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef}; | 7 | use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNode}; |
8 | use crate::{SyntaxKind::*, TextRange, TextUnit}; | 8 | use crate::{SyntaxKind::*, TextRange, TextUnit}; |
9 | use ra_text_edit::AtomTextEdit; | 9 | use ra_text_edit::AtomTextEdit; |
10 | 10 | ||
11 | pub(crate) fn incremental_reparse( | 11 | pub(crate) fn incremental_reparse( |
12 | node: SyntaxNodeRef, | 12 | node: &SyntaxNode, |
13 | edit: &AtomTextEdit, | 13 | edit: &AtomTextEdit, |
14 | errors: Vec<SyntaxError>, | 14 | errors: Vec<SyntaxError>, |
15 | ) -> Option<(GreenNode, Vec<SyntaxError>)> { | 15 | ) -> Option<(GreenNode, Vec<SyntaxError>)> { |
@@ -21,9 +21,9 @@ pub(crate) fn incremental_reparse( | |||
21 | } | 21 | } |
22 | 22 | ||
23 | fn reparse_leaf<'node>( | 23 | fn reparse_leaf<'node>( |
24 | node: SyntaxNodeRef<'node>, | 24 | node: &'node SyntaxNode, |
25 | edit: &AtomTextEdit, | 25 | edit: &AtomTextEdit, |
26 | ) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> { | 26 | ) -> Option<(&'node SyntaxNode, GreenNode, Vec<SyntaxError>)> { |
27 | let node = algo::find_covering_node(node, edit.delete); | 27 | let node = algo::find_covering_node(node, edit.delete); |
28 | match node.kind() { | 28 | match node.kind() { |
29 | WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => { | 29 | WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => { |
@@ -47,9 +47,9 @@ fn reparse_leaf<'node>( | |||
47 | } | 47 | } |
48 | 48 | ||
49 | fn reparse_block<'node>( | 49 | fn reparse_block<'node>( |
50 | node: SyntaxNodeRef<'node>, | 50 | node: &'node SyntaxNode, |
51 | edit: &AtomTextEdit, | 51 | edit: &AtomTextEdit, |
52 | ) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> { | 52 | ) -> Option<(&'node SyntaxNode, GreenNode, Vec<SyntaxError>)> { |
53 | let (node, reparser) = find_reparsable_node(node, edit.delete)?; | 53 | let (node, reparser) = find_reparsable_node(node, edit.delete)?; |
54 | let text = get_text_after_edit(node, &edit); | 54 | let text = get_text_after_edit(node, &edit); |
55 | let tokens = tokenize(&text); | 55 | let tokens = tokenize(&text); |
@@ -61,7 +61,7 @@ fn reparse_block<'node>( | |||
61 | Some((node, green, new_errors)) | 61 | Some((node, green, new_errors)) |
62 | } | 62 | } |
63 | 63 | ||
64 | fn get_text_after_edit(node: SyntaxNodeRef, edit: &AtomTextEdit) -> String { | 64 | fn get_text_after_edit(node: &SyntaxNode, edit: &AtomTextEdit) -> String { |
65 | replace_range( | 65 | replace_range( |
66 | node.text().to_string(), | 66 | node.text().to_string(), |
67 | edit.delete - node.range().start(), | 67 | edit.delete - node.range().start(), |
@@ -77,17 +77,14 @@ fn is_contextual_kw(text: &str) -> bool { | |||
77 | } | 77 | } |
78 | 78 | ||
79 | type ParseFn = fn(&mut Parser); | 79 | type ParseFn = fn(&mut Parser); |
80 | fn find_reparsable_node( | 80 | fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(&SyntaxNode, ParseFn)> { |
81 | node: SyntaxNodeRef<'_>, | ||
82 | range: TextRange, | ||
83 | ) -> Option<(SyntaxNodeRef<'_>, ParseFn)> { | ||
84 | let node = algo::find_covering_node(node, range); | 81 | let node = algo::find_covering_node(node, range); |
85 | return node | 82 | return node |
86 | .ancestors() | 83 | .ancestors() |
87 | .filter_map(|node| reparser(node).map(|r| (node, r))) | 84 | .filter_map(|node| reparser(node).map(|r| (node, r))) |
88 | .next(); | 85 | .next(); |
89 | 86 | ||
90 | fn reparser(node: SyntaxNodeRef) -> Option<ParseFn> { | 87 | fn reparser(node: &SyntaxNode) -> Option<ParseFn> { |
91 | let res = match node.kind() { | 88 | let res = match node.kind() { |
92 | BLOCK => grammar::block, | 89 | BLOCK => grammar::block, |
93 | NAMED_FIELD_DEF_LIST => grammar::named_field_def_list, | 90 | NAMED_FIELD_DEF_LIST => grammar::named_field_def_list, |
@@ -138,7 +135,7 @@ fn is_balanced(tokens: &[Token]) -> bool { | |||
138 | fn merge_errors( | 135 | fn merge_errors( |
139 | old_errors: Vec<SyntaxError>, | 136 | old_errors: Vec<SyntaxError>, |
140 | new_errors: Vec<SyntaxError>, | 137 | new_errors: Vec<SyntaxError>, |
141 | old_node: SyntaxNodeRef, | 138 | old_node: &SyntaxNode, |
142 | edit: &AtomTextEdit, | 139 | edit: &AtomTextEdit, |
143 | ) -> Vec<SyntaxError> { | 140 | ) -> Vec<SyntaxError> { |
144 | let mut res = Vec::new(); | 141 | let mut res = Vec::new(); |
@@ -159,22 +156,22 @@ fn merge_errors( | |||
159 | mod tests { | 156 | mod tests { |
160 | use test_utils::{extract_range, assert_eq_text}; | 157 | use test_utils::{extract_range, assert_eq_text}; |
161 | 158 | ||
162 | use crate::{SourceFileNode, text_utils::replace_range, utils::dump_tree }; | 159 | use crate::{SourceFile, AstNode, text_utils::replace_range, utils::dump_tree}; |
163 | use super::*; | 160 | use super::*; |
164 | 161 | ||
165 | fn do_check<F>(before: &str, replace_with: &str, reparser: F) | 162 | fn do_check<F>(before: &str, replace_with: &str, reparser: F) |
166 | where | 163 | where |
167 | for<'a> F: Fn( | 164 | for<'a> F: Fn( |
168 | SyntaxNodeRef<'a>, | 165 | &'a SyntaxNode, |
169 | &AtomTextEdit, | 166 | &AtomTextEdit, |
170 | ) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec<SyntaxError>)>, | 167 | ) -> Option<(&'a SyntaxNode, GreenNode, Vec<SyntaxError>)>, |
171 | { | 168 | { |
172 | let (range, before) = extract_range(before); | 169 | let (range, before) = extract_range(before); |
173 | let after = replace_range(before.clone(), range, replace_with); | 170 | let after = replace_range(before.clone(), range, replace_with); |
174 | 171 | ||
175 | let fully_reparsed = SourceFileNode::parse(&after); | 172 | let fully_reparsed = SourceFile::parse(&after); |
176 | let incrementally_reparsed = { | 173 | let incrementally_reparsed = { |
177 | let f = SourceFileNode::parse(&before); | 174 | let f = SourceFile::parse(&before); |
178 | let edit = AtomTextEdit { | 175 | let edit = AtomTextEdit { |
179 | delete: range, | 176 | delete: range, |
180 | insert: replace_with.to_string(), | 177 | insert: replace_with.to_string(), |
@@ -183,7 +180,7 @@ mod tests { | |||
183 | reparser(f.syntax(), &edit).expect("cannot incrementally reparse"); | 180 | reparser(f.syntax(), &edit).expect("cannot incrementally reparse"); |
184 | let green_root = node.replace_with(green); | 181 | let green_root = node.replace_with(green); |
185 | let errors = super::merge_errors(f.errors(), new_errors, node, &edit); | 182 | let errors = super::merge_errors(f.errors(), new_errors, node, &edit); |
186 | SourceFileNode::new(green_root, errors) | 183 | SourceFile::new(green_root, errors) |
187 | }; | 184 | }; |
188 | 185 | ||
189 | assert_eq_text!( | 186 | assert_eq_text!( |
diff --git a/crates/ra_syntax/src/utils.rs b/crates/ra_syntax/src/utils.rs index 0a2b6afbc..2e1b42da0 100644 --- a/crates/ra_syntax/src/utils.rs +++ b/crates/ra_syntax/src/utils.rs | |||
@@ -1,11 +1,11 @@ | |||
1 | use crate::{SourceFileNode, SyntaxKind, SyntaxNodeRef, WalkEvent, AstNode}; | 1 | use std::{str, fmt::Write}; |
2 | use std::fmt::Write; | 2 | |
3 | use std::str; | 3 | use crate::{SourceFile, SyntaxKind, WalkEvent, AstNode, SyntaxNode}; |
4 | 4 | ||
5 | /// Parse a file and create a string representation of the resulting parse tree. | 5 | /// Parse a file and create a string representation of the resulting parse tree. |
6 | pub fn dump_tree(syntax: SyntaxNodeRef) -> String { | 6 | pub fn dump_tree(syntax: &SyntaxNode) -> String { |
7 | let mut errors: Vec<_> = match syntax.ancestors().find_map(SourceFileNode::cast) { | 7 | let mut errors: Vec<_> = match syntax.ancestors().find_map(SourceFile::cast) { |
8 | Some(file) => file.owned().errors(), | 8 | Some(file) => file.errors(), |
9 | None => syntax.root_data().to_vec(), | 9 | None => syntax.root_data().to_vec(), |
10 | }; | 10 | }; |
11 | errors.sort_by_key(|e| e.offset()); | 11 | errors.sort_by_key(|e| e.offset()); |
@@ -48,14 +48,13 @@ pub fn dump_tree(syntax: SyntaxNodeRef) -> String { | |||
48 | } | 48 | } |
49 | 49 | ||
50 | pub fn check_fuzz_invariants(text: &str) { | 50 | pub fn check_fuzz_invariants(text: &str) { |
51 | let file = SourceFileNode::parse(text); | 51 | let file = SourceFile::parse(text); |
52 | let root = file.syntax(); | 52 | let root = file.syntax(); |
53 | validate_block_structure(root); | 53 | validate_block_structure(root); |
54 | let _ = file.ast(); | ||
55 | let _ = file.errors(); | 54 | let _ = file.errors(); |
56 | } | 55 | } |
57 | 56 | ||
58 | pub(crate) fn validate_block_structure(root: SyntaxNodeRef) { | 57 | pub(crate) fn validate_block_structure(root: &SyntaxNode) { |
59 | let mut stack = Vec::new(); | 58 | let mut stack = Vec::new(); |
60 | for node in root.descendants() { | 59 | for node in root.descendants() { |
61 | match node.kind() { | 60 | match node.kind() { |
diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index bdee8120c..73e1d20b9 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs | |||
@@ -1,16 +1,15 @@ | |||
1 | use crate::{ | ||
2 | algo::visit::{visitor_ctx, VisitorCtx}, | ||
3 | ast, | ||
4 | SourceFileNode, | ||
5 | yellow::SyntaxError, | ||
6 | }; | ||
7 | |||
8 | mod byte; | 1 | mod byte; |
9 | mod byte_string; | 2 | mod byte_string; |
10 | mod char; | 3 | mod char; |
11 | mod string; | 4 | mod string; |
12 | 5 | ||
13 | pub(crate) fn validate(file: &SourceFileNode) -> Vec<SyntaxError> { | 6 | use crate::{ |
7 | SourceFile, yellow::SyntaxError, AstNode, | ||
8 | ast, | ||
9 | algo::visit::{visitor_ctx, VisitorCtx}, | ||
10 | }; | ||
11 | |||
12 | pub(crate) fn validate(file: &SourceFile) -> Vec<SyntaxError> { | ||
14 | let mut errors = Vec::new(); | 13 | let mut errors = Vec::new(); |
15 | for node in file.syntax().descendants() { | 14 | for node in file.syntax().descendants() { |
16 | let _ = visitor_ctx(&mut errors) | 15 | let _ = visitor_ctx(&mut errors) |
diff --git a/crates/ra_syntax/src/validation/byte.rs b/crates/ra_syntax/src/validation/byte.rs index 714224b09..4deb302a7 100644 --- a/crates/ra_syntax/src/validation/byte.rs +++ b/crates/ra_syntax/src/validation/byte.rs | |||
@@ -11,7 +11,7 @@ use crate::{ | |||
11 | }, | 11 | }, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | pub(super) fn validate_byte_node(node: ast::Byte, errors: &mut Vec<SyntaxError>) { | 14 | pub(super) fn validate_byte_node(node: &ast::Byte, errors: &mut Vec<SyntaxError>) { |
15 | let literal_text = node.text(); | 15 | let literal_text = node.text(); |
16 | let literal_range = node.syntax().range(); | 16 | let literal_range = node.syntax().range(); |
17 | let mut components = string_lexing::parse_byte_literal(literal_text); | 17 | let mut components = string_lexing::parse_byte_literal(literal_text); |
@@ -106,11 +106,11 @@ fn validate_byte_code_escape(text: &str, range: TextRange, errors: &mut Vec<Synt | |||
106 | 106 | ||
107 | #[cfg(test)] | 107 | #[cfg(test)] |
108 | mod test { | 108 | mod test { |
109 | use crate::SourceFileNode; | 109 | use crate::{SourceFile, TreePtr}; |
110 | 110 | ||
111 | fn build_file(literal: &str) -> SourceFileNode { | 111 | fn build_file(literal: &str) -> TreePtr<SourceFile> { |
112 | let src = format!("const C: u8 = b'{}';", literal); | 112 | let src = format!("const C: u8 = b'{}';", literal); |
113 | SourceFileNode::parse(&src) | 113 | SourceFile::parse(&src) |
114 | } | 114 | } |
115 | 115 | ||
116 | fn assert_valid_byte(literal: &str) { | 116 | fn assert_valid_byte(literal: &str) { |
diff --git a/crates/ra_syntax/src/validation/byte_string.rs b/crates/ra_syntax/src/validation/byte_string.rs index f7a4fb156..670c43a09 100644 --- a/crates/ra_syntax/src/validation/byte_string.rs +++ b/crates/ra_syntax/src/validation/byte_string.rs | |||
@@ -9,7 +9,7 @@ use crate::{ | |||
9 | 9 | ||
10 | use super::byte; | 10 | use super::byte; |
11 | 11 | ||
12 | pub(crate) fn validate_byte_string_node(node: ast::ByteString, errors: &mut Vec<SyntaxError>) { | 12 | pub(crate) fn validate_byte_string_node(node: &ast::ByteString, errors: &mut Vec<SyntaxError>) { |
13 | let literal_text = node.text(); | 13 | let literal_text = node.text(); |
14 | let literal_range = node.syntax().range(); | 14 | let literal_range = node.syntax().range(); |
15 | let mut components = string_lexing::parse_byte_string_literal(literal_text); | 15 | let mut components = string_lexing::parse_byte_string_literal(literal_text); |
@@ -43,12 +43,12 @@ pub(crate) fn validate_byte_string_node(node: ast::ByteString, errors: &mut Vec< | |||
43 | 43 | ||
44 | #[cfg(test)] | 44 | #[cfg(test)] |
45 | mod test { | 45 | mod test { |
46 | use crate::SourceFileNode; | 46 | use crate::{SourceFile, TreePtr}; |
47 | 47 | ||
48 | fn build_file(literal: &str) -> SourceFileNode { | 48 | fn build_file(literal: &str) -> TreePtr<SourceFile> { |
49 | let src = format!(r#"const S: &'static [u8] = b"{}";"#, literal); | 49 | let src = format!(r#"const S: &'static [u8] = b"{}";"#, literal); |
50 | println!("Source: {}", src); | 50 | println!("Source: {}", src); |
51 | SourceFileNode::parse(&src) | 51 | SourceFile::parse(&src) |
52 | } | 52 | } |
53 | 53 | ||
54 | fn assert_valid_str(literal: &str) { | 54 | fn assert_valid_str(literal: &str) { |
diff --git a/crates/ra_syntax/src/validation/char.rs b/crates/ra_syntax/src/validation/char.rs index 1d6fe8837..9cbd43fba 100644 --- a/crates/ra_syntax/src/validation/char.rs +++ b/crates/ra_syntax/src/validation/char.rs | |||
@@ -14,7 +14,7 @@ use crate::{ | |||
14 | }, | 14 | }, |
15 | }; | 15 | }; |
16 | 16 | ||
17 | pub(super) fn validate_char_node(node: ast::Char, errors: &mut Vec<SyntaxError>) { | 17 | pub(super) fn validate_char_node(node: &ast::Char, errors: &mut Vec<SyntaxError>) { |
18 | let literal_text = node.text(); | 18 | let literal_text = node.text(); |
19 | let literal_range = node.syntax().range(); | 19 | let literal_range = node.syntax().range(); |
20 | let mut components = string_lexing::parse_char_literal(literal_text); | 20 | let mut components = string_lexing::parse_char_literal(literal_text); |
@@ -175,11 +175,11 @@ fn validate_unicode_escape(text: &str, range: TextRange, errors: &mut Vec<Syntax | |||
175 | 175 | ||
176 | #[cfg(test)] | 176 | #[cfg(test)] |
177 | mod test { | 177 | mod test { |
178 | use crate::SourceFileNode; | 178 | use crate::{SourceFile, TreePtr}; |
179 | 179 | ||
180 | fn build_file(literal: &str) -> SourceFileNode { | 180 | fn build_file(literal: &str) -> TreePtr<SourceFile> { |
181 | let src = format!("const C: char = '{}';", literal); | 181 | let src = format!("const C: char = '{}';", literal); |
182 | SourceFileNode::parse(&src) | 182 | SourceFile::parse(&src) |
183 | } | 183 | } |
184 | 184 | ||
185 | fn assert_valid_char(literal: &str) { | 185 | fn assert_valid_char(literal: &str) { |
diff --git a/crates/ra_syntax/src/validation/string.rs b/crates/ra_syntax/src/validation/string.rs index 1371bb1f0..7b2a68d12 100644 --- a/crates/ra_syntax/src/validation/string.rs +++ b/crates/ra_syntax/src/validation/string.rs | |||
@@ -9,7 +9,7 @@ use crate::{ | |||
9 | 9 | ||
10 | use super::char; | 10 | use super::char; |
11 | 11 | ||
12 | pub(crate) fn validate_string_node(node: ast::String, errors: &mut Vec<SyntaxError>) { | 12 | pub(crate) fn validate_string_node(node: &ast::String, errors: &mut Vec<SyntaxError>) { |
13 | let literal_text = node.text(); | 13 | let literal_text = node.text(); |
14 | let literal_range = node.syntax().range(); | 14 | let literal_range = node.syntax().range(); |
15 | let mut components = string_lexing::parse_string_literal(literal_text); | 15 | let mut components = string_lexing::parse_string_literal(literal_text); |
@@ -38,12 +38,12 @@ pub(crate) fn validate_string_node(node: ast::String, errors: &mut Vec<SyntaxErr | |||
38 | 38 | ||
39 | #[cfg(test)] | 39 | #[cfg(test)] |
40 | mod test { | 40 | mod test { |
41 | use crate::SourceFileNode; | 41 | use crate::{SourceFile, TreePtr}; |
42 | 42 | ||
43 | fn build_file(literal: &str) -> SourceFileNode { | 43 | fn build_file(literal: &str) -> TreePtr<SourceFile> { |
44 | let src = format!(r#"const S: &'static str = "{}";"#, literal); | 44 | let src = format!(r#"const S: &'static str = "{}";"#, literal); |
45 | println!("Source: {}", src); | 45 | println!("Source: {}", src); |
46 | SourceFileNode::parse(&src) | 46 | SourceFile::parse(&src) |
47 | } | 47 | } |
48 | 48 | ||
49 | fn assert_valid_str(literal: &str) { | 49 | fn assert_valid_str(literal: &str) { |
diff --git a/crates/ra_syntax/src/yellow.rs b/crates/ra_syntax/src/yellow.rs index cacd89dc8..6dc846f33 100644 --- a/crates/ra_syntax/src/yellow.rs +++ b/crates/ra_syntax/src/yellow.rs | |||
@@ -4,15 +4,12 @@ mod syntax_text; | |||
4 | 4 | ||
5 | use self::syntax_text::SyntaxText; | 5 | use self::syntax_text::SyntaxText; |
6 | use crate::{SmolStr, SyntaxKind, TextRange}; | 6 | use crate::{SmolStr, SyntaxKind, TextRange}; |
7 | use rowan::Types; | 7 | use rowan::{Types, TransparentNewType}; |
8 | use std::{ | 8 | use std::fmt; |
9 | fmt, | ||
10 | hash::{Hash, Hasher}, | ||
11 | }; | ||
12 | 9 | ||
13 | pub(crate) use self::builder::GreenBuilder; | 10 | pub(crate) use self::builder::GreenBuilder; |
14 | pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; | 11 | pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; |
15 | pub use rowan::{TreeRoot, WalkEvent}; | 12 | pub use rowan::WalkEvent; |
16 | 13 | ||
17 | #[derive(Debug, Clone, Copy)] | 14 | #[derive(Debug, Clone, Copy)] |
18 | pub enum RaTypes {} | 15 | pub enum RaTypes {} |
@@ -21,35 +18,19 @@ impl Types for RaTypes { | |||
21 | type RootData = Vec<SyntaxError>; | 18 | type RootData = Vec<SyntaxError>; |
22 | } | 19 | } |
23 | 20 | ||
24 | pub type OwnedRoot = ::rowan::OwnedRoot<RaTypes>; | ||
25 | pub type RefRoot<'a> = ::rowan::RefRoot<'a, RaTypes>; | ||
26 | |||
27 | pub type GreenNode = ::rowan::GreenNode<RaTypes>; | 21 | pub type GreenNode = ::rowan::GreenNode<RaTypes>; |
22 | pub type TreePtr<T> = ::rowan::TreePtr<RaTypes, T>; | ||
28 | 23 | ||
29 | #[derive(Clone, Copy)] | 24 | #[derive(PartialEq, Eq, Hash)] |
30 | pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>(pub(crate) ::rowan::SyntaxNode<RaTypes, R>); | 25 | #[repr(transparent)] |
31 | pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; | 26 | pub struct SyntaxNode(pub(crate) ::rowan::SyntaxNode<RaTypes>); |
32 | 27 | unsafe impl TransparentNewType for SyntaxNode { | |
33 | impl<R1, R2> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> | 28 | type Repr = ::rowan::SyntaxNode<RaTypes>; |
34 | where | ||
35 | R1: TreeRoot<RaTypes>, | ||
36 | R2: TreeRoot<RaTypes>, | ||
37 | { | ||
38 | fn eq(&self, other: &SyntaxNode<R1>) -> bool { | ||
39 | self.0 == other.0 | ||
40 | } | ||
41 | } | ||
42 | |||
43 | impl<R: TreeRoot<RaTypes>> Eq for SyntaxNode<R> {} | ||
44 | impl<R: TreeRoot<RaTypes>> Hash for SyntaxNode<R> { | ||
45 | fn hash<H: Hasher>(&self, state: &mut H) { | ||
46 | self.0.hash(state) | ||
47 | } | ||
48 | } | 29 | } |
49 | 30 | ||
50 | impl SyntaxNode { | 31 | impl SyntaxNode { |
51 | pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxNode { | 32 | pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreePtr<SyntaxNode> { |
52 | SyntaxNode(::rowan::SyntaxNode::new(green, errors)) | 33 | TreePtr::cast(::rowan::SyntaxNode::new(green, errors)) |
53 | } | 34 | } |
54 | } | 35 | } |
55 | 36 | ||
@@ -59,45 +40,42 @@ pub enum Direction { | |||
59 | Prev, | 40 | Prev, |
60 | } | 41 | } |
61 | 42 | ||
62 | impl<'a> SyntaxNodeRef<'a> { | 43 | impl SyntaxNode { |
63 | pub fn leaf_text(self) -> Option<&'a SmolStr> { | 44 | pub fn leaf_text(&self) -> Option<&SmolStr> { |
64 | self.0.leaf_text() | 45 | self.0.leaf_text() |
65 | } | 46 | } |
66 | pub fn ancestors(self) -> impl Iterator<Item = SyntaxNodeRef<'a>> { | 47 | pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> { |
67 | crate::algo::generate(Some(self), |&node| node.parent()) | 48 | crate::algo::generate(Some(self), |&node| node.parent()) |
68 | } | 49 | } |
69 | pub fn descendants(self) -> impl Iterator<Item = SyntaxNodeRef<'a>> { | 50 | pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> { |
70 | self.preorder().filter_map(|event| match event { | 51 | self.preorder().filter_map(|event| match event { |
71 | WalkEvent::Enter(node) => Some(node), | 52 | WalkEvent::Enter(node) => Some(node), |
72 | WalkEvent::Leave(_) => None, | 53 | WalkEvent::Leave(_) => None, |
73 | }) | 54 | }) |
74 | } | 55 | } |
75 | pub fn siblings(self, direction: Direction) -> impl Iterator<Item = SyntaxNodeRef<'a>> { | 56 | pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> { |
76 | crate::algo::generate(Some(self), move |&node| match direction { | 57 | crate::algo::generate(Some(self), move |&node| match direction { |
77 | Direction::Next => node.next_sibling(), | 58 | Direction::Next => node.next_sibling(), |
78 | Direction::Prev => node.prev_sibling(), | 59 | Direction::Prev => node.prev_sibling(), |
79 | }) | 60 | }) |
80 | } | 61 | } |
81 | pub fn preorder(self) -> impl Iterator<Item = WalkEvent<SyntaxNodeRef<'a>>> { | 62 | pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> { |
82 | self.0.preorder().map(|event| match event { | 63 | self.0.preorder().map(|event| match event { |
83 | WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode(n)), | 64 | WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)), |
84 | WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode(n)), | 65 | WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)), |
85 | }) | 66 | }) |
86 | } | 67 | } |
87 | } | 68 | } |
88 | 69 | ||
89 | impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { | 70 | impl SyntaxNode { |
90 | pub(crate) fn root_data(&self) -> &Vec<SyntaxError> { | 71 | pub(crate) fn root_data(&self) -> &Vec<SyntaxError> { |
91 | self.0.root_data() | 72 | self.0.root_data() |
92 | } | 73 | } |
93 | pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { | 74 | pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { |
94 | self.0.replace_with(replacement) | 75 | self.0.replace_self(replacement) |
95 | } | ||
96 | pub fn borrowed<'a>(&'a self) -> SyntaxNode<RefRoot<'a>> { | ||
97 | SyntaxNode(self.0.borrowed()) | ||
98 | } | 76 | } |
99 | pub fn owned(&self) -> SyntaxNode<OwnedRoot> { | 77 | pub fn to_owned(&self) -> TreePtr<SyntaxNode> { |
100 | SyntaxNode(self.0.owned()) | 78 | TreePtr::cast(self.0.to_owned()) |
101 | } | 79 | } |
102 | pub fn kind(&self) -> SyntaxKind { | 80 | pub fn kind(&self) -> SyntaxKind { |
103 | self.0.kind() | 81 | self.0.kind() |
@@ -106,32 +84,32 @@ impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { | |||
106 | self.0.range() | 84 | self.0.range() |
107 | } | 85 | } |
108 | pub fn text(&self) -> SyntaxText { | 86 | pub fn text(&self) -> SyntaxText { |
109 | SyntaxText::new(self.borrowed()) | 87 | SyntaxText::new(self) |
110 | } | 88 | } |
111 | pub fn is_leaf(&self) -> bool { | 89 | pub fn is_leaf(&self) -> bool { |
112 | self.0.is_leaf() | 90 | self.0.is_leaf() |
113 | } | 91 | } |
114 | pub fn parent(&self) -> Option<SyntaxNode<R>> { | 92 | pub fn parent(&self) -> Option<&SyntaxNode> { |
115 | self.0.parent().map(SyntaxNode) | 93 | self.0.parent().map(SyntaxNode::from_repr) |
116 | } | 94 | } |
117 | pub fn first_child(&self) -> Option<SyntaxNode<R>> { | 95 | pub fn first_child(&self) -> Option<&SyntaxNode> { |
118 | self.0.first_child().map(SyntaxNode) | 96 | self.0.first_child().map(SyntaxNode::from_repr) |
119 | } | 97 | } |
120 | pub fn last_child(&self) -> Option<SyntaxNode<R>> { | 98 | pub fn last_child(&self) -> Option<&SyntaxNode> { |
121 | self.0.last_child().map(SyntaxNode) | 99 | self.0.last_child().map(SyntaxNode::from_repr) |
122 | } | 100 | } |
123 | pub fn next_sibling(&self) -> Option<SyntaxNode<R>> { | 101 | pub fn next_sibling(&self) -> Option<&SyntaxNode> { |
124 | self.0.next_sibling().map(SyntaxNode) | 102 | self.0.next_sibling().map(SyntaxNode::from_repr) |
125 | } | 103 | } |
126 | pub fn prev_sibling(&self) -> Option<SyntaxNode<R>> { | 104 | pub fn prev_sibling(&self) -> Option<&SyntaxNode> { |
127 | self.0.prev_sibling().map(SyntaxNode) | 105 | self.0.prev_sibling().map(SyntaxNode::from_repr) |
128 | } | 106 | } |
129 | pub fn children(&self) -> SyntaxNodeChildren<R> { | 107 | pub fn children(&self) -> SyntaxNodeChildren { |
130 | SyntaxNodeChildren(self.0.children()) | 108 | SyntaxNodeChildren(self.0.children()) |
131 | } | 109 | } |
132 | } | 110 | } |
133 | 111 | ||
134 | impl<R: TreeRoot<RaTypes>> fmt::Debug for SyntaxNode<R> { | 112 | impl fmt::Debug for SyntaxNode { |
135 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | 113 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
136 | write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; | 114 | write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; |
137 | if has_short_text(self.kind()) { | 115 | if has_short_text(self.kind()) { |
@@ -142,13 +120,13 @@ impl<R: TreeRoot<RaTypes>> fmt::Debug for SyntaxNode<R> { | |||
142 | } | 120 | } |
143 | 121 | ||
144 | #[derive(Debug)] | 122 | #[derive(Debug)] |
145 | pub struct SyntaxNodeChildren<R: TreeRoot<RaTypes>>(::rowan::SyntaxNodeChildren<RaTypes, R>); | 123 | pub struct SyntaxNodeChildren<'a>(::rowan::SyntaxNodeChildren<'a, RaTypes>); |
146 | 124 | ||
147 | impl<R: TreeRoot<RaTypes>> Iterator for SyntaxNodeChildren<R> { | 125 | impl<'a> Iterator for SyntaxNodeChildren<'a> { |
148 | type Item = SyntaxNode<R>; | 126 | type Item = &'a SyntaxNode; |
149 | 127 | ||
150 | fn next(&mut self) -> Option<SyntaxNode<R>> { | 128 | fn next(&mut self) -> Option<&'a SyntaxNode> { |
151 | self.0.next().map(SyntaxNode) | 129 | self.0.next().map(SyntaxNode::from_repr) |
152 | } | 130 | } |
153 | } | 131 | } |
154 | 132 | ||
diff --git a/crates/ra_syntax/src/yellow/syntax_text.rs b/crates/ra_syntax/src/yellow/syntax_text.rs index 279a83b61..31db0fdab 100644 --- a/crates/ra_syntax/src/yellow/syntax_text.rs +++ b/crates/ra_syntax/src/yellow/syntax_text.rs | |||
@@ -3,17 +3,17 @@ use std::{fmt, ops}; | |||
3 | use ra_text_edit::text_utils::contains_offset_nonstrict; | 3 | use ra_text_edit::text_utils::contains_offset_nonstrict; |
4 | use crate::{ | 4 | use crate::{ |
5 | text_utils::intersect, | 5 | text_utils::intersect, |
6 | SyntaxNodeRef, TextRange, TextUnit, | 6 | SyntaxNode, TextRange, TextUnit, |
7 | }; | 7 | }; |
8 | 8 | ||
9 | #[derive(Clone)] | 9 | #[derive(Clone)] |
10 | pub struct SyntaxText<'a> { | 10 | pub struct SyntaxText<'a> { |
11 | node: SyntaxNodeRef<'a>, | 11 | node: &'a SyntaxNode, |
12 | range: TextRange, | 12 | range: TextRange, |
13 | } | 13 | } |
14 | 14 | ||
15 | impl<'a> SyntaxText<'a> { | 15 | impl<'a> SyntaxText<'a> { |
16 | pub(crate) fn new(node: SyntaxNodeRef<'a>) -> SyntaxText<'a> { | 16 | pub(crate) fn new(node: &'a SyntaxNode) -> SyntaxText<'a> { |
17 | SyntaxText { | 17 | SyntaxText { |
18 | node, | 18 | node, |
19 | range: node.range(), | 19 | range: node.range(), |