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 => {
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,24 +232,51 @@ 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)?;
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("}")?;
if let Some(limit) = f.entity_limit {
display_fields_or_variants(
&self.fields(f.db),
has_where_clause,
limit,
f,
)?;
}
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 {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
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 format: HoverDocFormat,
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)]

View file

@ -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),
};

View file

@ -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!(

View file

@ -312,8 +312,8 @@ config_data! {
/// How to render the size information in a memory layout hover.
hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = Some(MemoryLayoutHoverRenderKindDef::Both),
/// How many fields of a struct to display when hovering a struct.
hover_show_structFields: Option<usize> = 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<usize> = Some(10),
/// How many associated items of a trait to display when hovering a trait.
hover_show_traitAssocItems: Option<usize> = 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(),
}
}