diff options
Diffstat (limited to 'crates/ide/src/references')
-rw-r--r-- | crates/ide/src/references/rename.rs | 103 |
1 files changed, 102 insertions, 1 deletions
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 2408a0181..175e7a31d 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs | |||
@@ -50,6 +50,17 @@ pub(crate) fn prepare_rename( | |||
50 | let sema = Semantics::new(db); | 50 | let sema = Semantics::new(db); |
51 | let source_file = sema.parse(position.file_id); | 51 | let source_file = sema.parse(position.file_id); |
52 | let syntax = source_file.syntax(); | 52 | let syntax = source_file.syntax(); |
53 | |||
54 | let def = find_definition(&sema, syntax, position)?; | ||
55 | match def { | ||
56 | Definition::SelfType(_) => bail!("Cannot rename `Self`"), | ||
57 | Definition::ModuleDef(ModuleDef::BuiltinType(_)) => bail!("Cannot rename builtin type"), | ||
58 | _ => {} | ||
59 | }; | ||
60 | let nav = | ||
61 | def.try_to_nav(sema.db).ok_or_else(|| format_err!("No references found at position"))?; | ||
62 | nav.focus_range.ok_or_else(|| format_err!("No identifier available to rename"))?; | ||
63 | |||
53 | let name_like = sema | 64 | let name_like = sema |
54 | .find_node_at_offset_with_descend(&syntax, position.offset) | 65 | .find_node_at_offset_with_descend(&syntax, position.offset) |
55 | .ok_or_else(|| format_err!("No references found at position"))?; | 66 | .ok_or_else(|| format_err!("No references found at position"))?; |
@@ -507,7 +518,8 @@ fn source_edit_from_def( | |||
507 | def.try_to_nav(sema.db).ok_or_else(|| format_err!("No references found at position"))?; | 518 | def.try_to_nav(sema.db).ok_or_else(|| format_err!("No references found at position"))?; |
508 | 519 | ||
509 | let mut replacement_text = String::new(); | 520 | let mut replacement_text = String::new(); |
510 | let mut repl_range = nav.focus_or_full_range(); | 521 | let mut repl_range = |
522 | nav.focus_range.ok_or_else(|| format_err!("No identifier available to rename"))?; | ||
511 | if let Definition::Local(local) = def { | 523 | if let Definition::Local(local) = def { |
512 | if let Either::Left(pat) = local.source(sema.db).value { | 524 | if let Either::Left(pat) = local.source(sema.db).value { |
513 | if matches!( | 525 | if matches!( |
@@ -626,6 +638,49 @@ foo!(Foo$0);", | |||
626 | } | 638 | } |
627 | 639 | ||
628 | #[test] | 640 | #[test] |
641 | fn test_prepare_rename_tuple_field() { | ||
642 | check_prepare( | ||
643 | r#" | ||
644 | struct Foo(i32); | ||
645 | |||
646 | fn baz() { | ||
647 | let mut x = Foo(4); | ||
648 | x.0$0 = 5; | ||
649 | } | ||
650 | "#, | ||
651 | expect![[r#"No identifier available to rename"#]], | ||
652 | ); | ||
653 | } | ||
654 | |||
655 | #[test] | ||
656 | fn test_prepare_rename_builtin() { | ||
657 | check_prepare( | ||
658 | r#" | ||
659 | fn foo() { | ||
660 | let x: i32$0 = 0; | ||
661 | } | ||
662 | "#, | ||
663 | expect![[r#"Cannot rename builtin type"#]], | ||
664 | ); | ||
665 | } | ||
666 | |||
667 | #[test] | ||
668 | fn test_prepare_rename_self() { | ||
669 | check_prepare( | ||
670 | r#" | ||
671 | struct Foo {} | ||
672 | |||
673 | impl Foo { | ||
674 | fn foo(self) -> Self$0 { | ||
675 | self | ||
676 | } | ||
677 | } | ||
678 | "#, | ||
679 | expect![[r#"Cannot rename `Self`"#]], | ||
680 | ); | ||
681 | } | ||
682 | |||
683 | #[test] | ||
629 | fn test_rename_to_underscore() { | 684 | fn test_rename_to_underscore() { |
630 | check("_", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let _ = 1; }"#); | 685 | check("_", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let _ = 1; }"#); |
631 | } | 686 | } |
@@ -1787,4 +1842,50 @@ fn foo() { | |||
1787 | "#, | 1842 | "#, |
1788 | ) | 1843 | ) |
1789 | } | 1844 | } |
1845 | |||
1846 | #[test] | ||
1847 | fn test_rename_tuple_field() { | ||
1848 | check( | ||
1849 | "foo", | ||
1850 | r#" | ||
1851 | struct Foo(i32); | ||
1852 | |||
1853 | fn baz() { | ||
1854 | let mut x = Foo(4); | ||
1855 | x.0$0 = 5; | ||
1856 | } | ||
1857 | "#, | ||
1858 | "error: No identifier available to rename", | ||
1859 | ); | ||
1860 | } | ||
1861 | |||
1862 | #[test] | ||
1863 | fn test_rename_builtin() { | ||
1864 | check( | ||
1865 | "foo", | ||
1866 | r#" | ||
1867 | fn foo() { | ||
1868 | let x: i32$0 = 0; | ||
1869 | } | ||
1870 | "#, | ||
1871 | "error: Cannot rename builtin type", | ||
1872 | ); | ||
1873 | } | ||
1874 | |||
1875 | #[test] | ||
1876 | fn test_rename_self() { | ||
1877 | check( | ||
1878 | "foo", | ||
1879 | r#" | ||
1880 | struct Foo {} | ||
1881 | |||
1882 | impl Foo { | ||
1883 | fn foo(self) -> Self$0 { | ||
1884 | self | ||
1885 | } | ||
1886 | } | ||
1887 | "#, | ||
1888 | "error: Cannot rename `Self`", | ||
1889 | ); | ||
1890 | } | ||
1790 | } | 1891 | } |