diff options
author | Lukas Wirth <[email protected]> | 2021-01-15 17:57:32 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-01-15 18:21:23 +0000 |
commit | cb863390f23bc2eac6561d55def9bd3ba54605fc (patch) | |
tree | b19b39d9b6231e8857a4096cc803cf35e2ddbe81 /crates/syntax/src/ast/node_ext.rs | |
parent | 0c58aa9dc0e24f0fa6a6ee7eb0c35041dedddb0a (diff) |
Handle self/super/crate in PathSegment as NameRef
Diffstat (limited to 'crates/syntax/src/ast/node_ext.rs')
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 27381ba80..b8ce71d27 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs | |||
@@ -156,14 +156,28 @@ impl ast::PathSegment { | |||
156 | .expect("segments are always nested in paths") | 156 | .expect("segments are always nested in paths") |
157 | } | 157 | } |
158 | 158 | ||
159 | pub fn crate_token(&self) -> Option<SyntaxToken> { | ||
160 | self.name_ref().and_then(|it| it.crate_token()) | ||
161 | } | ||
162 | |||
163 | pub fn self_token(&self) -> Option<SyntaxToken> { | ||
164 | self.name_ref().and_then(|it| it.self_token()) | ||
165 | } | ||
166 | |||
167 | pub fn super_token(&self) -> Option<SyntaxToken> { | ||
168 | self.name_ref().and_then(|it| it.super_token()) | ||
169 | } | ||
170 | |||
159 | pub fn kind(&self) -> Option<PathSegmentKind> { | 171 | pub fn kind(&self) -> Option<PathSegmentKind> { |
160 | let res = if let Some(name_ref) = self.name_ref() { | 172 | let res = if let Some(name_ref) = self.name_ref() { |
161 | PathSegmentKind::Name(name_ref) | 173 | match name_ref.syntax().first_token().map(|it| it.kind()) { |
174 | Some(T![self]) => PathSegmentKind::SelfKw, | ||
175 | Some(T![super]) => PathSegmentKind::SuperKw, | ||
176 | Some(T![crate]) => PathSegmentKind::CrateKw, | ||
177 | _ => PathSegmentKind::Name(name_ref), | ||
178 | } | ||
162 | } else { | 179 | } else { |
163 | match self.syntax().first_child_or_token()?.kind() { | 180 | match self.syntax().first_child_or_token()?.kind() { |
164 | T![self] => PathSegmentKind::SelfKw, | ||
165 | T![super] => PathSegmentKind::SuperKw, | ||
166 | T![crate] => PathSegmentKind::CrateKw, | ||
167 | T![<] => { | 181 | T![<] => { |
168 | // <T> or <T as Trait> | 182 | // <T> or <T as Trait> |
169 | // T is any TypeRef, Trait has to be a PathType | 183 | // T is any TypeRef, Trait has to be a PathType |