Skip to content

Commit e52bb8c

Browse files
committed
Auto merge of rust-lang#16906 - Young-Flash:limit_struct_hover_display, r=Veykril
feat: limit struct hover display nums follow up rust-lang/rust-analyzer#15847, rust-lang/rust-analyzer#15938
2 parents 9b79fa2 + 58013ad commit e52bb8c

File tree

8 files changed

+149
-33
lines changed

8 files changed

+149
-33
lines changed

crates/hir/src/display.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,29 @@ impl HirDisplay for Struct {
186186
}
187187
StructKind::Record => {
188188
let has_where_clause = write_where_clause(def_id, f)?;
189-
let fields = self.fields(f.db);
190-
f.write_char(if !has_where_clause { ' ' } else { '\n' })?;
191-
if fields.is_empty() {
192-
f.write_str("{}")?;
193-
} else {
194-
f.write_str("{\n")?;
195-
for field in self.fields(f.db) {
196-
f.write_str(" ")?;
197-
field.hir_fmt(f)?;
198-
f.write_str(",\n")?;
189+
if let Some(limit) = f.entity_limit {
190+
let fields = self.fields(f.db);
191+
let count = fields.len().min(limit);
192+
f.write_char(if !has_where_clause { ' ' } else { '\n' })?;
193+
if count == 0 {
194+
if fields.is_empty() {
195+
f.write_str("{}")?;
196+
} else {
197+
f.write_str("{ /* … */ }")?;
198+
}
199+
} else {
200+
f.write_str(" {\n")?;
201+
for field in &fields[..count] {
202+
f.write_str(" ")?;
203+
field.hir_fmt(f)?;
204+
f.write_str(",\n")?;
205+
}
206+
207+
if fields.len() > count {
208+
f.write_str(" /* … */\n")?;
209+
}
210+
f.write_str("}")?;
199211
}
200-
f.write_str("}")?;
201212
}
202213
}
203214
StructKind::Unit => _ = write_where_clause(def_id, f)?,

crates/ide/src/hover.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +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_field_count: Option<usize>,
3637
}
3738

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

crates/ide/src/hover/render.rs

+3
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ 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::Struct(struct_)) => {
414+
struct_.display_limited(db, config.max_struct_field_count).to_string()
415+
}
413416
_ => def.label(db),
414417
};
415418
let docs = def.docs(db, famous_defs);

crates/ide/src/hover/tests.rs

+105-22
Original file line numberDiff line numberDiff line change
@@ -18,6 +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_field_count: None,
2122
};
2223

