aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-11-14 18:11:09 +0000
committerLukas Wirth <[email protected]>2020-11-14 18:11:09 +0000
commit924eecf4af4d57c597c2e77c5e58c22b2a37bdb6 (patch)
treede445be048cad200c909464ad5687ecaa71e5ca1 /crates/ide/src
parente55a44a831477e2fc8e11340c3d91db883b97c8e (diff)
Properly handle shorthands in destructure patterns when renaming
Diffstat (limited to 'crates/ide/src')
-rw-r--r--crates/ide/src/references.rs21
-rw-r--r--crates/ide/src/references/rename.rs54
2 files changed, 69 insertions, 6 deletions
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(
110 .filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind) 110 .filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)
111 .collect(); 111 .collect();
112 112
113 let decl_range = def.try_to_nav(sema.db)?.focus_or_full_range(); 113 let nav = def.try_to_nav(sema.db)?;
114 114 let decl_range = nav.focus_or_full_range();
115 let declaration = Declaration { 115
116 nav: def.try_to_nav(sema.db)?, 116 let mut kind = ReferenceKind::Other;
117 kind: ReferenceKind::Other, 117 if let Definition::Local(local) = def {
118 access: decl_access(&def, &syntax, decl_range), 118 if let either::Either::Left(pat) = local.source(sema.db).value {
119 if matches!(
120 pat.syntax().parent().and_then(ast::RecordPatField::cast),
121 Some(pat_field) if pat_field.name_ref().is_none()
122 ) {
123 kind = ReferenceKind::FieldShorthandForLocal;
124 }
125 }
119 }; 126 };
120 127
128 let declaration = Declaration { nav, kind, access: decl_access(&def, &syntax, decl_range) };
129
121 Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references })) 130 Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }))
122} 131}
123 132
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
@@ -1138,4 +1138,58 @@ fn foo(bar: i32) -> Foo {
1138"#, 1138"#,
1139 ); 1139 );
1140 } 1140 }
1141
1142 #[test]
1143 fn test_rename_binding_in_destructure_pat_shorthand() {
1144 check(
1145 "bar",
1146 r#"
1147struct Foo {
1148 i: i32,
1149}
1150
1151fn foo(foo: Foo) {
1152 let Foo { i } = foo;
1153 let _ = i<|>;
1154}
1155"#,
1156 r#"
1157struct Foo {
1158 i: i32,
1159}
1160
1161fn foo(foo: Foo) {
1162 let Foo { i: bar } = foo;
1163 let _ = bar;
1164}
1165"#,
1166 );
1167 }
1168
1169 #[test]
1170 fn test_rename_binding_in_destructure_pat() {
1171 check(
1172 "bar",
1173 r#"
1174struct Foo {
1175 i: i32,
1176}
1177
1178fn foo(foo: Foo) {
1179 let Foo { i: b } = foo;
1180 let _ = b<|>;
1181}
1182"#,
1183 r#"
1184struct Foo {
1185 i: i32,
1186}
1187
1188fn foo(foo: Foo) {
1189 let Foo { i: bar } = foo;
1190 let _ = bar;
1191}
1192"#,
1193 );
1194 }
1141} 1195}