Skip to content

Commit 7059ae2

Browse files
committed
Auto merge of #15847 - wasd96040501:feat/preview_adt, r=lnicola
feat: preview adt field when hover Closes #13977 ![20231108194345_rec_](https://github.com/rust-lang/rust-analyzer/assets/14040068/95894c4b-de6e-4ca4-98b3-6ab4559d0950)
2 parents 3b7c7f9 + 41bcd54 commit 7059ae2

File tree

2 files changed

+94
-26
lines changed

2 files changed

+94
-26
lines changed

crates/hir/src/display.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! HirDisplay implementations for various hir types.
22
use hir_def::{
3-
data::adt::VariantData,
3+
data::adt::{StructKind, VariantData},
44
generics::{
55
TypeOrConstParamData, TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
66
},
@@ -163,7 +163,40 @@ impl HirDisplay for Struct {
163163
write!(f, "{}", self.name(f.db).display(f.db.upcast()))?;
164164
let def_id = GenericDefId::AdtId(AdtId::StructId(self.id));
165165
write_generic_params(def_id, f)?;
166+
167+
let variant_data = self.variant_data(f.db);
168+
if let StructKind::Tuple = variant_data.kind() {
169+
f.write_char('(')?;
170+
let mut it = variant_data.fields().iter().peekable();
171+
172+
while let Some((id, _)) = it.next() {
173+
let field = Field { parent: (*self).into(), id };
174+
field.ty(f.db).hir_fmt(f)?;
175+
if it.peek().is_some() {
176+
f.write_str(", ")?;
177+
}
178+
}
179+
180+
f.write_str(");")?;
181+
}
182+
166183
write_where_clause(def_id, f)?;
184+
185+
if let StructKind::Record = variant_data.kind() {
186+
let fields = self.fields(f.db);
187+
if fields.is_empty() {
188+
f.write_str(" {}")?;
189+
} else {
190+
f.write_str(" {\n")?;
191+
for field in self.fields(f.db) {
192+
f.write_str(" ")?;
193+
field.hir_fmt(f)?;
194+
f.write_str(",\n")?;
195+
}
196+
f.write_str("}")?;
197+
}
198+
}
199+
167200
Ok(())
168201
}
169202
}
@@ -176,6 +209,18 @@ impl HirDisplay for Enum {
176209
let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id));
177210
write_generic_params(def_id, f)?;
178211
write_where_clause(def_id, f)?;
212+
213+
let variants = self.variants(f.db);
214+
if !variants.is_empty() {
215+
f.write_str(" {\n")?;
216+
for variant in variants {
217+
f.write_str(" ")?;
218+
variant.hir_fmt(f)?;
219+
f.write_str(",\n")?;
220+
}
221+
f.write_str("}")?;
222+
}
223+
179224
Ok(())
180225
}
181226
}
@@ -188,6 +233,18 @@ impl HirDisplay for Union {
188233
let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id));
189234
write_generic_params(def_id, f)?;
190235
write_where_clause(def_id, f)?;
236+
237+
let fields = self.fields(f.db);
238+
if !fields.is_empty() {
239+
f.write_str(" {\n")?;
240+
for field in self.fields(f.db) {
241+
f.write_str(" ")?;
242+
field.hir_fmt(f)?;
243+
f.write_str(",\n")?;
244+
}
245+
f.write_str("}")?;
246+
}
247+
191248
Ok(())
192249
}
193250
}

