From 51dda8ea8e4e64b3ee5ed6971d3182f6b3df987f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 17 Feb 2021 14:14:58 +0100 Subject: Show hover info of the definition of ConstReference patterns instead of its type --- crates/ide/src/hover.rs | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 00745238a..69b828f47 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -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 Foo {} ); } + #[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( -- cgit v1.2.3