aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ast/mod.rs')
-rw-r--r--crates/ra_syntax/src/ast/mod.rs34
1 files changed, 33 insertions, 1 deletions
diff --git a/crates/ra_syntax/src/ast/mod.rs b/crates/ra_syntax/src/ast/mod.rs
index 4355531d0..7077e3492 100644
--- a/crates/ra_syntax/src/ast/mod.rs
+++ b/crates/ra_syntax/src/ast/mod.rs
@@ -1,6 +1,7 @@
1mod generated; 1mod generated;
2 2
3use std::marker::PhantomData; 3use std::marker::PhantomData;
4use std::string::String as RustString;
4 5
5use itertools::Itertools; 6use itertools::Itertools;
6 7
@@ -12,6 +13,10 @@ use crate::{
12 SyntaxNodeRef, 13 SyntaxNodeRef,
13}; 14};
14 15
16/// 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
18/// the same representation: a pointer to the tree root and a pointer to the
19/// node itself.
15pub trait AstNode<'a>: Clone + Copy + 'a { 20pub trait AstNode<'a>: Clone + Copy + 'a {
16 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> 21 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self>
17 where 22 where
@@ -72,7 +77,7 @@ pub trait DocCommentsOwner<'a>: AstNode<'a> {
72 77
73 /// Returns the textual content of a doc comment block as a single string. 78 /// Returns the textual content of a doc comment block as a single string.
74 /// That is, strips leading `///` and joins lines 79 /// That is, strips leading `///` and joins lines
75 fn doc_comment_text(self) -> String { 80 fn doc_comment_text(self) -> RustString {
76 self.doc_comments() 81 self.doc_comments()
77 .map(|comment| { 82 .map(|comment| {
78 let prefix = comment.prefix(); 83 let prefix = comment.prefix();
@@ -129,6 +134,24 @@ impl<'a> Char<'a> {
129 } 134 }
130} 135}
131 136
137impl<'a> Byte<'a> {
138 pub fn text(&self) -> &SmolStr {
139 &self.syntax().leaf_text().unwrap()
140 }
141}
142
143impl<'a> ByteString<'a> {
144 pub fn text(&self) -> &SmolStr {
145 &self.syntax().leaf_text().unwrap()
146 }
147}
148
149impl<'a> String<'a> {
150 pub fn text(&self) -> &SmolStr {
151 &self.syntax().leaf_text().unwrap()
152 }
153}
154
132impl<'a> Comment<'a> { 155impl<'a> Comment<'a> {
133 pub fn text(&self) -> &SmolStr { 156 pub fn text(&self) -> &SmolStr {
134 self.syntax().leaf_text().unwrap() 157 self.syntax().leaf_text().unwrap()
@@ -292,6 +315,15 @@ impl<'a> PathSegment<'a> {
292 } 315 }
293} 316}
294 317
318impl<'a> UseTreeList<'a> {
319 pub fn parent_use_tree(self) -> UseTree<'a> {
320 self.syntax()
321 .parent()
322 .and_then(UseTree::cast)
323 .expect("UseTreeLists are always nested in UseTrees")
324 }
325}
326
295fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> { 327fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> {
296 children(parent).next() 328 children(parent).next()
297} 329}