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,8 +393,28 @@ 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| {
render::definition(sema.db, def, famous_defs.as_ref(), &notable_traits, config).map(|markup| {
HoverResult {
markup: render::process_markup(sema.db, def, &markup, config),
actions: [
show_implementations_action(sema.db, def),
show_fn_references_action(sema.db, def),
runnable_action(sema, def, file_id),
goto_type_action_for_def(sema.db, def, &notable_traits),
]
.into_iter()
.flatten()
.collect(),
}
})
}
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()) db.notable_traits_in_deps(ty.krate(db).into())
.iter() .iter()
.flat_map(|it| &**it) .flat_map(|it| &**it)
@ -415,23 +435,6 @@ pub(crate) fn hover_for_definition(
}) })
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
})
.unwrap_or_default();
render::definition(sema.db, def, famous_defs.as_ref(), &notable_traits, config).map(|markup| {
HoverResult {
markup: render::process_markup(sema.db, def, &markup, config),
actions: [
show_implementations_action(sema.db, def),
show_fn_references_action(sema.db, def),
runnable_action(sema, def, file_id),
goto_type_action_for_def(sema.db, def, &notable_traits),
]
.into_iter()
.flatten()
.collect(),
}
})
} }
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> { fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
@ -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,7 +471,28 @@ pub(super) fn definition(
_ => None, _ => None,
}; };
let notable_traits = { let mut desc = String::new();
if let Some(notable_traits) = render_notable_trait_comment(db, notable_traits) {
desc.push_str(&notable_traits);
desc.push('\n');
}
if let Some(layout_info) = layout_info {
desc.push_str(&layout_info);
desc.push('\n');
}
desc.push_str(&label);
if let Some(value) = value {
desc.push_str(" = ");
desc.push_str(&value);
}
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 desc = String::new();
let mut needs_impl_header = true; let mut needs_impl_header = true;
for (trait_, assoc_types) in notable_traits { for (trait_, assoc_types) in notable_traits {
@ -499,24 +520,6 @@ pub(super) fn definition(
} }
} }
desc.is_empty().not().then(|| desc) desc.is_empty().not().then(|| desc)
};
let mut desc = String::new();
if let Some(notable_traits) = notable_traits {
desc.push_str(&notable_traits);
desc.push('\n');
}
if let Some(layout_info) = layout_info {
desc.push_str(&layout_info);
desc.push('\n');
}
desc.push_str(&label);
if let Some(value) = value {
desc.push_str(" = ");
desc.push_str(&value);
}
markup(docs.map(Into::into), desc, mod_path)
} }
fn type_info( fn type_info(
@ -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,",
},
},
], ],
), ),
] ]
@ -2390,19 +2404,6 @@ fn main() { let s$0t = S{ f1:Arg(0) }; }
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData {
mod_path: "test::S",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 17..37,
focus_range: 24..25,
name: "S",
kind: Struct,
description: "struct S<T> {\n f1: T,\n}",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::Arg", mod_path: "test::Arg",
nav: NavigationTarget { nav: NavigationTarget {
@ -2416,6 +2417,19 @@ fn main() { let s$0t = S{ f1:Arg(0) }; }
description: "struct Arg(u32);", description: "struct Arg(u32);",
}, },
}, },
HoverGotoTypeData {
mod_path: "test::S",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 17..37,
focus_range: 24..25,
name: "S",
kind: Struct,
description: "struct S<T> {\n f1: T,\n}",
},
},
], ],
), ),
] ]
@ -2449,19 +2463,6 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData {
mod_path: "test::S",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 17..37,
focus_range: 24..25,
name: "S",
kind: Struct,
description: "struct S<T> {\n f1: T,\n}",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::Arg", mod_path: "test::Arg",
nav: NavigationTarget { nav: NavigationTarget {
@ -2475,6 +2476,19 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
description: "struct Arg(u32);", description: "struct Arg(u32);",
}, },
}, },
HoverGotoTypeData {
mod_path: "test::S",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 17..37,
focus_range: 24..25,
name: "S",
kind: Struct,
description: "struct S<T> {\n f1: T,\n}",
},
},
], ],
), ),
] ]
@ -2639,19 +2653,6 @@ fn main() { let s$0t = foo(); }
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData {
mod_path: "test::Foo",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 0..12,
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::Bar", mod_path: "test::Bar",
nav: NavigationTarget { nav: NavigationTarget {
@ -2665,6 +2666,19 @@ fn main() { let s$0t = foo(); }
description: "trait Bar", description: "trait Bar",
}, },
}, },
HoverGotoTypeData {
mod_path: "test::Foo",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 0..12,
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo",
},
},
], ],
), ),
] ]
@ -2689,19 +2703,6 @@ fn main() { let s$0t = foo(); }
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData {
mod_path: "test::Foo",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 0..15,
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo<T>",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::Bar", mod_path: "test::Bar",
nav: NavigationTarget { nav: NavigationTarget {
@ -2715,6 +2716,19 @@ fn main() { let s$0t = foo(); }
description: "trait Bar<T>", description: "trait Bar<T>",
}, },
}, },
HoverGotoTypeData {
mod_path: "test::Foo",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 0..15,
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo<T>",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::S1", mod_path: "test::S1",
nav: NavigationTarget { nav: NavigationTarget {
@ -2793,19 +2807,6 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData {
mod_path: "test::Foo",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 0..12,
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::Bar", mod_path: "test::Bar",
nav: NavigationTarget { nav: NavigationTarget {
@ -2819,6 +2820,19 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
description: "trait Bar<T>", description: "trait Bar<T>",
}, },
}, },
HoverGotoTypeData {
mod_path: "test::Foo",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 0..12,
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::S", mod_path: "test::S",
nav: NavigationTarget { nav: NavigationTarget {
@ -3080,19 +3094,6 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
[ [
GoToType( GoToType(
[ [
HoverGotoTypeData {
mod_path: "test::ImplTrait",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 0..21,
focus_range: 6..15,
name: "ImplTrait",
kind: Trait,
description: "trait ImplTrait<T>",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::B", mod_path: "test::B",
nav: NavigationTarget { nav: NavigationTarget {
@ -3119,6 +3120,19 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
description: "trait DynTrait<T>", description: "trait DynTrait<T>",
}, },
}, },
HoverGotoTypeData {
mod_path: "test::ImplTrait",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 0..21,
focus_range: 6..15,
name: "ImplTrait",
kind: Trait,
description: "trait ImplTrait<T>",
},
},
HoverGotoTypeData { HoverGotoTypeData {
mod_path: "test::S", mod_path: "test::S",
nav: NavigationTarget { nav: NavigationTarget {
@ -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 {