2324
fn check_hover_no_result(ra_fixture: &str) {
@@ -49,6 +50,28 @@ fn check(ra_fixture: &str, expect: Expect) {
4950
expect.assert_eq(&actual)
5051
}
5152

53+
#[track_caller]
54+
fn check_hover_struct_limit(count: usize, ra_fixture: &str, expect: Expect) {
55+
let (analysis, position) = fixture::position(ra_fixture);
56+
let hover = analysis
57+
.hover(
58+
&HoverConfig {
59+
links_in_hover: true,
60+
max_struct_field_count: Some(count),
61+
..HOVER_BASE_CONFIG
62+
},
63+
FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) },
64+
)
65+
.unwrap()
66+
.unwrap();
67+
68+
let content = analysis.db.file_text(position.file_id);
69+
let hovered_element = &content[hover.range];
70+
71+
let actual = format!("*{hovered_element}*\n{}\n", hover.info.markup);
72+
expect.assert_eq(&actual)
73+
}
74+
5275
#[track_caller]
5376
fn check_assoc_count(count: usize, ra_fixture: &str, expect: Expect) {
5477
let (analysis, position) = fixture::position(ra_fixture);
@@ -853,9 +876,7 @@ struct Foo$0 { field: u32 }
853876
854877
```rust
855878
// size = 4, align = 4
856-
struct Foo {
857-
field: u32,
858-
}
879+
struct Foo
859880
```
860881
"#]],
861882
);
@@ -875,8 +896,74 @@ struct Foo$0 where u32: Copy { field: u32 }
875896
struct Foo
876897
where
877898
u32: Copy,
878-
{
879-
field: u32,
899+
```
900+
"#]],
901+
);
902+
}
903+
904+
#[test]
905+
fn hover_record_struct_limit() {
906+
check_hover_struct_limit(
907+
3,
908+
r#"
909+
struct Foo$0 { a: u32, b: i32, c: i32 }
910+
"#,
911+
expect![[r#"
912+
*Foo*
913+
914+
```rust
915+
test
916+
```
917+
918+
```rust
919+
// size = 12 (0xC), align = 4
920+
struct Foo {
921+
a: u32,
922+
b: i32,
923+
c: i32,
924+
}
925+
```
926+
"#]],
927+
);
928+
check_hover_struct_limit(
929+
3,
930+
r#"
931+
struct Foo$0 { a: u32 }
932+
"#,
933+
expect![[r#"
934+
*Foo*
935+
936+
```rust
937+
test
938+
```
939+
940+
```rust
941+
// size = 4, align = 4
942+
struct Foo {
943+
a: u32,
944+
}
945+
```
946+
"#]],
947+
);
948+
check_hover_struct_limit(
949+
3,
950+
r#"
951+
struct Foo$0 { a: u32, b: i32, c: i32, d: u32 }
952+
"#,
953+
expect![[r#"
954+
*Foo*
955+
956+
```rust
957+
test
958+
```
959+
960+
```rust
961+
// size = 16 (0x10), align = 4
962+
struct Foo {
963+
a: u32,
964+
b: i32,
965+
c: i32,
966+
/* … */
880967
}
881968
```
882969
"#]],
@@ -1344,9 +1431,7 @@ impl Thing {
13441431
```
13451432
13461433
```rust
1347-
struct Thing {
1348-
x: u32,
1349-
}
1434+
struct Thing
13501435
```
13511436
"#]],
13521437
);
@@ -1365,9 +1450,7 @@ impl Thing {
13651450
```
13661451
13671452
```rust
1368-
struct Thing {
1369-
x: u32,
1370-
}
1453+
struct Thing
13711454
```
13721455
"#]],
13731456
);
@@ -2599,7 +2682,7 @@ fn main() { let s$0t = S{ f1:0 }; }
25992682
focus_range: 7..8,
26002683
name: "S",
26012684
kind: Struct,
2602-
description: "struct S {\n f1: u32,\n}",
2685+
description: "struct S",
26032686
},
26042687
},
26052688
],
@@ -2645,7 +2728,7 @@ fn main() { let s$0t = S{ f1:Arg(0) }; }
26452728
focus_range: 24..25,
26462729
name: "S",
26472730
kind: Struct,
2648-
description: "struct S<T> {\n f1: T,\n}",
2731+
description: "struct S<T>",
26492732
},
26502733
},
26512734
],
@@ -2704,7 +2787,7 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
27042787
focus_range: 24..25,
27052788
name: "S",
27062789
kind: Struct,
2707-
description: "struct S<T> {\n f1: T,\n}",
2790+
description: "struct S<T>",
27082791
},
27092792
},
27102793
],
@@ -2957,7 +3040,7 @@ fn main() { let s$0t = foo(); }
29573040
focus_range: 39..41,
29583041
name: "S1",
29593042
kind: Struct,
2960-
description: "struct S1 {}",
3043+
description: "struct S1",
29613044
},
29623045
},
29633046
HoverGotoTypeData {
@@ -2970,7 +3053,7 @@ fn main() { let s$0t = foo(); }
29703053
focus_range: 52..54,
29713054
name: "S2",
29723055
kind: Struct,
2973-
description: "struct S2 {}",
3056+
description: "struct S2",
29743057
},
29753058
},
29763059
],
@@ -3061,7 +3144,7 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
30613144
focus_range: 36..37,
30623145
name: "S",
30633146
kind: Struct,
3064-
description: "struct S {}",
3147+
description: "struct S",
30653148
},
30663149
},
30673150
],
@@ -3161,7 +3244,7 @@ fn foo(ar$0g: &impl Foo<S>) {}
31613244
focus_range: 23..24,
31623245
name: "S",
31633246
kind: Struct,
3164-
description: "struct S {}",
3247+
description: "struct S",
31653248
},
31663249
},
31673250
],
@@ -3198,7 +3281,7 @@ fn main() { let s$0t = foo(); }
31983281
focus_range: 49..50,
31993282
name: "B",
32003283
kind: Struct,
3201-
description: "struct B<T> {}",
3284+
description: "struct B<T>",
32023285
},
32033286
},
32043287
HoverGotoTypeData {
@@ -3287,7 +3370,7 @@ fn foo(ar$0g: &dyn Foo<S>) {}
32873370
focus_range: 23..24,
32883371
name: "S",
32893372
kind: Struct,
3290-
description: "struct S {}",
3373+
description: "struct S",
32913374
},
32923375
},
32933376
],
@@ -3322,7 +3405,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
33223405
focus_range: 50..51,
33233406
name: "B",
33243407
kind: Struct,
3325-
description: "struct B<T> {}",
3408+
description: "struct B<T>",
33263409
},
33273410
},
33283411
HoverGotoTypeData {
@@ -3361,7 +3444,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
33613444
focus_range: 65..66,
33623445
name: "S",
33633446
kind: Struct,
3364-
description: "struct S {}",
3447+
description: "struct S",
33653448
},
33663449
},
33673450
],

crates/ide/src/static_index.rs

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ impl StaticIndex<'_> {
167167
keywords: true,
168168
format: crate::HoverDocFormat::Markdown,
169169
max_trait_assoc_items_count: None,
170+
max_struct_field_count: None,
170171
};
171172
let tokens = tokens.filter(|token| {
172173
matches!(

crates/rust-analyzer/src/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ config_data! {
375375
/// How to render the size information in a memory layout hover.
376376
hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = "\"both\"",
377377

378+
/// How many fields of a struct to display when hovering a struct.
379+
hover_show_structFields: Option<usize> = "null",
378380
/// How many associated items of a trait to display when hovering a trait.
379381
hover_show_traitAssocItems: Option<usize> = "null",
380382

@@ -1690,6 +1692,7 @@ impl Config {
16901692
},
16911693
keywords: self.data.hover_documentation_keywords_enable,
16921694
max_trait_assoc_items_count: self.data.hover_show_traitAssocItems,
1695+
max_struct_field_count: self.data.hover_show_structFields,
16931696
}
16941697
}
16951698

docs/user/generated_config.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,11 @@ How to render the offset information in a memory layout hover.
520520
--
521521
How to render the size information in a memory layout hover.
522522
--
523+
[[rust-analyzer.hover.show.structFields]]rust-analyzer.hover.show.structFields (default: `null`)::
524+
+
525+
--
526+
How many fields of a struct to display when hovering a struct.
527+
--
523528
[[rust-analyzer.hover.show.traitAssocItems]]rust-analyzer.hover.show.traitAssocItems (default: `null`)::
524529
+
525530
--

editors/code/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,15 @@
11441144
}
11451145
]
11461146
},
1147+
"rust-analyzer.hover.show.structFields": {
1148+
"markdownDescription": "How many fields of a struct to display when hovering a struct.",
1149+
"default": null,
1150+
"type": [
1151+
"null",
1152+
"integer"
1153+
],
1154+
"minimum": 0
1155+
},
11471156
"rust-analyzer.hover.show.traitAssocItems": {
11481157
"markdownDescription": "How many associated items of a trait to display when hovering a trait.",
11491158
"default": null,

0 commit comments

Comments
 (0)