Skip to content

Commit

Permalink
Improve hover module path rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jan 10, 2025
1 parent 1b52a66 commit 3bae1f0
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 24 deletions.
4 changes: 4 additions & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
78 changes: 62 additions & 16 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
72 changes: 66 additions & 6 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4699,6 +4699,10 @@ fn hover_lifetime() {
expect![[r#"
*'lifetime*
```rust
ra_test_fixture::foo
```
```rust
'lifetime
```
Expand Down Expand Up @@ -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
```
Expand All @@ -4743,6 +4751,10 @@ impl<T> Foo<T$0> {}
expect![[r#"
*T*
```rust
ra_test_fixture::Foo
```
```rust
T
```
Expand All @@ -4757,6 +4769,10 @@ impl<T: 'static> Foo<T$0> {}
expect![[r#"
*T*
```rust
ra_test_fixture::Foo
```
```rust
T: 'static
```
Expand All @@ -4777,6 +4793,10 @@ impl<T$0: Trait> Foo<T> {}
expect![[r#"
*T*
```rust
ra_test_fixture::Foo
```
```rust
T: Trait
```
Expand All @@ -4792,6 +4812,10 @@ impl<T$0: Trait + ?Sized> Foo<T> {}
expect![[r#"
*T*
```rust
ra_test_fixture::Foo
```
```rust
T: Trait + ?Sized
```
Expand All @@ -4812,6 +4836,10 @@ fn foo<T$0>() {}
expect![[r#"
*T*
```rust
ra_test_fixture::foo
```
```rust
T
```
Expand All @@ -4833,6 +4861,10 @@ fn foo<T$0: Sized>() {}
expect![[r#"
*T*
```rust
ra_test_fixture::foo
```
```rust
T
```
Expand All @@ -4854,6 +4886,10 @@ fn foo<T$0: ?Sized>() {}
expect![[r#"
*T*
```rust
ra_test_fixture::foo
```
```rust
T: ?Sized
```
Expand All @@ -4876,6 +4912,10 @@ fn foo<T$0: Trait>() {}
expect![[r#"
*T*
```rust
ra_test_fixture::foo
```
```rust
T: Trait
```
Expand All @@ -4898,6 +4938,10 @@ fn foo<T$0: Trait + Sized>() {}
expect![[r#"
*T*
```rust
ra_test_fixture::foo
```
```rust
T: Trait
```
Expand All @@ -4920,6 +4964,10 @@ fn foo<T$0: Trait + ?Sized>() {}
expect![[r#"
*T*
```rust
ra_test_fixture::foo
```
```rust
T: Trait + ?Sized
```
Expand All @@ -4941,6 +4989,10 @@ fn foo<T$0: ?Sized + Sized + Sized>() {}
expect![[r#"
*T*
```rust
ra_test_fixture::foo
```
```rust
T
```
Expand All @@ -4963,6 +5015,10 @@ fn foo<T$0: Sized + ?Sized + Sized + Trait>() {}
expect![[r#"
*T*
```rust
ra_test_fixture::foo
```
```rust
T: Trait
```
Expand Down Expand Up @@ -5010,6 +5066,10 @@ impl<const LEN: usize> Foo<LEN$0> {}
expect![[r#"
*LEN*
```rust
ra_test_fixture::Foo
```
```rust
const LEN: usize
```
Expand Down Expand Up @@ -7857,7 +7917,7 @@ fn test() {
*foo*
```rust
ra_test_fixture::S
ra_test_fixture::m::S
```
```rust
Expand Down Expand Up @@ -7886,7 +7946,7 @@ fn test() {
*foo*
```rust
ra_test_fixture::S
ra_test_fixture::m::S
```
```rust
Expand Down Expand Up @@ -7916,7 +7976,7 @@ mod m {
*foo*
```rust
ra_test_fixture::S
ra_test_fixture::m::inner::S
```
```rust
Expand Down Expand Up @@ -7946,7 +8006,7 @@ fn test() {
*A*
```rust
ra_test_fixture::S
ra_test_fixture::m::S
```
```rust
Expand Down Expand Up @@ -7975,7 +8035,7 @@ fn test() {
*A*
```rust
ra_test_fixture::S
ra_test_fixture::m::S
```
```rust
Expand Down Expand Up @@ -8005,7 +8065,7 @@ mod m {
*A*
```rust
ra_test_fixture::S
ra_test_fixture::m::inner::S
```
```rust
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
.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>
Expand Down

0 comments on commit 3bae1f0

Please sign in to comment.