Update tests and docs for hover_show_adtFieldsOrVariants

This commit is contained in:
roife 2024-04-06 14:07:03 +08:00
parent 21da6c6164
commit 01c3559bf3
5 changed files with 235 additions and 59 deletions

View file

@ -188,12 +188,7 @@ impl HirDisplay for Struct {
StructKind::Record => { StructKind::Record => {
let has_where_clause = write_where_clause(def_id, f)?; let has_where_clause = write_where_clause(def_id, f)?;
if let Some(limit) = f.entity_limit { if let Some(limit) = f.entity_limit {
display_fields_or_variants( display_fields_or_variants(&self.fields(f.db), has_where_clause, limit, f)?;
&self.fields(f.db),
has_where_clause,
limit,
f,
)?;
} }
} }
StructKind::Unit => _ = write_where_clause(def_id, f)?, StructKind::Unit => _ = write_where_clause(def_id, f)?,
@ -213,12 +208,7 @@ impl HirDisplay for Enum {
let has_where_clause = write_where_clause(def_id, f)?; let has_where_clause = write_where_clause(def_id, f)?;
if let Some(limit) = f.entity_limit { if let Some(limit) = f.entity_limit {
display_fields_or_variants( display_fields_or_variants(&self.variants(f.db), has_where_clause, limit, f)?;
&self.variants(f.db),
has_where_clause,
limit,
f,
)?;
} }
Ok(()) Ok(())
@ -235,12 +225,7 @@ impl HirDisplay for Union {
let has_where_clause = write_where_clause(def_id, f)?; let has_where_clause = write_where_clause(def_id, f)?;
if let Some(limit) = f.entity_limit { if let Some(limit) = f.entity_limit {
display_fields_or_variants( display_fields_or_variants(&self.fields(f.db), has_where_clause, limit, f)?;
&self.fields(f.db),
has_where_clause,
limit,
f,
)?;
} }
Ok(()) Ok(())
} }
@ -251,7 +236,7 @@ fn display_fields_or_variants<T: HirDisplay>(
has_where_clause: bool, has_where_clause: bool,
limit: usize, limit: usize,
f: &mut HirFormatter<'_>, f: &mut HirFormatter<'_>,
)-> Result<(), HirDisplayError> { ) -> Result<(), HirDisplayError> {
let count = fields_or_variants.len().min(limit); let count = fields_or_variants.len().min(limit);
f.write_char(if !has_where_clause { ' ' } else { '\n' })?; f.write_char(if !has_where_clause { ' ' } else { '\n' })?;
if count == 0 { if count == 0 {

View file

@ -18,7 +18,7 @@ const HOVER_BASE_CONFIG: HoverConfig = HoverConfig {
format: HoverDocFormat::Markdown, format: HoverDocFormat::Markdown,
keywords: true, keywords: true,
max_trait_assoc_items_count: None, max_trait_assoc_items_count: None,
max_struct_field_count: None, max_adt_fields_or_variants_count: Some(10),
}; };
fn check_hover_no_result(ra_fixture: &str) { fn check_hover_no_result(ra_fixture: &str) {
@ -51,13 +51,17 @@ fn check(ra_fixture: &str, expect: Expect) {
} }
#[track_caller] #[track_caller]
fn check_hover_struct_limit(count: usize, ra_fixture: &str, expect: Expect) { fn check_hover_adt_fields_or_variants_limit(
count: Option<usize>,
ra_fixture: &str,
expect: Expect,
) {
let (analysis, position) = fixture::position(ra_fixture); let (analysis, position) = fixture::position(ra_fixture);
let hover = analysis let hover = analysis
.hover( .hover(
&HoverConfig { &HoverConfig {
links_in_hover: true, links_in_hover: true,
max_struct_field_count: Some(count), max_adt_fields_or_variants_count: count,
..HOVER_BASE_CONFIG ..HOVER_BASE_CONFIG
}, },
FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) }, FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) },
@ -876,7 +880,9 @@ struct Foo$0 { field: u32 }
```rust ```rust
// size = 4, align = 4 // size = 4, align = 4
struct Foo struct Foo {
field: u32,
}
``` ```
"#]], "#]],
); );
@ -896,6 +902,9 @@ struct Foo$0 where u32: Copy { field: u32 }
struct Foo struct Foo
where where
u32: Copy, u32: Copy,
{
field: u32,
}
``` ```
"#]], "#]],
); );
@ -903,8 +912,8 @@ struct Foo$0 where u32: Copy { field: u32 }
#[test] #[test]
fn hover_record_struct_limit() { fn hover_record_struct_limit() {
check_hover_struct_limit( check_hover_adt_fields_or_variants_limit(
3, Some(3),
r#" r#"
struct Foo$0 { a: u32, b: i32, c: i32 } struct Foo$0 { a: u32, b: i32, c: i32 }
"#, "#,
@ -917,7 +926,7 @@ fn hover_record_struct_limit() {
```rust ```rust
// size = 12 (0xC), align = 4 // size = 12 (0xC), align = 4
struct Foo { struct Foo {
a: u32, a: u32,
b: i32, b: i32,
c: i32, c: i32,
@ -925,8 +934,8 @@ fn hover_record_struct_limit() {
``` ```
"#]], "#]],
); );
check_hover_struct_limit( check_hover_adt_fields_or_variants_limit(
3, Some(3),
r#" r#"
struct Foo$0 { a: u32 } struct Foo$0 { a: u32 }
"#, "#,
@ -939,14 +948,14 @@ fn hover_record_struct_limit() {
```rust ```rust
// size = 4, align = 4 // size = 4, align = 4
struct Foo { struct Foo {
a: u32, a: u32,
} }
``` ```
"#]], "#]],
); );
check_hover_struct_limit( check_hover_adt_fields_or_variants_limit(
3, Some(3),
r#" r#"
struct Foo$0 { a: u32, b: i32, c: i32, d: u32 } struct Foo$0 { a: u32, b: i32, c: i32, d: u32 }
"#, "#,
@ -959,7 +968,7 @@ fn hover_record_struct_limit() {
```rust ```rust
// size = 16 (0x10), align = 4 // size = 16 (0x10), align = 4
struct Foo { struct Foo {
a: u32, a: u32,
b: i32, b: i32,
c: i32, c: i32,
@ -968,6 +977,190 @@ fn hover_record_struct_limit() {
``` ```
"#]], "#]],
); );
check_hover_adt_fields_or_variants_limit(
None,
r#"
struct Foo$0 { a: u32, b: i32, c: i32 }
"#,
expect![[r#"
*Foo*
```rust
test
```
```rust
// size = 12 (0xC), align = 4
struct Foo
```
"#]],
);
check_hover_adt_fields_or_variants_limit(
Some(0),
r#"
struct Foo$0 { a: u32, b: i32, c: i32 }
"#,
expect![[r#"
*Foo*
```rust
test
```
```rust
// size = 12 (0xC), align = 4
struct Foo { /**/ }
```
"#]],
)
}
#[test]
fn hover_enum_limit() {
check_hover_adt_fields_or_variants_limit(
Some(10),
r#"enum Foo$0 { A, B }"#,
expect![[r#"
*Foo*
```rust
test
```
```rust
// size = 1, align = 1, niches = 254
enum Foo {
A,
B,
}
```
"#]],
);
check_hover_adt_fields_or_variants_limit(
Some(1),
r#"enum Foo$0 { A, B }"#,
expect![[r#"
*Foo*
```rust
test
```
```rust
// size = 1, align = 1, niches = 254
enum Foo {
A,
/**/
}
```
"#]],
);
check_hover_adt_fields_or_variants_limit(
Some(0),
r#"enum Foo$0 { A, B }"#,
expect![[r#"
*Foo*
```rust
test
```
```rust
// size = 1, align = 1, niches = 254
enum Foo { /**/ }
```
"#]],
);
check_hover_adt_fields_or_variants_limit(
None,
r#"enum Foo$0 { A, B }"#,
expect![[r#"
*Foo*
```rust
test
```
```rust
// size = 1, align = 1, niches = 254
enum Foo
```
"#]],
);
}
#[test]
fn hover_union_limit() {
check_hover_adt_fields_or_variants_limit(
Some(10),
r#"union Foo$0 { a: u32, b: i32 }"#,
expect![[r#"
*Foo*
```rust
test
```
```rust
// size = 4, align = 4
union Foo {
a: u32,
b: i32,
}
```
"#]],
);
check_hover_adt_fields_or_variants_limit(
Some(1),
r#"union Foo$0 { a: u32, b: i32 }"#,
expect![[r#"
*Foo*
```rust
test
```
```rust
// size = 4, align = 4
union Foo {
a: u32,
/**/
}
```
"#]],
);
check_hover_adt_fields_or_variants_limit(
Some(0),
r#"union Foo$0 { a: u32, b: i32 }"#,
expect![[r#"
*Foo*
```rust
test
```
```rust
// size = 4, align = 4
union Foo { /**/ }
```
"#]],
);
check_hover_adt_fields_or_variants_limit(
None,
r#"union Foo$0 { a: u32, b: i32 }"#,
expect![[r#"
*Foo*
```rust
test
```
```rust
// size = 4, align = 4
union Foo
```
"#]],
);
} }
#[test] #[test]
@ -1462,18 +1655,16 @@ impl Thing {
} }
"#, "#,
expect![[r#" expect![[r#"
*Self* *Self*
```rust ```rust
test test
``` ```
```rust ```rust
enum Thing { enum Thing
A, ```
} "#]],
```
"#]],
); );
check( check(
r#" r#"
@ -1483,18 +1674,16 @@ impl Thing {
} }
"#, "#,
expect![[r#" expect![[r#"
*Self* *Self*
```rust ```rust
test test
``` ```
```rust ```rust
enum Thing { enum Thing
A, ```
} "#]],
```
"#]],
); );
check( check(
r#" r#"
@ -7936,7 +8125,9 @@ struct Pedro$0<'a> {
```rust ```rust
// size = 16 (0x10), align = 8, niches = 1 // size = 16 (0x10), align = 8, niches = 1
struct Pedro<'a> struct Pedro<'a> {
hola: &str,
}
``` ```
"#]], "#]],
) )

View file

@ -312,7 +312,7 @@ config_data! {
/// How to render the size information in a memory layout hover. /// How to render the size information in a memory layout hover.
hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = Some(MemoryLayoutHoverRenderKindDef::Both), hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = Some(MemoryLayoutHoverRenderKindDef::Both),
/// How many fields or variants of an ADT (struct, enum or union) to display when hovering on. Show all if empty. /// How many fields or variants of an ADT (struct, enum or union) to display when hovering on. Show none if empty.
hover_show_adtFieldsOrVariants: Option<usize> = Some(10), hover_show_adtFieldsOrVariants: Option<usize> = Some(10),
/// How many associated items of a trait to display when hovering a trait. /// How many associated items of a trait to display when hovering a trait.
hover_show_traitAssocItems: Option<usize> = None, hover_show_traitAssocItems: Option<usize> = None,

View file

@ -533,10 +533,10 @@ How to render the offset information in a memory layout hover.
-- --
How to render the size information in a memory layout hover. How to render the size information in a memory layout hover.
-- --
[[rust-analyzer.hover.show.structFields]]rust-analyzer.hover.show.structFields (default: `null`):: [[rust-analyzer.hover.show.adtFieldsOrVariants]]rust-analyzer.hover.show.adtFieldsOrVariants (default: `10`)::
+ +
-- --
How many fields of a struct to display when hovering a struct. How many fields or variants of an ADT (struct, enum or union) to display when hovering on. Show none if empty.
-- --
[[rust-analyzer.hover.show.traitAssocItems]]rust-analyzer.hover.show.traitAssocItems (default: `null`):: [[rust-analyzer.hover.show.traitAssocItems]]rust-analyzer.hover.show.traitAssocItems (default: `null`)::
+ +

View file

@ -1152,9 +1152,9 @@
} }
] ]
}, },
"rust-analyzer.hover.show.structFields": { "rust-analyzer.hover.show.adtFieldsOrVariants": {
"markdownDescription": "How many fields of a struct to display when hovering a struct.", "markdownDescription": "How many fields or variants of an ADT (struct, enum or union) to display when hovering on. Show none if empty.",
"default": null, "default": 10,
"type": [ "type": [
"null", "null",
"integer" "integer"