Repository: rust-lang/rust-analyzer
Pull request #18904 (2025-01-10 11:38:52)

Improve hover module path rendering


Patch:
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 0f485493f7..29649cac94 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -3916,6 +3916,10 @@ impl ToolModule {
             db.crate_def_map(self.krate).registered_tools()[self.idx as usize].clone(),
         )
     }
+
+    pub fn krate(&self) -> Crate {
+        Crate { id: self.krate }
+    }
 }
 
 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
diff --git a/crates/ide-db/src/defs.rs b/crates/ide-db/src/defs.rs
index a5587507f1..32d242e8f2 100644
--- a/crates/ide-db/src/defs.rs
+++ b/crates/ide-db/src/defs.rs
@@ -92,11 +92,11 @@ impl Definition {
             Definition::ExternCrateDecl(it) => it.module(db),
             Definition::DeriveHelper(it) => it.derive().module(db),
             Definition::InlineAsmOperand(it) => it.parent(db).module(db),
+            Definition::ToolModule(t) => t.krate().root_module(),
             Definition::BuiltinAttr(_)
             | Definition::BuiltinType(_)
             | Definition::BuiltinLifetime(_)
             | Definition::TupleField(_)
-            | Definition::ToolModule(_)
             | Definition::InlineAsmRegOrRegClass(_) => return None,
         };
         Some(module)
diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs
index 8fbd445d96..46242b75dd 100644
--- a/crates/ide/src/hover/render.rs
+++ b/crates/ide/src/hover/render.rs
@@ -3,7 +3,7 @@ use std::{env, mem, ops::Not};
 
 use either::Either;
 use hir::{
-    db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, AssocItemContainer, CaptureKind,
+    db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, CaptureKind,
     DynCompatibilityViolation, HasCrate, HasSource, HirDisplay, Layout, LayoutError,
     MethodViolationCode, Name, Semantics, Symbol, Trait, Type, TypeInfo, VariantDef,
 };
@@ -376,7 +376,7 @@ pub(super) fn process_markup(
     Markup::from(markup)
 }
 
