Skip to content

Commit

Permalink
Auto merge of #16756 - Veykril:hover-trait, r=Veykril
Browse files Browse the repository at this point in the history
internal: Adjust a few things for trait assoc item hovers

#15938 (minor turned major wrt diff because of test changes)
  • Loading branch information
bors committed Mar 5, 2024
2 parents ce3216e + b20e467 commit 124d5ff
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 308 deletions.
10 changes: 5 additions & 5 deletions crates/hir-ty/src/display.rs
Expand Up @@ -63,7 +63,7 @@ pub struct HirFormatter<'a> {
buf: String,
curr_size: usize,
pub(crate) max_size: Option<usize>,
pub limited_size: Option<usize>,
pub entity_limit: Option<usize>,
omit_verbose_types: bool,
closure_style: ClosureStyle,
display_target: DisplayTarget,
Expand Down Expand Up @@ -148,8 +148,8 @@ pub trait HirDisplay {
}
}

/// Returns a `Display`able type that is human-readable and tries to limit the item inside this type.
/// Use this for showing types which may contain two many item when user hover on, like `trait`, `struct`, `enum`
/// Returns a `Display`able type that is human-readable and tries to limit the number of items inside.
/// Use this for showing definitions which may contain too many items, like `trait`, `struct`, `enum`
fn display_limited<'a>(
&'a self,
db: &'a dyn HirDatabase,
Expand Down Expand Up @@ -184,7 +184,7 @@ pub trait HirDisplay {
buf: String::with_capacity(20),
curr_size: 0,
max_size: None,
limited_size: None,
entity_limit: None,
omit_verbose_types: false,
closure_style: ClosureStyle::ImplFn,
display_target: DisplayTarget::SourceCode { module_id, allow_opaque },
Expand Down Expand Up @@ -352,7 +352,7 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
buf: String::with_capacity(20),
curr_size: 0,
max_size: self.max_size,
limited_size: self.limited_size,
entity_limit: self.limited_size,
omit_verbose_types: self.omit_verbose_types,
display_target: self.display_target,
closure_style: self.closure_style,
Expand Down
49 changes: 24 additions & 25 deletions crates/hir/src/display.rs
Expand Up @@ -596,33 +596,32 @@ impl HirDisplay for Trait {
write_generic_params(def_id, f)?;
write_where_clause(def_id, f)?;

let assoc_items = self.items(f.db);
let assoc_items_size = assoc_items.len();
let limited_size = f.limited_size.unwrap_or(assoc_items_size);
if assoc_items.is_empty() {
f.write_str(" {}")?;
} else {
f.write_str(" {\n")?;
for (index, item) in assoc_items.iter().enumerate() {
f.write_str(" ")?;
match item {
AssocItem::Function(func) => {
func.hir_fmt(f)?;
}
AssocItem::Const(cst) => {
cst.hir_fmt(f)?;
}
AssocItem::TypeAlias(type_alias) => {
type_alias.hir_fmt(f)?;
}
};
f.write_str(",\n")?;
if index + 1 == limited_size && index + 1 != assoc_items_size {
f.write_str(" ...\n")?;
break;
if let Some(limit) = f.entity_limit {
let assoc_items = self.items(f.db);
let count = assoc_items.len().min(limit);
if count == 0 {
if assoc_items.is_empty() {
f.write_str(" {}")?;
} else {
f.write_str(" { /* … */ }")?;
}
} else {
f.write_str(" {\n")?;
for item in &assoc_items[..count] {
f.write_str(" ")?;
match item {
AssocItem::Function(func) => func.hir_fmt(f),
AssocItem::Const(cst) => cst.hir_fmt(f),
AssocItem::TypeAlias(type_alias) => type_alias.hir_fmt(f),
}?;
f.write_str(";\n")?;
}

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

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/hover.rs
Expand Up @@ -32,7 +32,7 @@ pub struct HoverConfig {
pub documentation: bool,
pub keywords: bool,
pub format: HoverDocFormat,
pub trait_assoc_items_size: Option<usize>,
pub max_trait_assoc_items_count: Option<usize>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/hover/render.rs
Expand Up @@ -408,7 +408,7 @@ pub(super) fn definition(
let mod_path = definition_mod_path(db, &def);
let label = match def {
Definition::Trait(trait_) => {
trait_.display_limited(db, config.trait_assoc_items_size).to_string()
trait_.display_limited(db, config.max_trait_assoc_items_count).to_string()
}
_ => def.label(db),
};
Expand Down

0 comments on commit 124d5ff

Please sign in to comment.