Skip to content

Commit cc620f6

Browse files
committed
feat: add hover display for trait assoc items
1 parent 58de0b1 commit cc620f6

File tree

2 files changed

+139
-22
lines changed

2 files changed

+139
-22
lines changed

crates/hir/src/display.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use hir_ty::{
1717
};
1818

1919
use crate::{
20-
Adt, AsAssocItem, AssocItemContainer, Const, ConstParam, Enum, ExternCrateDecl, Field,
21-
Function, GenericParam, HasCrate, HasVisibility, LifetimeParam, Macro, Module, SelfParam,
22-
Static, Struct, Trait, TraitAlias, TyBuilder, Type, TypeAlias, TypeOrConstParam, TypeParam,
23-
Union, Variant,
20+
Adt, AsAssocItem, AssocItem, AssocItemContainer, Const, ConstParam, Enum, ExternCrateDecl,
21+
Field, Function, GenericParam, HasCrate, HasVisibility, LifetimeParam, Macro, Module,
22+
SelfParam, Static, Struct, Trait, TraitAlias, TyBuilder, Type, TypeAlias, TypeOrConstParam,
23+
TypeParam, Union, Variant,
2424
};
2525

2626
impl HirDisplay for Function {
@@ -586,6 +586,30 @@ impl HirDisplay for Trait {
586586
let def_id = GenericDefId::TraitId(self.id);
587587
write_generic_params(def_id, f)?;
588588
write_where_clause(def_id, f)?;
589+
590+
let assoc_items = self.items(f.db);
591+
if assoc_items.is_empty() {
592+
f.write_str(" {}")?;
593+
} else {
594+
f.write_str(" {\n")?;
595+
for item in assoc_items {
596+
f.write_str(" ")?;
597+
match item {
598+
AssocItem::Function(func) => {
599+
func.hir_fmt(f)?;
600+
}
601+
AssocItem::Const(cst) => {
602+
cst.hir_fmt(f)?;
603+
}
604+
AssocItem::TypeAlias(type_alias) => {
605+
type_alias.hir_fmt(f)?;
606+
}
607+
};
608+
f.write_str(",\n")?;
609+
}
610+
f.write_str("}")?;
611+
}
612+
589613
Ok(())
590614
}
591615
}

crates/ide/src/hover/tests.rs

