Skip to content

Commit dba1675

Browse files
committed
feat: add hover display for trait assoc items
1 parent 543d7e9 commit dba1675

File tree

2 files changed

+127
-10
lines changed

2 files changed

+127
-10
lines changed

crates/hir/src/display.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,30 @@ impl HirDisplay for Trait {
595595
let def_id = GenericDefId::TraitId(self.id);
596596
write_generic_params(def_id, f)?;
597597
write_where_clause(def_id, f)?;
598+
599+
let assoc_items = self.items(f.db);
600+
if assoc_items.is_empty() {
601+
f.write_str(" {}")?;
602+
} else {
603+
f.write_str(" {\n")?;
604+
for item in assoc_items {
605+
f.write_str(" ")?;
606+
match item {
607+
AssocItem::Function(func) => {
608+
func.hir_fmt(f)?;
609+
}
610+
AssocItem::Const(cst) => {
611+
cst.hir_fmt(f)?;
612+
}
613+
AssocItem::TypeAlias(type_alias) => {
614+
type_alias.hir_fmt(f)?;
615+
}
616+
};
617+
f.write_str(",\n")?;
618+
}
619+
f.write_str("}")?;
620+
}
621+
598622
Ok(())
599623
}
600624
}

crates/ide/src/hover/tests.rs

Lines changed: 103 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,7 +2685,7 @@ fn main() { let s$0t = foo(); }
26852685
focus_range: 6..9,
26862686
name: "Foo",
26872687
kind: Trait,
2688-
description: "trait Foo",
2688+
description: "trait Foo {}",
26892689
},
26902690
},
26912691
],
@@ -2719,7 +2719,7 @@ fn main() { let s$0t = foo(); }
27192719
focus_range: 6..9,
27202720
name: "Foo",
27212721
kind: Trait,
2722-
description: "trait Foo<T>",
2722+
description: "trait Foo<T> {}",
27232723
},
27242724
},
27252725
HoverGotoTypeData {
@@ -2886,7 +2886,7 @@ fn foo(ar$0g: &impl Foo) {}
28862886
focus_range: 6..9,
28872887
name: "Foo",
28882888
kind: Trait,
2889-
description: "trait Foo",
2889+
description: "trait Foo {}",
28902890
},
28912891
},
28922892
],
@@ -2988,7 +2988,7 @@ pub mod future {
29882988
name: "Future",
29892989
kind: Trait,
29902990
container_name: "future",
2991-
description: "pub trait Future",
2991+
description: "pub trait Future {}",
29922992
},
29932993
},
29942994
HoverGotoTypeData {
@@ -3033,7 +3033,7 @@ fn foo(ar$0g: &impl Foo<S>) {}
30333033
focus_range: 6..9,
30343034
name: "Foo",
30353035
kind: Trait,
3036-
description: "trait Foo<T>",
3036+
description: "trait Foo<T> {}",
30373037
},
30383038
},
30393039
HoverGotoTypeData {
@@ -3096,7 +3096,7 @@ fn main() { let s$0t = foo(); }
30963096
focus_range: 6..9,
30973097
name: "Foo",
30983098
kind: Trait,
3099-
description: "trait Foo",
3099+
description: "trait Foo {}",
31003100
},
31013101
},
31023102
],
@@ -3127,7 +3127,7 @@ fn foo(ar$0g: &dyn Foo) {}
31273127
focus_range: 6..9,
31283128
name: "Foo",
31293129
kind: Trait,
3130-
description: "trait Foo",
3130+
description: "trait Foo {}",
31313131
},
31323132
},
31333133
],
@@ -3159,7 +3159,7 @@ fn foo(ar$0g: &dyn Foo<S>) {}
31593159
focus_range: 6..9,
31603160
name: "Foo",
31613161
kind: Trait,
3162-
description: "trait Foo<T>",
3162+
description: "trait Foo<T> {}",
31633163
},
31643164
},
31653165
HoverGotoTypeData {
@@ -3288,7 +3288,7 @@ fn main() { let s$0t = test().get(); }
32883288
focus_range: 6..9,
32893289
name: "Foo",
32903290
kind: Trait,
3291-
description: "trait Foo",
3291+
description: "trait Foo {\n type Item,\n fn get(self) -> Self::Item,\n}",
32923292
},
32933293
},
32943294
],
@@ -3353,7 +3353,7 @@ fn foo<T: Foo>(t: T$0){}
33533353
focus_range: 6..9,
33543354
name: "Foo",
33553355
kind: Trait,
3356-
description: "trait Foo",
3356+
description: "trait Foo {}",
33573357
},
33583358
},
33593359
],
@@ -6252,6 +6252,99 @@ impl T for () {
62526252
);
62536253
}
62546254

6255+
#[test]
6256+
fn hover_trait_show_assoc_items() {
6257+
check(
6258+
r#"
6259+
trait T {}
6260+
impl T$0 for () {}
6261+
"#,
6262+
expect![[r#"
6263+
*T*
6264+
6265+
```rust
6266+
test
6267+
```
6268+
6269+
```rust
6270+
trait T {}
6271+
```
6272+
"#]],
6273+
);
6274+
6275+
check(
6276+
r#"
6277+
trait T {
6278+
fn func() {}
6279+
}
6280+
impl T$0 for () {}
6281+
"#,
6282+
expect![[r#"
6283+
*T*
6284+
6285+
```rust
6286+
test
6287+
```
6288+
6289+
```rust
6290+
trait T {
6291+
fn func(),
6292+
}
6293+
```
6294+
"#]],
6295+
);
6296+
6297+
check(
6298+
r#"
6299+
trait T {
6300+
fn func() {}
6301+
const FLAG: i32 = 34;
6302+
}
6303+
impl T$0 for () {}
6304+
"#,
6305+
expect![[r#"
6306+
*T*
6307+
6308+
```rust
6309+
test
6310+
```
6311+
6312+
```rust
6313+
trait T {
6314+
fn func(),
6315+
const FLAG: i32,
6316+
}
6317+
```
6318+
"#]],
6319+
);
6320+
6321+
check(
6322+
r#"
6323+
trait T {
6324+
fn func() {}
6325+
const FLAG: i32 = 34;
6326+
type Bar;
6327+
}
6328+
impl T$0 for () {}
6329+
"#,
6330+
expect![[r#"
6331+
*T*
6332+
6333+
```rust
6334+
test
6335+
```
6336+
6337+
```rust
6338+
trait T {
6339+
fn func(),
6340+
const FLAG: i32,
6341+
type Bar,
6342+
}
6343+
```
6344+
"#]],
6345+
);
6346+
}
6347+
62556348
#[test]
62566349
fn hover_ranged_macro_call() {
62576350
check_hover_range(

0 commit comments

Comments
 (0)