Add config hover_show_adtFieldsOrVariants to handle hovering limitation for ADTs

This commit is contained in:
roife 2024-04-06 13:57:56 +08:00
parent 1179c3ee83
commit 21da6c6164
5 changed files with 60 additions and 52 deletions

View file

@ -188,28 +188,12 @@ 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 {
let fields = self.fields(f.db); display_fields_or_variants(
let count = fields.len().min(limit); &self.fields(f.db),
f.write_char(if !has_where_clause { ' ' } else { '\n' })?; has_where_clause,
if count == 0 { limit,
if fields.is_empty() { f,
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("}")?;
}
} }
} }
StructKind::Unit => _ = write_where_clause(def_id, 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()))?; write!(f, "{}", self.name(f.db).display(f.db.upcast()))?;
let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id)); let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id));
write_generic_params(def_id, f)?; write_generic_params(def_id, f)?;
let has_where_clause = write_where_clause(def_id, f)?;
let variants = self.variants(f.db); let has_where_clause = write_where_clause(def_id, f)?;
if !variants.is_empty() { if let Some(limit) = f.entity_limit {
f.write_char(if !has_where_clause { ' ' } else { '\n' })?; display_fields_or_variants(
f.write_str("{\n")?; &self.variants(f.db),
for variant in variants { has_where_clause,
f.write_str(" ")?; limit,
variant.hir_fmt(f)?; f,
f.write_str(",\n")?; )?;
}
f.write_str("}")?;
} }
Ok(()) Ok(())
@ -251,24 +232,51 @@ impl HirDisplay for Union {
write!(f, "{}", self.name(f.db).display(f.db.upcast()))?; write!(f, "{}", self.name(f.db).display(f.db.upcast()))?;
let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id)); let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id));
write_generic_params(def_id, f)?; write_generic_params(def_id, f)?;
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 {
let fields = self.fields(f.db); display_fields_or_variants(
if !fields.is_empty() { &self.fields(f.db),
f.write_char(if !has_where_clause { ' ' } else { '\n' })?; has_where_clause,
f.write_str("{\n")?; limit,
for field in self.fields(f.db) { f,
f.write_str(" ")?; )?;
field.hir_fmt(f)?;
f.write_str(",\n")?;
}
f.write_str("}")?;
} }
Ok(()) Ok(())
} }
} }
fn display_fields_or_variants<T: HirDisplay>(
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")?;
}
if fields_or_variants.len() > count {
f.write_str(" /* … */\n")?;
}
f.write_str("}")?;
}
Ok(())
}
impl HirDisplay for Field { impl HirDisplay for Field {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> { fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
write_visibility(self.parent.module(f.db).id, self.visibility(f.db), f)?; write_visibility(self.parent.module(f.db).id, self.visibility(f.db), f)?;

View file

@ -33,7 +33,7 @@ pub struct HoverConfig {
pub keywords: bool, pub keywords: bool,
pub format: HoverDocFormat, pub format: HoverDocFormat,
pub max_trait_assoc_items_count: Option<usize>, pub max_trait_assoc_items_count: Option<usize>,
pub max_struct_field_count: Option<usize>, pub max_adt_fields_or_variants_count: Option<usize>,
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]

View file

@ -410,8 +410,8 @@ pub(super) fn definition(
Definition::Trait(trait_) => { Definition::Trait(trait_) => {
trait_.display_limited(db, config.max_trait_assoc_items_count).to_string() trait_.display_limited(db, config.max_trait_assoc_items_count).to_string()
} }
Definition::Adt(Adt::Struct(struct_)) => { Definition::Adt(adt) => {
struct_.display_limited(db, config.max_struct_field_count).to_string() adt.display_limited(db, config.max_adt_fields_or_variants_count).to_string()
} }
_ => def.label(db), _ => def.label(db),
}; };

View file

@ -167,7 +167,7 @@ impl StaticIndex<'_> {
keywords: true, keywords: true,
format: crate::HoverDocFormat::Markdown, format: crate::HoverDocFormat::Markdown,
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),
}; };
let tokens = tokens.filter(|token| { let tokens = tokens.filter(|token| {
matches!( matches!(

View file

@ -312,8 +312,8 @@ 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 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 all if empty.
hover_show_structFields: Option<usize> = None, 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,
@ -1112,7 +1112,7 @@ impl Config {
}, },
keywords: self.hover_documentation_keywords_enable().to_owned(), keywords: self.hover_documentation_keywords_enable().to_owned(),
max_trait_assoc_items_count: self.hover_show_traitAssocItems().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(),
} }
} }