From e52bdc55ef05bae8e647a5a0a9f8c3605c4cdd34 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 16 Feb 2021 19:27:08 +0100 Subject: Implement ast::AstNode for NameLike and move it to node_ext --- crates/syntax/src/ast/node_ext.rs | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'crates/syntax/src/ast') diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 307e150e9..2fa7b8c1e 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -297,6 +297,52 @@ impl ast::RecordExprField { } } +#[derive(Debug, Clone)] +pub enum NameLike { + NameRef(ast::NameRef), + Name(ast::Name), + Lifetime(ast::Lifetime), +} + +impl NameLike { + pub fn as_name_ref(&self) -> Option<&ast::NameRef> { + match self { + NameLike::NameRef(name_ref) => Some(name_ref), + _ => None, + } + } +} + +impl ast::AstNode for NameLike { + fn can_cast(kind: SyntaxKind) -> bool { + matches!(kind, SyntaxKind::NAME | SyntaxKind::NAME_REF | SyntaxKind::LIFETIME) + } + fn cast(syntax: SyntaxNode) -> Option { + let res = match syntax.kind() { + SyntaxKind::NAME => NameLike::Name(ast::Name { syntax }), + SyntaxKind::NAME_REF => NameLike::NameRef(ast::NameRef { syntax }), + SyntaxKind::LIFETIME => NameLike::Lifetime(ast::Lifetime { syntax }), + _ => return None, + }; + Some(res) + } + fn syntax(&self) -> &SyntaxNode { + match self { + NameLike::NameRef(it) => it.syntax(), + NameLike::Name(it) => it.syntax(), + NameLike::Lifetime(it) => it.syntax(), + } + } +} + +mod __ { + use super::{ + ast::{Lifetime, Name, NameRef}, + NameLike, + }; + stdx::impl_from!(NameRef, Name, Lifetime for NameLike); +} + #[derive(Debug, Clone, PartialEq)] pub enum NameOrNameRef { Name(ast::Name), -- cgit v1.2.3 From e1dbf43cf85f84c3a7e40f9731fc1f7ac96f8979 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 17 Feb 2021 14:02:34 +0100 Subject: Replace usage of ast::NameOrNameRef with ast::NameLike --- crates/syntax/src/ast/node_ext.rs | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) (limited to 'crates/syntax/src/ast') diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 2fa7b8c1e..c1f8101b2 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -297,7 +297,7 @@ impl ast::RecordExprField { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum NameLike { NameRef(ast::NameRef), Name(ast::Name), @@ -335,6 +335,16 @@ impl ast::AstNode for NameLike { } } +impl fmt::Display for NameLike { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + NameLike::Name(it) => fmt::Display::fmt(it, f), + NameLike::NameRef(it) => fmt::Display::fmt(it, f), + NameLike::Lifetime(it) => fmt::Display::fmt(it, f), + } + } +} + mod __ { use super::{ ast::{Lifetime, Name, NameRef}, @@ -343,26 +353,11 @@ mod __ { stdx::impl_from!(NameRef, Name, Lifetime for NameLike); } -#[derive(Debug, Clone, PartialEq)] -pub enum NameOrNameRef { - Name(ast::Name), - NameRef(ast::NameRef), -} - -impl fmt::Display for NameOrNameRef { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - NameOrNameRef::Name(it) => fmt::Display::fmt(it, f), - NameOrNameRef::NameRef(it) => fmt::Display::fmt(it, f), - } - } -} - impl ast::RecordPatField { pub fn for_field_name_ref(field_name: &ast::NameRef) -> Option { let candidate = field_name.syntax().parent().and_then(ast::RecordPatField::cast)?; match candidate.field_name()? { - NameOrNameRef::NameRef(name_ref) if name_ref == *field_name => Some(candidate), + NameLike::NameRef(name_ref) if name_ref == *field_name => Some(candidate), _ => None, } } @@ -371,19 +366,19 @@ impl ast::RecordPatField { let candidate = field_name.syntax().ancestors().nth(2).and_then(ast::RecordPatField::cast)?; match candidate.field_name()? { - NameOrNameRef::Name(name) if name == *field_name => Some(candidate), + NameLike::Name(name) if name == *field_name => Some(candidate), _ => None, } } /// Deals with field init shorthand - pub fn field_name(&self) -> Option { + pub fn field_name(&self) -> Option { if let Some(name_ref) = self.name_ref() { - return Some(NameOrNameRef::NameRef(name_ref)); + return Some(NameLike::NameRef(name_ref)); } if let Some(ast::Pat::IdentPat(pat)) = self.pat() { let name = pat.name()?; - return Some(NameOrNameRef::Name(name)); + return Some(NameLike::Name(name)); } None } -- cgit v1.2.3