aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 09:23:34 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 09:23:34 +0000
commit1e0948a509e8f6ec7cbb5e2ef77669325fee0637 (patch)
tree81b546c105388e1fc3154c90a42a8cc5fb930d0b /crates/ra_syntax/src/ast.rs
parent3f4be819125ce4a22edd86721fa56b5caba99c2e (diff)
parentfa6e0b0d38d2a030b959be91232927b9c096272b (diff)
Merge #453
453: itroduce trait for ast tokens r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/ast.rs')
-rw-r--r--crates/ra_syntax/src/ast.rs52
1 files changed, 10 insertions, 42 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 0e303ee98..96879ae5a 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -23,6 +23,12 @@ pub trait AstNode: rowan::TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>
23 fn to_owned(&self) -> TreePtr<Self>; 23 fn to_owned(&self) -> TreePtr<Self>;
24} 24}
25 25
26pub trait AstToken: AstNode {
27 fn text(&self) -> &SmolStr {
28 self.syntax().leaf_text().unwrap()
29 }
30}
31
26pub trait NameOwner: AstNode { 32pub trait NameOwner: AstNode {
27 fn name(&self) -> Option<&Name> { 33 fn name(&self) -> Option<&Name> {
28 child_opt(self) 34 child_opt(self)
@@ -155,41 +161,7 @@ impl Attr {
155 } 161 }
156} 162}
157 163
158impl Lifetime {
159 pub fn text(&self) -> SmolStr {
160 self.syntax().leaf_text().unwrap().clone()
161 }
162}
163
164impl Char {
165 pub fn text(&self) -> &SmolStr {
166 &self.syntax().leaf_text().unwrap()
167 }
168}
169
170impl Byte {
171 pub fn text(&self) -> &SmolStr {
172 &self.syntax().leaf_text().unwrap()
173 }
174}
175
176impl ByteString {
177 pub fn text(&self) -> &SmolStr {
178 &self.syntax().leaf_text().unwrap()
179 }
180}
181
182impl String {
183 pub fn text(&self) -> &SmolStr {
184 &self.syntax().leaf_text().unwrap()
185 }
186}
187
188impl Comment { 164impl Comment {
189 pub fn text(&self) -> &SmolStr {
190 self.syntax().leaf_text().unwrap()
191 }
192
193 pub fn flavor(&self) -> CommentFlavor { 165 pub fn flavor(&self) -> CommentFlavor {
194 let text = self.text(); 166 let text = self.text();
195 if text.starts_with("///") { 167 if text.starts_with("///") {
@@ -248,10 +220,6 @@ impl CommentFlavor {
248} 220}
249 221
250impl Whitespace { 222impl Whitespace {
251 pub fn text(&self) -> &SmolStr {
252 &self.syntax().leaf_text().unwrap()
253 }
254
255 pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> { 223 pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> {
256 self.text().chars().filter(|&c| c == '\n').map(|_| &()) 224 self.text().chars().filter(|&c| c == '\n').map(|_| &())
257 } 225 }
@@ -262,16 +230,16 @@ impl Whitespace {
262} 230}
263 231
264impl Name { 232impl Name {
265 pub fn text(&self) -> SmolStr { 233 pub fn text(&self) -> &SmolStr {
266 let ident = self.syntax().first_child().unwrap(); 234 let ident = self.syntax().first_child().unwrap();
267 ident.leaf_text().unwrap().clone() 235 ident.leaf_text().unwrap()
268 } 236 }
269} 237}
270 238
271impl NameRef { 239impl NameRef {
272 pub fn text(&self) -> SmolStr { 240 pub fn text(&self) -> &SmolStr {
273 let ident = self.syntax().first_child().unwrap(); 241 let ident = self.syntax().first_child().unwrap();
274 ident.leaf_text().unwrap().clone() 242 ident.leaf_text().unwrap()
275 } 243 }
276} 244}
277 245