diff options
Diffstat (limited to 'crates/ra_ide')
-rw-r--r-- | crates/ra_ide/src/references/rename.rs | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index c46b78cb6..97a97c0ae 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs | |||
@@ -98,6 +98,23 @@ fn rename_mod( | |||
98 | }; | 98 | }; |
99 | source_file_edits.push(edit); | 99 | source_file_edits.push(edit); |
100 | 100 | ||
101 | if let Some(RangeInfo { range: _, info: refs }) = find_all_refs(db, position, None) { | ||
102 | let ref_edits = refs | ||
103 | .references | ||
104 | .into_iter() | ||
105 | .map(|reference| { | ||
106 | source_edit_from_file_id_range( | ||
107 | reference.file_range.file_id, | ||
108 | reference.file_range.range, | ||
109 | new_name, | ||
110 | ) | ||
111 | }) | ||
112 | .collect::<Vec<_>>(); | ||
113 | for ref_edit in ref_edits { | ||
114 | source_file_edits.push(ref_edit); | ||
115 | } | ||
116 | } | ||
117 | |||
101 | Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits)) | 118 | Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits)) |
102 | } | 119 | } |
103 | 120 | ||
@@ -383,6 +400,101 @@ mod tests { | |||
383 | ); | 400 | ); |
384 | } | 401 | } |
385 | 402 | ||
403 | #[test] | ||
404 | fn test_module_rename_in_path() { | ||
405 | test_rename( | ||
406 | r#" | ||
407 | mod <|>foo { | ||
408 | pub fn bar() {} | ||
409 | } | ||
410 | |||
411 | fn main() { | ||
412 | foo::bar(); | ||
413 | }"#, | ||
414 | "baz", | ||
415 | r#" | ||
416 | mod baz { | ||
417 | pub fn bar() {} | ||
418 | } | ||
419 | |||
420 | fn main() { | ||
421 | baz::bar(); | ||
422 | }"#, | ||
423 | ); | ||
424 | } | ||
425 | |||
426 | #[test] | ||
427 | fn test_rename_mod_filename_and_path() { | ||
428 | let (analysis, position) = analysis_and_position( | ||
429 | " | ||
430 | //- /lib.rs | ||
431 | mod bar; | ||
432 | fn f() { | ||
433 | bar::foo::fun() | ||
434 | } | ||
435 | |||
436 | //- /bar.rs | ||
437 | pub mod foo<|>; | ||
438 | |||
439 | //- /bar/foo.rs | ||
440 | // pub fn fun() {} | ||
441 | ", | ||
442 | ); | ||
443 | let new_name = "foo2"; | ||
444 | let source_change = analysis.rename(position, new_name).unwrap(); | ||
445 | assert_debug_snapshot!(&source_change, | ||
446 | @r###" | ||
447 | Some( | ||
448 | RangeInfo { | ||
449 | range: [8; 11), | ||
450 | info: SourceChange { | ||
451 | label: "rename", | ||
452 | source_file_edits: [ | ||
453 | SourceFileEdit { | ||
454 | file_id: FileId( | ||
455 | 2, | ||
456 | ), | ||
457 | edit: TextEdit { | ||
458 | atoms: [ | ||
459 | AtomTextEdit { | ||
460 | delete: [8; 11), | ||
461 | insert: "foo2", | ||
462 | }, | ||
463 | ], | ||
464 | }, | ||
465 | }, | ||
466 | SourceFileEdit { | ||
467 | file_id: FileId( | ||
468 | 1, | ||
469 | ), | ||
470 | edit: TextEdit { | ||
471 | atoms: [ | ||
472 | AtomTextEdit { | ||
473 | delete: [27; 30), | ||
474 | insert: "foo2", | ||
475 | }, | ||
476 | ], | ||
477 | }, | ||
478 | }, | ||
479 | ], | ||
480 | file_system_edits: [ | ||
481 | MoveFile { | ||
482 | src: FileId( | ||
483 | 3, | ||
484 | ), | ||
485 | dst_source_root: SourceRootId( | ||
486 | 0, | ||
487 | ), | ||
488 | dst_path: "bar/foo2.rs", | ||
489 | }, | ||
490 | ], | ||
491 | cursor_position: None, | ||
492 | }, | ||
493 | }, | ||
494 | ) | ||
495 | "###); | ||
496 | } | ||
497 | |||
386 | fn test_rename(text: &str, new_name: &str, expected: &str) { | 498 | fn test_rename(text: &str, new_name: &str, expected: &str) { |
387 | let (analysis, position) = single_file_with_position(text); | 499 | let (analysis, position) = single_file_with_position(text); |
388 | let source_change = analysis.rename(position, new_name).unwrap(); | 500 | let source_change = analysis.rename(position, new_name).unwrap(); |