diff options
author | Lukas Wirth <[email protected]> | 2020-11-14 16:49:36 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2020-11-14 16:51:09 +0000 |
commit | e55a44a831477e2fc8e11340c3d91db883b97c8e (patch) | |
tree | db334060759bcc4252689e305a466ec3dd2b5d8b /crates/ide/src | |
parent | 99fa139beab0c03583c676554c3d807525f602af (diff) |
Use shorthand record syntax when renaming struct initializer field
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/references/rename.rs | 49 |
1 files changed, 45 insertions, 4 deletions
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( | |||
106 | Some(module) | 106 | Some(module) |
107 | } | 107 | } |
108 | 108 | ||
109 | fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFileEdit { | 109 | fn source_edit_from_reference( |
110 | sema: &Semantics<RootDatabase>, | ||
111 | reference: Reference, | ||
112 | new_name: &str, | ||
113 | ) -> SourceFileEdit { | ||
110 | let mut replacement_text = String::new(); | 114 | let mut replacement_text = String::new(); |
111 | let file_id = reference.file_range.file_id; | 115 | let file_id = reference.file_range.file_id; |
112 | let range = match reference.kind { | 116 | let range = match reference.kind { |
@@ -122,6 +126,22 @@ fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFil | |||
122 | replacement_text.push_str(new_name); | 126 | replacement_text.push_str(new_name); |
123 | TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) | 127 | TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) |
124 | } | 128 | } |
129 | ReferenceKind::RecordExprField => { | ||
130 | replacement_text.push_str(new_name); | ||
131 | let mut range = reference.file_range.range; | ||
132 | if let Some(field_expr) = syntax::algo::find_node_at_range::<ast::RecordExprField>( | ||
133 | sema.parse(file_id).syntax(), | ||
134 | reference.file_range.range, | ||
135 | ) { | ||
136 | // use shorthand initializer if we were to write foo: foo | ||
137 | if let Some(name) = field_expr.expr().and_then(|e| e.name_ref()) { | ||
138 | if &name.to_string() == new_name { | ||
139 | range = field_expr.syntax().text_range(); | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | range | ||
144 | } | ||
125 | _ => { | 145 | _ => { |
126 | replacement_text.push_str(new_name); | 146 | replacement_text.push_str(new_name); |
127 | reference.file_range.range | 147 | reference.file_range.range |
@@ -170,7 +190,7 @@ fn rename_mod( | |||
170 | let ref_edits = refs | 190 | let ref_edits = refs |
171 | .references | 191 | .references |
172 | .into_iter() | 192 | .into_iter() |
173 | .map(|reference| source_edit_from_reference(reference, new_name)); | 193 | .map(|reference| source_edit_from_reference(sema, reference, new_name)); |
174 | source_file_edits.extend(ref_edits); | 194 | source_file_edits.extend(ref_edits); |
175 | 195 | ||
176 | Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits))) | 196 | Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits))) |
@@ -211,7 +231,7 @@ fn rename_to_self( | |||
211 | 231 | ||
212 | let mut edits = usages | 232 | let mut edits = usages |
213 | .into_iter() | 233 | .into_iter() |
214 | .map(|reference| source_edit_from_reference(reference, "self")) | 234 | .map(|reference| source_edit_from_reference(sema, reference, "self")) |
215 | .collect::<Vec<_>>(); | 235 | .collect::<Vec<_>>(); |
216 | 236 | ||
217 | edits.push(SourceFileEdit { | 237 | edits.push(SourceFileEdit { |
@@ -300,7 +320,7 @@ fn rename_reference( | |||
300 | 320 | ||
301 | let edit = refs | 321 | let edit = refs |
302 | .into_iter() | 322 | .into_iter() |
303 | .map(|reference| source_edit_from_reference(reference, new_name)) | 323 | .map(|reference| source_edit_from_reference(sema, reference, new_name)) |
304 | .collect::<Vec<_>>(); | 324 | .collect::<Vec<_>>(); |
305 | 325 | ||
306 | if edit.is_empty() { | 326 | if edit.is_empty() { |
@@ -1097,4 +1117,25 @@ impl Foo { | |||
1097 | "#, | 1117 | "#, |
1098 | ); | 1118 | ); |
1099 | } | 1119 | } |
1120 | |||
1121 | #[test] | ||
1122 | fn test_initializer_use_field_init_shorthand() { | ||
1123 | check( | ||
1124 | "bar", | ||
1125 | r#" | ||
1126 | struct Foo { i<|>: i32 } | ||
1127 | |||
1128 | fn foo(bar: i32) -> Foo { | ||
1129 | Foo { i: bar } | ||
1130 | } | ||
1131 | "#, | ||
1132 | r#" | ||
1133 | struct Foo { bar: i32 } | ||
1134 | |||
1135 | fn foo(bar: i32) -> Foo { | ||
1136 | Foo { bar } | ||
1137 | } | ||
1138 | "#, | ||
1139 | ); | ||
1140 | } | ||
1100 | } | 1141 | } |