about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYoung-Flash <871946895@qq.com>2023-11-16 17:22:53 +0800
committerYoung-Flash <dongyang@apache.org>2024-02-21 10:57:39 +0800
commitdba167592ee67748d2f24c8f646cbe193c95da14 (patch)
treed79a32cd5f053b699a6908e6ff48007ffebdb558
parent543d7e98dbcc0668528dbf3f5b32d752882baa33 (diff)
downloadrust-dba167592ee67748d2f24c8f646cbe193c95da14.tar.gz
rust-dba167592ee67748d2f24c8f646cbe193c95da14.zip
feat: add hover display for trait assoc items
-rw-r--r--crates/hir/src/display.rs24
-rw-r--r--crates/ide/src/hover/tests.rs113
2 files changed, 127 insertions, 10 deletions
diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs
index 30f402a79f3..d76eb873bf4 100644
--- a/crates/hir/src/display.rs
+++ b/crates/hir/src/display.rs
@@ -595,6 +595,30 @@ impl HirDisplay for Trait {
         let def_id = GenericDefId::TraitId(self.id);
         write_generic_params(def_id, f)?;
         write_where_clause(def_id, f)?;
+
+        let assoc_items = self.items(f.db);
+        if assoc_items.is_empty() {
+            f.write_str(" {}")?;
+        } else {
+            f.write_str(" {\n")?;
+            for item in assoc_items {
+                f.write_str("    ")?;
+                match item {
+                    AssocItem::Function(func) => {
+                        func.hir_fmt(f)?;
+                    }
+                    AssocItem::Const(cst) => {
+                        cst.hir_fmt(f)?;
+                    }
+                    AssocItem::TypeAlias(type_alias) => {
+                        type_alias.hir_fmt(f)?;
+                    }
+                };
+                f.write_str(",\n")?;
+            }
+            f.write_str("}")?;
+        }
+
         Ok(())
     }
 }
diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs
index ead4f91595f..73d9339a229 100644
--- a/crates/ide/src/hover/tests.rs
+++ b/crates/ide/src/hover/tests.rs
@@ -2685,7 +2685,7 @@ fn main() { let s$0t = foo(); }
                                     focus_range: 6..9,
                                     name: "Foo",
                                     kind: Trait,
-                                    description: "trait Foo",
+                                    description: "trait Foo {}",
                                 },
                             },
                         ],
@@ -2719,7 +2719,7 @@ fn main() { let s$0t = foo(); }
                                     focus_range: 6..9,
                                     name: "Foo",
                                     kind: Trait,
-                                    description: "trait Foo<T>",
+                                    description: "trait Foo<T> {}",
                                 },
                             },
                             HoverGotoTypeData {
@@ -2886,7 +2886,7 @@ fn foo(ar$0g: &impl Foo) {}
                                     focus_range: 6..9,
                                     name: "Foo",
                                     kind: Trait,
-                                    description: "trait Foo",
+                                    description: "trait Foo {}",
                                 },
                             },
                         ],
@@ -2988,7 +2988,7 @@ pub mod future {
                                 name: "Future",
                                 kind: Trait,
                                 container_name: "future",
-                                description: "pub trait Future",
+                                description: "pub trait Future {}",
                             },
                         },
                         HoverGotoTypeData {
@@ -3033,7 +3033,7 @@ fn foo(ar$0g: &impl Foo<S>) {}
                                     focus_range: 6..9,
                                     name: "Foo",
                                     kind: Trait,
-                                    description: "trait Foo<T>",
+                                    description: "trait Foo<T> {}",
                                 },
                             },
                             HoverGotoTypeData {
@@ -3096,7 +3096,7 @@ fn main() { let s$0t = foo(); }
                                     focus_range: 6..9,
                                     name: "Foo",
                                     kind: Trait,
-                                    description: "trait Foo",
+                                    description: "trait Foo {}",
                                 },
                             },
                         ],
@@ -3127,7 +3127,7 @@ fn foo(ar$0g: &dyn Foo) {}
                                     focus_range: 6..9,
                                     name: "Foo",
                                     kind: Trait,
-                                    description: "trait Foo",
+                                    description: "trait Foo {}",
                                 },
                             },
                         ],
@@ -3159,7 +3159,7 @@ fn foo(ar$0g: &dyn Foo<S>) {}
                                     focus_range: 6..9,
                                     name: "Foo",
                                     kind: Trait,
-                                    description: "trait Foo<T>",
+                                    description: "trait Foo<T> {}",
                                 },
                             },
                             HoverGotoTypeData {
@@ -3288,7 +3288,7 @@ fn main() { let s$0t = test().get(); }
                                     focus_range: 6..9,
                                     name: "Foo",
                                     kind: Trait,
-                                    description: "trait Foo",
+                                    description: "trait Foo {\n    type Item,\n    fn get(self) -> Self::Item,\n}",
                                 },
                             },
                         ],
@@ -3353,7 +3353,7 @@ fn foo<T: Foo>(t: T$0){}
                                     focus_range: 6..9,
                                     name: "Foo",
                                     kind: Trait,
-                                    description: "trait Foo",
+                                    description: "trait Foo {}",
                                 },
                             },
                         ],
@@ -6253,6 +6253,99 @@ impl T for () {
 }
 
 #[test]
+fn hover_trait_show_assoc_items() {
+    check(
+        r#"
+trait T {}
+impl T$0 for () {}
+"#,
+        expect![[r#"
+        *T*
+
+        ```rust
+        test
+        ```
+
+        ```rust
+        trait T {}
+        ```
+        "#]],
+    );
+
+    check(
+        r#"
+trait T {
+    fn func() {}
+}
+impl T$0 for () {}
+"#,
+        expect![[r#"
+        *T*
+
+        ```rust
+        test
+        ```
+
+        ```rust
+        trait T {
+            fn func(),
+        }
+        ```
+        "#]],
+    );
+
+    check(
+        r#"
+trait T {
+    fn func() {}
+    const FLAG: i32 = 34;
+}
+impl T$0 for () {}
+"#,
+        expect![[r#"
+        *T*
+
+        ```rust
+        test
+        ```
+
+        ```rust
+        trait T {
+            fn func(),
+            const FLAG: i32,
+        }
+        ```
+        "#]],
+    );
+
+    check(
+        r#"
+trait T {
+    fn func() {}
+    const FLAG: i32 = 34;
+    type Bar;
+}
+impl T$0 for () {}
+"#,
+        expect![[r#"
+        *T*
+
+        ```rust
+        test
+        ```
+
+        ```rust
+        trait T {
+            fn func(),
+            const FLAG: i32,
+            type Bar,
+        }
+        ```
+        "#]],
+    );
+}
+
+#[test]
 fn hover_ranged_macro_call() {
     check_hover_range(
         r#"