aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/references
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-11-14 18:57:47 +0000
committerLukas Wirth <[email protected]>2020-11-14 20:08:20 +0000
commitcb60708274dc7c8cff281364507d23047cd482cd (patch)
tree31ebdff3546086e3d0c784535ceaad1ed945cb8b /crates/ide/src/references
parent924eecf4af4d57c597c2e77c5e58c22b2a37bdb6 (diff)
Use shorthand field syntax in destructures
Diffstat (limited to 'crates/ide/src/references')
-rw-r--r--crates/ide/src/references/rename.rs77
1 files changed, 60 insertions, 17 deletions
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 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir::{Module, ModuleDef, ModuleSource, Semantics}; 3use hir::{Module, ModuleDef, ModuleSource, Semantics};
4use ide_db::base_db::SourceDatabaseExt; 4use ide_db::base_db::{FileRange, SourceDatabaseExt};
5use ide_db::{ 5use ide_db::{
6 defs::{Definition, NameClass, NameRefClass}, 6 defs::{Definition, NameClass, NameRefClass},
7 RootDatabase, 7 RootDatabase,
@@ -112,7 +112,6 @@ fn source_edit_from_reference(
112 new_name: &str, 112 new_name: &str,
113) -> SourceFileEdit { 113) -> SourceFileEdit {
114 let mut replacement_text = String::new(); 114 let mut replacement_text = String::new();
115 let file_id = reference.file_range.file_id;
116 let range = match reference.kind { 115 let range = match reference.kind {
117 ReferenceKind::FieldShorthandForField => { 116 ReferenceKind::FieldShorthandForField => {
118 mark::hit!(test_rename_struct_field_for_shorthand); 117 mark::hit!(test_rename_struct_field_for_shorthand);
@@ -126,28 +125,49 @@ fn source_edit_from_reference(
126 replacement_text.push_str(new_name); 125 replacement_text.push_str(new_name);
127 TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) 126 TextRange::new(reference.file_range.range.end(), reference.file_range.range.end())
128 } 127 }
129 ReferenceKind::RecordExprField => { 128 ReferenceKind::RecordFieldExprOrPat => {
130 replacement_text.push_str(new_name); 129 replacement_text.push_str(new_name);
131 let mut range = reference.file_range.range; 130 edit_text_range_for_record_field_expr_or_pat(sema, reference.file_range, new_name)
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 } 131 }
145 _ => { 132 _ => {
146 replacement_text.push_str(new_name); 133 replacement_text.push_str(new_name);
147 reference.file_range.range 134 reference.file_range.range
148 } 135 }
149 }; 136 };
150 SourceFileEdit { file_id, edit: TextEdit::replace(range, replacement_text) } 137 SourceFileEdit {
138 file_id: reference.file_range.file_id,
139 edit: TextEdit::replace(range, replacement_text),
140 }
141}
142
143fn edit_text_range_for_record_field_expr_or_pat(
144 sema: &Semantics<RootDatabase>,
145 file_range: FileRange,
146 new_name: &str,
147) -> TextRange {
148 let mut range = file_range.range;
149 let source_file = sema.parse(file_range.file_id);
150 let file_syntax = source_file.syntax();
151 if let Some(field_expr) =
152 syntax::algo::find_node_at_range::<ast::RecordExprField>(file_syntax, range)
153 {
154 match field_expr.expr().and_then(|e| e.name_ref()) {
155 Some(name) if &name.to_string() == new_name => range = field_expr.syntax().text_range(),
156 _ => (),
157 }
158 } else if let Some(field_pat) =
159 syntax::algo::find_node_at_range::<ast::RecordPatField>(file_syntax, range)
160 {
161 match field_pat.pat() {
162 Some(ast::Pat::IdentPat(pat))
163 if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) =>
164 {
165 range = field_pat.syntax().text_range()
166 }
167 _ => (),
168 }
169 }
170 range
151} 171}
152 172
153fn rename_mod( 173fn rename_mod(
@@ -1192,4 +1212,27 @@ fn foo(foo: Foo) {
1192"#, 1212"#,
1193 ); 1213 );
1194 } 1214 }
1215
1216 #[test]
1217 fn test_struct_field_destructure_into_shorthand() {
1218 check(
1219 "baz",
1220 r#"
1221struct Foo { i<|>: i32 }
1222
1223fn foo(foo: Foo) {
1224 let Foo { i: baz } = foo;
1225 let _ = baz;
1226}
1227"#,
1228 r#"
1229struct Foo { baz: i32 }
1230
1231fn foo(foo: Foo) {
1232 let Foo { baz } = foo;
1233 let _ = baz;
1234}
1235"#,
1236 );
1237 }
1195} 1238}