aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-01-04 14:19:09 +0000
committerLukas Wirth <[email protected]>2021-01-04 14:19:09 +0000
commitbd47e140b6f6f39743d52f72437e6eaa2bfd9de1 (patch)
treed28417cc2832a5d3126cb43b3b9800f2afb4aed1
parentcd6426afe5daaeb90c4398669bcae03a6c353658 (diff)
Show GotoTypeAction for ConstParam
-rw-r--r--crates/hir/src/code_model.rs6
-rw-r--r--crates/ide/src/hover.rs103
2 files changed, 72 insertions, 37 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index a2255508e..a7a38d43a 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -1343,6 +1343,12 @@ impl ConstParam {
1343 pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef { 1343 pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef {
1344 self.id.parent.into() 1344 self.id.parent.into()
1345 } 1345 }
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 }
1346} 1352}
1347 1353
1348#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 1354#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs
index 399e3e484..932279a06 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -228,45 +228,41 @@ fn runnable_action(
228} 228}
229 229
230fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> { 230fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
231 match def { 231 let ty = match def {
232 Definition::Local(it) => { 232 Definition::Local(it) => it.ty(db),
233 let mut targets: Vec<ModuleDef> = Vec::new(); 233 Definition::ConstParam(it) => it.ty(db),
234 let mut push_new_def = |item: ModuleDef| { 234 _ => return None,
235 if !targets.contains(&item) { 235 };
236 targets.push(item); 236 let mut targets: Vec<ModuleDef> = Vec::new();
237 } 237 let mut push_new_def = |item: ModuleDef| {
238 }; 238 if !targets.contains(&item) {
239 targets.push(item);
240 }
241 };
239 242
240 it.ty(db).walk(db, |t| { 243 ty.walk(db, |t| {
241 if let Some(adt) = t.as_adt() { 244 if let Some(adt) = t.as_adt() {
242 push_new_def(adt.into()); 245 push_new_def(adt.into());
243 } else if let Some(trait_) = t.as_dyn_trait() { 246 } else if let Some(trait_) = t.as_dyn_trait() {
244 push_new_def(trait_.into()); 247 push_new_def(trait_.into());
245 } else if let Some(traits) = t.as_impl_traits(db) { 248 } else if let Some(traits) = t.as_impl_traits(db) {
246 traits.into_iter().for_each(|it| push_new_def(it.into())); 249 traits.into_iter().for_each(|it| push_new_def(it.into()));
247 } else if let Some(trait_) = t.as_associated_type_parent_trait(db) { 250 } else if let Some(trait_) = t.as_associated_type_parent_trait(db) {
248 push_new_def(trait_.into()); 251 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))
267 } 252 }
268 _ => None, 253 });
269 } 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))
270} 266}
271 267
272fn hover_markup( 268fn hover_markup(
@@ -3084,6 +3080,39 @@ fn main() { let s<|>t = test().get(); }
3084 } 3080 }
3085 3081
3086 #[test] 3082 #[test]
3083 fn test_hover_const_param_has_goto_type_action() {
3084 check_actions(
3085 r#"
3086struct Bar;
3087struct Foo<const BAR: Bar>;
3088
3089impl<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
3115 #[test]
3087 fn hover_displays_normalized_crate_names() { 3116 fn hover_displays_normalized_crate_names() {
3088 check( 3117 check(
3089 r#" 3118 r#"