From cb863390f23bc2eac6561d55def9bd3ba54605fc Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 15 Jan 2021 18:57:32 +0100 Subject: Handle self/super/crate in PathSegment as NameRef --- crates/syntax/src/ast/node_ext.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'crates/syntax/src/ast/node_ext.rs') 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 { .expect("segments are always nested in paths") } + pub fn crate_token(&self) -> Option { + self.name_ref().and_then(|it| it.crate_token()) + } + + pub fn self_token(&self) -> Option { + self.name_ref().and_then(|it| it.self_token()) + } + + pub fn super_token(&self) -> Option { + self.name_ref().and_then(|it| it.super_token()) + } + pub fn kind(&self) -> Option { let res = if let Some(name_ref) = self.name_ref() { - PathSegmentKind::Name(name_ref) + match name_ref.syntax().first_token().map(|it| it.kind()) { + Some(T![self]) => PathSegmentKind::SelfKw, + Some(T![super]) => PathSegmentKind::SuperKw, + Some(T![crate]) => PathSegmentKind::CrateKw, + _ => PathSegmentKind::Name(name_ref), + } } else { match self.syntax().first_child_or_token()?.kind() { - T![self] => PathSegmentKind::SelfKw, - T![super] => PathSegmentKind::SuperKw, - T![crate] => PathSegmentKind::CrateKw, T![<] => { // or // T is any TypeRef, Trait has to be a PathType -- cgit v1.2.3