Skip to content

Commit

Permalink
Add config hover_show_adtFieldsOrVariants to handle hovering limitati…
Browse files Browse the repository at this point in the history
…on for ADTs
  • Loading branch information
roife committed Apr 6, 2024
1 parent 9cced6d commit 281faa9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 51 deletions.
96 changes: 52 additions & 44 deletions crates/hir/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?,
Expand All @@ -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(())
Expand All @@ -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<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")?;
}

Ok(())
if fields_or_variants.len() > count {
f.write_str(" /* … */\n")?;
}
f.write_str("}")?;
}

Ok(())
}

impl HirDisplay for Field {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/static_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
6 changes: 3 additions & 3 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ config_data! {
/// How to render the size information in a memory layout hover.
hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = "\"both\"",

/// How many fields of a struct to display when hovering a struct.
hover_show_structFields: Option<usize> = "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<usize> = "10",
/// How many associated items of a trait to display when hovering a trait.
hover_show_traitAssocItems: Option<usize> = "null",

Expand Down Expand Up @@ -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,
}
}

Expand Down

0 comments on commit 281faa9

Please sign in to comment.