From 54b9b03ca2e90083fd1d1fe199c5dde595423b53 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 4 Jan 2021 15:44:19 +0100 Subject: Show GotoTypeAction for TypeParam --- crates/ide/src/hover.rs | 71 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 17 deletions(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 932279a06..f2ad95cb6 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -228,11 +228,6 @@ fn runnable_action( } fn goto_type_action(db: &RootDatabase, def: Definition) -> Option { - let ty = match def { - Definition::Local(it) => it.ty(db), - Definition::ConstParam(it) => it.ty(db), - _ => return None, - }; let mut targets: Vec = Vec::new(); let mut push_new_def = |item: ModuleDef| { if !targets.contains(&item) { @@ -240,17 +235,27 @@ fn goto_type_action(db: &RootDatabase, def: Definition) -> Option { } }; - ty.walk(db, |t| { - if let Some(adt) = t.as_adt() { - push_new_def(adt.into()); - } else if let Some(trait_) = t.as_dyn_trait() { - push_new_def(trait_.into()); - } else if let Some(traits) = t.as_impl_traits(db) { - traits.into_iter().for_each(|it| push_new_def(it.into())); - } else if let Some(trait_) = t.as_associated_type_parent_trait(db) { - push_new_def(trait_.into()); - } - }); + if let Definition::TypeParam(it) = def { + it.trait_bounds(db).into_iter().for_each(|it| push_new_def(it.into())); + } else { + let ty = match def { + Definition::Local(it) => it.ty(db), + Definition::ConstParam(it) => it.ty(db), + _ => return None, + }; + + ty.walk(db, |t| { + if let Some(adt) = t.as_adt() { + push_new_def(adt.into()); + } else if let Some(trait_) = t.as_dyn_trait() { + push_new_def(trait_.into()); + } else if let Some(traits) = t.as_impl_traits(db) { + traits.into_iter().for_each(|it| push_new_def(it.into())); + } else if let Some(trait_) = t.as_associated_type_parent_trait(db) { + push_new_def(trait_.into()); + } + }); + } let targets = targets .into_iter() @@ -3086,7 +3091,7 @@ fn main() { let s<|>t = test().get(); } struct Bar; struct Foo; -impl Foo> {} +impl Foo> {} "#, expect![[r#" [ @@ -3112,6 +3117,38 @@ impl Foo> {} ); } + #[test] + fn test_hover_type_param_has_goto_type_action() { + check_actions( + r#" +trait Foo {} + +fn foo(t: T<|>){} +"#, + expect![[r#" + [ + 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", + }, + }, + ], + ), + ] + "#]], + ); + } + #[test] fn hover_displays_normalized_crate_names() { check( -- cgit v1.2.3