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