Skip to content

Commit e0e28ec

Browse files
committed
fix: add a separate setting for enum variants
1 parent 6bb8598 commit e0e28ec

File tree

8 files changed

+183
-52
lines changed

8 files changed

+183
-52
lines changed

crates/hir/src/display.rs

+57-9
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl HirDisplay for Struct {
188188
StructKind::Record => {
189189
let has_where_clause = write_where_clause(def_id, f)?;
190190
if let Some(limit) = f.entity_limit {
191-
display_fields_or_variants(&self.fields(f.db), has_where_clause, limit, f)?;
191+
display_fields(&self.fields(f.db), has_where_clause, limit, f)?;
192192
}
193193
}
194194
StructKind::Unit => _ = write_where_clause(def_id, f)?,
@@ -208,7 +208,7 @@ impl HirDisplay for Enum {
208208

209209
let has_where_clause = write_where_clause(def_id, f)?;
210210
if let Some(limit) = f.entity_limit {
211-
display_fields_or_variants(&self.variants(f.db), has_where_clause, limit, f)?;
211+
display_variants(&self.variants(f.db), has_where_clause, limit, f)?;
212212
}
213213

214214
Ok(())
@@ -225,35 +225,83 @@ impl HirDisplay for Union {
225225

226226
let has_where_clause = write_where_clause(def_id, f)?;
227227
if let Some(limit) = f.entity_limit {
228-
display_fields_or_variants(&self.fields(f.db), has_where_clause, limit, f)?;
228+
display_fields(&self.fields(f.db), has_where_clause, limit, f)?;
229229
}
230230
Ok(())
231231
}
232232
}
233233

234-
fn display_fields_or_variants<T: HirDisplay>(
235-
fields_or_variants: &[T],
234+
fn display_fields(
235+
fields: &[Field],
236236
has_where_clause: bool,
237237
limit: usize,
238238
f: &mut HirFormatter<'_>,
239239
) -> Result<(), HirDisplayError> {
240-
let count = fields_or_variants.len().min(limit);
240+
let count = fields.len().min(limit);
241241
f.write_char(if !has_where_clause { ' ' } else { '\n' })?;
242242
if count == 0 {
243-
if fields_or_variants.is_empty() {
243+
if fields.is_empty() {
244244
f.write_str("{}")?;
245245
} else {
246246
f.write_str("{ /* … */ }")?;
247247
}
248248
} else {
249249
f.write_str("{\n")?;
250-
for field in &fields_or_variants[..count] {
250+
for field in &fields[..count] {
251251
f.write_str(" ")?;
252252
field.hir_fmt(f)?;
253253
f.write_str(",\n")?;
254254
}
255255

256-
if fields_or_variants.len() > count {
256+
if fields.len() > count {
257+
f.write_str(" /* … */\n")?;
258+
}
259+
f.write_str("}")?;
260+
}
261+
262+
Ok(())
263+
}
264+
265+
fn display_variants(
266+
variants: &[Variant],
267+
has_where_clause: bool,
268+
limit: usize,
269+
f: &mut HirFormatter<'_>,
270+
) -> Result<(), HirDisplayError> {
271+
let count = variants.len().min(limit);
272+
f.write_char(if !has_where_clause { ' ' } else { '\n' })?;
273+
if count == 0 {
274+
if variants.is_empty() {
275+
f.write_str("{}")?;
276+
} else {
277+
f.write_str("{ /* … */ }")?;
278+
}
279+
} else {
280+
f.write_str("{\n")?;
281+
for variant in &variants[..count] {
282+
f.write_str(" ")?;
283+
write!(f, "{}", variant.name(f.db).display(f.db.upcast()))?;
284+
match variant.kind(f.db) {
285+
StructKind::Tuple => {
286+
if variant.fields(f.db).is_empty() {
287+
f.write_str("()")?;
288+
} else {
289+
f.write_str("( /* … */ )")?;
290+
}
291+
}
292+
StructKind::Record => {
293+
if variant.fields(f.db).is_empty() {
294+
f.write_str(" {}")?;
295+
} else {
296+
f.write_str(" { /* … */ }")?;
297+
}
298+
}
299+
StructKind::Unit => {}
300+
}
301+
f.write_str(",\n")?;
302+
}
303+
304+
if variants.len() > count {
257305
f.write_str(" /* … */\n")?;
258306
}
259307
f.write_str("}")?;

crates/ide/src/hover.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ pub struct HoverConfig {
3333
pub keywords: bool,
3434
pub format: HoverDocFormat,
3535
pub max_trait_assoc_items_count: Option<usize>,
36-
pub max_adt_fields_or_variants_count: Option<usize>,
36+
pub max_struct_or_union_fields_count: Option<usize>,
37+
pub max_enum_variants_count: Option<usize>,
3738
}
3839

3940
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

crates/ide/src/hover/render.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,17 @@ pub(super) fn definition(
410410
Definition::Trait(trait_) => {
411411
trait_.display_limited(db, config.max_trait_assoc_items_count).to_string()
412412
}
413-
Definition::Adt(adt) => {
414-
adt.display_limited(db, config.max_adt_fields_or_variants_count).to_string()
413+
Definition::Adt(adt @ (Adt::Struct(_) | Adt::Union(_))) => {
414+
adt.display_limited(db, config.max_struct_or_union_fields_count).to_string()
415+
}
416+
Definition::Adt(adt @ Adt::Enum(_)) => {
417+
adt.display_limited(db, config.max_enum_variants_count).to_string()
415418
}
416419
Definition::SelfType(impl_def) => {
417420
let self_ty = &impl_def.self_ty(db);
418421
match self_ty.as_adt() {
419422
Some(adt) => {
420-
adt.display_limited(db, config.max_adt_fields_or_variants_count).to_string()
423+
adt.display_limited(db, config.max_struct_or_union_fields_count).to_string()
421424
}
422425
None => self_ty.display(db).to_string(),
423426
}

crates/ide/src/hover/tests.rs

+92-31
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const HOVER_BASE_CONFIG: HoverConfig = HoverConfig {
1818
format: HoverDocFormat::Markdown,
1919
keywords: true,
2020
max_trait_assoc_items_count: None,
21-
max_adt_fields_or_variants_count: Some(5),
21+
max_struct_or_union_fields_count: Some(5),
22+
max_enum_variants_count: Some(5),
2223
};
2324

2425
fn check_hover_no_result(ra_fixture: &str) {
@@ -51,8 +52,8 @@ fn check(ra_fixture: &str, expect: Expect) {
5152
}
5253

5354
#[track_caller]
54-
fn check_hover_adt_fields_or_variants_limit(
55-
count: Option<usize>,
55+
fn check_hover_struct_or_union_fields_limit(
56+
fields_count: impl Into<Option<usize>>,
5657
ra_fixture: &str,
5758
expect: Expect,
5859
) {
@@ -61,7 +62,33 @@ fn check_hover_adt_fields_or_variants_limit(
6162
.hover(
6263
&HoverConfig {
6364
links_in_hover: true,
64-
max_adt_fields_or_variants_count: count,
65+
max_struct_or_union_fields_count: fields_count.into(),
66+
..HOVER_BASE_CONFIG
67+
},
68+
FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) },
69+
)
70+
.unwrap()
71+
.unwrap();
72+
73+
let content = analysis.db.file_text(position.file_id);
74+
let hovered_element = &content[hover.range];
75+
76+
let actual = format!("*{hovered_element}*\n{}\n", hover.info.markup);
77+
expect.assert_eq(&actual)
78+
}
79+
80+
#[track_caller]
81+
fn check_hover_enum_variants_limit(
82+
variants_count: impl Into<Option<usize>>,
83+
ra_fixture: &str,
84+
expect: Expect,
85+
) {
86+
let (analysis, position) = fixture::position(ra_fixture);
87+
let hover = analysis
88+
.hover(
89+
&HoverConfig {
90+
links_in_hover: true,
91+
max_enum_variants_count: variants_count.into(),
6592
..HOVER_BASE_CONFIG
6693
},
6794
FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) },
@@ -912,8 +939,8 @@ struct Foo$0 where u32: Copy { field: u32 }
912939

913940
#[test]
914941
fn hover_record_struct_limit() {
915-
check_hover_adt_fields_or_variants_limit(
916-
Some(3),
942+
check_hover_struct_or_union_fields_limit(
943+
3,
917944
r#"
918945
struct Foo$0 { a: u32, b: i32, c: i32 }
919946
"#,
@@ -934,8 +961,8 @@ fn hover_record_struct_limit() {
934961
```
935962
"#]],
936963
);
937-
check_hover_adt_fields_or_variants_limit(
938-
Some(3),
964+
check_hover_struct_or_union_fields_limit(
965+
3,
939966
r#"
940967
struct Foo$0 { a: u32 }
941968
"#,
@@ -954,8 +981,8 @@ fn hover_record_struct_limit() {
954981
```
955982
"#]],
956983
);
957-
check_hover_adt_fields_or_variants_limit(
958-
Some(3),
984+
check_hover_struct_or_union_fields_limit(
985+
3,
959986
r#"
960987
struct Foo$0 { a: u32, b: i32, c: i32, d: u32 }
961988
"#,
@@ -977,7 +1004,7 @@ fn hover_record_struct_limit() {
9771004
```
9781005
"#]],
9791006
);
980-
check_hover_adt_fields_or_variants_limit(
1007+
check_hover_struct_or_union_fields_limit(
9811008
None,
9821009
r#"
9831010
struct Foo$0 { a: u32, b: i32, c: i32 }
@@ -995,8 +1022,8 @@ fn hover_record_struct_limit() {
9951022
```
9961023
"#]],
9971024
);
998-
check_hover_adt_fields_or_variants_limit(
999-
Some(0),
1025+
check_hover_struct_or_union_fields_limit(
1026+
0,
10001027
r#"
10011028
struct Foo$0 { a: u32, b: i32, c: i32 }
10021029
"#,
@@ -1017,8 +1044,8 @@ fn hover_record_struct_limit() {
10171044

10181045
#[test]
10191046
fn hover_enum_limit() {
1020-
check_hover_adt_fields_or_variants_limit(
1021-
Some(5),
1047+
check_hover_enum_variants_limit(
1048+
5,
10221049
r#"enum Foo$0 { A, B }"#,
10231050
expect![[r#"
10241051
*Foo*
@@ -1036,8 +1063,8 @@ fn hover_enum_limit() {
10361063
```
10371064
"#]],
10381065
);
1039-
check_hover_adt_fields_or_variants_limit(
1040-
Some(1),
1066+
check_hover_enum_variants_limit(
1067+
1,
10411068
r#"enum Foo$0 { A, B }"#,
10421069
expect![[r#"
10431070
*Foo*
@@ -1055,8 +1082,8 @@ fn hover_enum_limit() {
10551082
```
10561083
"#]],
10571084
);
1058-
check_hover_adt_fields_or_variants_limit(
1059-
Some(0),
1085+
check_hover_enum_variants_limit(
1086+
0,
10601087
r#"enum Foo$0 { A, B }"#,
10611088
expect![[r#"
10621089
*Foo*
@@ -1071,7 +1098,7 @@ fn hover_enum_limit() {
10711098
```
10721099
"#]],
10731100
);
1074-
check_hover_adt_fields_or_variants_limit(
1101+
check_hover_enum_variants_limit(
10751102
None,
10761103
r#"enum Foo$0 { A, B }"#,
10771104
expect![[r#"
@@ -1087,12 +1114,46 @@ fn hover_enum_limit() {
10871114
```
10881115
"#]],
10891116
);
1117+
check_hover_enum_variants_limit(
1118+
7,
1119+
r#"enum Enum$0 {
1120+
Variant {},
1121+
Variant2 { field: i32 },
1122+
Variant3 { field: i32, field2: i32 },
1123+
Variant4(),
1124+
Variant5(i32),
1125+
Variant6(i32, i32),
1126+
Variant7,
1127+
Variant8,
1128+
}"#,
1129+
expect![[r#"
1130+
*Enum*
1131+
1132+
```rust
1133+
test
1134+
```
1135+
1136+
```rust
1137+
// size = 12 (0xC), align = 4, niches = 4294967288
1138+
enum Enum {
1139+
Variant {},
1140+
Variant2 { /* … */ },
1141+
Variant3 { /* … */ },
1142+
Variant4(),
1143+
Variant5( /* … */ ),
1144+
Variant6( /* … */ ),
1145+
Variant7,
1146+
/* … */
1147+
}
1148+
```
1149+
"#]],
1150+
);
10901151
}
10911152

10921153
#[test]
10931154
fn hover_union_limit() {
1094-
check_hover_adt_fields_or_variants_limit(
1095-
Some(5),
1155+
check_hover_struct_or_union_fields_limit(
1156+
5,
10961157
r#"union Foo$0 { a: u32, b: i32 }"#,
10971158
expect![[r#"
10981159
*Foo*
@@ -1110,8 +1171,8 @@ fn hover_union_limit() {
11101171
```
11111172
"#]],
11121173
);
1113-
check_hover_adt_fields_or_variants_limit(
1114-
Some(1),
1174+
check_hover_struct_or_union_fields_limit(
1175+
1,
11151176
r#"union Foo$0 { a: u32, b: i32 }"#,
11161177
expect![[r#"
11171178
*Foo*
@@ -1129,8 +1190,8 @@ fn hover_union_limit() {
11291190
```
11301191
"#]],
11311192
);
1132-
check_hover_adt_fields_or_variants_limit(
1133-
Some(0),
1193+
check_hover_struct_or_union_fields_limit(
1194+
0,
11341195
r#"union Foo$0 { a: u32, b: i32 }"#,
11351196
expect![[r#"
11361197
*Foo*
@@ -1145,7 +1206,7 @@ fn hover_union_limit() {
11451206
```
11461207
"#]],
11471208
);
1148-
check_hover_adt_fields_or_variants_limit(
1209+
check_hover_struct_or_union_fields_limit(
11491210
None,
11501211
r#"union Foo$0 { a: u32, b: i32 }"#,
11511212
expect![[r#"
@@ -1630,12 +1691,12 @@ impl Thing {
16301691
```
16311692
"#]],
16321693
);
1633-
check_hover_adt_fields_or_variants_limit(
1694+
check_hover_struct_or_union_fields_limit(
16341695
None,
16351696
r#"
16361697
struct Thing { x: u32 }
16371698
impl Thing {
1638-
fn new() -> Self { Self$0 { x: 0 } }
1699+
fn new() -> Self$0 { Self { x: 0 } }
16391700
}
16401701
"#,
16411702
expect![[r#"
@@ -2599,8 +2660,8 @@ fn test_hover_layout_of_enum() {
25992660
```rust
26002661
// size = 16 (0x10), align = 8, niches = 254
26012662
enum Foo {
2602-
Variant1(u8, u16),
2603-
Variant2(i32, u8, i64),
2663+
Variant1( /* … */ ),
2664+
Variant2( /* … */ ),
26042665
}
26052666
```
26062667
"#]],

0 commit comments

Comments
 (0)