Skip to content

Commit

Permalink
Notable traits for type info hovers
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jan 16, 2024
1 parent ffeaee8 commit 0a75a8c
Show file tree
Hide file tree
Showing 3 changed files with 378 additions and 322 deletions.
55 changes: 30 additions & 25 deletions crates/ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,30 +393,7 @@ pub(crate) fn hover_for_definition(
Definition::BuiltinType(it) => Some(it.ty(db)),
_ => None,
};
let notable_traits = def_ty
.map(|ty| {
db.notable_traits_in_deps(ty.krate(db).into())
.iter()
.flat_map(|it| &**it)
.filter_map(move |&trait_| {
let trait_ = trait_.into();
ty.impls_trait(db, trait_, &[]).then(|| {
(
trait_,
trait_
.items(db)
.into_iter()
.filter_map(hir::AssocItem::as_type_alias)
.map(|alias| {
(ty.normalize_trait_assoc_type(db, &[], alias), alias.name(db))
})
.collect::<Vec<_>>(),
)
})
})
.collect::<Vec<_>>()
})
.unwrap_or_default();
let notable_traits = def_ty.map(|ty| notable_traits(db, &ty)).unwrap_or_default();

render::definition(sema.db, def, famous_defs.as_ref(), &notable_traits, config).map(|markup| {
HoverResult {
Expand All @@ -434,6 +411,32 @@ pub(crate) fn hover_for_definition(
})
}

fn notable_traits(
db: &RootDatabase,
ty: &hir::Type,
) -> Vec<(hir::Trait, Vec<(Option<hir::Type>, hir::Name)>)> {
db.notable_traits_in_deps(ty.krate(db).into())
.iter()
.flat_map(|it| &**it)
.filter_map(move |&trait_| {
let trait_ = trait_.into();
ty.impls_trait(db, trait_, &[]).then(|| {
(
trait_,
trait_
.items(db)
.into_iter()
.filter_map(hir::AssocItem::as_type_alias)
.map(|alias| {
(ty.normalize_trait_assoc_type(db, &[], alias), alias.name(db))
})
.collect::<Vec<_>>(),
)
})
})
.collect::<Vec<_>>()
}

fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
fn to_action(nav_target: NavigationTarget) -> HoverAction {
HoverAction::Implementation(FilePosition {
Expand Down Expand Up @@ -583,7 +586,9 @@ fn dedupe_or_merge_hover_actions(actions: Vec<HoverAction>) -> Vec<HoverAction>
}

if !go_to_type_targets.is_empty() {
deduped_actions.push(HoverAction::GoToType(go_to_type_targets.into_iter().collect()));
deduped_actions.push(HoverAction::GoToType(
go_to_type_targets.into_iter().sorted_by(|a, b| a.mod_path.cmp(&b.mod_path)).collect(),
));
}

deduped_actions
Expand Down
86 changes: 48 additions & 38 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::{mem, ops::Not};

use either::Either;
use hir::{
Adt, AsAssocItem, CaptureKind, HasSource, HirDisplay, Layout, LayoutError, Name, Semantics,
Trait, Type, TypeInfo,
Adt, AsAssocItem, CaptureKind, HasCrate, HasSource, HirDisplay, Layout, LayoutError, Name,
Semantics, Trait, Type, TypeInfo,
};
use ide_db::{
base_db::SourceDatabase,
Expand All @@ -25,7 +25,7 @@ use syntax::{

use crate::{
doc_links::{remove_links, rewrite_links},
hover::walk_and_push_ty,
hover::{notable_traits, walk_and_push_ty},
HoverAction, HoverConfig, HoverResult, Markup, MemoryLayoutHoverConfig,
MemoryLayoutHoverRenderKind,
};
Expand Down Expand Up @@ -471,38 +471,8 @@ pub(super) fn definition(
_ => None,
};

let notable_traits = {
let mut desc = String::new();
let mut needs_impl_header = true;
for (trait_, assoc_types) in notable_traits {
desc.push_str(if mem::take(&mut needs_impl_header) {
" // notable traits implemented: "
} else {
", "
});
format_to!(desc, "{}", trait_.name(db).display(db),);
if !assoc_types.is_empty() {
desc.push('<');
format_to!(
desc,
"{}",
assoc_types.into_iter().format_with(", ", |(ty, name), f| {
f(&name.display(db))?;
f(&" = ")?;
match ty {
Some(ty) => f(&ty.display(db)),
None => f(&"?"),
}
})
);
desc.push('>');
}
}
desc.is_empty().not().then(|| desc)
};

let mut desc = String::new();
if let Some(notable_traits) = notable_traits {
if let Some(notable_traits) = render_notable_trait_comment(db, notable_traits) {
desc.push_str(&notable_traits);
desc.push('\n');
}
Expand All @@ -519,6 +489,39 @@ pub(super) fn definition(
markup(docs.map(Into::into), desc, mod_path)
}

fn render_notable_trait_comment(
db: &RootDatabase,
notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)],
) -> Option<String> {
let mut desc = String::new();
let mut needs_impl_header = true;
for (trait_, assoc_types) in notable_traits {
desc.push_str(if mem::take(&mut needs_impl_header) {
" // notable traits implemented: "
} else {
", "
});
format_to!(desc, "{}", trait_.name(db).display(db),);
if !assoc_types.is_empty() {
desc.push('<');
format_to!(
desc,
"{}",
assoc_types.into_iter().format_with(", ", |(ty, name), f| {
f(&name.display(db))?;
f(&" = ")?;
match ty {
Some(ty) => f(&ty.display(db)),
None => f(&"?"),
}
})
);
desc.push('>');
}
}
desc.is_empty().not().then(|| desc)
}

fn type_info(
sema: &Semantics<'_, RootDatabase>,
config: &HoverConfig,
Expand All @@ -536,8 +539,12 @@ fn type_info(
}
};
walk_and_push_ty(sema.db, &original, &mut push_new_def);

res.markup = if let Some(adjusted_ty) = adjusted {
let mut desc = match render_notable_trait_comment(sema.db, &notable_traits(sema.db, &original))
{
Some(desc) => desc + "\n",
None => String::new(),
};
desc += &if let Some(adjusted_ty) = adjusted {
walk_and_push_ty(sema.db, &adjusted_ty, &mut push_new_def);
let original = original.display(sema.db).to_string();
let adjusted = adjusted_ty.display(sema.db).to_string();
Expand All @@ -549,10 +556,10 @@ fn type_info(
apad = static_text_diff_len + adjusted.len().max(original.len()),
opad = original.len(),
)
.into()
} else {
Markup::fenced_block(&original.display(sema.db))
Markup::fenced_block(&original.display(sema.db)).into()
};
res.markup = desc.into();
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets) {
res.actions.push(actions);
}
Expand Down Expand Up @@ -607,6 +614,9 @@ fn closure_ty(
{
format_to!(markup, "{layout}");
}
if let Some(trait_) = c.fn_trait(sema.db).get_id(sema.db, original.krate(sema.db).into()) {
push_new_def(hir::Trait::from(trait_).into())
}
format_to!(
markup,
"\n{}\n```{adjusted}\n\n## Captures\n{}",
Expand Down

0 comments on commit 0a75a8c

Please sign in to comment.