mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-11 20:58:54 +00:00
Merge pull request #18904 from Veykril/push-yztnorquuyzw
Improve hover module path rendering
This commit is contained in:
commit
897f7e579e
5 changed files with 133 additions and 23 deletions
|
@ -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)]
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue