Skip to content

Commit 4357698

Browse files
committed
Add hovering limitations support for variants
1 parent e0e28ec commit 4357698

File tree

8 files changed

+138
-48
lines changed

8 files changed

+138
-48
lines changed

crates/hir/src/display.rs

+16-21
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(&self.fields(f.db), has_where_clause, limit, f)?;
191+
display_fields(&self.fields(f.db), has_where_clause, limit, false, f)?;
192192
}
193193
}
194194
StructKind::Unit => _ = write_where_clause(def_id, f)?,
@@ -225,7 +225,7 @@ 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(&self.fields(f.db), has_where_clause, limit, f)?;
228+
display_fields(&self.fields(f.db), has_where_clause, limit, false, f)?;
229229
}
230230
Ok(())
231231
}
@@ -235,26 +235,32 @@ fn display_fields(
235235
fields: &[Field],
236236
has_where_clause: bool,
237237
limit: usize,
238+
in_line: bool,
238239
f: &mut HirFormatter<'_>,
239240
) -> Result<(), HirDisplayError> {
240241
let count = fields.len().min(limit);
241-
f.write_char(if !has_where_clause { ' ' } else { '\n' })?;
242+
let (indent, separator) = if in_line { ("", ' ') } else { (" ", '\n') };
243+
f.write_char(if !has_where_clause { ' ' } else { separator })?;
242244
if count == 0 {
243245
if fields.is_empty() {
244246
f.write_str("{}")?;
245247
} else {
246248
f.write_str("{ /* … */ }")?;
247249
}
248250
} else {
249-
f.write_str("{\n")?;
251+
f.write_char('{')?;
252+
f.write_char(separator)?;
250253
for field in &fields[..count] {
251-
f.write_str(" ")?;
254+
f.write_str(indent)?;
252255
field.hir_fmt(f)?;
253-
f.write_str(",\n")?;
256+
f.write_char(',')?;
257+
f.write_char(separator)?;
254258
}
255259

256260
if fields.len() > count {
257-
f.write_str(" /* … */\n")?;
261+
f.write_str(indent)?;
262+
f.write_str("/* … */")?;
263+
f.write_char(separator)?;
258264
}
259265
f.write_str("}")?;
260266
}
@@ -345,21 +351,10 @@ impl HirDisplay for Variant {
345351
}
346352
f.write_char(')')?;
347353
}
348-
VariantData::Record(fields) => {
349-
f.write_str(" {")?;
350-
let mut first = true;
351-
for (_, field) in fields.iter() {
352-
if first {
353-
first = false;
354-
f.write_char(' ')?;
355-
} else {
356-
f.write_str(", ")?;
357-
}
358-
// Enum variant fields must be pub.
359-
write!(f, "{}: ", field.name.display(f.db.upcast()))?;
360-
field.type_ref.hir_fmt(f)?;
354+
VariantData::Record(_) => {
355+
if let Some(limit) = f.entity_limit {
356+
display_fields(&self.fields(f.db), false, limit, true, f)?;
361357
}
362-
f.write_str(" }")?;
363358
}
364359
}
365360
Ok(())

crates/ide/src/hover.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct HoverConfig {
3333
pub keywords: bool,
3434
pub format: HoverDocFormat,
3535
pub max_trait_assoc_items_count: Option<usize>,
36-
pub max_struct_or_union_fields_count: Option<usize>,
36+
pub max_fields_count: Option<usize>,
3737
pub max_enum_variants_count: Option<usize>,
3838
}
3939

crates/ide/src/hover/render.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -411,17 +411,18 @@ pub(super) fn definition(
411411
trait_.display_limited(db, config.max_trait_assoc_items_count).to_string()
412412
}
413413
Definition::Adt(adt @ (Adt::Struct(_) | Adt::Union(_))) => {
414-
adt.display_limited(db, config.max_struct_or_union_fields_count).to_string()
414+
adt.display_limited(db, config.max_fields_count).to_string()
415+
}
416+
Definition::Variant(variant) => {
417+
variant.display_limited(db, config.max_fields_count).to_string()
415418
}
416419
Definition::Adt(adt @ Adt::Enum(_)) => {
417420
adt.display_limited(db, config.max_enum_variants_count).to_string()
418421
}
419422
Definition::SelfType(impl_def) => {
420423
let self_ty = &impl_def.self_ty(db);
421424
match self_ty.as_adt() {
422-
Some(adt) => {
423-
adt.display_limited(db, config.max_struct_or_union_fields_count).to_string()
424-
}
425+
Some(adt) => adt.display_limited(db, config.max_fields_count).to_string(),
425426
None => self_ty.display(db).to_string(),
426427
}
427428
}

crates/ide/src/hover/tests.rs

+108-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const HOVER_BASE_CONFIG: HoverConfig = HoverConfig {
1818
format: HoverDocFormat::Markdown,
1919
keywords: true,
2020
max_trait_assoc_items_count: None,
21-
max_struct_or_union_fields_count: Some(5),
21+
max_fields_count: Some(5),
2222
max_enum_variants_count: Some(5),
2323
};
2424

@@ -52,7 +52,7 @@ fn check(ra_fixture: &str, expect: Expect) {
5252
}
5353

5454
#[track_caller]
55-
fn check_hover_struct_or_union_fields_limit(
55+
fn check_hover_fields_limit(
5656
fields_count: impl Into<Option<usize>>,
5757
ra_fixture: &str,
5858
expect: Expect,
@@ -62,7 +62,7 @@ fn check_hover_struct_or_union_fields_limit(
6262
.hover(
6363
&HoverConfig {
6464
links_in_hover: true,
65-
max_struct_or_union_fields_count: fields_count.into(),
65+
max_fields_count: fields_count.into(),
6666
..HOVER_BASE_CONFIG
6767
},
6868
FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) },
@@ -939,7 +939,7 @@ struct Foo$0 where u32: Copy { field: u32 }
939939

940940
#[test]
941941
fn hover_record_struct_limit() {
942-
check_hover_struct_or_union_fields_limit(
942+
check_hover_fields_limit(
943943
3,
944944
r#"
945945
struct Foo$0 { a: u32, b: i32, c: i32 }
@@ -961,7 +961,7 @@ fn hover_record_struct_limit() {
961961
```
962962
"#]],
963963
);
964-
check_hover_struct_or_union_fields_limit(
964+
check_hover_fields_limit(
965965
3,
966966
r#"
967967
struct Foo$0 { a: u32 }
@@ -981,7 +981,7 @@ fn hover_record_struct_limit() {
981981
```
982982
"#]],
983983
);
984-
check_hover_struct_or_union_fields_limit(
984+
check_hover_fields_limit(
985985
3,
986986
r#"
987987
struct Foo$0 { a: u32, b: i32, c: i32, d: u32 }
@@ -1004,7 +1004,7 @@ fn hover_record_struct_limit() {
10041004
```
10051005
"#]],
10061006
);
1007-
check_hover_struct_or_union_fields_limit(
1007+
check_hover_fields_limit(
10081008
None,
10091009
r#"
10101010
struct Foo$0 { a: u32, b: i32, c: i32 }
@@ -1022,7 +1022,7 @@ fn hover_record_struct_limit() {
10221022
```
10231023
"#]],
10241024
);
1025-
check_hover_struct_or_union_fields_limit(
1025+
check_hover_fields_limit(
10261026
0,
10271027
r#"
10281028
struct Foo$0 { a: u32, b: i32, c: i32 }
@@ -1042,6 +1042,100 @@ fn hover_record_struct_limit() {
10421042
)
10431043
}
10441044

1045+
#[test]
1046+
fn hover_record_variant_limit() {
1047+
check_hover_fields_limit(
1048+
3,
1049+
r#"
1050+
enum Foo { A$0 { a: u32, b: i32, c: i32 } }
1051+
"#,
1052+
expect![[r#"
1053+
*A*
1054+
1055+
```rust
1056+
test::Foo
1057+
```
1058+
1059+
```rust
1060+
// size = 12 (0xC), align = 4
1061+
A { a: u32, b: i32, c: i32, }
1062+
```
1063+
"#]],
1064+
);
1065+
check_hover_fields_limit(
1066+
3,
1067+
r#"
1068+
enum Foo { A$0 { a: u32 } }
1069+
"#,
1070+
expect![[r#"
1071+
*A*
1072+
1073+
```rust
1074+
test::Foo
1075+
```
1076+
1077+
```rust
1078+
// size = 4, align = 4
1079+
A { a: u32, }
1080+
```
1081+
"#]],
1082+
);
1083+
check_hover_fields_limit(
1084+
3,
1085+
r#"
1086+
enum Foo { A$0 { a: u32, b: i32, c: i32, d: u32 } }
1087+
"#,
1088+
expect![[r#"
1089+
*A*
1090+
1091+
```rust
1092+
test::Foo
1093+
```
1094+
1095+
```rust
1096+
// size = 16 (0x10), align = 4
1097+
A { a: u32, b: i32, c: i32, /* … */ }
1098+
```
1099+
"#]],
1100+
);
1101+
check_hover_fields_limit(
1102+
None,
1103+
r#"
1104+
enum Foo { A$0 { a: u32, b: i32, c: i32 } }
1105+
"#,
1106+
expect![[r#"
1107+
*A*
1108+
1109+
```rust
1110+
test::Foo
1111+
```
1112+
1113+
```rust
1114+
// size = 12 (0xC), align = 4
1115+
A
1116+
```
1117+
"#]],
1118+
);
1119+
check_hover_fields_limit(
1120+
0,
1121+
r#"
1122+
enum Foo { A$0 { a: u32, b: i32, c: i32 } }
1123+
"#,
1124+
expect![[r#"
1125+
*A*
1126+
1127+
```rust
1128+
test::Foo
1129+
```
1130+
1131+
```rust
1132+
// size = 12 (0xC), align = 4
1133+
A { /* … */ }
1134+
```
1135+
"#]],
1136+
);
1137+
}
1138+
10451139
#[test]
10461140
fn hover_enum_limit() {
10471141
check_hover_enum_variants_limit(
@@ -1152,7 +1246,7 @@ fn hover_enum_limit() {
11521246

11531247
#[test]
11541248
fn hover_union_limit() {
1155-
check_hover_struct_or_union_fields_limit(
1249+
check_hover_fields_limit(
11561250
5,
11571251
r#"union Foo$0 { a: u32, b: i32 }"#,
11581252
expect![[r#"
@@ -1171,7 +1265,7 @@ fn hover_union_limit() {
11711265
```
11721266
"#]],
11731267
);
1174-
check_hover_struct_or_union_fields_limit(
1268+
check_hover_fields_limit(
11751269
1,
11761270
r#"union Foo$0 { a: u32, b: i32 }"#,
11771271
expect![[r#"
@@ -1190,7 +1284,7 @@ fn hover_union_limit() {
11901284
```
11911285
"#]],
11921286
);
1193-
check_hover_struct_or_union_fields_limit(
1287+
check_hover_fields_limit(
11941288
0,
11951289
r#"union Foo$0 { a: u32, b: i32 }"#,
11961290
expect![[r#"
@@ -1206,7 +1300,7 @@ fn hover_union_limit() {
12061300
```
12071301
"#]],
12081302
);
1209-
check_hover_struct_or_union_fields_limit(
1303+
check_hover_fields_limit(
12101304
None,
12111305
r#"union Foo$0 { a: u32, b: i32 }"#,
12121306
expect![[r#"
@@ -1691,7 +1785,7 @@ impl Thing {
16911785
```
16921786
"#]],
16931787
);
1694-
check_hover_struct_or_union_fields_limit(
1788+
check_hover_fields_limit(
16951789
None,
16961790
r#"
16971791
struct Thing { x: u32 }
@@ -6832,7 +6926,7 @@ enum Enum {
68326926
68336927
```rust
68346928
// size = 4, align = 4
6835-
RecordV { field: u32 }
6929+
RecordV { field: u32, }
68366930
```
68376931
"#]],
68386932
);

crates/ide/src/static_index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl StaticIndex<'_> {
167167
keywords: true,
168168
format: crate::HoverDocFormat::Markdown,
169169
max_trait_assoc_items_count: None,
170-
max_struct_or_union_fields_count: Some(5),
170+
max_fields_count: Some(5),
171171
max_enum_variants_count: Some(5),
172172
};
173173
let tokens = tokens.filter(|token| {

crates/rust-analyzer/src/config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ config_data! {
314314

315315
/// How many variants of an enum to display when hovering on. Show none if empty.
316316
hover_show_enumVariants: Option<usize> = Some(5),
317-
/// How many fields of a struct or union to display when hovering on. Show none if empty.
318-
hover_show_structOrUnionFields: Option<usize> = Some(5),
317+
/// How many fields of a struct, variant or union to display when hovering on. Show none if empty.
318+
hover_show_fields: Option<usize> = Some(5),
319319
/// How many associated items of a trait to display when hovering a trait.
320320
hover_show_traitAssocItems: Option<usize> = None,
321321

@@ -1114,7 +1114,7 @@ impl Config {
11141114
},
11151115
keywords: self.hover_documentation_keywords_enable().to_owned(),
11161116
max_trait_assoc_items_count: self.hover_show_traitAssocItems().to_owned(),
1117-
max_struct_or_union_fields_count: self.hover_show_structOrUnionFields().to_owned(),
1117+
max_fields_count: self.hover_show_fields().to_owned(),
11181118
max_enum_variants_count: self.hover_show_enumVariants().to_owned(),
11191119
}
11201120
}

docs/user/generated_config.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,10 @@ How to render the size information in a memory layout hover.
538538
--
539539
How many variants of an enum to display when hovering on. Show none if empty.
540540
--
541-
[[rust-analyzer.hover.show.structOrUnionFields]]rust-analyzer.hover.show.structOrUnionFields (default: `5`)::
541+
[[rust-analyzer.hover.show.fields]]rust-analyzer.hover.show.fields (default: `5`)::
542542
+
543543
--
544-
How many fields of a struct or union to display when hovering on. Show none if empty.
544+
How many fields of a struct, variant or union to display when hovering on. Show none if empty.
545545
--
546546
[[rust-analyzer.hover.show.traitAssocItems]]rust-analyzer.hover.show.traitAssocItems (default: `null`)::
547547
+

editors/code/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1161,8 +1161,8 @@
11611161
],
11621162
"minimum": 0
11631163
},
1164-
"rust-analyzer.hover.show.structOrUnionFields": {
1165-
"markdownDescription": "How many fields of a struct or union to display when hovering on. Show none if empty.",
1164+
"rust-analyzer.hover.show.fields": {
1165+
"markdownDescription": "How many fields of a struct, variant or union to display when hovering on. Show none if empty.",
11661166
"default": 5,
11671167
"type": [
11681168
"null",

0 commit comments

Comments
 (0)