Skip to content

Commit 0a75a8c

Browse files
committed
Notable traits for type info hovers
1 parent ffeaee8 commit 0a75a8c

File tree

3 files changed

+378
-322
lines changed

3 files changed

+378
-322
lines changed

crates/ide/src/hover.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -393,30 +393,7 @@ pub(crate) fn hover_for_definition(
393393
Definition::BuiltinType(it) => Some(it.ty(db)),
394394
_ => None,
395395
};
396-
let notable_traits = def_ty
397-
.map(|ty| {
398-
db.notable_traits_in_deps(ty.krate(db).into())
399-
.iter()
400-
.flat_map(|it| &**it)
401-
.filter_map(move |&trait_| {
402-
let trait_ = trait_.into();
403-
ty.impls_trait(db, trait_, &[]).then(|| {
404-
(
405-
trait_,
406-
trait_
407-
.items(db)
408-
.into_iter()
409-
.filter_map(hir::AssocItem::as_type_alias)
410-
.map(|alias| {
411-
(ty.normalize_trait_assoc_type(db, &[], alias), alias.name(db))
412-
})
413-
.collect::<Vec<_>>(),
414-
)
415-
})
416-
})
417-
.collect::<Vec<_>>()
418-
})
419-
.unwrap_or_default();
396+
let notable_traits = def_ty.map(|ty| notable_traits(db, &ty)).unwrap_or_default();
420397

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

