From 98718e0544f42e55642d2838b00d6a7bef1e2414 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 15 Jan 2021 21:07:38 +0100 Subject: Wrap remaining self/super/crate in Name{Ref} --- crates/syntax/src/ast/generated/nodes.rs | 7 ++----- crates/syntax/src/ast/node_ext.rs | 33 ++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 15 deletions(-) (limited to 'crates/syntax/src/ast') diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs index 1d722db3c..6407d7c85 100644 --- a/crates/syntax/src/ast/generated/nodes.rs +++ b/crates/syntax/src/ast/generated/nodes.rs @@ -11,6 +11,7 @@ pub struct Name { } impl Name { pub fn ident_token(&self) -> Option { support::token(&self.syntax, T![ident]) } + pub fn self_token(&self) -> Option { support::token(&self.syntax, T![self]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct NameRef { @@ -238,7 +239,6 @@ impl ExternCrate { pub fn extern_token(&self) -> Option { support::token(&self.syntax, T![extern]) } pub fn crate_token(&self) -> Option { support::token(&self.syntax, T![crate]) } pub fn name_ref(&self) -> Option { support::child(&self.syntax) } - pub fn self_token(&self) -> Option { support::token(&self.syntax, T![self]) } pub fn rename(&self) -> Option { support::child(&self.syntax) } pub fn semicolon_token(&self) -> Option { support::token(&self.syntax, T![;]) } } @@ -406,9 +406,6 @@ pub struct Visibility { impl Visibility { pub fn pub_token(&self) -> Option { support::token(&self.syntax, T![pub]) } pub fn l_paren_token(&self) -> Option { support::token(&self.syntax, T!['(']) } - pub fn super_token(&self) -> Option { support::token(&self.syntax, T![super]) } - pub fn self_token(&self) -> Option { support::token(&self.syntax, T![self]) } - pub fn crate_token(&self) -> Option { support::token(&self.syntax, T![crate]) } pub fn in_token(&self) -> Option { support::token(&self.syntax, T![in]) } pub fn path(&self) -> Option { support::child(&self.syntax) } pub fn r_paren_token(&self) -> Option { support::token(&self.syntax, T![')']) } @@ -492,11 +489,11 @@ pub struct SelfParam { pub(crate) syntax: SyntaxNode, } impl ast::AttrsOwner for SelfParam {} +impl ast::NameOwner for SelfParam {} impl SelfParam { pub fn amp_token(&self) -> Option { support::token(&self.syntax, T![&]) } pub fn lifetime(&self) -> Option { support::child(&self.syntax) } pub fn mut_token(&self) -> Option { support::token(&self.syntax, T![mut]) } - pub fn self_token(&self) -> Option { support::token(&self.syntax, T![self]) } pub fn colon_token(&self) -> Option { support::token(&self.syntax, T![:]) } pub fn ty(&self) -> Option { support::child(&self.syntax) } } diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index b8ce71d27..738c92a5b 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -198,6 +198,13 @@ impl ast::Path { pub fn parent_path(&self) -> Option { self.syntax().parent().and_then(ast::Path::cast) } + + pub fn as_single_segment(&self) -> Option { + match self.qualifier() { + Some(_) => None, + None => self.segment(), + } + } } impl ast::UseTreeList { @@ -448,16 +455,22 @@ pub enum VisibilityKind { impl ast::Visibility { pub fn kind(&self) -> VisibilityKind { - if let Some(path) = support::children(self.syntax()).next() { - VisibilityKind::In(path) - } else if self.crate_token().is_some() { - VisibilityKind::PubCrate - } else if self.super_token().is_some() { - VisibilityKind::PubSuper - } else if self.self_token().is_some() { - VisibilityKind::PubSelf - } else { - VisibilityKind::Pub + match self.path() { + Some(path) => { + if let Some(segment) = + path.as_single_segment().filter(|it| it.coloncolon_token().is_none()) + { + if segment.crate_token().is_some() { + return VisibilityKind::PubCrate; + } else if segment.super_token().is_some() { + return VisibilityKind::PubSuper; + } else if segment.self_token().is_some() { + return VisibilityKind::PubSelf; + } + } + VisibilityKind::In(path) + } + None => VisibilityKind::Pub, } } } -- cgit v1.2.3