]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide/src/hover.rs
Wrap remaining self/super/crate in Name{Ref}
[rust.git] / crates / ide / src / hover.rs
index e331f8886c891504f1788e0d04950a18805a7b62..2024acd94e353fb065a1b69fee93018f9db48305 100644 (file)
@@ -134,17 +134,14 @@ pub(crate) fn hover(
         return None;
     }
 
-    let node = token.ancestors().find(|n| {
-        ast::Expr::can_cast(n.kind())
-            || ast::Pat::can_cast(n.kind())
-            || ast::SelfParam::can_cast(n.kind())
-    })?;
+    let node = token
+        .ancestors()
+        .find(|n| ast::Expr::can_cast(n.kind()) || ast::Pat::can_cast(n.kind()))?;
 
     let ty = match_ast! {
         match node {
             ast::Expr(it) => sema.type_of_expr(&it)?,
             ast::Pat(it) => sema.type_of_pat(&it)?,
-            ast::SelfParam(self_param) => sema.type_of_self(&self_param)?,
             // If this node is a MACRO_CALL, it means that `descend_into_macros` failed to resolve.
             // (e.g expanding a builtin macro). So we give up here.
             ast::MacroCall(_it) => return None,
@@ -175,12 +172,7 @@ fn to_action(nav_target: NavigationTarget) -> HoverAction {
         Definition::SelfType(it) => it.target_ty(db).as_adt(),
         _ => None,
     }?;
-    match adt {
-        Adt::Struct(it) => it.try_to_nav(db),
-        Adt::Union(it) => it.try_to_nav(db),
-        Adt::Enum(it) => it.try_to_nav(db),
-    }
-    .map(to_action)
+    adt.try_to_nav(db).map(to_action)
 }
 
 fn runnable_action(
@@ -391,7 +383,7 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
     return tokens.max_by_key(priority);
     fn priority(n: &SyntaxToken) -> usize {
         match n.kind() {
-            IDENT | INT_NUMBER | LIFETIME_IDENT => 3,
+            IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] => 3,
             T!['('] | T![')'] => 2,
             kind if kind.is_trivia() => 0,
             _ => 1,
@@ -1953,16 +1945,16 @@ fn test_hover_no_links() {
 /// Test cases:
 /// case 1.  bare URL: https://www.example.com/
 /// case 2.  inline URL with title: [example](https://www.example.com/)
-/// case 3.  code refrence: [`Result`]
-/// case 4.  code refrence but miss footnote: [`String`]
+/// case 3.  code reference: [`Result`]
+/// case 4.  code reference but miss footnote: [`String`]
 /// case 5.  autolink: <http://www.example.com/>
 /// case 6.  email address: <test@example.com>
-/// case 7.  refrence: [example][example]
+/// case 7.  reference: [example][example]
 /// case 8.  collapsed link: [example][]
 /// case 9.  shortcut link: [example]
 /// case 10. inline without URL: [example]()
-/// case 11. refrence: [foo][foo]
-/// case 12. refrence: [foo][bar]
+/// case 11. reference: [foo][foo]
+/// case 12. reference: [foo][bar]
 /// case 13. collapsed link: [foo][]
 /// case 14. shortcut link: [foo]
 /// case 15. inline without URL: [foo]()
@@ -1989,16 +1981,16 @@ pub fn foo()
                 Test cases:
                 case 1.  bare URL: https://www.example.com/
                 case 2.  inline URL with title: [example](https://www.example.com/)
-                case 3.  code refrence: `Result`
-                case 4.  code refrence but miss footnote: `String`
+                case 3.  code reference: `Result`
+                case 4.  code reference but miss footnote: `String`
                 case 5.  autolink: http://www.example.com/
                 case 6.  email address: test@example.com
-                case 7.  refrence: example
+                case 7.  reference: example
                 case 8.  collapsed link: example
                 case 9.  shortcut link: example
                 case 10. inline without URL: example
-                case 11. refrence: foo
-                case 12. refrence: foo
+                case 11. reference: foo
+                case 12. reference: foo
                 case 13. collapsed link: foo
                 case 14. shortcut link: foo
                 case 15. inline without URL: foo
@@ -3134,6 +3126,39 @@ fn foo<T: Foo>(t: T$0){}
         );
     }
 
+    #[test]
+    fn test_hover_self_has_go_to_type() {
+        check_actions(
+            r#"
+struct Foo;
+impl Foo {
+    fn foo(&self$0) {}
+}
+"#,
+            expect![[r#"
+                [
+                    GoToType(
+                        [
+                            HoverGotoTypeData {
+                                mod_path: "test::Foo",
+                                nav: NavigationTarget {
+                                    file_id: FileId(
+                                        0,
+                                    ),
+                                    full_range: 0..11,
+                                    focus_range: 7..10,
+                                    name: "Foo",
+                                    kind: Struct,
+                                    description: "struct Foo",
+                                },
+                            },
+                        ],
+                    ),
+                ]
+            "#]],
+        );
+    }
+
     #[test]
     fn hover_displays_normalized_crate_names() {
         check(
@@ -3197,7 +3222,8 @@ fn bar(&sel$0f) {}
 }
 "#,
             expect![[r#"
-                *&self*
+                *self*
+
                 ```rust
                 &Foo
                 ```
@@ -3216,7 +3242,8 @@ fn bar(sel$0f: Arc<Foo>) {}
 }
 "#,
             expect![[r#"
-                *self: Arc<Foo>*
+                *self*
+
                 ```rust
                 Arc<Foo>
                 ```