aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/hover.rs
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-01-01 14:07:41 +0000
committerLukas Wirth <[email protected]>2021-01-01 14:19:47 +0000
commitbbc0b41c3753dd1aee3f41075f0d242efcc5a827 (patch)
tree2842e25f9f68d0bfa487a8d90f4163fac0dd5961 /crates/ide/src/hover.rs
parent0e5fe4715360ab2f0d2954c9a8b37b247335bbcc (diff)
Show lifetimes and labels on hover
Diffstat (limited to 'crates/ide/src/hover.rs')
-rw-r--r--crates/ide/src/hover.rs50
1 files changed, 43 insertions, 7 deletions
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs
index 49eb8caae..98c7bfbe5 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -109,6 +109,8 @@ pub(crate) fn hover(
109 match node { 109 match node {
110 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.defined(sema.db)), 110 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.defined(sema.db)),
111 ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), 111 ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)),
112 ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime)
113 .map_or_else(|| NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(sema.db)), |d| d.defined(sema.db)),
112 _ => None, 114 _ => None,
113 } 115 }
114 }; 116 };
@@ -360,9 +362,9 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
360 ModuleDef::Static(it) => from_def_source(db, it, mod_path), 362 ModuleDef::Static(it) => from_def_source(db, it, mod_path),
361 ModuleDef::Trait(it) => from_def_source(db, it, mod_path), 363 ModuleDef::Trait(it) => from_def_source(db, it, mod_path),
362 ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), 364 ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path),
363 ModuleDef::BuiltinType(it) => return Some(it.to_string().into()), 365 ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it)),
364 }, 366 },
365 Definition::Local(it) => return Some(Markup::fenced_block(&it.ty(db).display(db))), 367 Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))),
366 Definition::SelfType(impl_def) => { 368 Definition::SelfType(impl_def) => {
367 impl_def.target_ty(db).as_adt().and_then(|adt| match adt { 369 impl_def.target_ty(db).as_adt().and_then(|adt| match adt {
368 Adt::Struct(it) => from_def_source(db, it, mod_path), 370 Adt::Struct(it) => from_def_source(db, it, mod_path),
@@ -370,10 +372,9 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
370 Adt::Enum(it) => from_def_source(db, it, mod_path), 372 Adt::Enum(it) => from_def_source(db, it, mod_path),
371 }) 373 })
372 } 374 }
373 Definition::TypeParam(_) 375 Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))),
374 | Definition::LifetimeParam(_) 376 Definition::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))),
375 | Definition::ConstParam(_) 377 Definition::TypeParam(_) | Definition::ConstParam(_) => {
376 | Definition::Label(_) => {
377 // FIXME: Hover for generic param 378 // FIXME: Hover for generic param
378 None 379 None
379 } 380 }
@@ -406,7 +407,7 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
406 return tokens.max_by_key(priority); 407 return tokens.max_by_key(priority);
407 fn priority(n: &SyntaxToken) -> usize { 408 fn priority(n: &SyntaxToken) -> usize {
408 match n.kind() { 409 match n.kind() {
409 IDENT | INT_NUMBER => 3, 410 IDENT | INT_NUMBER | LIFETIME_IDENT => 3,
410 T!['('] | T![')'] => 2, 411 T!['('] | T![')'] => 2,
411 kind if kind.is_trivia() => 0, 412 kind if kind.is_trivia() => 0,
412 _ => 1, 413 _ => 1,
@@ -1172,7 +1173,10 @@ fn f() { fo<|>o!(); }
1172 r#"struct TS(String, i32<|>);"#, 1173 r#"struct TS(String, i32<|>);"#,
1173 expect![[r#" 1174 expect![[r#"
1174 *i32* 1175 *i32*
1176
1177 ```rust
1175 i32 1178 i32
1179 ```
1176 "#]], 1180 "#]],
1177 ) 1181 )
1178 } 1182 }
@@ -3224,4 +3228,36 @@ fn no_hover() {
3224"#, 3228"#,
3225 ); 3229 );
3226 } 3230 }
3231
3232 #[test]
3233 fn hover_label() {
3234 check(
3235 r#"
3236fn foo() {
3237 'label<|>: loop {}
3238}
3239"#,
3240 expect![[r#"
3241 *'label*
3242
3243 ```rust
3244 'label
3245 ```
3246 "#]],
3247 );
3248 }
3249
3250 #[test]
3251 fn hover_lifetime() {
3252 check(
3253 r#"fn foo<'lifetime>(_: &'lifetime<|> ()) {}"#,
3254 expect![[r#"
3255 *'lifetime*
3256
3257 ```rust
3258 'lifetime
3259 ```
3260 "#]],
3261 );
3262 }
3227} 3263}