]> git.lizzy.rs Git - rust.git/commitdiff
Show hover info of the definition of ConstReference patterns instead of its type
authorLukas Wirth <lukastw97@gmail.com>
Wed, 17 Feb 2021 13:14:58 +0000 (14:14 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Wed, 17 Feb 2021 13:14:58 +0000 (14:14 +0100)
crates/ide/src/hover.rs

index 00745238afd206b398c5a362b869d966414383ad..69b828f477fda1bf7a3efd5db90987569c4a71f8 100644 (file)
@@ -2,8 +2,8 @@
     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(