Show GotoTypeAction for ConstParam

This commit is contained in:
Lukas Wirth 2021-01-04 15:19:09 +01:00
parent cd6426afe5
commit bd47e140b6
2 changed files with 73 additions and 38 deletions

View File

@ -1343,6 +1343,12 @@ impl ConstParam {
pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef {
self.id.parent.into()
}
pub fn ty(self, db: &dyn HirDatabase) -> Type {
let def = self.id.parent;
let krate = def.module(db.upcast()).krate;
Type::new(db, krate, def, db.const_param_ty(self.id))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

View File

@ -228,8 +228,11 @@ fn runnable_action(
}
fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
match def {
Definition::Local(it) => {
let ty = match def {
Definition::Local(it) => it.ty(db),
Definition::ConstParam(it) => it.ty(db),
_ => return None,
};
let mut targets: Vec<ModuleDef> = Vec::new();
let mut push_new_def = |item: ModuleDef| {
if !targets.contains(&item) {
@ -237,7 +240,7 @@ fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
}
};
it.ty(db).walk(db, |t| {
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() {
@ -253,20 +256,13 @@ fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
.into_iter()
.filter_map(|it| {
Some(HoverGotoTypeData {
mod_path: render_path(
db,
it.module(db)?,
it.name(db).map(|name| name.to_string()),
),
mod_path: render_path(db, it.module(db)?, it.name(db).map(|name| name.to_string())),
nav: it.try_to_nav(db)?,
})
})
.collect();
Some(HoverAction::GoToType(targets))
}
_ => None,
}
}
fn hover_markup(
@ -3083,6 +3079,39 @@ fn main() { let s<|>t = test().get(); }
);
}
#[test]
fn test_hover_const_param_has_goto_type_action() {
check_actions(
r#"
struct Bar;
struct Foo<const BAR: Bar>;
impl<const BAR: Bar> Foo<BAR<|>> {}
"#,
expect![[r#"
[
GoToType(
[
HoverGotoTypeData {
mod_path: "test::Bar",
nav: NavigationTarget {
file_id: FileId(
0,
),
full_range: 0..11,
focus_range: 7..10,
name: "Bar",
kind: Struct,
description: "struct Bar",
},
},
],
),
]
"#]],
);
}
#[test]
fn hover_displays_normalized_crate_names() {
check(