From 21da6c6164f17ac39483a73e3d10ecbd8de5b53e Mon Sep 17 00:00:00 2001 From: roife Date: Sat, 6 Apr 2024 13:57:56 +0800 Subject: [PATCH 1/7] Add config hover_show_adtFieldsOrVariants to handle hovering limitation for ADTs --- crates/hir/src/display.rs | 96 ++++++++++++++++-------------- crates/ide/src/hover.rs | 2 +- crates/ide/src/hover/render.rs | 4 +- crates/ide/src/static_index.rs | 2 +- crates/rust-analyzer/src/config.rs | 6 +- 5 files changed, 59 insertions(+), 51 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 84f03d111f27..b0468ea0809f 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -188,28 +188,12 @@ impl HirDisplay for Struct { StructKind::Record => { let has_where_clause = write_where_clause(def_id, f)?; if let Some(limit) = f.entity_limit { - let fields = self.fields(f.db); - let count = fields.len().min(limit); - f.write_char(if !has_where_clause { ' ' } else { '\n' })?; - if count == 0 { - if fields.is_empty() { - f.write_str("{}")?; - } else { - f.write_str("{ /* … */ }")?; - } - } else { - f.write_str(" {\n")?; - for field in &fields[..count] { - f.write_str(" ")?; - field.hir_fmt(f)?; - f.write_str(",\n")?; - } - - if fields.len() > count { - f.write_str(" /* … */\n")?; - } - f.write_str("}")?; - } + display_fields_or_variants( + &self.fields(f.db), + has_where_clause, + limit, + f, + )?; } } StructKind::Unit => _ = write_where_clause(def_id, f)?, @@ -226,18 +210,15 @@ impl HirDisplay for Enum { write!(f, "{}", self.name(f.db).display(f.db.upcast()))?; let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id)); write_generic_params(def_id, f)?; - let has_where_clause = write_where_clause(def_id, f)?; - let variants = self.variants(f.db); - if !variants.is_empty() { - f.write_char(if !has_where_clause { ' ' } else { '\n' })?; - f.write_str("{\n")?; - for variant in variants { - f.write_str(" ")?; - variant.hir_fmt(f)?; - f.write_str(",\n")?; - } - f.write_str("}")?; + 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, + )?; } Ok(()) @@ -251,22 +232,49 @@ impl HirDisplay for Union { write!(f, "{}", self.name(f.db).display(f.db.upcast()))?; let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id)); write_generic_params(def_id, f)?; + 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, + )?; + } + Ok(()) + } +} - let fields = self.fields(f.db); - if !fields.is_empty() { - f.write_char(if !has_where_clause { ' ' } else { '\n' })?; - f.write_str("{\n")?; - for field in self.fields(f.db) { - f.write_str(" ")?; - field.hir_fmt(f)?; - f.write_str(",\n")?; - } - f.write_str("}")?; +fn display_fields_or_variants( + fields_or_variants: &[T], + has_where_clause: bool, + limit: usize, + f: &mut HirFormatter<'_>, +)-> Result<(), HirDisplayError> { + let count = fields_or_variants.len().min(limit); + f.write_char(if !has_where_clause { ' ' } else { '\n' })?; + if count == 0 { + if fields_or_variants.is_empty() { + f.write_str("{}")?; + } else { + f.write_str("{ /* … */ }")?; + } + } else { + f.write_str("{\n")?; + for field in &fields_or_variants[..count] { + f.write_str(" ")?; + field.hir_fmt(f)?; + f.write_str(",\n")?; } - Ok(()) + if fields_or_variants.len() > count { + f.write_str(" /* … */\n")?; + } + f.write_str("}")?; } + + Ok(()) } impl HirDisplay for Field { diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 822751c0e4ce..2a2be6e4e0cd 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -33,7 +33,7 @@ pub struct HoverConfig { pub keywords: bool, pub format: HoverDocFormat, pub max_trait_assoc_items_count: Option, - pub max_struct_field_count: Option, + pub max_adt_fields_or_variants_count: Option, } #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 7579d4d6d8b9..33a8a4f695f4 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -410,8 +410,8 @@ pub(super) fn definition( Definition::Trait(trait_) => { trait_.display_limited(db, config.max_trait_assoc_items_count).to_string() } - Definition::Adt(Adt::Struct(struct_)) => { - struct_.display_limited(db, config.max_struct_field_count).to_string() + Definition::Adt(adt) => { + adt.display_limited(db, config.max_adt_fields_or_variants_count).to_string() } _ => def.label(db), }; diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index 3fef16df25e3..012778f0586f 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -167,7 +167,7 @@ impl StaticIndex<'_> { keywords: true, format: crate::HoverDocFormat::Markdown, max_trait_assoc_items_count: None, - max_struct_field_count: None, + max_adt_fields_or_variants_count: Some(10), }; let tokens = tokens.filter(|token| { matches!( diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 14a418565429..3b22c2ef7fbf 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -312,8 +312,8 @@ config_data! { /// How to render the size information in a memory layout hover. hover_memoryLayout_size: Option = Some(MemoryLayoutHoverRenderKindDef::Both), - /// How many fields of a struct to display when hovering a struct. - hover_show_structFields: Option = None, + /// How many fields or variants of an ADT (struct, enum or union) to display when hovering on. Show all 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, @@ -1112,7 +1112,7 @@ impl Config { }, keywords: self.hover_documentation_keywords_enable().to_owned(), max_trait_assoc_items_count: self.hover_show_traitAssocItems().to_owned(), - max_struct_field_count: self.hover_show_structFields().to_owned(), + max_adt_fields_or_variants_count: self.hover_show_adtFieldsOrVariants().to_owned(), } } From 01c3559bf3d189aa61238d022ac0e89547a719f4 Mon Sep 17 00:00:00 2001 From: roife Date: Sat, 6 Apr 2024 14:07:03 +0800 Subject: [PATCH 2/7] 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 b0468ea0809f..ec57708a084e 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 67f10f0374d8..6e2ebd7967be 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 3b22c2ef7fbf..0211fcfdf5f1 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 af4483a2cc31..2df318f36dc8 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 c387e72a0c6f..504185fc226c 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" From c06d670f8f9333a1381792a466e9e07d32d500b0 Mon Sep 17 00:00:00 2001 From: roife Date: Sat, 6 Apr 2024 14:36:34 +0800 Subject: [PATCH 3/7] fix: the fields or variants of ADT was not restricted by limitations when hovering on Self type --- crates/ide/src/hover/render.rs | 9 +++++++++ crates/ide/src/hover/tests.rs | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 33a8a4f695f4..e0a9907a4138 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -413,6 +413,15 @@ pub(super) fn definition( Definition::Adt(adt) => { adt.display_limited(db, config.max_adt_fields_or_variants_count).to_string() } + Definition::SelfType(impl_def) => { + let self_ty = &impl_def.self_ty(db); + match self_ty.as_adt() { + Some(adt) => { + adt.display_limited(db, config.max_adt_fields_or_variants_count).to_string() + } + None => self_ty.display(db).to_string(), + } + } _ => def.label(db), }; let docs = def.docs(db, famous_defs); diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 6e2ebd7967be..30e64f450a15 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -1623,6 +1623,28 @@ impl Thing { test ``` + ```rust + struct Thing { + x: u32, + } + ``` + "#]], + ); + check_hover_adt_fields_or_variants_limit( + None, + r#" +struct Thing { x: u32 } +impl Thing { + fn new() -> Self { Self$0 { x: 0 } } +} +"#, + expect![[r#" + *Self* + + ```rust + test + ``` + ```rust struct Thing ``` @@ -1643,7 +1665,9 @@ impl Thing { ``` ```rust - struct Thing + struct Thing { + x: u32, + } ``` "#]], ); @@ -1662,7 +1686,9 @@ impl Thing { ``` ```rust - enum Thing + enum Thing { + A, + } ``` "#]], ); @@ -1681,7 +1707,9 @@ impl Thing { ``` ```rust - enum Thing + enum Thing { + A, + } ``` "#]], ); From 6bb85985d7449ede22c047f4692a72c1e51fe287 Mon Sep 17 00:00:00 2001 From: roife Date: Sun, 7 Apr 2024 13:24:33 +0800 Subject: [PATCH 4/7] fix: adjust the limitation for ADTs' fields to 5 --- crates/ide/src/hover/tests.rs | 6 +++--- crates/ide/src/static_index.rs | 2 +- crates/rust-analyzer/src/config.rs | 2 +- docs/user/generated_config.adoc | 2 +- editors/code/package.json | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 30e64f450a15..326c248b7a2f 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_adt_fields_or_variants_count: Some(10), + max_adt_fields_or_variants_count: Some(5), }; fn check_hover_no_result(ra_fixture: &str) { @@ -1018,7 +1018,7 @@ fn hover_record_struct_limit() { #[test] fn hover_enum_limit() { check_hover_adt_fields_or_variants_limit( - Some(10), + Some(5), r#"enum Foo$0 { A, B }"#, expect![[r#" *Foo* @@ -1092,7 +1092,7 @@ fn hover_enum_limit() { #[test] fn hover_union_limit() { check_hover_adt_fields_or_variants_limit( - Some(10), + Some(5), r#"union Foo$0 { a: u32, b: i32 }"#, expect![[r#" *Foo* diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index 012778f0586f..1378ab2ab0f7 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -167,7 +167,7 @@ impl StaticIndex<'_> { keywords: true, format: crate::HoverDocFormat::Markdown, max_trait_assoc_items_count: None, - max_adt_fields_or_variants_count: Some(10), + max_adt_fields_or_variants_count: Some(5), }; let tokens = tokens.filter(|token| { matches!( diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 0211fcfdf5f1..10bc19b98879 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -313,7 +313,7 @@ config_data! { 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 none if empty. - hover_show_adtFieldsOrVariants: Option = Some(10), + hover_show_adtFieldsOrVariants: Option = Some(5), /// 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 2df318f36dc8..d275b51abc3f 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -533,7 +533,7 @@ 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.adtFieldsOrVariants]]rust-analyzer.hover.show.adtFieldsOrVariants (default: `10`):: +[[rust-analyzer.hover.show.adtFieldsOrVariants]]rust-analyzer.hover.show.adtFieldsOrVariants (default: `5`):: + -- How many fields or variants of an ADT (struct, enum or union) to display when hovering on. Show none if empty. diff --git a/editors/code/package.json b/editors/code/package.json index 504185fc226c..88a382f1fb81 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1154,7 +1154,7 @@ }, "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, + "default": 5, "type": [ "null", "integer" From e0e28ec856d70abe55013068d2faf44807eb41a9 Mon Sep 17 00:00:00 2001 From: roife Date: Fri, 19 Apr 2024 21:31:54 +0800 Subject: [PATCH 5/7] fix: add a separate setting for enum variants --- crates/hir/src/display.rs | 66 +++++++++++++--- crates/ide/src/hover.rs | 3 +- crates/ide/src/hover/render.rs | 9 ++- crates/ide/src/hover/tests.rs | 123 +++++++++++++++++++++-------- crates/ide/src/static_index.rs | 3 +- crates/rust-analyzer/src/config.rs | 9 ++- docs/user/generated_config.adoc | 9 ++- editors/code/package.json | 13 ++- 8 files changed, 183 insertions(+), 52 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index ec57708a084e..42fafd4ee806 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -188,7 +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(&self.fields(f.db), has_where_clause, limit, f)?; } } StructKind::Unit => _ = write_where_clause(def_id, f)?, @@ -208,7 +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_variants(&self.variants(f.db), has_where_clause, limit, f)?; } Ok(()) @@ -225,35 +225,83 @@ 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(&self.fields(f.db), has_where_clause, limit, f)?; } Ok(()) } } -fn display_fields_or_variants( - fields_or_variants: &[T], +fn display_fields( + fields: &[Field], has_where_clause: bool, limit: usize, f: &mut HirFormatter<'_>, ) -> Result<(), HirDisplayError> { - let count = fields_or_variants.len().min(limit); + let count = fields.len().min(limit); f.write_char(if !has_where_clause { ' ' } else { '\n' })?; if count == 0 { - if fields_or_variants.is_empty() { + if fields.is_empty() { f.write_str("{}")?; } else { f.write_str("{ /* … */ }")?; } } else { f.write_str("{\n")?; - for field in &fields_or_variants[..count] { + for field in &fields[..count] { f.write_str(" ")?; field.hir_fmt(f)?; f.write_str(",\n")?; } - if fields_or_variants.len() > count { + if fields.len() > count { + f.write_str(" /* … */\n")?; + } + f.write_str("}")?; + } + + Ok(()) +} + +fn display_variants( + variants: &[Variant], + has_where_clause: bool, + limit: usize, + f: &mut HirFormatter<'_>, +) -> Result<(), HirDisplayError> { + let count = variants.len().min(limit); + f.write_char(if !has_where_clause { ' ' } else { '\n' })?; + if count == 0 { + if variants.is_empty() { + f.write_str("{}")?; + } else { + f.write_str("{ /* … */ }")?; + } + } else { + f.write_str("{\n")?; + for variant in &variants[..count] { + f.write_str(" ")?; + write!(f, "{}", variant.name(f.db).display(f.db.upcast()))?; + match variant.kind(f.db) { + StructKind::Tuple => { + if variant.fields(f.db).is_empty() { + f.write_str("()")?; + } else { + f.write_str("( /* … */ )")?; + } + } + StructKind::Record => { + if variant.fields(f.db).is_empty() { + f.write_str(" {}")?; + } else { + f.write_str(" { /* … */ }")?; + } + } + StructKind::Unit => {} + } + f.write_str(",\n")?; + } + + if variants.len() > count { f.write_str(" /* … */\n")?; } f.write_str("}")?; diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 2a2be6e4e0cd..6958ae16230a 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -33,7 +33,8 @@ pub struct HoverConfig { pub keywords: bool, pub format: HoverDocFormat, pub max_trait_assoc_items_count: Option, - pub max_adt_fields_or_variants_count: Option, + pub max_struct_or_union_fields_count: Option, + pub max_enum_variants_count: Option, } #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index e0a9907a4138..1044a3f4c11b 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -410,14 +410,17 @@ pub(super) fn definition( Definition::Trait(trait_) => { trait_.display_limited(db, config.max_trait_assoc_items_count).to_string() } - Definition::Adt(adt) => { - adt.display_limited(db, config.max_adt_fields_or_variants_count).to_string() + Definition::Adt(adt @ (Adt::Struct(_) | Adt::Union(_))) => { + adt.display_limited(db, config.max_struct_or_union_fields_count).to_string() + } + Definition::Adt(adt @ Adt::Enum(_)) => { + adt.display_limited(db, config.max_enum_variants_count).to_string() } Definition::SelfType(impl_def) => { let self_ty = &impl_def.self_ty(db); match self_ty.as_adt() { Some(adt) => { - adt.display_limited(db, config.max_adt_fields_or_variants_count).to_string() + adt.display_limited(db, config.max_struct_or_union_fields_count).to_string() } None => self_ty.display(db).to_string(), } diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 326c248b7a2f..f17a0fa2cd92 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -18,7 +18,8 @@ const HOVER_BASE_CONFIG: HoverConfig = HoverConfig { format: HoverDocFormat::Markdown, keywords: true, max_trait_assoc_items_count: None, - max_adt_fields_or_variants_count: Some(5), + max_struct_or_union_fields_count: Some(5), + max_enum_variants_count: Some(5), }; fn check_hover_no_result(ra_fixture: &str) { @@ -51,8 +52,8 @@ fn check(ra_fixture: &str, expect: Expect) { } #[track_caller] -fn check_hover_adt_fields_or_variants_limit( - count: Option, +fn check_hover_struct_or_union_fields_limit( + fields_count: impl Into>, ra_fixture: &str, expect: Expect, ) { @@ -61,7 +62,33 @@ fn check_hover_adt_fields_or_variants_limit( .hover( &HoverConfig { links_in_hover: true, - max_adt_fields_or_variants_count: count, + max_struct_or_union_fields_count: fields_count.into(), + ..HOVER_BASE_CONFIG + }, + FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) }, + ) + .unwrap() + .unwrap(); + + let content = analysis.db.file_text(position.file_id); + let hovered_element = &content[hover.range]; + + let actual = format!("*{hovered_element}*\n{}\n", hover.info.markup); + expect.assert_eq(&actual) +} + +#[track_caller] +fn check_hover_enum_variants_limit( + variants_count: impl Into>, + ra_fixture: &str, + expect: Expect, +) { + let (analysis, position) = fixture::position(ra_fixture); + let hover = analysis + .hover( + &HoverConfig { + links_in_hover: true, + max_enum_variants_count: variants_count.into(), ..HOVER_BASE_CONFIG }, FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) }, @@ -912,8 +939,8 @@ struct Foo$0 where u32: Copy { field: u32 } #[test] fn hover_record_struct_limit() { - check_hover_adt_fields_or_variants_limit( - Some(3), + check_hover_struct_or_union_fields_limit( + 3, r#" struct Foo$0 { a: u32, b: i32, c: i32 } "#, @@ -934,8 +961,8 @@ fn hover_record_struct_limit() { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( - Some(3), + check_hover_struct_or_union_fields_limit( + 3, r#" struct Foo$0 { a: u32 } "#, @@ -954,8 +981,8 @@ fn hover_record_struct_limit() { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( - Some(3), + check_hover_struct_or_union_fields_limit( + 3, r#" struct Foo$0 { a: u32, b: i32, c: i32, d: u32 } "#, @@ -977,7 +1004,7 @@ fn hover_record_struct_limit() { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( + check_hover_struct_or_union_fields_limit( None, r#" struct Foo$0 { a: u32, b: i32, c: i32 } @@ -995,8 +1022,8 @@ fn hover_record_struct_limit() { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( - Some(0), + check_hover_struct_or_union_fields_limit( + 0, r#" struct Foo$0 { a: u32, b: i32, c: i32 } "#, @@ -1017,8 +1044,8 @@ fn hover_record_struct_limit() { #[test] fn hover_enum_limit() { - check_hover_adt_fields_or_variants_limit( - Some(5), + check_hover_enum_variants_limit( + 5, r#"enum Foo$0 { A, B }"#, expect![[r#" *Foo* @@ -1036,8 +1063,8 @@ fn hover_enum_limit() { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( - Some(1), + check_hover_enum_variants_limit( + 1, r#"enum Foo$0 { A, B }"#, expect![[r#" *Foo* @@ -1055,8 +1082,8 @@ fn hover_enum_limit() { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( - Some(0), + check_hover_enum_variants_limit( + 0, r#"enum Foo$0 { A, B }"#, expect![[r#" *Foo* @@ -1071,7 +1098,7 @@ fn hover_enum_limit() { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( + check_hover_enum_variants_limit( None, r#"enum Foo$0 { A, B }"#, expect![[r#" @@ -1087,12 +1114,46 @@ fn hover_enum_limit() { ``` "#]], ); + check_hover_enum_variants_limit( + 7, + r#"enum Enum$0 { + Variant {}, + Variant2 { field: i32 }, + Variant3 { field: i32, field2: i32 }, + Variant4(), + Variant5(i32), + Variant6(i32, i32), + Variant7, + Variant8, + }"#, + expect![[r#" + *Enum* + + ```rust + test + ``` + + ```rust + // size = 12 (0xC), align = 4, niches = 4294967288 + enum Enum { + Variant {}, + Variant2 { /* … */ }, + Variant3 { /* … */ }, + Variant4(), + Variant5( /* … */ ), + Variant6( /* … */ ), + Variant7, + /* … */ + } + ``` + "#]], + ); } #[test] fn hover_union_limit() { - check_hover_adt_fields_or_variants_limit( - Some(5), + check_hover_struct_or_union_fields_limit( + 5, r#"union Foo$0 { a: u32, b: i32 }"#, expect![[r#" *Foo* @@ -1110,8 +1171,8 @@ fn hover_union_limit() { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( - Some(1), + check_hover_struct_or_union_fields_limit( + 1, r#"union Foo$0 { a: u32, b: i32 }"#, expect![[r#" *Foo* @@ -1129,8 +1190,8 @@ fn hover_union_limit() { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( - Some(0), + check_hover_struct_or_union_fields_limit( + 0, r#"union Foo$0 { a: u32, b: i32 }"#, expect![[r#" *Foo* @@ -1145,7 +1206,7 @@ fn hover_union_limit() { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( + check_hover_struct_or_union_fields_limit( None, r#"union Foo$0 { a: u32, b: i32 }"#, expect![[r#" @@ -1630,12 +1691,12 @@ impl Thing { ``` "#]], ); - check_hover_adt_fields_or_variants_limit( + check_hover_struct_or_union_fields_limit( None, r#" struct Thing { x: u32 } impl Thing { - fn new() -> Self { Self$0 { x: 0 } } + fn new() -> Self$0 { Self { x: 0 } } } "#, expect![[r#" @@ -2599,8 +2660,8 @@ fn test_hover_layout_of_enum() { ```rust // size = 16 (0x10), align = 8, niches = 254 enum Foo { - Variant1(u8, u16), - Variant2(i32, u8, i64), + Variant1( /* … */ ), + Variant2( /* … */ ), } ``` "#]], diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index 1378ab2ab0f7..c5623062af4c 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -167,7 +167,8 @@ impl StaticIndex<'_> { keywords: true, format: crate::HoverDocFormat::Markdown, max_trait_assoc_items_count: None, - max_adt_fields_or_variants_count: Some(5), + max_struct_or_union_fields_count: Some(5), + max_enum_variants_count: Some(5), }; let tokens = tokens.filter(|token| { matches!( diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 10bc19b98879..910ee70ca8c9 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -312,8 +312,10 @@ 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 none if empty. - hover_show_adtFieldsOrVariants: Option = Some(5), + /// How many variants of an enum to display when hovering on. Show none if empty. + hover_show_enumVariants: Option = Some(5), + /// How many fields of a struct or union to display when hovering on. Show none if empty. + hover_show_structOrUnionFields: Option = Some(5), /// How many associated items of a trait to display when hovering a trait. hover_show_traitAssocItems: Option = None, @@ -1112,7 +1114,8 @@ impl Config { }, keywords: self.hover_documentation_keywords_enable().to_owned(), max_trait_assoc_items_count: self.hover_show_traitAssocItems().to_owned(), - max_adt_fields_or_variants_count: self.hover_show_adtFieldsOrVariants().to_owned(), + max_struct_or_union_fields_count: self.hover_show_structOrUnionFields().to_owned(), + max_enum_variants_count: self.hover_show_enumVariants().to_owned(), } } diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index d275b51abc3f..acc10cdb8fb8 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -533,10 +533,15 @@ 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.adtFieldsOrVariants]]rust-analyzer.hover.show.adtFieldsOrVariants (default: `5`):: +[[rust-analyzer.hover.show.enumVariants]]rust-analyzer.hover.show.enumVariants (default: `5`):: + -- -How many fields or variants of an ADT (struct, enum or union) to display when hovering on. Show none if empty. +How many variants of an enum to display when hovering on. Show none if empty. +-- +[[rust-analyzer.hover.show.structOrUnionFields]]rust-analyzer.hover.show.structOrUnionFields (default: `5`):: ++ +-- +How many fields of a struct 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 88a382f1fb81..ffbceb8ad4fa 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1152,8 +1152,17 @@ } ] }, - "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.", + "rust-analyzer.hover.show.enumVariants": { + "markdownDescription": "How many variants of an enum to display when hovering on. Show none if empty.", + "default": 5, + "type": [ + "null", + "integer" + ], + "minimum": 0 + }, + "rust-analyzer.hover.show.structOrUnionFields": { + "markdownDescription": "How many fields of a struct or union to display when hovering on. Show none if empty.", "default": 5, "type": [ "null", From 43576989a1a577e3680bf1c3327810ce1b5a5b8b Mon Sep 17 00:00:00 2001 From: roife Date: Sat, 20 Apr 2024 01:06:04 +0800 Subject: [PATCH 6/7] Add hovering limitations support for variants --- crates/hir/src/display.rs | 37 ++++----- crates/ide/src/hover.rs | 2 +- crates/ide/src/hover/render.rs | 9 ++- crates/ide/src/hover/tests.rs | 122 +++++++++++++++++++++++++---- crates/ide/src/static_index.rs | 2 +- crates/rust-analyzer/src/config.rs | 6 +- docs/user/generated_config.adoc | 4 +- editors/code/package.json | 4 +- 8 files changed, 138 insertions(+), 48 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 42fafd4ee806..1c3eac1590d5 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -188,7 +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(&self.fields(f.db), has_where_clause, limit, f)?; + display_fields(&self.fields(f.db), has_where_clause, limit, false, f)?; } } StructKind::Unit => _ = write_where_clause(def_id, f)?, @@ -225,7 +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(&self.fields(f.db), has_where_clause, limit, f)?; + display_fields(&self.fields(f.db), has_where_clause, limit, false, f)?; } Ok(()) } @@ -235,10 +235,12 @@ fn display_fields( fields: &[Field], has_where_clause: bool, limit: usize, + in_line: bool, f: &mut HirFormatter<'_>, ) -> Result<(), HirDisplayError> { let count = fields.len().min(limit); - f.write_char(if !has_where_clause { ' ' } else { '\n' })?; + let (indent, separator) = if in_line { ("", ' ') } else { (" ", '\n') }; + f.write_char(if !has_where_clause { ' ' } else { separator })?; if count == 0 { if fields.is_empty() { f.write_str("{}")?; @@ -246,15 +248,19 @@ fn display_fields( f.write_str("{ /* … */ }")?; } } else { - f.write_str("{\n")?; + f.write_char('{')?; + f.write_char(separator)?; for field in &fields[..count] { - f.write_str(" ")?; + f.write_str(indent)?; field.hir_fmt(f)?; - f.write_str(",\n")?; + f.write_char(',')?; + f.write_char(separator)?; } if fields.len() > count { - f.write_str(" /* … */\n")?; + f.write_str(indent)?; + f.write_str("/* … */")?; + f.write_char(separator)?; } f.write_str("}")?; } @@ -345,21 +351,10 @@ impl HirDisplay for Variant { } f.write_char(')')?; } - VariantData::Record(fields) => { - f.write_str(" {")?; - let mut first = true; - for (_, field) in fields.iter() { - if first { - first = false; - f.write_char(' ')?; - } else { - f.write_str(", ")?; - } - // Enum variant fields must be pub. - write!(f, "{}: ", field.name.display(f.db.upcast()))?; - field.type_ref.hir_fmt(f)?; + VariantData::Record(_) => { + if let Some(limit) = f.entity_limit { + display_fields(&self.fields(f.db), false, limit, true, f)?; } - f.write_str(" }")?; } } Ok(()) diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 6958ae16230a..cd76ee4d7bdd 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -33,7 +33,7 @@ pub struct HoverConfig { pub keywords: bool, pub format: HoverDocFormat, pub max_trait_assoc_items_count: Option, - pub max_struct_or_union_fields_count: Option, + pub max_fields_count: Option, pub max_enum_variants_count: Option, } diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 1044a3f4c11b..0809ea81d757 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -411,7 +411,10 @@ pub(super) fn definition( trait_.display_limited(db, config.max_trait_assoc_items_count).to_string() } Definition::Adt(adt @ (Adt::Struct(_) | Adt::Union(_))) => { - adt.display_limited(db, config.max_struct_or_union_fields_count).to_string() + adt.display_limited(db, config.max_fields_count).to_string() + } + Definition::Variant(variant) => { + variant.display_limited(db, config.max_fields_count).to_string() } Definition::Adt(adt @ Adt::Enum(_)) => { adt.display_limited(db, config.max_enum_variants_count).to_string() @@ -419,9 +422,7 @@ pub(super) fn definition( Definition::SelfType(impl_def) => { let self_ty = &impl_def.self_ty(db); match self_ty.as_adt() { - Some(adt) => { - adt.display_limited(db, config.max_struct_or_union_fields_count).to_string() - } + Some(adt) => adt.display_limited(db, config.max_fields_count).to_string(), None => self_ty.display(db).to_string(), } } diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index f17a0fa2cd92..ff3258b4dbbc 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_or_union_fields_count: Some(5), + max_fields_count: Some(5), max_enum_variants_count: Some(5), }; @@ -52,7 +52,7 @@ fn check(ra_fixture: &str, expect: Expect) { } #[track_caller] -fn check_hover_struct_or_union_fields_limit( +fn check_hover_fields_limit( fields_count: impl Into>, ra_fixture: &str, expect: Expect, @@ -62,7 +62,7 @@ fn check_hover_struct_or_union_fields_limit( .hover( &HoverConfig { links_in_hover: true, - max_struct_or_union_fields_count: fields_count.into(), + max_fields_count: fields_count.into(), ..HOVER_BASE_CONFIG }, FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) }, @@ -939,7 +939,7 @@ struct Foo$0 where u32: Copy { field: u32 } #[test] fn hover_record_struct_limit() { - check_hover_struct_or_union_fields_limit( + check_hover_fields_limit( 3, r#" struct Foo$0 { a: u32, b: i32, c: i32 } @@ -961,7 +961,7 @@ fn hover_record_struct_limit() { ``` "#]], ); - check_hover_struct_or_union_fields_limit( + check_hover_fields_limit( 3, r#" struct Foo$0 { a: u32 } @@ -981,7 +981,7 @@ fn hover_record_struct_limit() { ``` "#]], ); - check_hover_struct_or_union_fields_limit( + check_hover_fields_limit( 3, r#" struct Foo$0 { a: u32, b: i32, c: i32, d: u32 } @@ -1004,7 +1004,7 @@ fn hover_record_struct_limit() { ``` "#]], ); - check_hover_struct_or_union_fields_limit( + check_hover_fields_limit( None, r#" struct Foo$0 { a: u32, b: i32, c: i32 } @@ -1022,7 +1022,7 @@ fn hover_record_struct_limit() { ``` "#]], ); - check_hover_struct_or_union_fields_limit( + check_hover_fields_limit( 0, r#" struct Foo$0 { a: u32, b: i32, c: i32 } @@ -1042,6 +1042,100 @@ fn hover_record_struct_limit() { ) } +#[test] +fn hover_record_variant_limit() { + check_hover_fields_limit( + 3, + r#" + enum Foo { A$0 { a: u32, b: i32, c: i32 } } + "#, + expect![[r#" + *A* + + ```rust + test::Foo + ``` + + ```rust + // size = 12 (0xC), align = 4 + A { a: u32, b: i32, c: i32, } + ``` + "#]], + ); + check_hover_fields_limit( + 3, + r#" + enum Foo { A$0 { a: u32 } } + "#, + expect![[r#" + *A* + + ```rust + test::Foo + ``` + + ```rust + // size = 4, align = 4 + A { a: u32, } + ``` + "#]], + ); + check_hover_fields_limit( + 3, + r#" + enum Foo { A$0 { a: u32, b: i32, c: i32, d: u32 } } + "#, + expect![[r#" + *A* + + ```rust + test::Foo + ``` + + ```rust + // size = 16 (0x10), align = 4 + A { a: u32, b: i32, c: i32, /* … */ } + ``` + "#]], + ); + check_hover_fields_limit( + None, + r#" + enum Foo { A$0 { a: u32, b: i32, c: i32 } } + "#, + expect![[r#" + *A* + + ```rust + test::Foo + ``` + + ```rust + // size = 12 (0xC), align = 4 + A + ``` + "#]], + ); + check_hover_fields_limit( + 0, + r#" + enum Foo { A$0 { a: u32, b: i32, c: i32 } } + "#, + expect![[r#" + *A* + + ```rust + test::Foo + ``` + + ```rust + // size = 12 (0xC), align = 4 + A { /* … */ } + ``` + "#]], + ); +} + #[test] fn hover_enum_limit() { check_hover_enum_variants_limit( @@ -1152,7 +1246,7 @@ fn hover_enum_limit() { #[test] fn hover_union_limit() { - check_hover_struct_or_union_fields_limit( + check_hover_fields_limit( 5, r#"union Foo$0 { a: u32, b: i32 }"#, expect![[r#" @@ -1171,7 +1265,7 @@ fn hover_union_limit() { ``` "#]], ); - check_hover_struct_or_union_fields_limit( + check_hover_fields_limit( 1, r#"union Foo$0 { a: u32, b: i32 }"#, expect![[r#" @@ -1190,7 +1284,7 @@ fn hover_union_limit() { ``` "#]], ); - check_hover_struct_or_union_fields_limit( + check_hover_fields_limit( 0, r#"union Foo$0 { a: u32, b: i32 }"#, expect![[r#" @@ -1206,7 +1300,7 @@ fn hover_union_limit() { ``` "#]], ); - check_hover_struct_or_union_fields_limit( + check_hover_fields_limit( None, r#"union Foo$0 { a: u32, b: i32 }"#, expect![[r#" @@ -1691,7 +1785,7 @@ impl Thing { ``` "#]], ); - check_hover_struct_or_union_fields_limit( + check_hover_fields_limit( None, r#" struct Thing { x: u32 } @@ -6832,7 +6926,7 @@ enum Enum { ```rust // size = 4, align = 4 - RecordV { field: u32 } + RecordV { field: u32, } ``` "#]], ); diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index c5623062af4c..df625f740480 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -167,7 +167,7 @@ impl StaticIndex<'_> { keywords: true, format: crate::HoverDocFormat::Markdown, max_trait_assoc_items_count: None, - max_struct_or_union_fields_count: Some(5), + max_fields_count: Some(5), max_enum_variants_count: Some(5), }; let tokens = tokens.filter(|token| { diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 910ee70ca8c9..52c09fb3945b 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -314,8 +314,8 @@ config_data! { /// How many variants of an enum to display when hovering on. Show none if empty. hover_show_enumVariants: Option = Some(5), - /// How many fields of a struct or union to display when hovering on. Show none if empty. - hover_show_structOrUnionFields: Option = Some(5), + /// How many fields of a struct, variant or union to display when hovering on. Show none if empty. + hover_show_fields: Option = Some(5), /// How many associated items of a trait to display when hovering a trait. hover_show_traitAssocItems: Option = None, @@ -1114,7 +1114,7 @@ impl Config { }, keywords: self.hover_documentation_keywords_enable().to_owned(), max_trait_assoc_items_count: self.hover_show_traitAssocItems().to_owned(), - max_struct_or_union_fields_count: self.hover_show_structOrUnionFields().to_owned(), + max_fields_count: self.hover_show_fields().to_owned(), max_enum_variants_count: self.hover_show_enumVariants().to_owned(), } } diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index acc10cdb8fb8..ebacc5b7016e 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -538,10 +538,10 @@ How to render the size information in a memory layout hover. -- How many variants of an enum to display when hovering on. Show none if empty. -- -[[rust-analyzer.hover.show.structOrUnionFields]]rust-analyzer.hover.show.structOrUnionFields (default: `5`):: +[[rust-analyzer.hover.show.fields]]rust-analyzer.hover.show.fields (default: `5`):: + -- -How many fields of a struct or union to display when hovering on. Show none if empty. +How many fields of a struct, variant 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 ffbceb8ad4fa..aed0d9f898f9 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1161,8 +1161,8 @@ ], "minimum": 0 }, - "rust-analyzer.hover.show.structOrUnionFields": { - "markdownDescription": "How many fields of a struct or union to display when hovering on. Show none if empty.", + "rust-analyzer.hover.show.fields": { + "markdownDescription": "How many fields of a struct, variant or union to display when hovering on. Show none if empty.", "default": 5, "type": [ "null", From aa1f1344cc961924bdce0a76913ed54f934418e4 Mon Sep 17 00:00:00 2001 From: roife Date: Sat, 20 Apr 2024 10:07:33 +0800 Subject: [PATCH 7/7] fix: remove space within `{}` when no fields in struct --- crates/hir/src/display.rs | 24 ++++++++++++++---------- crates/ide/src/hover/tests.rs | 22 +++++++++++++++++++++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 1c3eac1590d5..c276e87786dd 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -249,19 +249,23 @@ fn display_fields( } } else { f.write_char('{')?; - f.write_char(separator)?; - for field in &fields[..count] { - f.write_str(indent)?; - field.hir_fmt(f)?; - f.write_char(',')?; - f.write_char(separator)?; - } - if fields.len() > count { - f.write_str(indent)?; - f.write_str("/* … */")?; + if !fields.is_empty() { f.write_char(separator)?; + for field in &fields[..count] { + f.write_str(indent)?; + field.hir_fmt(f)?; + f.write_char(',')?; + f.write_char(separator)?; + } + + if fields.len() > count { + f.write_str(indent)?; + f.write_str("/* … */")?; + f.write_char(separator)?; + } } + f.write_str("}")?; } diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index ff3258b4dbbc..52e57d788923 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -1039,7 +1039,27 @@ fn hover_record_struct_limit() { struct Foo { /* … */ } ``` "#]], - ) + ); + + // No extra spaces within `{}` when there are no fields + check_hover_fields_limit( + 5, + r#" + struct Foo$0 {} + "#, + expect![[r#" + *Foo* + + ```rust + test + ``` + + ```rust + // size = 0, align = 1 + struct Foo {} + ``` + "#]], + ); } #[test]