diff options
Diffstat (limited to 'crates/ra_syntax/src/ast.rs')
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 52 |
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 | ||
26 | pub trait AstToken: AstNode { | ||
27 | fn text(&self) -> &SmolStr { | ||
28 | self.syntax().leaf_text().unwrap() | ||
29 | } | ||
30 | } | ||
31 | |||
26 | pub trait NameOwner: AstNode { | 32 | pub 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 | ||
158 | impl Lifetime { | ||
159 | pub fn text(&self) -> SmolStr { | ||
160 | self.syntax().leaf_text().unwrap().clone() | ||
161 | } | ||
162 | } | ||
163 | |||
164 | impl Char { | ||
165 | pub fn text(&self) -> &SmolStr { | ||
166 | &self.syntax().leaf_text().unwrap() | ||
167 | } | ||
168 | } | ||
169 | |||
170 | impl Byte { | ||
171 | pub fn text(&self) -> &SmolStr { | ||
172 | &self.syntax().leaf_text().unwrap() | ||
173 | } | ||
174 | } | ||
175 | |||
176 | impl ByteString { | ||
177 | pub fn text(&self) -> &SmolStr { | ||
178 | &self.syntax().leaf_text().unwrap() | ||
179 | } | ||
180 | } | ||
181 | |||
182 | impl String { | ||
183 | pub fn text(&self) -> &SmolStr { | ||
184 | &self.syntax().leaf_text().unwrap() | ||
185 | } | ||
186 | } | ||
187 | |||
188 | impl Comment { | 164 | impl 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 | ||
250 | impl Whitespace { | 222 | impl 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 | ||
264 | impl Name { | 232 | impl 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 | ||
271 | impl NameRef { | 239 | impl 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 | ||