Skip to content

Commit adbefb8

Browse files
committed
fix: add a separate setting for enum variants
1 parent 6bb8598 commit adbefb8

File tree

8 files changed

+164
-109
lines changed

8 files changed

+164
-109
lines changed

crates/hir/src/display.rs

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl HirDisplay for Struct {
188188
StructKind::Record => {
189189
let has_where_clause = write_where_clause(def_id, f)?;
190190
if let Some(limit) = f.entity_limit {
191-
display_fields_or_variants(&self.fields(f.db), has_where_clause, limit, f)?;
191+
display_fields(&self.fields(f.db), has_where_clause, limit, f)?;
192192
}
193193
}
194194
StructKind::Unit => _ = write_where_clause(def_id, f)?,
@@ -208,7 +208,7 @@ impl HirDisplay for Enum {
208208

209209
let has_where_clause = write_where_clause(def_id, f)?;
210210
if let Some(limit) = f.entity_limit {
211-
display_fields_or_variants(&self.variants(f.db), has_where_clause, limit, f)?;
211+
display_variants(&self.variants(f.db), has_where_clause, limit, f)?;
212212
}
213213

214214
Ok(())
@@ -225,35 +225,83 @@ impl HirDisplay for Union {
225225

226226
let has_where_clause = write_where_clause(def_id, f)?;
227227
if let Some(limit) = f.entity_limit {
228-
display_fields_or_variants(&self.fields(f.db), has_where_clause, limit, f)?;
228+
display_fields(&self.fields(f.db), has_where_clause, limit, f)?;
229229
}
230230
Ok(())
231231
}
232232
}
233233

234-
fn display_fields_or_variants<T: HirDisplay>(
235-
fields_or_variants: &[T],
234+
fn display_fields(
235+
fields: &[Field],
236236
has_where_clause: bool,
237237
limit: usize,
238238
f: &mut HirFormatter<'_>,
239239
) -> Result<(), HirDisplayError> {
240-
let count = fields_or_variants.len().min(limit);
240+
let count = fields.len().min(limit);
241241
f.write_char(if !has_where_clause { ' ' } else { '\n' })?;
242242
if count == 0 {
243-
if fields_or_variants.is_empty() {
243+
if fields.is_empty() {
244244
f.write_str("{}")?;
245245
} else {
246246
f.write_str("{ /* … */ }")?;
247247
}
248248
} else {
249249
f.write_str("{\n")?;
250-
for field in &fields_or_variants[..count] {
250+
for field in &fields[..count] {
251251
f.write_str(" ")?;
252252
field.hir_fmt(f)?;
253253
f.write_str(",\n")?;
254254
}
255255

256-
if fields_or_variants.len() > count {
256+
if fields.len() > count {
257+
f.write_str(" /* … */\n")?;
258+
}
259+
f.write_str("}")?;
260+
}
261+
262+
Ok(())
263+
}
264+
265+
fn display_variants(
266+
variants: &[Variant],
267+
has_where_clause: bool,
268+
limit: usize,
269+
f: &mut HirFormatter<'_>,
270+
) -> Result<(), HirDisplayError> {
271+
let count = variants.len().min(limit);
272+
f.write_char(if !has_where_clause { ' ' } else { '\n' })?;
273+
if count == 0 {
274+
if variants.is_empty() {
275+
f.write_str("{}")?;
276+
} else {
277+
f.write_str("{ /* … */ }")?;
278+
}
279+
} else {
280+
f.write_str("{\n")?;
281+
for variant in &variants[..count] {
282+
f.write_str(" ")?;
283+
write!(f, "{}", variant.name(f.db).display(f.db.upcast()))?;
284+
match variant.kind(f.db) {
285+
StructKind::Tuple => {
286+
if variant.fields(f.db).is_empty() {
287+
f.write_str("()")?;
288+
} else {
289+
f.write_str("( /* … */ )")?;
290+
}
291+
}
292+
StructKind::Record => {
293+
if variant.fields(f.db).is_empty() {
294+
f.write_str(" {}")?;
295+
} else {
296+
f.write_str(" { /* … */ }")?;
297+
}
298+
}
299+
StructKind::Unit => {}
300+
}
301+
f.write_str(",\n")?;
302+
}
303+
304+
if variants.len() > count {
257305
f.write_str(" /* … */\n")?;
258306
}
259307
f.write_str("}")?;

crates/ide/src/hover.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ pub struct HoverConfig {
3333
pub keywords: bool,
3434
pub format: HoverDocFormat,
3535
pub max_trait_assoc_items_count: Option<usize>,
36-
pub max_adt_fields_or_variants_count: Option<usize>,
36+
pub max_struct_or_union_fields_count: Option<usize>,
37+
pub max_enum_variants_count: Option<usize>,
3738
}
3839

3940
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

crates/ide/src/hover/render.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,16 @@ pub(super) fn definition(
410410
Definition::Trait(trait_) => {
411411
trait_.display_limited(db, config.max_trait_assoc_items_count).to_string()
412412
}
413-
Definition::Adt(adt) => {
414-
adt.display_limited(db, config.max_adt_fields_or_variants_count).to_string()
413+
Definition::Adt(adt @ (Adt::Struct(_) | Adt::Union(_))) => {
414+
adt.display_limited(db, config.max_struct_or_union_fields_count).to_string()
415+
}
416+
Definition::Adt(adt @ Adt::Enum(_)) => {
417+
adt.display_limited(db, config.max_enum_variants_count).to_string()
415418
}
416419
Definition::SelfType(impl_def) => {
417420
let self_ty = &impl_def.self_ty(db);
418421
match self_ty.as_adt() {
419-
Some(adt) => {
420-
adt.display_limited(db, config.max_adt_fields_or_variants_count).to_string()
421-
}
422+
Some(adt) => adt.display_limited(db, config.max_struct_or_union_fields_count).to_string(),
422423
None => self_ty.display(db).to_string(),
423424
}
424425
}

0 commit comments

Comments
 (0)