diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-04-09 17:54:14 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-04-09 17:54:14 +0100 |
commit | dde9488559514e7d039cec9407e1f8627f665cd0 (patch) | |
tree | 5ddc0361d4d3538b60cb63a645042cfdcd51f4c6 /crates/ra_syntax/src/ast/extensions.rs | |
parent | 4cea01fdf8dc0a647007bde3010f9fbe607f1e75 (diff) | |
parent | 2bfb65db93e48d8f9e8ecac0b2ea837c081a4db5 (diff) |
Merge #3919
3919: Refactor tokena accessors r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/ast/extensions.rs')
-rw-r--r-- | crates/ra_syntax/src/ast/extensions.rs | 141 |
1 files changed, 18 insertions, 123 deletions
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 33fe60762..b50a89864 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs | |||
@@ -2,16 +2,14 @@ | |||
2 | //! Extensions for various expressions live in a sibling `expr_extensions` module. | 2 | //! Extensions for various expressions live in a sibling `expr_extensions` module. |
3 | 3 | ||
4 | use itertools::Itertools; | 4 | use itertools::Itertools; |
5 | use ra_parser::SyntaxKind; | ||
5 | 6 | ||
6 | use crate::{ | 7 | use crate::{ |
7 | ast::{ | 8 | ast::{ |
8 | self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode, | 9 | self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode, |
9 | }, | 10 | }, |
10 | SmolStr, SyntaxElement, | 11 | SmolStr, SyntaxElement, SyntaxToken, T, |
11 | SyntaxKind::*, | ||
12 | SyntaxToken, T, | ||
13 | }; | 12 | }; |
14 | use ra_parser::SyntaxKind; | ||
15 | 13 | ||
16 | impl ast::Name { | 14 | impl ast::Name { |
17 | pub fn text(&self) -> &SmolStr { | 15 | pub fn text(&self) -> &SmolStr { |
@@ -25,13 +23,11 @@ impl ast::NameRef { | |||
25 | } | 23 | } |
26 | 24 | ||
27 | pub fn as_tuple_field(&self) -> Option<usize> { | 25 | pub fn as_tuple_field(&self) -> Option<usize> { |
28 | self.syntax().children_with_tokens().find_map(|c| { | 26 | if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token_token() { |
29 | if c.kind() == SyntaxKind::INT_NUMBER { | 27 | token.text().as_str().parse().ok() |
30 | c.as_token().and_then(|tok| tok.text().as_str().parse().ok()) | 28 | } else { |
31 | } else { | 29 | None |
32 | None | 30 | } |
33 | } | ||
34 | }) | ||
35 | } | 31 | } |
36 | } | 32 | } |
37 | 33 | ||
@@ -140,15 +136,6 @@ impl ast::Path { | |||
140 | } | 136 | } |
141 | } | 137 | } |
142 | 138 | ||
143 | impl ast::Module { | ||
144 | pub fn has_semi(&self) -> bool { | ||
145 | match self.syntax().last_child_or_token() { | ||
146 | None => false, | ||
147 | Some(node) => node.kind() == T![;], | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | |||
152 | impl ast::UseTreeList { | 139 | impl ast::UseTreeList { |
153 | pub fn parent_use_tree(&self) -> ast::UseTree { | 140 | pub fn parent_use_tree(&self) -> ast::UseTree { |
154 | self.syntax() | 141 | self.syntax() |
@@ -179,10 +166,6 @@ impl ast::ImplDef { | |||
179 | let second = types.next(); | 166 | let second = types.next(); |
180 | (first, second) | 167 | (first, second) |
181 | } | 168 | } |
182 | |||
183 | pub fn is_negative(&self) -> bool { | ||
184 | self.syntax().children_with_tokens().any(|t| t.kind() == T![!]) | ||
185 | } | ||
186 | } | 169 | } |
187 | 170 | ||
188 | #[derive(Debug, Clone, PartialEq, Eq)] | 171 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -223,41 +206,6 @@ impl ast::EnumVariant { | |||
223 | } | 206 | } |
224 | } | 207 | } |
225 | 208 | ||
226 | impl ast::FnDef { | ||
227 | pub fn semicolon_token(&self) -> Option<SyntaxToken> { | ||
228 | self.syntax() | ||
229 | .last_child_or_token() | ||
230 | .and_then(|it| it.into_token()) | ||
231 | .filter(|it| it.kind() == T![;]) | ||
232 | } | ||
233 | |||
234 | pub fn is_async(&self) -> bool { | ||
235 | self.syntax().children_with_tokens().any(|it| it.kind() == T![async]) | ||
236 | } | ||
237 | } | ||
238 | |||
239 | impl ast::LetStmt { | ||
240 | pub fn has_semi(&self) -> bool { | ||
241 | match self.syntax().last_child_or_token() { | ||
242 | None => false, | ||
243 | Some(node) => node.kind() == T![;], | ||
244 | } | ||
245 | } | ||
246 | |||
247 | pub fn eq_token(&self) -> Option<SyntaxToken> { | ||
248 | self.syntax().children_with_tokens().find(|t| t.kind() == EQ).and_then(|it| it.into_token()) | ||
249 | } | ||
250 | } | ||
251 | |||
252 | impl ast::ExprStmt { | ||
253 | pub fn has_semi(&self) -> bool { | ||
254 | match self.syntax().last_child_or_token() { | ||
255 | None => false, | ||
256 | Some(node) => node.kind() == T![;], | ||
257 | } | ||
258 | } | ||
259 | } | ||
260 | |||
261 | #[derive(Debug, Clone, PartialEq, Eq)] | 209 | #[derive(Debug, Clone, PartialEq, Eq)] |
262 | pub enum FieldKind { | 210 | pub enum FieldKind { |
263 | Name(ast::NameRef), | 211 | Name(ast::NameRef), |
@@ -286,25 +234,6 @@ impl ast::FieldExpr { | |||
286 | } | 234 | } |
287 | } | 235 | } |
288 | 236 | ||
289 | impl ast::RefPat { | ||
290 | pub fn is_mut(&self) -> bool { | ||
291 | self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) | ||
292 | } | ||
293 | } | ||
294 | |||
295 | impl ast::BindPat { | ||
296 | pub fn is_mutable(&self) -> bool { | ||
297 | self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) | ||
298 | } | ||
299 | |||
300 | pub fn is_ref(&self) -> bool { | ||
301 | self.syntax().children_with_tokens().any(|n| n.kind() == T![ref]) | ||
302 | } | ||
303 | pub fn has_at(&self) -> bool { | ||
304 | self.syntax().children_with_tokens().any(|it| it.kind() == T![@]) | ||
305 | } | ||
306 | } | ||
307 | |||
308 | pub struct SlicePatComponents { | 237 | pub struct SlicePatComponents { |
309 | pub prefix: Vec<ast::Pat>, | 238 | pub prefix: Vec<ast::Pat>, |
310 | pub slice: Option<ast::Pat>, | 239 | pub slice: Option<ast::Pat>, |
@@ -339,18 +268,6 @@ impl ast::SlicePat { | |||
339 | } | 268 | } |
340 | } | 269 | } |
341 | 270 | ||
342 | impl ast::PointerType { | ||
343 | pub fn is_mut(&self) -> bool { | ||
344 | self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) | ||
345 | } | ||
346 | } | ||
347 | |||
348 | impl ast::ReferenceType { | ||
349 | pub fn is_mut(&self) -> bool { | ||
350 | self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) | ||
351 | } | ||
352 | } | ||
353 | |||
354 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | 271 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] |
355 | pub enum SelfParamKind { | 272 | pub enum SelfParamKind { |
356 | /// self | 273 | /// self |
@@ -363,8 +280,8 @@ pub enum SelfParamKind { | |||
363 | 280 | ||
364 | impl ast::SelfParam { | 281 | impl ast::SelfParam { |
365 | pub fn kind(&self) -> SelfParamKind { | 282 | pub fn kind(&self) -> SelfParamKind { |
366 | if self.amp().is_some() { | 283 | if self.amp_token().is_some() { |
367 | if self.amp_mut_kw().is_some() { | 284 | if self.amp_mut_kw_token().is_some() { |
368 | SelfParamKind::MutRef | 285 | SelfParamKind::MutRef |
369 | } else { | 286 | } else { |
370 | SelfParamKind::Ref | 287 | SelfParamKind::Ref |
@@ -375,7 +292,7 @@ impl ast::SelfParam { | |||
375 | } | 292 | } |
376 | 293 | ||
377 | /// the "mut" in "mut self", not the one in "&mut self" | 294 | /// the "mut" in "mut self", not the one in "&mut self" |
378 | pub fn mut_kw(&self) -> Option<ast::MutKw> { | 295 | pub fn mut_kw_token(&self) -> Option<ast::MutKw> { |
379 | self.syntax() | 296 | self.syntax() |
380 | .children_with_tokens() | 297 | .children_with_tokens() |
381 | .filter_map(|it| it.into_token()) | 298 | .filter_map(|it| it.into_token()) |
@@ -384,7 +301,7 @@ impl ast::SelfParam { | |||
384 | } | 301 | } |
385 | 302 | ||
386 | /// the "mut" in "&mut self", not the one in "mut self" | 303 | /// the "mut" in "&mut self", not the one in "mut self" |
387 | pub fn amp_mut_kw(&self) -> Option<ast::MutKw> { | 304 | pub fn amp_mut_kw_token(&self) -> Option<ast::MutKw> { |
388 | self.syntax() | 305 | self.syntax() |
389 | .children_with_tokens() | 306 | .children_with_tokens() |
390 | .filter_map(|it| it.into_token()) | 307 | .filter_map(|it| it.into_token()) |
@@ -409,18 +326,14 @@ impl ast::TypeBound { | |||
409 | TypeBoundKind::PathType(path_type) | 326 | TypeBoundKind::PathType(path_type) |
410 | } else if let Some(for_type) = children(self).next() { | 327 | } else if let Some(for_type) = children(self).next() { |
411 | TypeBoundKind::ForType(for_type) | 328 | TypeBoundKind::ForType(for_type) |
412 | } else if let Some(lifetime) = self.lifetime() { | 329 | } else if let Some(lifetime) = self.lifetime_token() { |
413 | TypeBoundKind::Lifetime(lifetime) | 330 | TypeBoundKind::Lifetime(lifetime) |
414 | } else { | 331 | } else { |
415 | unreachable!() | 332 | unreachable!() |
416 | } | 333 | } |
417 | } | 334 | } |
418 | 335 | ||
419 | pub fn has_question_mark(&self) -> bool { | 336 | pub fn const_question_token(&self) -> Option<ast::Question> { |
420 | self.question().is_some() | ||
421 | } | ||
422 | |||
423 | pub fn const_question(&self) -> Option<ast::Question> { | ||
424 | self.syntax() | 337 | self.syntax() |
425 | .children_with_tokens() | 338 | .children_with_tokens() |
426 | .filter_map(|it| it.into_token()) | 339 | .filter_map(|it| it.into_token()) |
@@ -428,8 +341,8 @@ impl ast::TypeBound { | |||
428 | .find_map(ast::Question::cast) | 341 | .find_map(ast::Question::cast) |
429 | } | 342 | } |
430 | 343 | ||
431 | pub fn question(&self) -> Option<ast::Question> { | 344 | pub fn question_token(&self) -> Option<ast::Question> { |
432 | if self.const_kw().is_some() { | 345 | if self.const_kw_token().is_some() { |
433 | self.syntax() | 346 | self.syntax() |
434 | .children_with_tokens() | 347 | .children_with_tokens() |
435 | .filter_map(|it| it.into_token()) | 348 | .filter_map(|it| it.into_token()) |
@@ -441,12 +354,6 @@ impl ast::TypeBound { | |||
441 | } | 354 | } |
442 | } | 355 | } |
443 | 356 | ||
444 | impl ast::TraitDef { | ||
445 | pub fn is_auto(&self) -> bool { | ||
446 | self.syntax().children_with_tokens().any(|t| t.kind() == T![auto]) | ||
447 | } | ||
448 | } | ||
449 | |||
450 | pub enum VisibilityKind { | 357 | pub enum VisibilityKind { |
451 | In(ast::Path), | 358 | In(ast::Path), |
452 | PubCrate, | 359 | PubCrate, |
@@ -459,28 +366,16 @@ impl ast::Visibility { | |||
459 | pub fn kind(&self) -> VisibilityKind { | 366 | pub fn kind(&self) -> VisibilityKind { |
460 | if let Some(path) = children(self).next() { | 367 | if let Some(path) = children(self).next() { |
461 | VisibilityKind::In(path) | 368 | VisibilityKind::In(path) |
462 | } else if self.is_pub_crate() { | 369 | } else if self.crate_kw_token().is_some() { |
463 | VisibilityKind::PubCrate | 370 | VisibilityKind::PubCrate |
464 | } else if self.is_pub_super() { | 371 | } else if self.super_kw_token().is_some() { |
465 | VisibilityKind::PubSuper | 372 | VisibilityKind::PubSuper |
466 | } else if self.is_pub_self() { | 373 | } else if self.self_kw_token().is_some() { |
467 | VisibilityKind::PubSuper | 374 | VisibilityKind::PubSuper |
468 | } else { | 375 | } else { |
469 | VisibilityKind::Pub | 376 | VisibilityKind::Pub |
470 | } | 377 | } |
471 | } | 378 | } |
472 | |||
473 | fn is_pub_crate(&self) -> bool { | ||
474 | self.syntax().children_with_tokens().any(|it| it.kind() == T![crate]) | ||
475 | } | ||
476 | |||
477 | fn is_pub_super(&self) -> bool { | ||
478 | self.syntax().children_with_tokens().any(|it| it.kind() == T![super]) | ||
479 | } | ||
480 | |||
481 | fn is_pub_self(&self) -> bool { | ||
482 | self.syntax().children_with_tokens().any(|it| it.kind() == T![self]) | ||
483 | } | ||
484 | } | 379 | } |
485 | 380 | ||
486 | impl ast::MacroCall { | 381 | impl ast::MacroCall { |