7705: Show hover info of the definition of ConstReference patterns instead of its type r=Veykril a=Veykril

Closes #7671
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-02-17 13:17:15 +00:00 committed by GitHub
commit 056601b41f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,8 +2,8 @@ use hir::{
Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource,
HirDisplay, Module, ModuleDef, ModuleSource, Semantics,
};
use ide_db::base_db::SourceDatabase;
use ide_db::{
base_db::SourceDatabase,
defs::{Definition, NameClass, NameRefClass},
RootDatabase,
};
@ -94,7 +94,12 @@ pub(crate) fn hover(
let node = token.parent();
let definition = match_ast! {
match node {
ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.defined(sema.db)),
// we don't use NameClass::referenced_or_defined here as we do not want to resolve
// field pattern shorthands to their definition
ast::Name(name) => NameClass::classify(&sema, &name).and_then(|class| match class {
NameClass::ConstReference(def) => Some(def),
def => def.defined(sema.db),
}),
ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)),
ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime)
.map_or_else(|| NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(sema.db)), |d| d.defined(sema.db)),
@ -3445,6 +3450,37 @@ impl<const LEN: usize> Foo<LEN$0> {}
);
}
#[test]
fn hover_const_pat() {
check(
r#"
/// This is a doc
const FOO: usize = 3;
fn foo() {
match 5 {
FOO$0 => (),
_ => ()
}
}
"#,
expect![[r#"
*FOO*
```rust
test
```
```rust
const FOO: usize = 3
```
---
This is a doc
"#]],
);
}
#[test]
fn hover_mod_def() {
check(