]> git.lizzy.rs Git - rust.git/commitdiff
Complete associated methods on enums (and unions) as well
authorFlorian Diebold <flodiebold@gmail.com>
Sat, 29 Jun 2019 10:40:01 +0000 (12:40 +0200)
committerFlorian Diebold <flodiebold@gmail.com>
Sat, 29 Jun 2019 10:40:01 +0000 (12:40 +0200)
crates/ra_hir/src/code_model.rs
crates/ra_ide_api/src/completion/complete_path.rs
crates/ra_ide_api/src/completion/snapshots/completion_item__enum_associated_method.snap [new file with mode: 0644]
crates/ra_ide_api/src/completion/snapshots/completion_item__union_associated_method.snap [new file with mode: 0644]

index ebbc37c0e537f60c139c046b3765b37693c3e914..32f98e394af80c7f3a39f38265e3b2229d00b5da 100644 (file)
@@ -390,6 +390,10 @@ pub fn module(self, db: &impl HirDatabase) -> Module {
         self.id.module(db)
     }
 
+    pub fn ty(self, db: &impl HirDatabase) -> Ty {
+        db.type_for_def(self.into(), Namespace::Types)
+    }
+
     // FIXME move to a more general type
     /// Builds a resolver for type references inside this union.
     pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
index c14af593ba3368c04748dc4a54278725472b8bb9..b42b7c458ef8e5de58a8a2456ad1488b5a0a0575 100644 (file)
@@ -37,13 +37,18 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
                 acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def));
             }
         }
-        hir::ModuleDef::Enum(e) => {
-            for variant in e.variants(ctx.db) {
-                acc.add_enum_variant(ctx, variant);
+        hir::ModuleDef::Enum(_) | hir::ModuleDef::Struct(_) | hir::ModuleDef::Union(_) => {
+            if let hir::ModuleDef::Enum(e) = def {
+                for variant in e.variants(ctx.db) {
+                    acc.add_enum_variant(ctx, variant);
+                }
             }
-        }
-        hir::ModuleDef::Struct(s) => {
-            let ty = s.ty(ctx.db);
+            let ty = match def {
+                hir::ModuleDef::Enum(e) => e.ty(ctx.db),
+                hir::ModuleDef::Struct(s) => s.ty(ctx.db),
+                hir::ModuleDef::Union(u) => u.ty(ctx.db),
+                _ => unreachable!(),
+            };
             let krate = ctx.module.and_then(|m| m.krate(ctx.db));
             if let Some(krate) = krate {
                 ty.iterate_impl_items(ctx.db, krate, |item| {
@@ -280,6 +285,44 @@ fn foo() { let _ = S::<|> }
         );
     }
 
+    #[test]
+    fn completes_enum_associated_method() {
+        check_reference_completion(
+            "enum_associated_method",
+            "
+            //- /lib.rs
+            /// An enum
+            enum S {};
+
+            impl S {
+                /// An associated method
+                fn m() { }
+            }
+
+            fn foo() { let _ = S::<|> }
+            ",
+        );
+    }
+
+    #[test]
+    fn completes_union_associated_method() {
+        check_reference_completion(
+            "union_associated_method",
+            "
+            //- /lib.rs
+            /// A union
+            union U {};
+
+            impl U {
+                /// An associated method
+                fn m() { }
+            }
+
+            fn foo() { let _ = U::<|> }
+            ",
+        );
+    }
+
     #[test]
     fn completes_use_paths_across_crates() {
         check_reference_completion(
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_associated_method.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_associated_method.snap
new file mode 100644 (file)
index 0000000..ee6518f
--- /dev/null
@@ -0,0 +1,19 @@
+---
+created: "2019-06-29T10:30:34.110468474Z"
+creator: insta@0.8.1
+source: crates/ra_ide_api/src/completion/completion_item.rs
+expression: kind_completions
+---
+[
+    CompletionItem {
+        label: "m",
+        source_range: [100; 100),
+        delete: [100; 100),
+        insert: "m()$0",
+        kind: Function,
+        detail: "fn m()",
+        documentation: Documentation(
+            "An associated method",
+        ),
+    },
+]
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__union_associated_method.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__union_associated_method.snap
new file mode 100644 (file)
index 0000000..1c1a250
--- /dev/null
@@ -0,0 +1,19 @@
+---
+created: "2019-06-29T10:37:44.968500164Z"
+creator: insta@0.8.1
+source: crates/ra_ide_api/src/completion/completion_item.rs
+expression: kind_completions
+---
+[
+    CompletionItem {
+        label: "m",
+        source_range: [101; 101),
+        delete: [101; 101),
+        insert: "m()$0",
+        kind: Function,
+        detail: "fn m()",
+        documentation: Documentation(
+            "An associated method",
+        ),
+    },
+]