Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support hovering limits for adts #17021

Merged
merged 7 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 37 additions & 44 deletions crates/hir/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,28 +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 {
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 +205,10 @@ 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 +222,44 @@ 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>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should split this into two, one for fields one for variants. Reason being for an enum like

enum Enum {
    Variant {},
    Variant2 { field: Field },
    Variant3(),
    Variant4(Field),
    Variant5,
}

hovering the enum should yield

enum Enum {
    Variant {},
    Variant2 { .. }, // or the field if the limit is enabled and non-zero
    Variant3(),
    Variant4(,.), , // or the field if the limit is enabled and non-zero
    Variant5,
}

That is hovering an enum should only show its shape, not its vairants fields. To see those the person can hover the variant instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variant2 { .. }, // or the field if the limit is enabled and non-zero

Here I use a single config to limit the num of fields or variants of ADTs, should we separate it into two different ones?

Copy link
Member

@Veykril Veykril Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah wait I made a mistake, hovering enums should not render the variants fields (well I said that already but my example has incorrect comments I guess).

Splitting the two into two configs sounds reasonable nevertheless.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, for hovering on enums, we can display it as 🤔

enum Enum {
    Variant {},
    Variant2 { /* .. */ },
    Variant3(),
    Variant4( /* .. */ ),
    Variant5,
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, basically for the variants we want to use the same format that is used for when we set the config to 0 for fields on structs / unions. (which fits your example here)

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
13 changes: 11 additions & 2 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,17 @@ 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()
}
Definition::SelfType(impl_def) => {
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()
}
None => self_ty.display(db).to_string(),
}
}
_ => def.label(db),
};
Expand Down
Loading