From e55a44a831477e2fc8e11340c3d91db883b97c8e Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 14 Nov 2020 17:49:36 +0100 Subject: Use shorthand record syntax when renaming struct initializer field --- crates/ide_db/src/search.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'crates/ide_db/src/search.rs') diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index a24335240..4248606c8 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -30,6 +30,7 @@ pub enum ReferenceKind { FieldShorthandForField, FieldShorthandForLocal, StructLiteral, + RecordExprField, Other, } @@ -278,12 +279,15 @@ impl<'a> FindUsages<'a> { ) -> bool { match NameRefClass::classify(self.sema, &name_ref) { Some(NameRefClass::Definition(def)) if &def == self.def => { - let kind = if is_record_lit_name_ref(&name_ref) || is_call_expr_name_ref(&name_ref) - { - ReferenceKind::StructLiteral - } else { - ReferenceKind::Other - }; + let kind = + if name_ref.syntax().parent().and_then(ast::RecordExprField::cast).is_some() { + ReferenceKind::RecordExprField + } else if is_record_lit_name_ref(&name_ref) || is_call_expr_name_ref(&name_ref) + { + ReferenceKind::StructLiteral + } else { + ReferenceKind::Other + }; let reference = Reference { file_range: self.sema.original_range(name_ref.syntax()), -- cgit v1.2.3 From cb60708274dc7c8cff281364507d23047cd482cd Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 14 Nov 2020 19:57:47 +0100 Subject: Use shorthand field syntax in destructures --- crates/ide_db/src/search.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'crates/ide_db/src/search.rs') diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 4248606c8..a3e765d05 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -30,7 +30,7 @@ pub enum ReferenceKind { FieldShorthandForField, FieldShorthandForLocal, StructLiteral, - RecordExprField, + RecordFieldExprOrPat, Other, } @@ -279,15 +279,13 @@ impl<'a> FindUsages<'a> { ) -> bool { match NameRefClass::classify(self.sema, &name_ref) { Some(NameRefClass::Definition(def)) if &def == self.def => { - let kind = - if name_ref.syntax().parent().and_then(ast::RecordExprField::cast).is_some() { - ReferenceKind::RecordExprField - } else if is_record_lit_name_ref(&name_ref) || is_call_expr_name_ref(&name_ref) - { - ReferenceKind::StructLiteral - } else { - ReferenceKind::Other - }; + let kind = if is_record_field_expr_or_pat(&name_ref) { + ReferenceKind::RecordFieldExprOrPat + } else if is_record_lit_name_ref(&name_ref) || is_call_expr_name_ref(&name_ref) { + ReferenceKind::StructLiteral + } else { + ReferenceKind::Other + }; let reference = Reference { file_range: self.sema.original_range(name_ref.syntax()), @@ -389,3 +387,17 @@ fn is_record_lit_name_ref(name_ref: &ast::NameRef) -> bool { .map(|p| p.name_ref().as_ref() == Some(name_ref)) .unwrap_or(false) } + +fn is_record_field_expr_or_pat(name_ref: &ast::NameRef) -> bool { + if let Some(parent) = name_ref.syntax().parent() { + match_ast! { + match parent { + ast::RecordExprField(it) => true, + ast::RecordPatField(_it) => true, + _ => false, + } + } + } else { + false + } +} -- cgit v1.2.3