Lines changed: 111 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,7 +2558,7 @@ fn main() { let s$0t = foo(); }
25582558
focus_range: 6..9,
25592559
name: "Foo",
25602560
kind: Trait,
2561-
description: "trait Foo",
2561+
description: "trait Foo {}",
25622562
},
25632563
},
25642564
],
@@ -2592,7 +2592,7 @@ fn main() { let s$0t = foo(); }
25922592
focus_range: 6..9,
25932593
name: "Foo",
25942594
kind: Trait,
2595-
description: "trait Foo<T>",
2595+
description: "trait Foo<T> {}",
25962596
},
25972597
},
25982598
HoverGotoTypeData {
@@ -2639,7 +2639,7 @@ fn main() { let s$0t = foo(); }
26392639
focus_range: 6..9,
26402640
name: "Foo",
26412641
kind: Trait,
2642-
description: "trait Foo",
2642+
description: "trait Foo {}",
26432643
},
26442644
},
26452645
HoverGotoTypeData {
@@ -2652,7 +2652,7 @@ fn main() { let s$0t = foo(); }
26522652
focus_range: 19..22,
26532653
name: "Bar",
26542654
kind: Trait,
2655-
description: "trait Bar",
2655+
description: "trait Bar {}",
26562656
},
26572657
},
26582658
],
@@ -2689,7 +2689,7 @@ fn main() { let s$0t = foo(); }
26892689
focus_range: 6..9,
26902690
name: "Foo",
26912691
kind: Trait,
2692-
description: "trait Foo<T>",
2692+
description: "trait Foo<T> {}",
26932693
},
26942694
},
26952695
HoverGotoTypeData {
@@ -2702,7 +2702,7 @@ fn main() { let s$0t = foo(); }
27022702
focus_range: 22..25,
27032703
name: "Bar",
27042704
kind: Trait,
2705-
description: "trait Bar<T>",
2705+
description: "trait Bar<T> {}",
27062706
},
27072707
},
27082708
HoverGotoTypeData {
@@ -2759,7 +2759,7 @@ fn foo(ar$0g: &impl Foo) {}
27592759
focus_range: 6..9,
27602760
name: "Foo",
27612761
kind: Trait,
2762-
description: "trait Foo",
2762+
description: "trait Foo {}",
27632763
},
27642764
},
27652765
],
@@ -2793,7 +2793,7 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
27932793
focus_range: 6..9,
27942794
name: "Foo",
27952795
kind: Trait,
2796-
description: "trait Foo",
2796+
description: "trait Foo {}",
27972797
},
27982798
},
27992799
HoverGotoTypeData {
@@ -2806,7 +2806,7 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
28062806
focus_range: 19..22,
28072807
name: "Bar",
28082808
kind: Trait,
2809-
description: "trait Bar<T>",
2809+
description: "trait Bar<T> {}",
28102810
},
28112811
},
28122812
HoverGotoTypeData {
@@ -2861,7 +2861,7 @@ pub mod future {
28612861
name: "Future",
28622862
kind: Trait,
28632863
container_name: "future",
2864-
description: "pub trait Future",
2864+
description: "pub trait Future {}",
28652865
},
28662866
},
28672867
HoverGotoTypeData {
@@ -2906,7 +2906,7 @@ fn foo(ar$0g: &impl Foo<S>) {}
29062906
focus_range: 6..9,
29072907
name: "Foo",
29082908
kind: Trait,
2909-
description: "trait Foo<T>",
2909+
description: "trait Foo<T> {}",
29102910
},
29112911
},
29122912
HoverGotoTypeData {
@@ -2969,7 +2969,7 @@ fn main() { let s$0t = foo(); }
29692969
focus_range: 6..9,
29702970
name: "Foo",
29712971
kind: Trait,
2972-
description: "trait Foo",
2972+
description: "trait Foo {}",
29732973
},
29742974
},
29752975
],
@@ -3000,7 +3000,7 @@ fn foo(ar$0g: &dyn Foo) {}
30003000
focus_range: 6..9,
30013001
name: "Foo",
30023002
kind: Trait,
3003-
description: "trait Foo",
3003+
description: "trait Foo {}",
30043004
},
30053005
},
30063006
],
@@ -3032,7 +3032,7 @@ fn foo(ar$0g: &dyn Foo<S>) {}
30323032
focus_range: 6..9,
30333033
name: "Foo",
30343034
kind: Trait,
3035-
description: "trait Foo<T>",
3035+
description: "trait Foo<T> {}",
30363036
},
30373037
},
30383038
HoverGotoTypeData {
@@ -3080,7 +3080,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
30803080
focus_range: 6..15,
30813081
name: "ImplTrait",
30823082
kind: Trait,
3083-
description: "trait ImplTrait<T>",
3083+
description: "trait ImplTrait<T> {}",
30843084
},
30853085
},
30863086
HoverGotoTypeData {
@@ -3106,7 +3106,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
31063106
focus_range: 28..36,
31073107
name: "DynTrait",
31083108
kind: Trait,
3109-
description: "trait DynTrait<T>",
3109+
description: "trait DynTrait<T> {}",
31103110
},
31113111
},
31123112
HoverGotoTypeData {
@@ -3161,7 +3161,7 @@ fn main() { let s$0t = test().get(); }
31613161
focus_range: 6..9,
31623162
name: "Foo",
31633163
kind: Trait,
3164-
description: "trait Foo",
3164+
description: "trait Foo {\n type Item,\n fn get(self) -> Self::Item,\n}",
31653165
},
31663166
},
31673167
],
@@ -3226,7 +3226,7 @@ fn foo<T: Foo>(t: T$0){}
32263226
focus_range: 6..9,
32273227
name: "Foo",
32283228
kind: Trait,
3229-
description: "trait Foo",
3229+
description: "trait Foo {}",
32303230
},
32313231
},
32323232
],
@@ -6103,6 +6103,99 @@ impl T for () {
61036103
);
61046104
}
61056105

6106+
#[test]
6107+
fn hover_trait_show_assoc_items() {
6108+
check(
6109+
r#"
6110+
trait T {}
6111+
impl T$0 for () {}
6112+
"#,
6113+
expect![[r#"
6114+
*T*
6115+
6116+
```rust
6117+
test
6118+
```
6119+
6120+
```rust
6121+
trait T {}
6122+
```
6123+
"#]],
6124+
);
6125+
6126+
check(
6127+
r#"
6128+
trait T {
6129+
fn func() {}
6130+
}
6131+
impl T$0 for () {}
6132+
"#,
6133+
expect![[r#"
6134+
*T*
6135+
6136+
```rust
6137+
test
6138+
```
6139+
6140+
```rust
6141+
trait T {
6142+
fn func(),
6143+
}
6144+
```
6145+
"#]],
6146+
);
6147+
6148+
check(
6149+
r#"
6150+
trait T {
6151+
fn func() {}
6152+
const FLAG: i32 = 34;
6153+
}
6154+
impl T$0 for () {}
6155+
"#,
6156+
expect![[r#"
6157+
*T*
6158+
6159+
```rust
6160+
test
6161+
```
6162+
6163+
```rust
6164+
trait T {
6165+
fn func(),
6166+
const FLAG: i32,
6167+
}
6168+
```
6169+
"#]],
6170+
);
6171+
6172+
check(
6173+
r#"
6174+
trait T {
6175+
fn func() {}
6176+
const FLAG: i32 = 34;
6177+
type Bar;
6178+
}
6179+
impl T$0 for () {}
6180+
"#,
6181+
expect![[r#"
6182+
*T*
6183+
6184+
```rust
6185+
test
6186+
```
6187+
6188+
```rust
6189+
trait T {
6190+
fn func(),
6191+
const FLAG: i32,
6192+
type Bar,
6193+
}
6194+
```
6195+
"#]],
6196+
);
6197+
}
6198+
61066199
#[test]
61076200
fn hover_ranged_macro_call() {
61086201
check_hover_range(

0 commit comments

Comments
 (0)