From 01c3559bf3d189aa61238d022ac0e89547a719f4 Mon Sep 17 00:00:00 2001 From: roife Date: Sat, 6 Apr 2024 14:07:03 +0800 Subject: [PATCH] Update tests and docs for hover_show_adtFieldsOrVariants --- crates/hir/src/display.rs | 23 +-- crates/ide/src/hover/tests.rs | 259 +++++++++++++++++++++++++---- crates/rust-analyzer/src/config.rs | 2 +- docs/user/generated_config.adoc | 4 +- editors/code/package.json | 6 +- 5 files changed, 235 insertions(+), 59 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index b0468ea080..ec57708a08 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -188,12 +188,7 @@ impl HirDisplay for Struct { StructKind::Record => { let has_where_clause = write_where_clause(def_id, f)?; if let Some(limit) = f.entity_limit { - display_fields_or_variants( - &self.fields(f.db), - has_where_clause, - limit, - f, - )?; + display_fields_or_variants(&self.fields(f.db), has_where_clause, limit, 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)?; if let Some(limit) = f.entity_limit { - display_fields_or_variants( - &self.variants(f.db), - has_where_clause, - limit, - f, - )?; + display_fields_or_variants(&self.variants(f.db), has_where_clause, limit, f)?; } Ok(()) @@ -235,12 +225,7 @@ impl HirDisplay for Union { let has_where_clause = write_where_clause(def_id, f)?; if let Some(limit) = f.entity_limit { - display_fields_or_variants( - &self.fields(f.db), - has_where_clause, - limit, - f, - )?; + display_fields_or_variants(&self.fields(f.db), has_where_clause, limit, f)?; } Ok(()) } @@ -251,7 +236,7 @@ fn display_fields_or_variants( has_where_clause: bool, limit: usize, f: &mut HirFormatter<'_>, -)-> Result<(), HirDisplayError> { +) -> Result<(), HirDisplayError> { let count = fields_or_variants.len().min(limit); f.write_char(if !has_where_clause { ' ' } else { '\n' })?; if count == 0 { diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 67f10f0374..6e2ebd7967 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -18,7 +18,7 @@ const HOVER_BASE_CONFIG: HoverConfig = HoverConfig { format: HoverDocFormat::Markdown, keywords: true, 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) { @@ -51,13 +51,17 @@ fn check(ra_fixture: &str, expect: Expect) { } #[track_caller] -fn check_hover_struct_limit(count: usize, ra_fixture: &str, expect: Expect) { +fn check_hover_adt_fields_or_variants_limit( + count: Option, + ra_fixture: &str, + expect: Expect, +) { let (analysis, position) = fixture::position(ra_fixture); let hover = analysis .hover( &HoverConfig { links_in_hover: true, - max_struct_field_count: Some(count), + max_adt_fields_or_variants_count: count, ..HOVER_BASE_CONFIG }, FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) }, @@ -876,7 +880,9 @@ struct Foo$0 { field: u32 } ```rust // size = 4, align = 4 - struct Foo + struct Foo { + field: u32, + } ``` "#]], ); @@ -896,6 +902,9 @@ struct Foo$0 where u32: Copy { field: u32 } struct Foo where u32: Copy, + { + field: u32, + } ``` "#]], ); @@ -903,8 +912,8 @@ struct Foo$0 where u32: Copy { field: u32 } #[test] fn hover_record_struct_limit() { - check_hover_struct_limit( - 3, + check_hover_adt_fields_or_variants_limit( + Some(3), r#" struct Foo$0 { a: u32, b: i32, c: i32 } "#, @@ -917,7 +926,7 @@ fn hover_record_struct_limit() { ```rust // size = 12 (0xC), align = 4 - struct Foo { + struct Foo { a: u32, b: i32, c: i32, @@ -925,8 +934,8 @@ fn hover_record_struct_limit() { ``` "#]], ); - check_hover_struct_limit( - 3, + check_hover_adt_fields_or_variants_limit( + Some(3), r#" struct Foo$0 { a: u32 } "#, @@ -939,14 +948,14 @@ fn hover_record_struct_limit() { ```rust // size = 4, align = 4 - struct Foo { + struct Foo { a: u32, } ``` "#]], ); - check_hover_struct_limit( - 3, + check_hover_adt_fields_or_variants_limit( + Some(3), r#" struct Foo$0 { a: u32, b: i32, c: i32, d: u32 } "#, @@ -959,7 +968,7 @@ fn hover_record_struct_limit() { ```rust // size = 16 (0x10), align = 4 - struct Foo { + struct Foo { a: u32, b: 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] @@ -1462,18 +1655,16 @@ impl Thing { } "#, expect![[r#" - *Self* + *Self* - ```rust - test - ``` + ```rust + test + ``` - ```rust - enum Thing { - A, - } - ``` - "#]], + ```rust + enum Thing + ``` + "#]], ); check( r#" @@ -1483,18 +1674,16 @@ impl Thing { } "#, expect![[r#" - *Self* + *Self* - ```rust - test - ``` + ```rust + test + ``` - ```rust - enum Thing { - A, - } - ``` - "#]], + ```rust + enum Thing + ``` + "#]], ); check( r#" @@ -7936,7 +8125,9 @@ struct Pedro$0<'a> { ```rust // size = 16 (0x10), align = 8, niches = 1 - struct Pedro<'a> + struct Pedro<'a> { + hola: &str, + } ``` "#]], ) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 3b22c2ef7f..0211fcfdf5 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -312,7 +312,7 @@ config_data! { /// How to render the size information in a memory layout hover. hover_memoryLayout_size: Option = 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 = Some(10), /// How many associated items of a trait to display when hovering a trait. hover_show_traitAssocItems: Option = None, diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index af4483a2cc..2df318f36d 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -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. -- -[[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`):: + diff --git a/editors/code/package.json b/editors/code/package.json index c387e72a0c..504185fc22 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1152,9 +1152,9 @@ } ] }, - "rust-analyzer.hover.show.structFields": { - "markdownDescription": "How many fields of a struct to display when hovering a struct.", - "default": null, + "rust-analyzer.hover.show.adtFieldsOrVariants": { + "markdownDescription": "How many fields or variants of an ADT (struct, enum or union) to display when hovering on. Show none if empty.", + "default": 10, "type": [ "null", "integer"