diff options
Diffstat (limited to 'crates/syntax')
-rw-r--r-- | crates/syntax/src/ast.rs | 2 | ||||
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 46 |
2 files changed, 47 insertions, 1 deletions
diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs index a25ff655e..b3a24d39d 100644 --- a/crates/syntax/src/ast.rs +++ b/crates/syntax/src/ast.rs | |||
@@ -19,7 +19,7 @@ pub use self::{ | |||
19 | expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp}, | 19 | expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp}, |
20 | generated::{nodes::*, tokens::*}, | 20 | generated::{nodes::*, tokens::*}, |
21 | node_ext::{ | 21 | node_ext::{ |
22 | AttrKind, FieldKind, Macro, NameOrNameRef, PathSegmentKind, SelfParamKind, | 22 | AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind, |
23 | SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind, | 23 | SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind, |
24 | }, | 24 | }, |
25 | token_ext::*, | 25 | token_ext::*, |
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 { | |||
297 | } | 297 | } |
298 | } | 298 | } |
299 | 299 | ||
300 | #[derive(Debug, Clone)] | ||
301 | pub enum NameLike { | ||
302 | NameRef(ast::NameRef), | ||
303 | Name(ast::Name), | ||
304 | Lifetime(ast::Lifetime), | ||
305 | } | ||
306 | |||
307 | impl NameLike { | ||
308 | pub fn as_name_ref(&self) -> Option<&ast::NameRef> { | ||
309 | match self { | ||
310 | NameLike::NameRef(name_ref) => Some(name_ref), | ||
311 | _ => None, | ||
312 | } | ||
313 | } | ||
314 | } | ||
315 | |||
316 | impl ast::AstNode for NameLike { | ||
317 | fn can_cast(kind: SyntaxKind) -> bool { | ||
318 | matches!(kind, SyntaxKind::NAME | SyntaxKind::NAME_REF | SyntaxKind::LIFETIME) | ||
319 | } | ||
320 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
321 | let res = match syntax.kind() { | ||
322 | SyntaxKind::NAME => NameLike::Name(ast::Name { syntax }), | ||
323 | SyntaxKind::NAME_REF => NameLike::NameRef(ast::NameRef { syntax }), | ||
324 | SyntaxKind::LIFETIME => NameLike::Lifetime(ast::Lifetime { syntax }), | ||
325 | _ => return None, | ||
326 | }; | ||
327 | Some(res) | ||
328 | } | ||
329 | fn syntax(&self) -> &SyntaxNode { | ||
330 | match self { | ||
331 | NameLike::NameRef(it) => it.syntax(), | ||
332 | NameLike::Name(it) => it.syntax(), | ||
333 | NameLike::Lifetime(it) => it.syntax(), | ||
334 | } | ||
335 | } | ||
336 | } | ||
337 | |||
338 | mod __ { | ||
339 | use super::{ | ||
340 | ast::{Lifetime, Name, NameRef}, | ||
341 | NameLike, | ||
342 | }; | ||
343 | stdx::impl_from!(NameRef, Name, Lifetime for NameLike); | ||
344 | } | ||
345 | |||
300 | #[derive(Debug, Clone, PartialEq)] | 346 | #[derive(Debug, Clone, PartialEq)] |
301 | pub enum NameOrNameRef { | 347 | pub enum NameOrNameRef { |
302 | Name(ast::Name), | 348 | Name(ast::Name), |