about summary refs log tree commit diff
diff options
context:
space:
mode:
authorroife <roifewu@gmail.com>2024-04-06 14:07:03 +0800
committerroife <roifewu@gmail.com>2024-04-16 16:27:56 +0800
commit3994fcb2452cab174b7066b610db137e7c5781b6 (patch)
tree21997af56488f3ff3816d8c788d088187a7591c1
parent3a4a69a51047d13be50439907c5c9f989d366264 (diff)
downloadrust-3994fcb2452cab174b7066b610db137e7c5781b6.tar.gz
rust-3994fcb2452cab174b7066b610db137e7c5781b6.zip
Update tests and docs for hover_show_adtFieldsOrVariants
-rw-r--r--src/tools/rust-analyzer/crates/hir/src/display.rs23
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/hover/tests.rs259
-rw-r--r--src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs2
-rw-r--r--src/tools/rust-analyzer/docs/user/generated_config.adoc4
-rw-r--r--src/tools/rust-analyzer/editors/code/package.json6
5 files changed, 235 insertions, 59 deletions
diff --git a/src/tools/rust-analyzer/crates/hir/src/display.rs b/src/tools/rust-analyzer/crates/hir/src/display.rs
index b0468ea0809..ec57708a084 100644
--- a/src/tools/rust-analyzer/crates/hir/src/display.rs
+++ b/src/tools/rust-analyzer/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<T: HirDisplay>(
     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/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
index 67f10f0374d..6e2ebd7967b 100644
--- a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
+++ b/src/tools/rust-analyzer/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<usize>,
+    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/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs
index 3b22c2ef7fb..0211fcfdf5f 100644
--- a/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs
+++ b/src/tools/rust-analyzer/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<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),
         /// How many associated items of a trait to display when hovering a trait.
         hover_show_traitAssocItems: Option<usize> = None,
diff --git a/src/tools/rust-analyzer/docs/user/generated_config.adoc b/src/tools/rust-analyzer/docs/user/generated_config.adoc
index af4483a2cc3..2df318f36dc 100644
--- a/src/tools/rust-analyzer/docs/user/generated_config.adoc
+++ b/src/tools/rust-analyzer/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/src/tools/rust-analyzer/editors/code/package.json b/src/tools/rust-analyzer/editors/code/package.json
index c387e72a0c6..504185fc226 100644
--- a/src/tools/rust-analyzer/editors/code/package.json
+++ b/src/tools/rust-analyzer/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"