Skip to content

Commit bd47e14

Browse files
committed
Show GotoTypeAction for ConstParam
1 parent cd6426a commit bd47e14

File tree

2 files changed

+72
-37
lines changed

2 files changed

+72
-37
lines changed

crates/hir/src/code_model.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,12 @@ impl ConstParam {
13431343
pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef {
13441344
self.id.parent.into()
13451345
}
1346+
1347+
pub fn ty(self, db: &dyn HirDatabase) -> Type {
1348+
let def = self.id.parent;
1349+
let krate = def.module(db.upcast()).krate;
1350+
Type::new(db, krate, def, db.const_param_ty(self.id))
1351+
}
13461352
}
13471353

13481354
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

crates/ide/src/hover.rs

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -228,45 +228,41 @@ fn runnable_action(
228228
}
229229

230230
fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
231-
match def {
232-
Definition::Local(it) => {
233-
let mut targets: Vec<ModuleDef> = Vec::new();
234-
let mut push_new_def = |item: ModuleDef| {
235-
if !targets.contains(&item) {
236-
targets.push(item);
237-
}
238-
};
231+
let ty = match def {
232+
Definition::Local(it) => it.ty(db),
233+
Definition::ConstParam(it) => it.ty(db),
234+
_ => return None,
235+
};
236+
let mut targets: Vec<ModuleDef> = Vec::new();
237+
let mut push_new_def = |item: ModuleDef| {
238+
if !targets.contains(&item) {
239+
targets.push(item);
240+
}
241+
};
239242

240-
it.ty(db).walk(db, |t| {
241-
if let Some(adt) = t.as_adt() {
242-
push_new_def(adt.into());
243-
} else if let Some(trait_) = t.as_dyn_trait() {
244-
push_new_def(trait_.into());
245-
} else if let Some(traits) = t.as_impl_traits(db) {
246-
traits.into_iter().for_each(|it| push_new_def(it.into()));
247-
} else if let Some(trait_) = t.as_associated_type_parent_trait(db) {
248-
push_new_def(trait_.into());
249-
}
250-
});
251-
252-
let targets = targets
253-
.into_iter()
254-
.filter_map(|it| {
255-
Some(HoverGotoTypeData {
256-
mod_path: render_path(
257-
db,
258-
it.module(db)?,
259-
it.name(db).map(|name| name.to_string()),
260-
),
261-
nav: it.try_to_nav(db)?,
262-
})
263-
})
264-
.collect();
265-
266-
Some(HoverAction::GoToType(targets))
243+
ty.walk(db, |t| {
244+
if let Some(adt) = t.as_adt() {
245+
push_new_def(adt.into());
246+
} else if let Some(trait_) = t.as_dyn_trait() {
247+
push_new_def(trait_.into());
248+
} else if let Some(traits) = t.as_impl_traits(db) {
249+
traits.into_iter().for_each(|it| push_new_def(it.into()));
250+
} else if let Some(trait_) = t.as_associated_type_parent_trait(db) {
251+
push_new_def(trait_.into());
267252
}
268-
_ => None,
269-
}
253+
});
254+
255+
let targets = targets
256+
.into_iter()
257+
.filter_map(|it| {
258+
Some(HoverGotoTypeData {
259+
mod_path: render_path(db, it.module(db)?, it.name(db).map(|name| name.to_string())),
260+
nav: it.try_to_nav(db)?,
261+
})
262+
})
263+
.collect();
264+
265+
Some(HoverAction::GoToType(targets))
270266
}
271267

272268
fn hover_markup(
@@ -3083,6 +3079,39 @@ fn main() { let s<|>t = test().get(); }
30833079
);
30843080
}
30853081

3082+
#[test]
3083+
fn test_hover_const_param_has_goto_type_action() {
3084+
check_actions(
3085+
r#"
3086+
struct Bar;
3087+
struct Foo<const BAR: Bar>;
3088+
3089+
impl<const BAR: Bar> Foo<BAR<|>> {}
3090+
"#,
3091+
expect![[r#"
3092+
[
3093+
GoToType(
3094+
[
3095+
HoverGotoTypeData {
3096+
mod_path: "test::Bar",
3097+
nav: NavigationTarget {
3098+
file_id: FileId(
3099+
0,
3100+
),
3101+
full_range: 0..11,
3102+
focus_range: 7..10,
3103+
name: "Bar",
3104+
kind: Struct,
3105+
description: "struct Bar",
3106+
},
3107+
},
3108+
],
3109+
),
3110+
]
3111+
"#]],
3112+
);
3113+
}
3114+
30863115
#[test]
30873116
fn hover_displays_normalized_crate_names() {
30883117
check(

0 commit comments

Comments
 (0)