Notable traits for type info hovers

This commit is contained in:
Lukas Wirth 2024-01-16 20:15:31 +01:00
parent ffeaee84af
commit 0a75a8c061
3 changed files with 378 additions and 322 deletions

View file

@ -393,30 +393,7 @@ pub(crate) fn hover_for_definition(
Definition::BuiltinType(it) => Some(it.ty(db)), Definition::BuiltinType(it) => Some(it.ty(db)),
_ => None, _ => None,
}; };
let notable_traits = def_ty let notable_traits = def_ty.map(|ty| notable_traits(db, &ty)).unwrap_or_default();
.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();
render::definition(sema.db, def, famous_defs.as_ref(), &notable_traits, config).map(|markup| { render::definition(sema.db, def, famous_defs.as_ref(), &notable_traits, config).map(|markup| {
HoverResult { HoverResult {
@ -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 show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
fn to_action(nav_target: NavigationTarget) -> HoverAction { fn to_action(nav_target: NavigationTarget) -> HoverAction {
HoverAction::Implementation(FilePosition { HoverAction::Implementation(FilePosition {
@ -583,7 +586,9 @@ fn dedupe_or_merge_hover_actions(actions: Vec<HoverAction>) -> Vec<HoverAction>
} }
if !go_to_type_targets.is_empty() { 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 deduped_actions

View file

@ -3,8 +3,8 @@ use std::{mem, ops::Not};
use either::Either; use either::Either;
use hir::{ use hir::{
Adt, AsAssocItem, CaptureKind, HasSource, HirDisplay, Layout, LayoutError, Name, Semantics, Adt, AsAssocItem, CaptureKind, HasCrate, HasSource, HirDisplay, Layout, LayoutError, Name,
Trait, Type, TypeInfo, Semantics, Trait, Type, TypeInfo,
}; };
use ide_db::{ use ide_db::{
base_db::SourceDatabase, base_db::SourceDatabase,
@ -25,7 +25,7 @@ use syntax::{
use crate::{ use crate::{
doc_links::{remove_links, rewrite_links}, doc_links::{remove_links, rewrite_links},
hover::walk_and_push_ty, hover::{notable_traits, walk_and_push_ty},
HoverAction, HoverConfig, HoverResult, Markup, MemoryLayoutHoverConfig, HoverAction, HoverConfig, HoverResult, Markup, MemoryLayoutHoverConfig,
MemoryLayoutHoverRenderKind, MemoryLayoutHoverRenderKind,
}; };
@ -471,38 +471,8 @@ pub(super) fn definition(
_ => None, _ => 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(); 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_str(&notable_traits);
desc.push('\n'); desc.push('\n');
} }
@ -519,6 +489,39 @@ pub(super) fn definition(
markup(docs.map(Into::into), desc, mod_path) 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( fn type_info(
sema: &Semantics<'_, RootDatabase>, sema: &Semantics<'_, RootDatabase>,
config: &HoverConfig, config: &HoverConfig,
@ -536,8 +539,12 @@ fn type_info(
} }
}; };
walk_and_push_ty(sema.db, &original, &mut push_new_def); walk_and_push_ty(sema.db, &original, &mut push_new_def);
let mut desc = match render_notable_trait_comment(sema.db, &notable_traits(sema.db, &original))
res.markup = if let Some(adjusted_ty) = adjusted { {
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); walk_and_push_ty(sema.db, &adjusted_ty, &mut push_new_def);
let original = original.display(sema.db).to_string(); let original = original.display(sema.db).to_string();
let adjusted = adjusted_ty.display(sema.db).to_string(); let adjusted = adjusted_ty.display(sema.db).to_string();
@ -549,10 +556,10 @@ fn type_info(
apad = static_text_diff_len + adjusted.len().max(original.len()), apad = static_text_diff_len + adjusted.len().max(original.len()),
opad = original.len(), opad = original.len(),
) )
.into()
} else { } 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) { if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets) {
res.actions.push(actions); res.actions.push(actions);
} }
@ -607,6 +614,9 @@ fn closure_ty(
{ {
format_to!(markup, "{layout}"); 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!( format_to!(
markup, markup,
"\n{}\n```{adjusted}\n\n## Captures\n{}", "\n{}\n```{adjusted}\n\n## Captures\n{}",

View file

@ -400,6 +400,20 @@ fn main() {
description: "struct S", description: "struct S",
}, },
}, },
HoverGotoTypeData {
mod_path: "core::ops::function::FnOnce",
nav: NavigationTarget {
file_id: FileId(
1,
),
full_range: 631..866,
focus_range: 692..698,
name: "FnOnce",
kind: Trait,
container_name: "function",
description: "pub trait FnOnce<Args>\nwhere\n Args: Tuple,",
},
},
], ],
), ),
] ]
@ -2387,39 +2401,39 @@ struct S<T>{ f1: T }
fn main() { let s$0t = S{ f1:Arg(0) }; } fn main() { let s$0t = S{ f1:Arg(0) }; }
"#, "#,
expect![[r#" expect![[r#"
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::S", mod_path: "test::Arg",
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
0, 0,
), ),
full_range: 17..37, full_range: 0..16,
focus_range: 24..25, focus_range: 7..10,
name: "S", name: "Arg",
kind: Struct, kind: Struct,
description: "struct S<T> {\n f1: T,\n}", description: "struct Arg(u32);",
},
}, },
HoverGotoTypeData { },
mod_path: "test::Arg", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::S",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 0..16, ),
focus_range: 7..10, full_range: 17..37,
name: "Arg", focus_range: 24..25,
kind: Struct, name: "S",
description: "struct Arg(u32);", kind: Struct,
}, description: "struct S<T> {\n f1: T,\n}",
}, },
], },
), ],
] ),
"#]], ]
"#]],
); );
} }
@ -2446,39 +2460,39 @@ struct S<T>{ f1: T }
fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; } fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
"#, "#,
expect![[r#" expect![[r#"
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::S", mod_path: "test::Arg",
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
0, 0,
), ),
full_range: 17..37, full_range: 0..16,
focus_range: 24..25, focus_range: 7..10,
name: "S", name: "Arg",
kind: Struct, kind: Struct,
description: "struct S<T> {\n f1: T,\n}", description: "struct Arg(u32);",
},
}, },
HoverGotoTypeData { },
mod_path: "test::Arg", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::S",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 0..16, ),
focus_range: 7..10, full_range: 17..37,
name: "Arg", focus_range: 24..25,
kind: Struct, name: "S",
description: "struct Arg(u32);", kind: Struct,
}, description: "struct S<T> {\n f1: T,\n}",
}, },
], },
), ],
] ),
"#]], ]
"#]],
); );
} }
@ -2636,39 +2650,39 @@ fn foo() -> impl Foo + Bar {}
fn main() { let s$0t = foo(); } fn main() { let s$0t = foo(); }
"#, "#,
expect![[r#" expect![[r#"
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::Foo", mod_path: "test::Bar",
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
0, 0,
), ),
full_range: 0..12, full_range: 13..25,
focus_range: 6..9, focus_range: 19..22,
name: "Foo", name: "Bar",
kind: Trait, kind: Trait,
description: "trait Foo", description: "trait Bar",
},
}, },
HoverGotoTypeData { },
mod_path: "test::Bar", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::Foo",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 13..25, ),
focus_range: 19..22, full_range: 0..12,
name: "Bar", focus_range: 6..9,
kind: Trait, name: "Foo",
description: "trait Bar", kind: Trait,
}, description: "trait Foo",
}, },
], },
), ],
] ),
"#]], ]
"#]],
); );
} }
@ -2686,65 +2700,65 @@ fn foo() -> impl Foo<S1> + Bar<S2> {}
fn main() { let s$0t = foo(); } fn main() { let s$0t = foo(); }
"#, "#,
expect![[r#" expect![[r#"
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::Foo", mod_path: "test::Bar",
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
0, 0,
), ),
full_range: 0..15, full_range: 16..31,
focus_range: 6..9, focus_range: 22..25,
name: "Foo", name: "Bar",
kind: Trait, kind: Trait,
description: "trait Foo<T>", description: "trait Bar<T>",
},
}, },
HoverGotoTypeData { },
mod_path: "test::Bar", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::Foo",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 16..31, ),
focus_range: 22..25, full_range: 0..15,
name: "Bar", focus_range: 6..9,
kind: Trait, name: "Foo",
description: "trait Bar<T>", kind: Trait,
}, description: "trait Foo<T>",
}, },
HoverGotoTypeData { },
mod_path: "test::S1", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::S1",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 32..44, ),
focus_range: 39..41, full_range: 32..44,
name: "S1", focus_range: 39..41,
kind: Struct, name: "S1",
description: "struct S1 {}", kind: Struct,
}, description: "struct S1 {}",
}, },
HoverGotoTypeData { },
mod_path: "test::S2", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::S2",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 45..57, ),
focus_range: 52..54, full_range: 45..57,
name: "S2", focus_range: 52..54,
kind: Struct, name: "S2",
description: "struct S2 {}", kind: Struct,
}, description: "struct S2 {}",
}, },
], },
), ],
] ),
"#]], ]
"#]],
); );
} }
@ -2790,52 +2804,52 @@ struct S{}
fn foo(ar$0g: &impl Foo + Bar<S>) {} fn foo(ar$0g: &impl Foo + Bar<S>) {}
"#, "#,
expect![[r#" expect![[r#"
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::Foo", mod_path: "test::Bar",
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
0, 0,
), ),
full_range: 0..12, full_range: 13..28,
focus_range: 6..9, focus_range: 19..22,
name: "Foo", name: "Bar",
kind: Trait, kind: Trait,
description: "trait Foo", description: "trait Bar<T>",
},
}, },
HoverGotoTypeData { },
mod_path: "test::Bar", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::Foo",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 13..28, ),
focus_range: 19..22, full_range: 0..12,
name: "Bar", focus_range: 6..9,
kind: Trait, name: "Foo",
description: "trait Bar<T>", kind: Trait,
}, description: "trait Foo",
}, },
HoverGotoTypeData { },
mod_path: "test::S", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::S",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 29..39, ),
focus_range: 36..37, full_range: 29..39,
name: "S", focus_range: 36..37,
kind: Struct, name: "S",
description: "struct S {}", kind: Struct,
}, description: "struct S {}",
}, },
], },
), ],
] ),
"#]], ]
"#]],
); );
} }
@ -3077,65 +3091,65 @@ struct S {}
fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {} fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
"#, "#,
expect![[r#" expect![[r#"
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::ImplTrait", mod_path: "test::B",
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
0, 0,
), ),
full_range: 0..21, full_range: 43..57,
focus_range: 6..15, focus_range: 50..51,
name: "ImplTrait", name: "B",
kind: Trait, kind: Struct,
description: "trait ImplTrait<T>", description: "struct B<T> {}",
},
}, },
HoverGotoTypeData { },
mod_path: "test::B", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::DynTrait",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 43..57, ),
focus_range: 50..51, full_range: 22..42,
name: "B", focus_range: 28..36,
kind: Struct, name: "DynTrait",
description: "struct B<T> {}", kind: Trait,
}, description: "trait DynTrait<T>",
}, },
HoverGotoTypeData { },
mod_path: "test::DynTrait", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::ImplTrait",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 22..42, ),
focus_range: 28..36, full_range: 0..21,
name: "DynTrait", focus_range: 6..15,
kind: Trait, name: "ImplTrait",
description: "trait DynTrait<T>", kind: Trait,
}, description: "trait ImplTrait<T>",
}, },
HoverGotoTypeData { },
mod_path: "test::S", HoverGotoTypeData {
nav: NavigationTarget { mod_path: "test::S",
file_id: FileId( nav: NavigationTarget {
0, file_id: FileId(
), 0,
full_range: 58..69, ),
focus_range: 65..66, full_range: 58..69,
name: "S", focus_range: 65..66,
kind: Struct, name: "S",
description: "struct S {}", kind: Struct,
}, description: "struct S {}",
}, },
], },
), ],
] ),
"#]], ]
"#]],
); );
} }
@ -7120,6 +7134,33 @@ impl Iterator for S {
); );
} }
#[test]
fn notable_ranged() {
check_hover_range(
r#"
//- minicore: future, iterator
struct S;
#[doc(notable_trait)]
trait Notable {}
impl Notable for S {}
impl core::future::Future for S {
type Output = u32;
}
impl Iterator for S {
type Item = S;
}
fn main() {
$0S$0;
}
"#,
expect![[r#"
// notable traits implemented: Notable, Future<Output = u32>, Iterator<Item = S>
```rust
S
```"#]],
);
}
#[test] #[test]
fn notable_actions() { fn notable_actions() {
check_actions( check_actions(
@ -7149,19 +7190,6 @@ impl Iterator for S {
), ),
GoToType( GoToType(
[ [
HoverGotoTypeData {
mod_path: "test::Notable",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 21..59,
focus_range: 49..56,
name: "Notable",
kind: Trait,
description: "trait Notable",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "core::future::Future", mod_path: "core::future::Future",
nav: NavigationTarget { nav: NavigationTarget {
@ -7190,6 +7218,19 @@ impl Iterator for S {
description: "pub trait Iterator", description: "pub trait Iterator",
}, },
}, },
HoverGotoTypeData {
mod_path: "test::Notable",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 21..59,
focus_range: 49..56,
name: "Notable",
kind: Trait,
description: "trait Notable",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::S2", mod_path: "test::S2",
nav: NavigationTarget { nav: NavigationTarget {