aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFedor Sakharov <[email protected]>2020-05-06 15:17:35 +0100
committerFedor Sakharov <[email protected]>2020-05-06 15:35:22 +0100
commitbd9f1f7eb78843ddd91d259a04e988b0681a5db4 (patch)
treee9fcabf13e376266b3a7065743fa3392dfdb53e9
parentefd8e34c396f1524623a495e47111f1047cf2879 (diff)
Fix rename of enum variant visible from module
-rw-r--r--crates/ra_hir/src/code_model.rs7
-rw-r--r--crates/ra_hir_def/src/adt.rs4
-rw-r--r--crates/ra_ide/src/references/rename.rs62
-rw-r--r--crates/ra_ide_db/src/defs.rs5
4 files changed, 76 insertions, 2 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 5f480c304..7eba0b233 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -474,6 +474,13 @@ impl EnumVariant {
474 } 474 }
475} 475}
476 476
477impl HasVisibility for EnumVariant {
478 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
479 let visibility = &db.enum_data(self.parent.id).visibility;
480 visibility.resolve(db.upcast(), &self.parent.id.resolver(db.upcast()))
481 }
482}
483
477/// A Data Type 484/// A Data Type
478#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 485#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
479pub enum Adt { 486pub enum Adt {
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index 2bc34d449..0fda4d6c9 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -33,6 +33,7 @@ pub struct StructData {
33#[derive(Debug, Clone, PartialEq, Eq)] 33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct EnumData { 34pub struct EnumData {
35 pub name: Name, 35 pub name: Name,
36 pub visibility: RawVisibility,
36 pub variants: Arena<EnumVariantData>, 37 pub variants: Arena<EnumVariantData>,
37} 38}
38 39
@@ -91,7 +92,8 @@ impl EnumData {
91 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); 92 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
92 let mut trace = Trace::new_for_arena(); 93 let mut trace = Trace::new_for_arena();
93 lower_enum(db, &mut trace, &src, e.lookup(db).container.module(db)); 94 lower_enum(db, &mut trace, &src, e.lookup(db).container.module(db));
94 Arc::new(EnumData { name, variants: trace.into_arena() }) 95 let visibility = RawVisibility::from_ast(db, src.with_value(src.value.visibility()));
96 Arc::new(EnumData { name, visibility, variants: trace.into_arena() })
95 } 97 }
96 98
97 pub fn variant(&self, name: &Name) -> Option<LocalEnumVariantId> { 99 pub fn variant(&self, name: &Name) -> Option<LocalEnumVariantId> {
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs
index 0398d53bc..2cbb82c1a 100644
--- a/crates/ra_ide/src/references/rename.rs
+++ b/crates/ra_ide/src/references/rename.rs
@@ -712,6 +712,68 @@ mod tests {
712 "###); 712 "###);
713 } 713 }
714 714
715 #[test]
716 fn test_enum_variant_from_module_1() {
717 test_rename(
718 r#"
719 mod foo {
720 pub enum Foo {
721 Bar<|>,
722 }
723 }
724
725 fn func(f: foo::Foo) {
726 match f {
727 foo::Foo::Bar => {}
728 }
729 }
730 "#,
731 "Baz",
732 r#"
733 mod foo {
734 pub enum Foo {
735 Baz,
736 }
737 }
738
739 fn func(f: foo::Foo) {
740 match f {
741 foo::Foo::Baz => {}
742 }
743 }
744 "#,
745 );
746 }
747
748 #[test]
749 fn test_enum_variant_from_module_2() {
750 test_rename(
751 r#"
752 mod foo {
753 pub struct Foo {
754 pub bar<|>: uint,
755 }
756 }
757
758 fn foo(f: foo::Foo) {
759 let _ = f.bar;
760 }
761 "#,
762 "baz",
763 r#"
764 mod foo {
765 pub struct Foo {
766 pub baz: uint,
767 }
768 }
769
770 fn foo(f: foo::Foo) {
771 let _ = f.baz;
772 }
773 "#,
774 );
775 }
776
715 fn test_rename(text: &str, new_name: &str, expected: &str) { 777 fn test_rename(text: &str, new_name: &str, expected: &str) {
716 let (analysis, position) = single_file_with_position(text); 778 let (analysis, position) = single_file_with_position(text);
717 let source_change = analysis.rename(position, new_name).unwrap(); 779 let source_change = analysis.rename(position, new_name).unwrap();
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index 40d0e77b5..2e2850efb 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -47,7 +47,10 @@ impl Definition {
47 match self { 47 match self {
48 Definition::Macro(_) => None, 48 Definition::Macro(_) => None,
49 Definition::Field(sf) => Some(sf.visibility(db)), 49 Definition::Field(sf) => Some(sf.visibility(db)),
50 Definition::ModuleDef(def) => module?.visibility_of(db, def), 50 Definition::ModuleDef(def) => match def {
51 ModuleDef::EnumVariant(id) => Some(id.visibility(db)),
52 _ => module?.visibility_of(db, def),
53 },
51 Definition::SelfType(_) => None, 54 Definition::SelfType(_) => None,
52 Definition::Local(_) => None, 55 Definition::Local(_) => None,
53 Definition::TypeParam(_) => None, 56 Definition::TypeParam(_) => None,