Skip to content

Commit b20e467

Browse files
committed
internal: Adjust a few things for trait assoc item hovers
1 parent ce3216e commit b20e467

File tree

7 files changed

+381
-308
lines changed

7 files changed

+381
-308
lines changed

crates/hir-ty/src/display.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub struct HirFormatter<'a> {
6363
buf: String,
6464
curr_size: usize,
6565
pub(crate) max_size: Option<usize>,
66-
pub limited_size: Option<usize>,
66+
pub entity_limit: Option<usize>,
6767
omit_verbose_types: bool,
6868
closure_style: ClosureStyle,
6969
display_target: DisplayTarget,
@@ -148,8 +148,8 @@ pub trait HirDisplay {
148148
}
149149
}
150150

151-
/// Returns a `Display`able type that is human-readable and tries to limit the item inside this type.
152-
/// Use this for showing types which may contain two many item when user hover on, like `trait`, `struct`, `enum`
151+
/// Returns a `Display`able type that is human-readable and tries to limit the number of items inside.
152+
/// Use this for showing definitions which may contain too many items, like `trait`, `struct`, `enum`
153153
fn display_limited<'a>(
154154
&'a self,
155155
db: &'a dyn HirDatabase,
@@ -184,7 +184,7 @@ pub trait HirDisplay {
184184
buf: String::with_capacity(20),
185185
curr_size: 0,
186186
max_size: None,
187-
limited_size: None,
187+
entity_limit: None,
188188
omit_verbose_types: false,
189189
closure_style: ClosureStyle::ImplFn,
190190
display_target: DisplayTarget::SourceCode { module_id, allow_opaque },
@@ -352,7 +352,7 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
352352
buf: String::with_capacity(20),
353353
curr_size: 0,
354354
max_size: self.max_size,
355-
limited_size: self.limited_size,
355+
entity_limit: self.limited_size,
356356
omit_verbose_types: self.omit_verbose_types,
357357
display_target: self.display_target,
358358
closure_style: self.closure_style,

crates/hir/src/display.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -596,33 +596,32 @@ impl HirDisplay for Trait {
596596
write_generic_params(def_id, f)?;
597597
write_where_clause(def_id, f)?;
598598

599-
let assoc_items = self.items(f.db);
600-
let assoc_items_size = assoc_items.len();
601-
let limited_size = f.limited_size.unwrap_or(assoc_items_size);
602-
if assoc_items.is_empty() {
603-
f.write_str(" {}")?;
604-
} else {
605-
f.write_str(" {\n")?;
606-
for (index, item) in assoc_items.iter().enumerate() {
607-
f.write_str(" ")?;
608-
match item {
609-
AssocItem::Function(func) => {
610-
func.hir_fmt(f)?;
611-
}
612-
AssocItem::Const(cst) => {
613-
cst.hir_fmt(f)?;
614-
}
615-
AssocItem::TypeAlias(type_alias) => {
616-
type_alias.hir_fmt(f)?;
617-
}
618-
};
619-
f.write_str(",\n")?;
620-
if index + 1 == limited_size && index + 1 != assoc_items_size {
621-
f.write_str(" ...\n")?;
622-
break;
599+
if let Some(limit) = f.entity_limit {
600+
let assoc_items = self.items(f.db);
601+
let count = assoc_items.len().min(limit);
602+
if count == 0 {
603+
if assoc_items.is_empty() {
604+
f.write_str(" {}")?;
605+
} else {
606+
f.write_str(" { /* … */ }")?;
623607
}
608+
} else {
609+
f.write_str(" {\n")?;
610+
for item in &assoc_items[..count] {
611+
f.write_str(" ")?;
612+
match item {
613+
AssocItem::Function(func) => func.hir_fmt(f),
614+
AssocItem::Const(cst) => cst.hir_fmt(f),
615+
AssocItem::TypeAlias(type_alias) => type_alias.hir_fmt(f),
616+
}?;
617+
f.write_str(";\n")?;
618+
}
619+
620+
if assoc_items.len() > count {
621+
f.write_str(" /* … */\n")?;
622+
}
623+
f.write_str("}")?;
624624
}
625-
f.write_str("}")?;
626625
}
627626

628627
Ok(())

crates/ide/src/hover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct HoverConfig {
3232
pub documentation: bool,
3333
pub keywords: bool,
3434
pub format: HoverDocFormat,
35-
pub trait_assoc_items_size: Option<usize>,
35+
pub max_trait_assoc_items_count: Option<usize>,
3636
}
3737

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

crates/ide/src/hover/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ pub(super) fn definition(
408408
let mod_path = definition_mod_path(db, &def);
409409
let label = match def {
410410
Definition::Trait(trait_) => {
411-
trait_.display_limited(db, config.trait_assoc_items_size).to_string()
411+
trait_.display_limited(db, config.max_trait_assoc_items_count).to_string()
412412
}
413413
_ => def.label(db),
414414
};

0 commit comments

Comments
 (0)