414+
fn notable_traits(
415+
db: &RootDatabase,
416+
ty: &hir::Type,
417+
) -> Vec<(hir::Trait, Vec<(Option<hir::Type>, hir::Name)>)> {
418+
db.notable_traits_in_deps(ty.krate(db).into())
419+
.iter()
420+
.flat_map(|it| &**it)
421+
.filter_map(move |&trait_| {
422+
let trait_ = trait_.into();
423+
ty.impls_trait(db, trait_, &[]).then(|| {
424+
(
425+
trait_,
426+
trait_
427+
.items(db)
428+
.into_iter()
429+
.filter_map(hir::AssocItem::as_type_alias)
430+
.map(|alias| {
431+
(ty.normalize_trait_assoc_type(db, &[], alias), alias.name(db))
432+
})
433+
.collect::<Vec<_>>(),
434+
)
435+
})
436+
})
437+
.collect::<Vec<_>>()
438+
}
439+
437440
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
438441
fn to_action(nav_target: NavigationTarget) -> HoverAction {
439442
HoverAction::Implementation(FilePosition {
@@ -583,7 +586,9 @@ fn dedupe_or_merge_hover_actions(actions: Vec<HoverAction>) -> Vec<HoverAction>
583586
}
584587

585588
if !go_to_type_targets.is_empty() {
586-
deduped_actions.push(HoverAction::GoToType(go_to_type_targets.into_iter().collect()));
589+
deduped_actions.push(HoverAction::GoToType(
590+
go_to_type_targets.into_iter().sorted_by(|a, b| a.mod_path.cmp(&b.mod_path)).collect(),
591+
));
587592
}
588593

589594
deduped_actions

crates/ide/src/hover/render.rs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::{mem, ops::Not};
33

44
use either::Either;
55
use hir::{
6-
Adt, AsAssocItem, CaptureKind, HasSource, HirDisplay, Layout, LayoutError, Name, Semantics,
7-
Trait, Type, TypeInfo,
6+
Adt, AsAssocItem, CaptureKind, HasCrate, HasSource, HirDisplay, Layout, LayoutError, Name,
7+
Semantics, Trait, Type, TypeInfo,
88
};
99
use ide_db::{
1010
base_db::SourceDatabase,
@@ -25,7 +25,7 @@ use syntax::{
2525

2626
use crate::{
2727
doc_links::{remove_links, rewrite_links},
28-
hover::walk_and_push_ty,
28+
hover::{notable_traits, walk_and_push_ty},
2929
HoverAction, HoverConfig, HoverResult, Markup, MemoryLayoutHoverConfig,
3030
MemoryLayoutHoverRenderKind,
3131
};
@@ -471,38 +471,8 @@ pub(super) fn definition(
471471
_ => None,
472472
};
473473

474-
let notable_traits = {
475-
let mut desc = String::new();
476-
let mut needs_impl_header = true;
477-
for (trait_, assoc_types) in notable_traits {
478-
desc.push_str(if mem::take(&mut needs_impl_header) {
479-
" // notable traits implemented: "
480-
} else {
481-
", "
482-
});
483-
format_to!(desc, "{}", trait_.name(db).display(db),);
484-
if !assoc_types.is_empty() {
485-
desc.push('<');
486-
format_to!(
487-
desc,
488-
"{}",
489-
assoc_types.into_iter().format_with(", ", |(ty, name), f| {
490-
f(&name.display(db))?;
491-
f(&" = ")?;
492-
match ty {
493-
Some(ty) => f(&ty.display(db)),
494-
None => f(&"?"),
495-
}
496-
})
497-
);
498-
desc.push('>');
499-
}
500-
}
501-
desc.is_empty().not().then(|| desc)
502-
};
503-
504474
let mut desc = String::new();
505-
if let Some(notable_traits) = notable_traits {
475+
if let Some(notable_traits) = render_notable_trait_comment(db, notable_traits) {
506476
desc.push_str(&notable_traits);
507477
desc.push('\n');
508478
}
@@ -519,6 +489,39 @@ pub(super) fn definition(
519489
markup(docs.map(Into::into), desc, mod_path)
520490
}
521491

492+
fn render_notable_trait_comment(
493+
db: &RootDatabase,
494+
notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)],
495+
) -> Option<String> {
496+
let mut desc = String::new();
497+
let mut needs_impl_header = true;
498+
for (trait_, assoc_types) in notable_traits {
499+
desc.push_str(if mem::take(&mut needs_impl_header) {
500+
" // notable traits implemented: "
501+
} else {
502+
", "
503+
});
504+
format_to!(desc, "{}", trait_.name(db).display(db),);
505+
if !assoc_types.is_empty() {
506+
desc.push('<');
507+
format_to!(
508+
desc,
509+
"{}",
510+
assoc_types.into_iter().format_with(", ", |(ty, name), f| {
511+
f(&name.display(db))?;
512+
f(&" = ")?;
513+
match ty {
514+
Some(ty) => f(&ty.display(db)),
515+
None => f(&"?"),
516+
}
517+
})
518+
);
519+
desc.push('>');
520+
}
521+
}
522+
desc.is_empty().not().then(|| desc)
523+
}
524+
522525
fn type_info(
523526
sema: &Semantics<'_, RootDatabase>,
524527
config: &HoverConfig,
@@ -536,8 +539,12 @@ fn type_info(
536539
}
537540
};
538541
walk_and_push_ty(sema.db, &original, &mut push_new_def);
539-
540-
res.markup = if let Some(adjusted_ty) = adjusted {
542+
let mut desc = match render_notable_trait_comment(sema.db, &notable_traits(sema.db, &original))
543+
{
544+
Some(desc) => desc + "\n",
545+
None => String::new(),
546+
};
547+
desc += &if let Some(adjusted_ty) = adjusted {
541548
walk_and_push_ty(sema.db, &adjusted_ty, &mut push_new_def);
542549
let original = original.display(sema.db).to_string();
543550
let adjusted = adjusted_ty.display(sema.db).to_string();
@@ -549,10 +556,10 @@ fn type_info(
549556
apad = static_text_diff_len + adjusted.len().max(original.len()),
550557
opad = original.len(),
551558
)
552-
.into()
553559
} else {
554-
Markup::fenced_block(&original.display(sema.db))
560+
Markup::fenced_block(&original.display(sema.db)).into()
555561
};
562+
res.markup = desc.into();
556563
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets) {
557564
res.actions.push(actions);
558565
}
@@ -607,6 +614,9 @@ fn closure_ty(
607614
{
608615
format_to!(markup, "{layout}");
609616
}
617+
if let Some(trait_) = c.fn_trait(sema.db).get_id(sema.db, original.krate(sema.db).into()) {
618+
push_new_def(hir::Trait::from(trait_).into())
619+
}
610620
format_to!(
611621
markup,
612622
"\n{}\n```{adjusted}\n\n## Captures\n{}",

0 commit comments

Comments
 (0)