diff options
author | Lukas Wirth <[email protected]> | 2021-02-17 13:02:34 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-02-17 13:02:34 +0000 |
commit | e1dbf43cf85f84c3a7e40f9731fc1f7ac96f8979 (patch) | |
tree | 98ecf23f95ea818cedea1931609a2610cefbf737 /crates/syntax/src | |
parent | e52bdc55ef05bae8e647a5a0a9f8c3605c4cdd34 (diff) |
Replace usage of ast::NameOrNameRef with ast::NameLike
Diffstat (limited to 'crates/syntax/src')
-rw-r--r-- | crates/syntax/src/ast.rs | 4 | ||||
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 37 |
2 files changed, 18 insertions, 23 deletions
diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs index b3a24d39d..72214a4f0 100644 --- a/crates/syntax/src/ast.rs +++ b/crates/syntax/src/ast.rs | |||
@@ -19,8 +19,8 @@ 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, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind, | 22 | AttrKind, FieldKind, Macro, NameLike, PathSegmentKind, SelfParamKind, SlicePatComponents, |
23 | SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind, | 23 | StructKind, TypeBoundKind, VisibilityKind, |
24 | }, | 24 | }, |
25 | token_ext::*, | 25 | token_ext::*, |
26 | traits::*, | 26 | traits::*, |
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 { | |||
297 | } | 297 | } |
298 | } | 298 | } |
299 | 299 | ||
300 | #[derive(Debug, Clone)] | 300 | #[derive(Debug, Clone, PartialEq)] |
301 | pub enum NameLike { | 301 | pub enum NameLike { |
302 | NameRef(ast::NameRef), | 302 | NameRef(ast::NameRef), |
303 | Name(ast::Name), | 303 | Name(ast::Name), |
@@ -335,6 +335,16 @@ impl ast::AstNode for NameLike { | |||
335 | } | 335 | } |
336 | } | 336 | } |
337 | 337 | ||
338 | impl fmt::Display for NameLike { | ||
339 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
340 | match self { | ||
341 | NameLike::Name(it) => fmt::Display::fmt(it, f), | ||
342 | NameLike::NameRef(it) => fmt::Display::fmt(it, f), | ||
343 | NameLike::Lifetime(it) => fmt::Display::fmt(it, f), | ||
344 | } | ||
345 | } | ||
346 | } | ||
347 | |||
338 | mod __ { | 348 | mod __ { |
339 | use super::{ | 349 | use super::{ |
340 | ast::{Lifetime, Name, NameRef}, | 350 | ast::{Lifetime, Name, NameRef}, |
@@ -343,26 +353,11 @@ mod __ { | |||
343 | stdx::impl_from!(NameRef, Name, Lifetime for NameLike); | 353 | stdx::impl_from!(NameRef, Name, Lifetime for NameLike); |
344 | } | 354 | } |
345 | 355 | ||
346 | #[derive(Debug, Clone, PartialEq)] | ||
347 | pub enum NameOrNameRef { | ||
348 | Name(ast::Name), | ||
349 | NameRef(ast::NameRef), | ||
350 | } | ||
351 | |||
352 | impl fmt::Display for NameOrNameRef { | ||
353 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
354 | match self { | ||
355 | NameOrNameRef::Name(it) => fmt::Display::fmt(it, f), | ||
356 | NameOrNameRef::NameRef(it) => fmt::Display::fmt(it, f), | ||
357 | } | ||
358 | } | ||
359 | } | ||
360 | |||
361 | impl ast::RecordPatField { | 356 | impl ast::RecordPatField { |
362 | pub fn for_field_name_ref(field_name: &ast::NameRef) -> Option<ast::RecordPatField> { | 357 | pub fn for_field_name_ref(field_name: &ast::NameRef) -> Option<ast::RecordPatField> { |
363 | let candidate = field_name.syntax().parent().and_then(ast::RecordPatField::cast)?; | 358 | let candidate = field_name.syntax().parent().and_then(ast::RecordPatField::cast)?; |
364 | match candidate.field_name()? { | 359 | match candidate.field_name()? { |
365 | NameOrNameRef::NameRef(name_ref) if name_ref == *field_name => Some(candidate), | 360 | NameLike::NameRef(name_ref) if name_ref == *field_name => Some(candidate), |
366 | _ => None, | 361 | _ => None, |
367 | } | 362 | } |
368 | } | 363 | } |
@@ -371,19 +366,19 @@ impl ast::RecordPatField { | |||
371 | let candidate = | 366 | let candidate = |
372 | field_name.syntax().ancestors().nth(2).and_then(ast::RecordPatField::cast)?; | 367 | field_name.syntax().ancestors().nth(2).and_then(ast::RecordPatField::cast)?; |
373 | match candidate.field_name()? { | 368 | match candidate.field_name()? { |
374 | NameOrNameRef::Name(name) if name == *field_name => Some(candidate), | 369 | NameLike::Name(name) if name == *field_name => Some(candidate), |
375 | _ => None, | 370 | _ => None, |
376 | } | 371 | } |
377 | } | 372 | } |
378 | 373 | ||
379 | /// Deals with field init shorthand | 374 | /// Deals with field init shorthand |
380 | pub fn field_name(&self) -> Option<NameOrNameRef> { | 375 | pub fn field_name(&self) -> Option<NameLike> { |
381 | if let Some(name_ref) = self.name_ref() { | 376 | if let Some(name_ref) = self.name_ref() { |
382 | return Some(NameOrNameRef::NameRef(name_ref)); | 377 | return Some(NameLike::NameRef(name_ref)); |
383 | } | 378 | } |
384 | if let Some(ast::Pat::IdentPat(pat)) = self.pat() { | 379 | if let Some(ast::Pat::IdentPat(pat)) = self.pat() { |
385 | let name = pat.name()?; | 380 | let name = pat.name()?; |
386 | return Some(NameOrNameRef::Name(name)); | 381 | return Some(NameLike::Name(name)); |
387 | } | 382 | } |
388 | None | 383 | None |
389 | } | 384 | } |