diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-22 00:19:28 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-22 00:19:28 +0000 |
commit | d8b09435357462dccf7f026f568b2cd1dc3ec67a (patch) | |
tree | 72909c3e5d3051357c0bc0f6e121f65eec4ba0f1 /crates/ra_ide | |
parent | f3ab290bd4d20b2c1531980eb87a32981efb7826 (diff) | |
parent | fe8ce4c41d5f82ab164e76aef6c504c99fdff410 (diff) |
Merge #3244
3244: Rename module references r=matklad a=adamrk
Rename references to a module when the module is renamed. This fixes some missing renames in the existing implementation. For example, renaming the module `foo` to `foo2` in this case:
```rust
mod foo {
pub fn bar() {}
}
fn main() {
foo::bar()
}
```
previously would not change the call `foo::bar()` to `foo2::bar()`, but now it will.
Co-authored-by: adamrk <[email protected]>
Co-authored-by: Adam Bratschi-Kaye <[email protected]>
Diffstat (limited to 'crates/ra_ide')
-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(); |