diff options
author | Matt Niemeir <[email protected]> | 2020-03-10 02:34:33 +0000 |
---|---|---|
committer | Matt Niemeir <[email protected]> | 2020-03-10 03:14:17 +0000 |
commit | ce8121bd65daecd9e92371c244dd1ac2b0e5ecda (patch) | |
tree | 7782adb94467f268349d9f86b16d55af22c46b49 /crates | |
parent | a9b6aec8a788d33ebe9667d53d127be59d93e555 (diff) |
Renaming a local renames struct field shorthand
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide/src/references/rename.rs | 48 | ||||
-rw-r--r-- | crates/ra_ide_db/src/search.rs | 24 |
2 files changed, 58 insertions, 14 deletions
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index 794a109f3..1fca6de1f 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs | |||
@@ -48,17 +48,26 @@ fn find_name_and_module_at_offset( | |||
48 | } | 48 | } |
49 | 49 | ||
50 | fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFileEdit { | 50 | fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFileEdit { |
51 | let mut replacement_text = String::from(new_name); | 51 | let mut replacement_text = String::new(); |
52 | let file_id = reference.file_range.file_id; | 52 | let file_id = reference.file_range.file_id; |
53 | let range = match reference.kind { | 53 | let range = match reference.kind { |
54 | ReferenceKind::StructFieldShorthand => { | 54 | ReferenceKind::StructFieldShorthandForField => { |
55 | replacement_text.push_str(new_name); | ||
55 | replacement_text.push_str(": "); | 56 | replacement_text.push_str(": "); |
56 | TextRange::from_to( | 57 | TextRange::from_to( |
57 | reference.file_range.range.start(), | 58 | reference.file_range.range.start(), |
58 | reference.file_range.range.start(), | 59 | reference.file_range.range.start(), |
59 | ) | 60 | ) |
60 | } | 61 | } |
61 | _ => reference.file_range.range, | 62 | ReferenceKind::StructFieldShorthandForLocal => { |
63 | replacement_text.push_str(": "); | ||
64 | replacement_text.push_str(new_name); | ||
65 | TextRange::from_to(reference.file_range.range.end(), reference.file_range.range.end()) | ||
66 | } | ||
67 | _ => { | ||
68 | replacement_text.push_str(new_name); | ||
69 | reference.file_range.range | ||
70 | } | ||
62 | }; | 71 | }; |
63 | SourceFileEdit { file_id, edit: TextEdit::replace(range, replacement_text) } | 72 | SourceFileEdit { file_id, edit: TextEdit::replace(range, replacement_text) } |
64 | } | 73 | } |
@@ -286,7 +295,7 @@ mod tests { | |||
286 | } | 295 | } |
287 | 296 | ||
288 | #[test] | 297 | #[test] |
289 | fn test_rename_for_struct_field() { | 298 | fn test_rename_struct_field() { |
290 | test_rename( | 299 | test_rename( |
291 | r#" | 300 | r#" |
292 | struct Foo { | 301 | struct Foo { |
@@ -315,7 +324,7 @@ mod tests { | |||
315 | } | 324 | } |
316 | 325 | ||
317 | #[test] | 326 | #[test] |
318 | fn test_rename_for_struct_field_shorthand() { | 327 | fn test_rename_struct_field_for_shorthand() { |
319 | test_rename( | 328 | test_rename( |
320 | r#" | 329 | r#" |
321 | struct Foo { | 330 | struct Foo { |
@@ -344,6 +353,35 @@ mod tests { | |||
344 | } | 353 | } |
345 | 354 | ||
346 | #[test] | 355 | #[test] |
356 | fn test_rename_local_for_field_shorthand() { | ||
357 | test_rename( | ||
358 | r#" | ||
359 | struct Foo { | ||
360 | i: i32, | ||
361 | } | ||
362 | |||
363 | impl Foo { | ||
364 | fn new(i<|>: i32) -> Self { | ||
365 | Self { i } | ||
366 | } | ||
367 | } | ||
368 | "#, | ||
369 | "j", | ||
370 | r#" | ||
371 | struct Foo { | ||
372 | i: i32, | ||
373 | } | ||
374 | |||
375 | impl Foo { | ||
376 | fn new(j: i32) -> Self { | ||
377 | Self { i: j } | ||
378 | } | ||
379 | } | ||
380 | "#, | ||
381 | ); | ||
382 | } | ||
383 | |||
384 | #[test] | ||
347 | fn test_rename_mod() { | 385 | fn test_rename_mod() { |
348 | let (analysis, position) = analysis_and_position( | 386 | let (analysis, position) = analysis_and_position( |
349 | " | 387 | " |
diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs index 9436a7562..b843b5b57 100644 --- a/crates/ra_ide_db/src/search.rs +++ b/crates/ra_ide_db/src/search.rs | |||
@@ -30,7 +30,8 @@ pub struct Reference { | |||
30 | 30 | ||
31 | #[derive(Debug, Clone, PartialEq)] | 31 | #[derive(Debug, Clone, PartialEq)] |
32 | pub enum ReferenceKind { | 32 | pub enum ReferenceKind { |
33 | StructFieldShorthand, | 33 | StructFieldShorthandForField, |
34 | StructFieldShorthandForLocal, | ||
34 | StructLiteral, | 35 | StructLiteral, |
35 | Other, | 36 | Other, |
36 | } | 37 | } |
@@ -238,8 +239,8 @@ impl Definition { | |||
238 | // FIXME: reuse sb | 239 | // FIXME: reuse sb |
239 | // See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098 | 240 | // See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098 |
240 | 241 | ||
241 | match (classify_name_ref(&sema, &name_ref), self) { | 242 | match classify_name_ref(&sema, &name_ref) { |
242 | (Some(NameRefClass::Definition(def)), _) if &def == self => { | 243 | Some(NameRefClass::Definition(def)) if &def == self => { |
243 | let kind = if is_record_lit_name_ref(&name_ref) | 244 | let kind = if is_record_lit_name_ref(&name_ref) |
244 | || is_call_expr_name_ref(&name_ref) | 245 | || is_call_expr_name_ref(&name_ref) |
245 | { | 246 | { |
@@ -255,14 +256,19 @@ impl Definition { | |||
255 | access: reference_access(&def, &name_ref), | 256 | access: reference_access(&def, &name_ref), |
256 | }); | 257 | }); |
257 | } | 258 | } |
258 | ( | 259 | Some(NameRefClass::FieldShorthand { local, field: _ }) => { |
259 | Some(NameRefClass::FieldShorthand { local, field: _ }), | 260 | let kind = match self { |
260 | Definition::StructField(_), | 261 | Definition::StructField(_) => { |
261 | ) => { | 262 | ReferenceKind::StructFieldShorthandForField |
263 | } | ||
264 | Definition::Local(_) => ReferenceKind::StructFieldShorthandForLocal, | ||
265 | _ => continue, | ||
266 | }; | ||
267 | |||
262 | let file_range = sema.original_range(name_ref.syntax()); | 268 | let file_range = sema.original_range(name_ref.syntax()); |
263 | refs.push(Reference { | 269 | refs.push(Reference { |
264 | file_range: file_range, | 270 | file_range, |
265 | kind: ReferenceKind::StructFieldShorthand, | 271 | kind, |
266 | access: reference_access(&Definition::Local(local), &name_ref), | 272 | access: reference_access(&Definition::Local(local), &name_ref), |
267 | }); | 273 | }); |
268 | } | 274 | } |