aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/hir_expand/src/name.rs13
-rw-r--r--crates/syntax/src/ast.rs4
-rw-r--r--crates/syntax/src/ast/node_ext.rs37
3 files changed, 26 insertions, 28 deletions
diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs
index 74cf64aab..c7609e90d 100644
--- a/crates/hir_expand/src/name.rs
+++ b/crates/hir_expand/src/name.rs
@@ -87,18 +87,11 @@ impl AsName for ast::Name {
87 } 87 }
88} 88}
89 89
90impl AsName for ast::Lifetime { 90impl AsName for ast::NameOrNameRef {
91 fn as_name(&self) -> Name {
92 Name::resolve(self.text())
93 }
94}
95
96impl AsName for ast::NameLike {
97 fn as_name(&self) -> Name { 91 fn as_name(&self) -> Name {
98 match self { 92 match self {
99 ast::NameLike::Name(it) => it.as_name(), 93 ast::NameOrNameRef::Name(it) => it.as_name(),
100 ast::NameLike::NameRef(it) => it.as_name(), 94 ast::NameOrNameRef::NameRef(it) => it.as_name(),
101 ast::NameLike::Lifetime(it) => it.as_name(),
102 } 95 }
103 } 96 }
104} 97}
diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs
index 72214a4f0..b3a24d39d 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, PathSegmentKind, SelfParamKind, SlicePatComponents, 22 AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind,
23 StructKind, TypeBoundKind, VisibilityKind, 23 SlicePatComponents, 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 c1f8101b2..2fa7b8c1e 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, PartialEq)] 300#[derive(Debug, Clone)]
301pub enum NameLike { 301pub enum NameLike {
302 NameRef(ast::NameRef), 302 NameRef(ast::NameRef),
303 Name(ast::Name), 303 Name(ast::Name),
@@ -335,16 +335,6 @@ impl ast::AstNode for NameLike {
335 } 335 }
336} 336}
337 337
338impl 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
348mod __ { 338mod __ {
349 use super::{ 339 use super::{
350 ast::{Lifetime, Name, NameRef}, 340 ast::{Lifetime, Name, NameRef},
@@ -353,11 +343,26 @@ mod __ {
353 stdx::impl_from!(NameRef, Name, Lifetime for NameLike); 343 stdx::impl_from!(NameRef, Name, Lifetime for NameLike);
354} 344}
355 345
346#[derive(Debug, Clone, PartialEq)]
347pub enum NameOrNameRef {
348 Name(ast::Name),
349 NameRef(ast::NameRef),
350}
351
352impl 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
356impl ast::RecordPatField { 361impl ast::RecordPatField {
357 pub fn for_field_name_ref(field_name: &ast::NameRef) -> Option<ast::RecordPatField> { 362 pub fn for_field_name_ref(field_name: &ast::NameRef) -> Option<ast::RecordPatField> {
358 let candidate = field_name.syntax().parent().and_then(ast::RecordPatField::cast)?; 363 let candidate = field_name.syntax().parent().and_then(ast::RecordPatField::cast)?;
359 match candidate.field_name()? { 364 match candidate.field_name()? {
360 NameLike::NameRef(name_ref) if name_ref == *field_name => Some(candidate), 365 NameOrNameRef::NameRef(name_ref) if name_ref == *field_name => Some(candidate),
361 _ => None, 366 _ => None,
362 } 367 }
363 } 368 }
@@ -366,19 +371,19 @@ impl ast::RecordPatField {
366 let candidate = 371 let candidate =
367 field_name.syntax().ancestors().nth(2).and_then(ast::RecordPatField::cast)?; 372 field_name.syntax().ancestors().nth(2).and_then(ast::RecordPatField::cast)?;
368 match candidate.field_name()? { 373 match candidate.field_name()? {
369 NameLike::Name(name) if name == *field_name => Some(candidate), 374 NameOrNameRef::Name(name) if name == *field_name => Some(candidate),
370 _ => None, 375 _ => None,
371 } 376 }
372 } 377 }
373 378
374 /// Deals with field init shorthand 379 /// Deals with field init shorthand
375 pub fn field_name(&self) -> Option<NameLike> { 380 pub fn field_name(&self) -> Option<NameOrNameRef> {
376 if let Some(name_ref) = self.name_ref() { 381 if let Some(name_ref) = self.name_ref() {
377 return Some(NameLike::NameRef(name_ref)); 382 return Some(NameOrNameRef::NameRef(name_ref));
378 } 383 }
379 if let Some(ast::Pat::IdentPat(pat)) = self.pat() { 384 if let Some(ast::Pat::IdentPat(pat)) = self.pat() {
380 let name = pat.name()?; 385 let name = pat.name()?;
381 return Some(NameLike::Name(name)); 386 return Some(NameOrNameRef::Name(name));
382 } 387 }
383 None 388 None
384 } 389 }