]> 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 8cb4a51d8e220d818dc02c9fe77a7b2e82b65114..2024acd94e353fb065a1b69fee93018f9db48305 100644 (file)
@@ -1,6 +1,6 @@
 use hir::{
-    Adt, AsAssocItem, AssocItemContainer, FieldSource, HasAttrs, HasSource, HirDisplay, Module,
-    ModuleDef, ModuleSource, Semantics,
+    Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource,
+    HirDisplay, Module, ModuleDef, ModuleSource, Semantics,
 };
 use ide_db::base_db::SourceDatabase;
 use ide_db::{
@@ -17,7 +17,7 @@
     doc_links::{remove_links, rewrite_links},
     markdown_remove::remove_markdown,
     markup::Markup,
-    runnables::{runnable, runnable_fn},
+    runnables::{runnable_fn, runnable_mod},
     FileId, FilePosition, NavigationTarget, RangeInfo, Runnable,
 };
 
@@ -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(
@@ -192,7 +184,7 @@ fn runnable_action(
         Definition::ModuleDef(it) => match it {
             ModuleDef::Module(it) => match it.definition_source(sema.db).value {
                 ModuleSource::Module(it) => {
-                    runnable(&sema, it.syntax().clone()).map(|it| HoverAction::Runnable(it))
+                    runnable_mod(&sema, it).map(|it| HoverAction::Runnable(it))
                 }
                 _ => None,
             },
@@ -220,12 +212,12 @@ fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
         }
     };
 
-    if let Definition::TypeParam(it) = def {
+    if let Definition::GenericParam(GenericParam::TypeParam(it)) = def {
         it.trait_bounds(db).into_iter().for_each(|it| push_new_def(it.into()));
     } else {
         let ty = match def {
             Definition::Local(it) => it.ty(db),
-            Definition::ConstParam(it) => it.ty(db),
+            Definition::GenericParam(GenericParam::ConstParam(it)) => it.ty(db),
             _ => return None,
         };
 
@@ -357,9 +349,11 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
             })
         }
         Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))),
-        Definition::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))),
-        Definition::TypeParam(type_param) => Some(Markup::fenced_block(&type_param.display(db))),
-        Definition::ConstParam(it) => from_def_source(db, it, None),
+        Definition::GenericParam(it) => match it {
+            GenericParam::TypeParam(it) => Some(Markup::fenced_block(&it.display(db))),
+            GenericParam::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))),
+            GenericParam::ConstParam(it) => from_def_source(db, it, None),
+        },
     };
 
     fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup>
@@ -389,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,
@@ -1951,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]()
@@ -1987,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
@@ -3132,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(
@@ -3195,7 +3222,8 @@ fn bar(&sel$0f) {}
 }
 "#,
             expect![[r#"
-                *&self*
+                *self*
+
                 ```rust
                 &Foo
                 ```
@@ -3214,7 +3242,8 @@ fn bar(sel$0f: Arc<Foo>) {}
 }
 "#,
             expect![[r#"
-                *self: Arc<Foo>*
+                *self*
+
                 ```rust
                 Arc<Foo>
                 ```