From 281faa9ae20af058ca91f9bc5ab7942464a8a624 Mon Sep 17 00:00:00 2001 From: roife Date: Sat, 6 Apr 2024 13:57:56 +0800 Subject: [PATCH] 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 5890af17eb98..fe870527f242 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -378,8 +378,8 @@ config_data! { /// How to render the size information in a memory layout hover. hover_memoryLayout_size: Option = "\"both\"", - /// How many fields of a struct to display when hovering a struct. - hover_show_structFields: Option = "null", + /// 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 = "10", /// How many associated items of a trait to display when hovering a trait. hover_show_traitAssocItems: Option = "null", @@ -1725,7 +1725,7 @@ impl Config { }, keywords: self.data.hover_documentation_keywords_enable, max_trait_assoc_items_count: self.data.hover_show_traitAssocItems, - max_struct_field_count: self.data.hover_show_structFields, + max_adt_fields_or_variants_count: self.data.hover_show_adtFieldsOrVariants, } }