Skip to content

Commit 8ec9094

Browse files
authored
Merge pull request rust-lang#18946 from lh123/goto-to-type-def-func-param-and-trait-bound
feat: show go-to-type-def actions for func param and trait bound when hovering
2 parents d6477b9 + fdc672e commit 8ec9094

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

src/tools/rust-analyzer/crates/ide/src/hover.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,28 @@ fn goto_type_action_for_def(
557557
.into_iter()
558558
.filter(|&it| Some(it.into()) != sized_trait)
559559
.for_each(|it| push_new_def(it.into()));
560+
} else if let Definition::Function(function) = def {
561+
walk_and_push_ty(db, &function.ret_type(db), &mut push_new_def);
562+
563+
let krate = function.module(db).krate();
564+
let sized_trait =
565+
db.lang_item(krate.into(), LangItem::Sized).and_then(|lang_item| lang_item.as_trait());
566+
for param in function.params_without_self(db) {
567+
if let Some(type_param) = param.ty().as_type_param(db) {
568+
type_param
569+
.trait_bounds(db)
570+
.into_iter()
571+
.filter(|&it| Some(it.into()) != sized_trait)
572+
.for_each(|it| push_new_def(it.into()));
573+
} else {
574+
walk_and_push_ty(db, param.ty(), &mut push_new_def);
575+
}
576+
}
560577
} else {
561578
let ty = match def {
562579
Definition::Local(it) => it.ty(db),
563580
Definition::GenericParam(hir::GenericParam::ConstParam(it)) => it.ty(db),
564581
Definition::Field(field) => field.ty(db),
565-
Definition::Function(function) => function.ret_type(db),
566582
_ => return HoverAction::goto_type_from_targets(db, targets, edition),
567583
};
568584

src/tools/rust-analyzer/crates/ide/src/hover/tests.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,97 @@ fn test() {
23682368
);
23692369
}
23702370

2371+
#[test]
2372+
fn test_hover_show_type_def_for_func_param() {
2373+
check_actions(
2374+
r#"
2375+
struct Bar;
2376+
fn f(b: Bar) {
2377+
2378+
}
2379+
2380+
fn test() {
2381+
let b = Bar;
2382+
f$0(b);
2383+
}
2384+
"#,
2385+
expect![[r#"
2386+
[
2387+
Reference(
2388+
FilePositionWrapper {
2389+
file_id: FileId(
2390+
0,
2391+
),
2392+
offset: 15,
2393+
},
2394+
),
2395+
GoToType(
2396+
[
2397+
HoverGotoTypeData {
2398+
mod_path: "ra_test_fixture::Bar",
2399+
nav: NavigationTarget {
2400+
file_id: FileId(
2401+
0,
2402+
),
2403+
full_range: 0..11,
2404+
focus_range: 7..10,
2405+
name: "Bar",
2406+
kind: Struct,
2407+
description: "struct Bar",
2408+
},
2409+
},
2410+
],
2411+
),
2412+
]
2413+
"#]],
2414+
);
2415+
}
2416+
2417+
#[test]
2418+
fn test_hover_show_type_def_for_trait_bound() {
2419+
check_actions(
2420+
r#"
2421+
trait Bar {}
2422+
fn f<T: Bar>(b: T) {
2423+
2424+
}
2425+
2426+
fn test() {
2427+
f$0();
2428+
}
2429+
"#,
2430+
expect![[r#"
2431+
[
2432+
Reference(
2433+
FilePositionWrapper {
2434+
file_id: FileId(
2435+
0,
2436+
),
2437+
offset: 16,
2438+
},
2439+
),
2440+
GoToType(
2441+
[
2442+
HoverGotoTypeData {
2443+
mod_path: "ra_test_fixture::Bar",
2444+
nav: NavigationTarget {
2445+
file_id: FileId(
2446+
0,
2447+
),
2448+
full_range: 0..12,
2449+
focus_range: 6..9,
2450+
name: "Bar",
2451+
kind: Trait,
2452+
description: "trait Bar",
2453+
},
2454+
},
2455+
],
2456+
),
2457+
]
2458+
"#]],
2459+
);
2460+
}
2461+
23712462
#[test]
23722463
fn test_hover_non_ascii_space_doc() {
23732464
check(

0 commit comments

Comments
 (0)