From cba4dd1a8ac8e0dcd8a82efc1a490e4676340388 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 22 Mar 2020 19:53:28 +0800 Subject: Improve find_all_ref work inside macro --- crates/ra_ide/src/references.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index 3ea0ac230..746cc86ba 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -94,12 +94,16 @@ pub(crate) fn find_all_refs( let sema = Semantics::new(db); let syntax = sema.parse(position.file_id).syntax().clone(); - let (opt_name, search_kind) = - if let Some(name) = get_struct_def_name_for_struct_literal_search(&syntax, position) { - (Some(name), ReferenceKind::StructLiteral) - } else { - (find_node_at_offset::(&syntax, position.offset), ReferenceKind::Other) - }; + let (opt_name, search_kind) = if let Some(name) = + get_struct_def_name_for_struct_literal_search(&sema, &syntax, position) + { + (Some(name), ReferenceKind::StructLiteral) + } else { + ( + sema.find_node_at_offset_with_descend::(&syntax, position.offset), + ReferenceKind::Other, + ) + }; let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?; @@ -131,7 +135,8 @@ fn find_name( let range = name.syntax().text_range(); return Some(RangeInfo::new(range, def)); } - let name_ref = find_node_at_offset::(&syntax, position.offset)?; + let name_ref = + sema.find_node_at_offset_with_descend::(&syntax, position.offset)?; let def = classify_name_ref(sema, &name_ref)?.definition(); let range = name_ref.syntax().text_range(); Some(RangeInfo::new(range, def)) @@ -157,6 +162,7 @@ fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Optio } fn get_struct_def_name_for_struct_literal_search( + sema: &Semantics, syntax: &SyntaxNode, position: FilePosition, ) -> Option { @@ -164,10 +170,18 @@ fn get_struct_def_name_for_struct_literal_search( if right.kind() != SyntaxKind::L_CURLY && right.kind() != SyntaxKind::L_PAREN { return None; } - if let Some(name) = find_node_at_offset::(&syntax, left.text_range().start()) { + if let Some(name) = + sema.find_node_at_offset_with_descend::(&syntax, left.text_range().start()) + { return name.syntax().ancestors().find_map(ast::StructDef::cast).and_then(|l| l.name()); } - if find_node_at_offset::(&syntax, left.text_range().start()).is_some() { + if sema + .find_node_at_offset_with_descend::( + &syntax, + left.text_range().start(), + ) + .is_some() + { return left.ancestors().find_map(ast::StructDef::cast).and_then(|l| l.name()); } } -- cgit v1.2.3 From 6d5443ef945c415f21ced1594003e8e6eed5e44a Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 22 Mar 2020 19:53:34 +0800 Subject: Add test --- crates/ra_ide/src/references/rename.rs | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index 7d1190af9..9acc6158a 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -249,6 +249,63 @@ mod tests { ); } + #[test] + fn test_rename_for_macro_args_rev() { + test_rename( + r#" + macro_rules! foo {($i:ident) => {$i} } + fn main() { + let a = "test"; + foo!(a<|>); + }"#, + "b", + r#" + macro_rules! foo {($i:ident) => {$i} } + fn main() { + let b = "test"; + foo!(b); + }"#, + ); + } + + #[test] + fn test_rename_for_macro_define_fn() { + test_rename( + r#" + macro_rules! define_fn {($id:ident) => { fn $id{} }} + define_fn!(foo); + fn main() { + fo<|>o(); + }"#, + "bar", + r#" + macro_rules! define_fn {($id:ident) => { fn $id{} }} + define_fn!(bar); + fn main() { + bar(); + }"#, + ); + } + + #[test] + fn test_rename_for_macro_define_fn_rev() { + test_rename( + r#" + macro_rules! define_fn {($id:ident) => { fn $id{} }} + define_fn!(fo<|>o); + fn main() { + foo(); + }"#, + "bar", + r#" + macro_rules! define_fn {($id:ident) => { fn $id{} }} + define_fn!(bar); + fn main() { + bar(); + }"#, + ); + } + #[test] fn test_rename_for_param_inside() { test_rename( -- cgit v1.2.3