diff options
Diffstat (limited to 'crates/syntax/src/ast/node_ext.rs')
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 43 |
1 files changed, 28 insertions, 15 deletions
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index b8ce71d27..5c8cf900f 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs | |||
@@ -13,19 +13,19 @@ use crate::{ | |||
13 | }; | 13 | }; |
14 | 14 | ||
15 | impl ast::Lifetime { | 15 | impl ast::Lifetime { |
16 | pub fn text(&self) -> &SmolStr { | 16 | pub fn text(&self) -> &str { |
17 | text_of_first_token(self.syntax()) | 17 | text_of_first_token(self.syntax()) |
18 | } | 18 | } |
19 | } | 19 | } |
20 | 20 | ||
21 | impl ast::Name { | 21 | impl ast::Name { |
22 | pub fn text(&self) -> &SmolStr { | 22 | pub fn text(&self) -> &str { |
23 | text_of_first_token(self.syntax()) | 23 | text_of_first_token(self.syntax()) |
24 | } | 24 | } |
25 | } | 25 | } |
26 | 26 | ||
27 | impl ast::NameRef { | 27 | impl ast::NameRef { |
28 | pub fn text(&self) -> &SmolStr { | 28 | pub fn text(&self) -> &str { |
29 | text_of_first_token(self.syntax()) | 29 | text_of_first_token(self.syntax()) |
30 | } | 30 | } |
31 | 31 | ||
@@ -34,7 +34,7 @@ impl ast::NameRef { | |||
34 | } | 34 | } |
35 | } | 35 | } |
36 | 36 | ||
37 | fn text_of_first_token(node: &SyntaxNode) -> &SmolStr { | 37 | fn text_of_first_token(node: &SyntaxNode) -> &str { |
38 | node.green().children().next().and_then(|it| it.into_token()).unwrap().text() | 38 | node.green().children().next().and_then(|it| it.into_token()).unwrap().text() |
39 | } | 39 | } |
40 | 40 | ||
@@ -121,7 +121,7 @@ impl ast::Attr { | |||
121 | pub fn simple_name(&self) -> Option<SmolStr> { | 121 | pub fn simple_name(&self) -> Option<SmolStr> { |
122 | let path = self.path()?; | 122 | let path = self.path()?; |
123 | match (path.segment(), path.qualifier()) { | 123 | match (path.segment(), path.qualifier()) { |
124 | (Some(segment), None) => Some(segment.syntax().first_token()?.text().clone()), | 124 | (Some(segment), None) => Some(segment.syntax().first_token()?.text().into()), |
125 | _ => None, | 125 | _ => None, |
126 | } | 126 | } |
127 | } | 127 | } |
@@ -198,6 +198,13 @@ impl ast::Path { | |||
198 | pub fn parent_path(&self) -> Option<ast::Path> { | 198 | pub fn parent_path(&self) -> Option<ast::Path> { |
199 | self.syntax().parent().and_then(ast::Path::cast) | 199 | self.syntax().parent().and_then(ast::Path::cast) |
200 | } | 200 | } |
201 | |||
202 | pub fn as_single_segment(&self) -> Option<ast::PathSegment> { | ||
203 | match self.qualifier() { | ||
204 | Some(_) => None, | ||
205 | None => self.segment(), | ||
206 | } | ||
207 | } | ||
201 | } | 208 | } |
202 | 209 | ||
203 | impl ast::UseTreeList { | 210 | impl ast::UseTreeList { |
@@ -448,16 +455,22 @@ pub enum VisibilityKind { | |||
448 | 455 | ||
449 | impl ast::Visibility { | 456 | impl ast::Visibility { |
450 | pub fn kind(&self) -> VisibilityKind { | 457 | pub fn kind(&self) -> VisibilityKind { |
451 | if let Some(path) = support::children(self.syntax()).next() { | 458 | match self.path() { |
452 | VisibilityKind::In(path) | 459 | Some(path) => { |
453 | } else if self.crate_token().is_some() { | 460 | if let Some(segment) = |
454 | VisibilityKind::PubCrate | 461 | path.as_single_segment().filter(|it| it.coloncolon_token().is_none()) |
455 | } else if self.super_token().is_some() { | 462 | { |
456 | VisibilityKind::PubSuper | 463 | if segment.crate_token().is_some() { |
457 | } else if self.self_token().is_some() { | 464 | return VisibilityKind::PubCrate; |
458 | VisibilityKind::PubSelf | 465 | } else if segment.super_token().is_some() { |
459 | } else { | 466 | return VisibilityKind::PubSuper; |
460 | VisibilityKind::Pub | 467 | } else if segment.self_token().is_some() { |
468 | return VisibilityKind::PubSelf; | ||
469 | } | ||
470 | } | ||
471 | VisibilityKind::In(path) | ||
472 | } | ||
473 | None => VisibilityKind::Pub, | ||
461 | } | 474 | } |
462 | } | 475 | } |
463 | } | 476 | } |