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/src/references/rename.rs | 49 ++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 4 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 26ac2371a..b3ade20ef 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -106,7 +106,11 @@ fn find_module_at_offset( Some(module) } -fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFileEdit { +fn source_edit_from_reference( + sema: &Semantics, + reference: Reference, + new_name: &str, +) -> SourceFileEdit { let mut replacement_text = String::new(); let file_id = reference.file_range.file_id; let range = match reference.kind { @@ -122,6 +126,22 @@ fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFil replacement_text.push_str(new_name); TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) } + ReferenceKind::RecordExprField => { + replacement_text.push_str(new_name); + let mut range = reference.file_range.range; + if let Some(field_expr) = syntax::algo::find_node_at_range::( + sema.parse(file_id).syntax(), + reference.file_range.range, + ) { + // use shorthand initializer if we were to write foo: foo + if let Some(name) = field_expr.expr().and_then(|e| e.name_ref()) { + if &name.to_string() == new_name { + range = field_expr.syntax().text_range(); + } + } + } + range + } _ => { replacement_text.push_str(new_name); reference.file_range.range @@ -170,7 +190,7 @@ fn rename_mod( let ref_edits = refs .references .into_iter() - .map(|reference| source_edit_from_reference(reference, new_name)); + .map(|reference| source_edit_from_reference(sema, reference, new_name)); source_file_edits.extend(ref_edits); Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits))) @@ -211,7 +231,7 @@ fn rename_to_self( let mut edits = usages .into_iter() - .map(|reference| source_edit_from_reference(reference, "self")) + .map(|reference| source_edit_from_reference(sema, reference, "self")) .collect::>(); edits.push(SourceFileEdit { @@ -300,7 +320,7 @@ fn rename_reference( let edit = refs .into_iter() - .map(|reference| source_edit_from_reference(reference, new_name)) + .map(|reference| source_edit_from_reference(sema, reference, new_name)) .collect::>(); if edit.is_empty() { @@ -1094,6 +1114,27 @@ impl Foo { foo.i } } +"#, + ); + } + + #[test] + fn test_initializer_use_field_init_shorthand() { + check( + "bar", + r#" +struct Foo { i<|>: i32 } + +fn foo(bar: i32) -> Foo { + Foo { i: bar } +} +"#, + r#" +struct Foo { bar: i32 } + +fn foo(bar: i32) -> Foo { + Foo { bar } +} "#, ); } -- cgit v1.2.3 From 924eecf4af4d57c597c2e77c5e58c22b2a37bdb6 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 14 Nov 2020 19:11:09 +0100 Subject: Properly handle shorthands in destructure patterns when renaming --- crates/ide/src/references.rs | 21 ++++++++++----- crates/ide/src/references/rename.rs | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index e05465b32..10cd42032 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -110,14 +110,23 @@ pub(crate) fn find_all_refs( .filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind) .collect(); - let decl_range = def.try_to_nav(sema.db)?.focus_or_full_range(); - - let declaration = Declaration { - nav: def.try_to_nav(sema.db)?, - kind: ReferenceKind::Other, - access: decl_access(&def, &syntax, decl_range), + let nav = def.try_to_nav(sema.db)?; + let decl_range = nav.focus_or_full_range(); + + let mut kind = ReferenceKind::Other; + if let Definition::Local(local) = def { + if let either::Either::Left(pat) = local.source(sema.db).value { + if matches!( + pat.syntax().parent().and_then(ast::RecordPatField::cast), + Some(pat_field) if pat_field.name_ref().is_none() + ) { + kind = ReferenceKind::FieldShorthandForLocal; + } + } }; + let declaration = Declaration { nav, kind, access: decl_access(&def, &syntax, decl_range) }; + Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references })) } diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index b3ade20ef..bc4aa25bf 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -1135,6 +1135,60 @@ struct Foo { bar: i32 } fn foo(bar: i32) -> Foo { Foo { bar } } +"#, + ); + } + + #[test] + fn test_rename_binding_in_destructure_pat_shorthand() { + check( + "bar", + r#" +struct Foo { + i: i32, +} + +fn foo(foo: Foo) { + let Foo { i } = foo; + let _ = i<|>; +} +"#, + r#" +struct Foo { + i: i32, +} + +fn foo(foo: Foo) { + let Foo { i: bar } = foo; + let _ = bar; +} +"#, + ); + } + + #[test] + fn test_rename_binding_in_destructure_pat() { + check( + "bar", + r#" +struct Foo { + i: i32, +} + +fn foo(foo: Foo) { + let Foo { i: b } = foo; + let _ = b<|>; +} +"#, + r#" +struct Foo { + i: i32, +} + +fn foo(foo: Foo) { + let Foo { i: bar } = foo; + let _ = bar; +} "#, ); } -- 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/src/references.rs | 4 +- crates/ide/src/references/rename.rs | 77 +++++++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 19 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 10cd42032..5693dd400 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -622,7 +622,7 @@ fn foo() { expect![[r#" f RECORD_FIELD FileId(0) 15..21 15..16 Other - FileId(0) 55..56 Other Read + FileId(0) 55..56 RecordFieldExprOrPat Read FileId(0) 68..69 Other Write "#]], ); @@ -757,7 +757,7 @@ fn f() -> m::En { expect![[r#" field RECORD_FIELD FileId(0) 56..65 56..61 Other - FileId(0) 125..130 Other Read + FileId(0) 125..130 RecordFieldExprOrPat Read "#]], ); } diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index bc4aa25bf..449cfa4ae 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir::{Module, ModuleDef, ModuleSource, Semantics}; -use ide_db::base_db::SourceDatabaseExt; +use ide_db::base_db::{FileRange, SourceDatabaseExt}; use ide_db::{ defs::{Definition, NameClass, NameRefClass}, RootDatabase, @@ -112,7 +112,6 @@ fn source_edit_from_reference( new_name: &str, ) -> SourceFileEdit { let mut replacement_text = String::new(); - let file_id = reference.file_range.file_id; let range = match reference.kind { ReferenceKind::FieldShorthandForField => { mark::hit!(test_rename_struct_field_for_shorthand); @@ -126,28 +125,49 @@ fn source_edit_from_reference( replacement_text.push_str(new_name); TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) } - ReferenceKind::RecordExprField => { + ReferenceKind::RecordFieldExprOrPat => { replacement_text.push_str(new_name); - let mut range = reference.file_range.range; - if let Some(field_expr) = syntax::algo::find_node_at_range::( - sema.parse(file_id).syntax(), - reference.file_range.range, - ) { - // use shorthand initializer if we were to write foo: foo - if let Some(name) = field_expr.expr().and_then(|e| e.name_ref()) { - if &name.to_string() == new_name { - range = field_expr.syntax().text_range(); - } - } - } - range + edit_text_range_for_record_field_expr_or_pat(sema, reference.file_range, new_name) } _ => { replacement_text.push_str(new_name); reference.file_range.range } }; - SourceFileEdit { file_id, edit: TextEdit::replace(range, replacement_text) } + SourceFileEdit { + file_id: reference.file_range.file_id, + edit: TextEdit::replace(range, replacement_text), + } +} + +fn edit_text_range_for_record_field_expr_or_pat( + sema: &Semantics, + 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 } fn rename_mod( @@ -1189,6 +1209,29 @@ fn foo(foo: Foo) { let Foo { i: bar } = foo; let _ = bar; } +"#, + ); + } + + #[test] + fn test_struct_field_destructure_into_shorthand() { + check( + "baz", + r#" +struct Foo { i<|>: i32 } + +fn foo(foo: Foo) { + let Foo { i: baz } = foo; + let _ = baz; +} +"#, + r#" +struct Foo { baz: i32 } + +fn foo(foo: Foo) { + let Foo { baz } = foo; + let _ = baz; +} "#, ); } -- cgit v1.2.3 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') 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