crates/ide/src/hover/tests.rs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,9 @@ impl Thing {
11361136
```
11371137
11381138
```rust
1139-
struct Thing
1139+
struct Thing {
1140+
x: u32,
1141+
}
11401142
```
11411143
"#]],
11421144
);
@@ -1155,7 +1157,9 @@ impl Thing {
11551157
```
11561158
11571159
```rust
1158-
struct Thing
1160+
struct Thing {
1161+
x: u32,
1162+
}
11591163
```
11601164
"#]],
11611165
);
@@ -1174,7 +1178,9 @@ impl Thing {
11741178
```
11751179
11761180
```rust
1177-
enum Thing
1181+
enum Thing {
1182+
A,
1183+
}
11781184
```
11791185
"#]],
11801186
);
@@ -1193,7 +1199,9 @@ impl Thing {
11931199
```
11941200
11951201
```rust
1196-
enum Thing
1202+
enum Thing {
1203+
A,
1204+
}
11971205
```
11981206
"#]],
11991207
);
@@ -2005,7 +2013,10 @@ fn test_hover_layout_of_enum() {
20052013
```
20062014
20072015
```rust
2008-
enum Foo // size = 16 (0x10), align = 8, niches = 254
2016+
enum Foo {
2017+
Variant1(u8, u16),
2018+
Variant2(i32, u8, i64),
2019+
} // size = 16 (0x10), align = 8, niches = 254
20092020
```
20102021
"#]],
20112022
);
@@ -2346,7 +2357,7 @@ fn main() { let s$0t = S{ f1:0 }; }
23462357
focus_range: 7..8,
23472358
name: "S",
23482359
kind: Struct,
2349-
description: "struct S",
2360+
description: "struct S {\n f1: u32,\n}",
23502361
},
23512362
},
23522363
],
@@ -2379,7 +2390,7 @@ fn main() { let s$0t = S{ f1:Arg(0) }; }
23792390
focus_range: 24..25,
23802391
name: "S",
23812392
kind: Struct,
2382-
description: "struct S<T>",
2393+
description: "struct S<T> {\n f1: T,\n}",
23832394
},
23842395
},
23852396
HoverGotoTypeData {
@@ -2392,7 +2403,7 @@ fn main() { let s$0t = S{ f1:Arg(0) }; }
23922403
focus_range: 7..10,
23932404
name: "Arg",
23942405
kind: Struct,
2395-
description: "struct Arg",
2406+
description: "struct Arg(u32);",
23962407
},
23972408
},
23982409
],
@@ -2438,7 +2449,7 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
24382449
focus_range: 24..25,
24392450
name: "S",
24402451
kind: Struct,
2441-
description: "struct S<T>",
2452+
description: "struct S<T> {\n f1: T,\n}",
24422453
},
24432454
},
24442455
HoverGotoTypeData {
@@ -2451,7 +2462,7 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
24512462
focus_range: 7..10,
24522463
name: "Arg",
24532464
kind: Struct,
2454-
description: "struct Arg",
2465+
description: "struct Arg(u32);",
24552466
},
24562467
},
24572468
],
@@ -2487,7 +2498,7 @@ fn main() { let s$0t = (A(1), B(2), M::C(3) ); }
24872498
focus_range: 7..8,
24882499
name: "A",
24892500
kind: Struct,
2490-
description: "struct A",
2501+
description: "struct A(u32);",
24912502
},
24922503
},
24932504
HoverGotoTypeData {
@@ -2500,7 +2511,7 @@ fn main() { let s$0t = (A(1), B(2), M::C(3) ); }
25002511
focus_range: 22..23,
25012512
name: "B",
25022513
kind: Struct,
2503-
description: "struct B",
2514+
description: "struct B(u32);",
25042515
},
25052516
},
25062517
HoverGotoTypeData {
@@ -2514,7 +2525,7 @@ fn main() { let s$0t = (A(1), B(2), M::C(3) ); }
25142525
name: "C",
25152526
kind: Struct,
25162527
container_name: "M",
2517-
description: "pub struct C",
2528+
description: "pub struct C(u32);",
25182529
},
25192530
},
25202531
],
@@ -2704,7 +2715,7 @@ fn main() { let s$0t = foo(); }
27042715
focus_range: 39..41,
27052716
name: "S1",
27062717
kind: Struct,
2707-
description: "struct S1",
2718+
description: "struct S1 {}",
27082719
},
27092720
},
27102721
HoverGotoTypeData {
@@ -2717,7 +2728,7 @@ fn main() { let s$0t = foo(); }
27172728
focus_range: 52..54,
27182729
name: "S2",
27192730
kind: Struct,
2720-
description: "struct S2",
2731+
description: "struct S2 {}",
27212732
},
27222733
},
27232734
],
@@ -2808,7 +2819,7 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
28082819
focus_range: 36..37,
28092820
name: "S",
28102821
kind: Struct,
2811-
description: "struct S",
2822+
description: "struct S {}",
28122823
},
28132824
},
28142825
],
@@ -2908,7 +2919,7 @@ fn foo(ar$0g: &impl Foo<S>) {}
29082919
focus_range: 23..24,
29092920
name: "S",
29102921
kind: Struct,
2911-
description: "struct S",
2922+
description: "struct S {}",
29122923
},
29132924
},
29142925
],
@@ -2945,7 +2956,7 @@ fn main() { let s$0t = foo(); }
29452956
focus_range: 49..50,
29462957
name: "B",
29472958
kind: Struct,
2948-
description: "struct B<T>",
2959+
description: "struct B<T> {}",
29492960
},
29502961
},
29512962
HoverGotoTypeData {
@@ -3034,7 +3045,7 @@ fn foo(ar$0g: &dyn Foo<S>) {}
30343045
focus_range: 23..24,
30353046
name: "S",
30363047
kind: Struct,
3037-
description: "struct S",
3048+
description: "struct S {}",
30383049
},
30393050
},
30403051
],
@@ -3082,7 +3093,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
30823093
focus_range: 50..51,
30833094
name: "B",
30843095
kind: Struct,
3085-
description: "struct B<T>",
3096+
description: "struct B<T> {}",
30863097
},
30873098
},
30883099
HoverGotoTypeData {
@@ -3108,7 +3119,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
31083119
focus_range: 65..66,
31093120
name: "S",
31103121
kind: Struct,
3111-
description: "struct S",
3122+
description: "struct S {}",
31123123
},
31133124
},
31143125
],
@@ -3335,7 +3346,7 @@ struct S$0T<const C: usize = 1, T = Foo>(T);
33353346
```
33363347
33373348
```rust
3338-
struct ST<const C: usize = 1, T = Foo>
3349+
struct ST<const C: usize = 1, T = Foo>(T);
33393350
```
33403351
"#]],
33413352
);
@@ -3356,7 +3367,7 @@ struct S$0T<const C: usize = {40 + 2}, T = Foo>(T);
33563367
```
33573368
33583369
```rust
3359-
struct ST<const C: usize = {const}, T = Foo>
3370+
struct ST<const C: usize = {const}, T = Foo>(T);
33603371
```
33613372
"#]],
33623373
);
@@ -3378,7 +3389,7 @@ struct S$0T<const C: usize = VAL, T = Foo>(T);
33783389
```
33793390
33803391
```rust
3381-
struct ST<const C: usize = VAL, T = Foo>
3392+
struct ST<const C: usize = VAL, T = Foo>(T);
33823393
```
33833394
"#]],
33843395
);
@@ -5935,7 +5946,7 @@ pub struct Foo(i32);
59355946
```
59365947
59375948
```rust
5938-
pub struct Foo // size = 4, align = 4
5949+
pub struct Foo(i32); // size = 4, align = 4
59395950
```
59405951
59415952
---

0 commit comments

Comments
 (0)