-fn definition_owner_name(db: &RootDatabase, def: &Definition, edition: Edition) -> Option<String> {
+fn definition_owner_name(db: &RootDatabase, def: Definition, edition: Edition) -> Option<String> {
     match def {
         Definition::Field(f) => {
             let parent = f.parent_def(db);
@@ -390,9 +390,52 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition, edition: Edition)
                 _ => Some(parent_name),
             };
         }
-        Definition::Local(l) => l.parent(db).name(db),
         Definition::Variant(e) => Some(e.parent_enum(db).name(db)),
-
+        Definition::GenericParam(generic_param) => match generic_param.parent() {
+            hir::GenericDef::Adt(it) => Some(it.name(db)),
+            hir::GenericDef::Trait(it) => Some(it.name(db)),
+            hir::GenericDef::TraitAlias(it) => Some(it.name(db)),
+            hir::GenericDef::TypeAlias(it) => Some(it.name(db)),
+
+            hir::GenericDef::Impl(i) => i.self_ty(db).as_adt().map(|adt| adt.name(db)),
+            hir::GenericDef::Function(it) => {
+                let container = it.as_assoc_item(db).and_then(|assoc| match assoc.container(db) {
+                    hir::AssocItemContainer::Trait(t) => Some(t.name(db)),
+                    hir::AssocItemContainer::Impl(i) => {
+                        i.self_ty(db).as_adt().map(|adt| adt.name(db))
+                    }
+                });
+                match container {
+                    Some(name) => {
+                        return Some(format!(
+                            "{}::{}",
+                            name.display(db, edition),
+                            it.name(db).display(db, edition)
+                        ))
+                    }
+                    None => Some(it.name(db)),
+                }
+            }
+            hir::GenericDef::Const(it) => {
+                let container = it.as_assoc_item(db).and_then(|assoc| match assoc.container(db) {
+                    hir::AssocItemContainer::Trait(t) => Some(t.name(db)),
+                    hir::AssocItemContainer::Impl(i) => {
+                        i.self_ty(db).as_adt().map(|adt| adt.name(db))
+                    }
+                });
+                match container {
+                    Some(name) => {
+                        return Some(format!(
+                            "{}::{}",
+                            name.display(db, edition),
+                            it.name(db)?.display(db, edition)
+                        ))
+                    }
+                    None => it.name(db),
+                }
+            }
+        },
+        Definition::DeriveHelper(derive_helper) => Some(derive_helper.derive().name(db)),
         d => {
             if let Some(assoc_item) = d.as_assoc_item(db) {
                 match assoc_item.container(db) {
@@ -436,7 +479,7 @@ pub(super) fn definition(
     config: &HoverConfig,
     edition: Edition,
 ) -> Markup {
-    let mod_path = definition_mod_path(db, &def, edition);
+    let mod_path = definition_path(db, &def, edition);
     let label = match def {
         Definition::Trait(trait_) => {
             trait_.display_limited(db, config.max_trait_assoc_items_count, edition).to_string()
@@ -915,19 +958,22 @@ fn closure_ty(
     Some(res)
 }
 
-fn definition_mod_path(db: &RootDatabase, def: &Definition, edition: Edition) -> Option<String> {
-    if matches!(def, Definition::GenericParam(_) | Definition::Local(_) | Definition::Label(_)) {
+fn definition_path(db: &RootDatabase, &def: &Definition, edition: Edition) -> Option<String> {
+    if matches!(
+        def,
+        Definition::TupleField(_)
+            | Definition::Label(_)
+            | Definition::Local(_)
+            | Definition::BuiltinAttr(_)
+            | Definition::BuiltinLifetime(_)
+            | Definition::BuiltinType(_)
+            | Definition::InlineAsmRegOrRegClass(_)
+            | Definition::InlineAsmOperand(_)
+    ) {
         return None;
     }
-    let container: Option<Definition> =
-        def.as_assoc_item(db).and_then(|assoc| match assoc.container(db) {
-            AssocItemContainer::Trait(trait_) => Some(trait_.into()),
-            AssocItemContainer::Impl(impl_) => impl_.self_ty(db).as_adt().map(|adt| adt.into()),
-        });
-    container
-        .unwrap_or(*def)
-        .module(db)
-        .map(|module| path(db, module, definition_owner_name(db, def, edition), edition))
+    let rendered_parent = definition_owner_name(db, def, edition);
+    def.module(db).map(|module| path(db, module, rendered_parent, edition))
 }
 
 fn markup(


Test patch:
diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs
index 7f3644c08d..501dd642a8 100644
--- a/crates/ide/src/hover/tests.rs
+++ b/crates/ide/src/hover/tests.rs
@@ -4699,6 +4699,10 @@ fn hover_lifetime() {
         expect![[r#"
             *'lifetime*
 
+            ```rust
+            ra_test_fixture::foo
+            ```
+
             ```rust
             'lifetime
             ```
@@ -4729,6 +4733,10 @@ impl<T: TraitA + TraitB> Foo<T$0> where T: Sized {}
         expect![[r#"
             *T*
 
+            ```rust
+            ra_test_fixture::Foo
+            ```
+
             ```rust
             T: TraitA + TraitB
             ```
@@ -4743,6 +4751,10 @@ impl<T> Foo<T$0> {}
         expect![[r#"
             *T*
 
+            ```rust
+            ra_test_fixture::Foo
+            ```
+
             ```rust
             T
             ```
@@ -4757,6 +4769,10 @@ impl<T: 'static> Foo<T$0> {}
         expect![[r#"
             *T*
 
+            ```rust
+            ra_test_fixture::Foo
+            ```
+
             ```rust
             T: 'static
             ```
@@ -4777,6 +4793,10 @@ impl<T$0: Trait> Foo<T> {}
         expect![[r#"
             *T*
 
+            ```rust
+            ra_test_fixture::Foo
+            ```
+
             ```rust
             T: Trait
             ```
@@ -4792,6 +4812,10 @@ impl<T$0: Trait + ?Sized> Foo<T> {}
         expect![[r#"
             *T*
 
+            ```rust
+            ra_test_fixture::Foo
+            ```
+
             ```rust
             T: Trait + ?Sized
             ```
@@ -4812,6 +4836,10 @@ fn foo<T$0>() {}
             expect![[r#"
                 *T*
 
+                ```rust
+                ra_test_fixture::foo
+                ```
+
                 ```rust
                 T
                 ```
@@ -4833,6 +4861,10 @@ fn foo<T$0: Sized>() {}
             expect![[r#"
                 *T*
 
+                ```rust
+                ra_test_fixture::foo
+                ```
+
                 ```rust
                 T
                 ```
@@ -4854,6 +4886,10 @@ fn foo<T$0: ?Sized>() {}
             expect![[r#"
                 *T*
 
+                ```rust
+                ra_test_fixture::foo
+                ```
+
                 ```rust
                 T: ?Sized
                 ```
@@ -4876,6 +4912,10 @@ fn foo<T$0: Trait>() {}
             expect![[r#"
                 *T*
 
+                ```rust
+                ra_test_fixture::foo
+                ```
+
                 ```rust
                 T: Trait
                 ```
@@ -4898,6 +4938,10 @@ fn foo<T$0: Trait + Sized>() {}
             expect![[r#"
                 *T*
 
+                ```rust
+                ra_test_fixture::foo
+                ```
+
                 ```rust
                 T: Trait
                 ```
@@ -4920,6 +4964,10 @@ fn foo<T$0: Trait + ?Sized>() {}
             expect![[r#"
                 *T*
 
+                ```rust
+                ra_test_fixture::foo
+                ```
+
                 ```rust
                 T: Trait + ?Sized
                 ```
@@ -4941,6 +4989,10 @@ fn foo<T$0: ?Sized + Sized + Sized>() {}
             expect![[r#"
                 *T*
 
+                ```rust
+                ra_test_fixture::foo
+                ```
+
                 ```rust
                 T
                 ```
@@ -4963,6 +5015,10 @@ fn foo<T$0: Sized + ?Sized + Sized + Trait>() {}
             expect![[r#"
                 *T*
 
+                ```rust
+                ra_test_fixture::foo
+                ```
+
                 ```rust
                 T: Trait
                 ```
@@ -5010,6 +5066,10 @@ impl<const LEN: usize> Foo<LEN$0> {}
         expect![[r#"
             *LEN*
 
+            ```rust
+            ra_test_fixture::Foo
+            ```
+
             ```rust
             const LEN: usize
             ```
@@ -7857,7 +7917,7 @@ fn test() {
             *foo*
 
             ```rust
-            ra_test_fixture::S
+            ra_test_fixture::m::S
             ```
 
             ```rust
@@ -7886,7 +7946,7 @@ fn test() {
             *foo*
 
             ```rust
-            ra_test_fixture::S
+            ra_test_fixture::m::S
             ```
 
             ```rust
@@ -7916,7 +7976,7 @@ mod m {
             *foo*
 
             ```rust
-            ra_test_fixture::S
+            ra_test_fixture::m::inner::S
             ```
 
             ```rust
@@ -7946,7 +8006,7 @@ fn test() {
             *A*
 
             ```rust
-            ra_test_fixture::S
+            ra_test_fixture::m::S
             ```
 
             ```rust
@@ -7975,7 +8035,7 @@ fn test() {
             *A*
 
             ```rust
-            ra_test_fixture::S
+            ra_test_fixture::m::S
             ```
 
             ```rust
@@ -8005,7 +8065,7 @@ mod m {
             *A*
 
             ```rust
-            ra_test_fixture::S
+            ra_test_fixture::m::inner::S
             ```
 
             ```rust
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html b/crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html
index cad5a8b593..485d44f97e 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html
@@ -46,7 +46,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 .unresolved_reference    { color: #FC5555; text-decoration: wavy underline; }
 </style>
 <pre><code><span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">allow</span><span class="parenthesis attribute">(</span><span class="none attribute">dead_code</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span>
-<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="tool_module attribute library">rustfmt</span><span class="operator attribute">::</span><span class="tool_module attribute library">skip</span><span class="attribute_bracket attribute">]</span>
+<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="tool_module attribute">rustfmt</span><span class="operator attribute">::</span><span class="tool_module attribute">skip</span><span class="attribute_bracket attribute">]</span>
 <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="module attribute crate_root library">proc_macros</span><span class="operator attribute">::</span><span class="attribute attribute library">identity</span><span class="attribute_bracket attribute">]</span>
 <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="attribute attribute default_library library">derive</span><span class="parenthesis attribute">(</span><span class="derive attribute default_library library">Default</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span>
 <span class="comment documentation">/// This is a doc comment</span>