Skip to content

Commit

Permalink
fix: add a separate setting for enum variants
Browse files Browse the repository at this point in the history
  • Loading branch information
roife committed Apr 19, 2024
1 parent 6bb8598 commit adbefb8
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 109 deletions.
66 changes: 57 additions & 9 deletions crates/hir/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?,
Expand All @@ -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(())
Expand All @@ -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<T: HirDisplay>(
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("}")?;
Expand Down
3 changes: 2 additions & 1 deletion crates/ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub struct HoverConfig {
pub keywords: bool,
pub format: HoverDocFormat,
pub max_trait_assoc_items_count: Option<usize>,
pub max_adt_fields_or_variants_count: Option<usize>,
pub max_struct_or_union_fields_count: Option<usize>,
pub max_enum_variants_count: Option<usize>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down
11 changes: 6 additions & 5 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,16 @@ 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) => {

Check warning on line 419 in crates/ide/src/hover/render.rs

View workflow job for this annotation

GitHub Actions / Rust (ubuntu-latest)

Diff in /home/runner/work/rust-analyzer/rust-analyzer/crates/ide/src/hover/render.rs
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()
}
Some(adt) => adt.display_limited(db, config.max_struct_or_union_fields_count).to_string(),
None => self_ty.display(db).to_string(),
}
}
Expand Down
Loading

0 comments on commit adbefb8

Please sign in to comment.