From f3e297331c5c4b8c03810acc745ae5b4a9c4f71f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 15 Nov 2020 16:11:06 +0100 Subject: Cleanup edit_text_range_for_record_field_expr_or_pat --- crates/ide/src/references/rename.rs | 105 ++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 46 deletions(-) (limited to 'crates/ide/src/references') diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 449cfa4ae..b8725693a 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -126,6 +126,7 @@ fn source_edit_from_reference( TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) } ReferenceKind::RecordFieldExprOrPat => { + mark::hit!(test_rename_field_expr_pat); replacement_text.push_str(new_name); edit_text_range_for_record_field_expr_or_pat(sema, reference.file_range, new_name) } @@ -145,29 +146,27 @@ fn edit_text_range_for_record_field_expr_or_pat( file_range: FileRange, new_name: &str, ) -> TextRange { - let mut range = file_range.range; let source_file = sema.parse(file_range.file_id); let file_syntax = source_file.syntax(); - if let Some(field_expr) = - syntax::algo::find_node_at_range::(file_syntax, range) - { - match field_expr.expr().and_then(|e| e.name_ref()) { - Some(name) if &name.to_string() == new_name => range = field_expr.syntax().text_range(), - _ => (), - } - } else if let Some(field_pat) = - syntax::algo::find_node_at_range::(file_syntax, range) - { - match field_pat.pat() { - Some(ast::Pat::IdentPat(pat)) - if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) => - { - range = field_pat.syntax().text_range() - } - _ => (), - } - } - range + let original_range = file_range.range; + + syntax::algo::find_node_at_range::(file_syntax, original_range) + .and_then(|field_expr| match field_expr.expr().and_then(|e| e.name_ref()) { + Some(name) if &name.to_string() == new_name => Some(field_expr.syntax().text_range()), + _ => None, + }) + .or_else(|| { + syntax::algo::find_node_at_range::(file_syntax, original_range) + .and_then(|field_pat| match field_pat.pat() { + Some(ast::Pat::IdentPat(pat)) + if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) => + { + Some(field_pat.syntax().text_range()) + } + _ => None, + }) + }) + .unwrap_or(original_range) } fn rename_mod( @@ -1140,6 +1139,7 @@ impl Foo { #[test] fn test_initializer_use_field_init_shorthand() { + mark::check!(test_rename_field_expr_pat); check( "bar", r#" @@ -1160,27 +1160,23 @@ fn foo(bar: i32) -> Foo { } #[test] - fn test_rename_binding_in_destructure_pat_shorthand() { + fn test_struct_field_destructure_into_shorthand() { check( - "bar", + "baz", r#" -struct Foo { - i: i32, -} +struct Foo { i<|>: i32 } fn foo(foo: Foo) { - let Foo { i } = foo; - let _ = i<|>; + let Foo { i: baz } = foo; + let _ = baz; } "#, r#" -struct Foo { - i: i32, -} +struct Foo { baz: i32 } fn foo(foo: Foo) { - let Foo { i: bar } = foo; - let _ = bar; + let Foo { baz } = foo; + let _ = baz; } "#, ); @@ -1188,6 +1184,16 @@ fn foo(foo: Foo) { #[test] fn test_rename_binding_in_destructure_pat() { + let expected_fixture = r#" +struct Foo { + i: i32, +} + +fn foo(foo: Foo) { + let Foo { i: bar } = foo; + let _ = bar; +} +"#; check( "bar", r#" @@ -1200,39 +1206,46 @@ fn foo(foo: Foo) { let _ = b<|>; } "#, + expected_fixture, + ); + check( + "bar", r#" struct Foo { i: i32, } fn foo(foo: Foo) { - let Foo { i: bar } = foo; - let _ = bar; + let Foo { i } = foo; + let _ = i<|>; } "#, + expected_fixture, ); } #[test] - fn test_struct_field_destructure_into_shorthand() { + fn test_rename_binding_in_destructure_param_pat() { check( - "baz", + "bar", r#" -struct Foo { i<|>: i32 } +struct Foo { + i: i32 +} -fn foo(foo: Foo) { - let Foo { i: baz } = foo; - let _ = baz; +fn foo(Foo { i }: foo) -> i32 { + i<|> } "#, r#" -struct Foo { baz: i32 } +struct Foo { + i: i32 +} -fn foo(foo: Foo) { - let Foo { baz } = foo; - let _ = baz; +fn foo(Foo { i: bar }: foo) -> i32 { + bar } "#, - ); + ) } } -- cgit v1.2.3