Skip to content

Commit 897f7e5

Browse files
authored
Merge pull request #18904 from Veykril/push-yztnorquuyzw
Improve hover module path rendering
2 parents 67fd72d + 3bae1f0 commit 897f7e5

File tree

5 files changed

+134
-24
lines changed

5 files changed

+134
-24
lines changed

crates/hir/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3916,6 +3916,10 @@ impl ToolModule {
39163916
db.crate_def_map(self.krate).registered_tools()[self.idx as usize].clone(),
39173917
)
39183918
}
3919+
3920+
pub fn krate(&self) -> Crate {
3921+
Crate { id: self.krate }
3922+
}
39193923
}
39203924

39213925
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]

crates/ide-db/src/defs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ impl Definition {
9292
Definition::ExternCrateDecl(it) => it.module(db),
9393
Definition::DeriveHelper(it) => it.derive().module(db),
9494
Definition::InlineAsmOperand(it) => it.parent(db).module(db),
95+
Definition::ToolModule(t) => t.krate().root_module(),
9596
Definition::BuiltinAttr(_)
9697
| Definition::BuiltinType(_)
9798
| Definition::BuiltinLifetime(_)
9899
| Definition::TupleField(_)
99-
| Definition::ToolModule(_)
100100
| Definition::InlineAsmRegOrRegClass(_) => return None,
101101
};
102102
Some(module)

crates/ide/src/hover/render.rs

+62-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{env, mem, ops::Not};
33

44
use either::Either;
55
use hir::{
6-
db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, AssocItemContainer, CaptureKind,
6+
db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, CaptureKind,
77
DynCompatibilityViolation, HasCrate, HasSource, HirDisplay, Layout, LayoutError,
88
MethodViolationCode, Name, Semantics, Symbol, Trait, Type, TypeInfo, VariantDef,
99
};
@@ -376,7 +376,7 @@ pub(super) fn process_markup(
376376
Markup::from(markup)
377377
}
378378

379-
fn definition_owner_name(db: &RootDatabase, def: &Definition, edition: Edition) -> Option<String> {
379+
fn definition_owner_name(db: &RootDatabase, def: Definition, edition: Edition) -> Option<String> {
380380
match def {
381381
Definition::Field(f) => {
382382
let parent = f.parent_def(db);
@@ -390,9 +390,52 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition, edition: Edition)
390390
_ => Some(parent_name),
391391
};
392392
}
393-
Definition::Local(l) => l.parent(db).name(db),
394393
Definition::Variant(e) => Some(e.parent_enum(db).name(db)),
395-
394+
Definition::GenericParam(generic_param) => match generic_param.parent() {
395+
hir::GenericDef::Adt(it) => Some(it.name(db)),
396+
hir::GenericDef::Trait(it) => Some(it.name(db)),
397+
hir::GenericDef::TraitAlias(it) => Some(it.name(db)),
398+
hir::GenericDef::TypeAlias(it) => Some(it.name(db)),
399+
400+
hir::GenericDef::Impl(i) => i.self_ty(db).as_adt().map(|adt| adt.name(db)),
401+
hir::GenericDef::Function(it) => {
402+
let container = it.as_assoc_item(db).and_then(|assoc| match assoc.container(db) {
403+
hir::AssocItemContainer::Trait(t) => Some(t.name(db)),
404+
hir::AssocItemContainer::Impl(i) => {
405+
i.self_ty(db).as_adt().map(|adt| adt.name(db))
406+
}
407+
});
408+
match container {
409+
Some(name) => {
410+
return Some(format!(
411+
"{}::{}",
412+
name.display(db, edition),
413+
it.name(db).display(db, edition)
414+
))
415+
}
416+
None => Some(it.name(db)),
417+
}
418+
}
419+
hir::GenericDef::Const(it) => {
420+
let container = it.as_assoc_item(db).and_then(|assoc| match assoc.container(db) {
421+
hir::AssocItemContainer::Trait(t) => Some(t.name(db)),
422+
hir::AssocItemContainer::Impl(i) => {
423+
i.self_ty(db).as_adt().map(|adt| adt.name(db))
424+
}
425+
});
426+
match container {
427+
Some(name) => {
428+
return Some(format!(
429+
"{}::{}",
430+
name.display(db, edition),
431+
it.name(db)?.display(db, edition)
432+
))
433+
}
434+
None => it.name(db),
435+
}
436+
}
437+
},
438+
Definition::DeriveHelper(derive_helper) => Some(derive_helper.derive().name(db)),
396439
d => {
397440
if let Some(assoc_item) = d.as_assoc_item(db) {
398441
match assoc_item.container(db) {
@@ -436,7 +479,7 @@ pub(super) fn definition(
436479
config: &HoverConfig,
437480
edition: Edition,
438481
) -> Markup {
439-
let mod_path = definition_mod_path(db, &def, edition);
482+
let mod_path = definition_path(db, &def, edition);
440483
let label = match def {
441484
Definition::Trait(trait_) => {
442485
trait_.display_limited(db, config.max_trait_assoc_items_count, edition).to_string()
@@ -915,19 +958,22 @@ fn closure_ty(
915958
Some(res)
916959
}
917960

918-
fn definition_mod_path(db: &RootDatabase, def: &Definition, edition: Edition) -> Option<String> {
919-
if matches!(def, Definition::GenericParam(_) | Definition::Local(_) | Definition::Label(_)) {
961+
fn definition_path(db: &RootDatabase, &def: &Definition, edition: Edition) -> Option<String> {
962+
if matches!(
963+
def,
964+
Definition::TupleField(_)
965+
| Definition::Label(_)
966+
| Definition::Local(_)
967+
| Definition::BuiltinAttr(_)
968+
| Definition::BuiltinLifetime(_)
969+
| Definition::BuiltinType(_)
970+
| Definition::InlineAsmRegOrRegClass(_)
971+
| Definition::InlineAsmOperand(_)
972+
) {
920973
return None;
921974
}
922-
let container: Option<Definition> =
923-
def.as_assoc_item(db).and_then(|assoc| match assoc.container(db) {
924-
AssocItemContainer::Trait(trait_) => Some(trait_.into()),
925-
AssocItemContainer::Impl(impl_) => impl_.self_ty(db).as_adt().map(|adt| adt.into()),
926-
});
927-
container
928-
.unwrap_or(*def)
929-
.module(db)
930-
.map(|module| path(db, module, definition_owner_name(db, def, edition), edition))
975+
let rendered_parent = definition_owner_name(db, def, edition);
976+
def.module(db).map(|module| path(db, module, rendered_parent, edition))
931977
}
932978

933979
fn markup(

crates/ide/src/hover/tests.rs

+66-6
Original file line numberDiff line numberDiff line change
@@ -4699,6 +4699,10 @@ fn hover_lifetime() {
46994699
expect![[r#"
47004700
*'lifetime*
47014701
4702+
```rust
4703+
ra_test_fixture::foo
4704+
```
4705+
47024706
```rust
47034707
'lifetime
47044708
```
@@ -4729,6 +4733,10 @@ impl<T: TraitA + TraitB> Foo<T$0> where T: Sized {}
47294733
expect![[r#"
47304734
*T*
47314735
4736+
```rust
4737+
ra_test_fixture::Foo
4738+
```
4739+
47324740
```rust
47334741
T: TraitA + TraitB
47344742
```
@@ -4743,6 +4751,10 @@ impl<T> Foo<T$0> {}
47434751
expect![[r#"
47444752
*T*
47454753
4754+
```rust
4755+
ra_test_fixture::Foo
4756+
```
4757+
47464758
```rust
47474759
T
47484760
```
@@ -4757,6 +4769,10 @@ impl<T: 'static> Foo<T$0> {}
47574769
expect![[r#"
47584770
*T*
47594771
4772+
```rust
4773+
ra_test_fixture::Foo
4774+
```
4775+
47604776
```rust
47614777
T: 'static
47624778
```
@@ -4777,6 +4793,10 @@ impl<T$0: Trait> Foo<T> {}
47774793
expect![[r#"
47784794
*T*
47794795
4796+
```rust
4797+
ra_test_fixture::Foo
4798+
```
4799+
47804800
```rust
47814801
T: Trait
47824802
```
@@ -4792,6 +4812,10 @@ impl<T$0: Trait + ?Sized> Foo<T> {}
47924812
expect![[r#"
47934813
*T*
47944814
4815+
```rust
4816+
ra_test_fixture::Foo
4817+
```
4818+
47954819
```rust
47964820
T: Trait + ?Sized
47974821
```
@@ -4812,6 +4836,10 @@ fn foo<T$0>() {}
48124836
expect![[r#"
48134837
*T*
48144838
4839+
```rust
4840+
ra_test_fixture::foo
4841+
```
4842+
48154843
```rust
48164844
T
48174845
```
@@ -4833,6 +4861,10 @@ fn foo<T$0: Sized>() {}
48334861
expect![[r#"
48344862
*T*
48354863
4864+
```rust
4865+
ra_test_fixture::foo
4866+
```
4867+
48364868
```rust
48374869
T
48384870
```
@@ -4854,6 +4886,10 @@ fn foo<T$0: ?Sized>() {}
48544886
expect![[r#"
48554887
*T*
48564888
4889+
```rust
4890+
ra_test_fixture::foo
4891+
```
4892+
48574893
```rust
48584894
T: ?Sized
48594895
```
@@ -4876,6 +4912,10 @@ fn foo<T$0: Trait>() {}
48764912
expect![[r#"
48774913
*T*
48784914
4915+
```rust
4916+
ra_test_fixture::foo
4917+
```
4918+
48794919
```rust
48804920
T: Trait
48814921
```
@@ -4898,6 +4938,10 @@ fn foo<T$0: Trait + Sized>() {}
48984938
expect![[r#"
48994939
*T*
49004940
4941+
```rust
4942+
ra_test_fixture::foo
4943+
```
4944+
49014945
```rust
49024946
T: Trait
49034947
```
@@ -4920,6 +4964,10 @@ fn foo<T$0: Trait + ?Sized>() {}
49204964
expect![[r#"
49214965
*T*
49224966
4967+
```rust
4968+
ra_test_fixture::foo
4969+
```
4970+
49234971
```rust
49244972
T: Trait + ?Sized
49254973
```
@@ -4941,6 +4989,10 @@ fn foo<T$0: ?Sized + Sized + Sized>() {}
49414989
expect![[r#"
49424990
*T*
49434991
4992+
```rust
4993+
ra_test_fixture::foo
4994+
```
4995+
49444996
```rust
49454997
T
49464998
```
@@ -4963,6 +5015,10 @@ fn foo<T$0: Sized + ?Sized + Sized + Trait>() {}
49635015
expect![[r#"
49645016
*T*
49655017
5018+
```rust
5019+
ra_test_fixture::foo
5020+
```
5021+
49665022
```rust
49675023
T: Trait
49685024
```
@@ -5010,6 +5066,10 @@ impl<const LEN: usize> Foo<LEN$0> {}
50105066
expect![[r#"
50115067
*LEN*
50125068
5069+
```rust
5070+
ra_test_fixture::Foo
5071+
```
5072+
50135073
```rust
50145074
const LEN: usize
50155075
```
@@ -7857,7 +7917,7 @@ fn test() {
78577917
*foo*
78587918
78597919
```rust
7860-
ra_test_fixture::S
7920+
ra_test_fixture::m::S
78617921
```
78627922
78637923
```rust
@@ -7886,7 +7946,7 @@ fn test() {
78867946
*foo*
78877947
78887948
```rust
7889-
ra_test_fixture::S
7949+
ra_test_fixture::m::S
78907950
```
78917951
78927952
```rust
@@ -7916,7 +7976,7 @@ mod m {
79167976
*foo*
79177977
79187978
```rust
7919-
ra_test_fixture::S
7979+
ra_test_fixture::m::inner::S
79207980
```
79217981
79227982
```rust
@@ -7946,7 +8006,7 @@ fn test() {
79468006
*A*
79478007
79488008
```rust
7949-
ra_test_fixture::S
8009+
ra_test_fixture::m::S
79508010
```
79518011
79528012
```rust
@@ -7975,7 +8035,7 @@ fn test() {
79758035
*A*
79768036
79778037
```rust
7978-
ra_test_fixture::S
8038+
ra_test_fixture::m::S
79798039
```
79808040
79818041
```rust
@@ -8005,7 +8065,7 @@ mod m {
80058065
*A*
80068066
80078067
```rust
8008-
ra_test_fixture::S
8068+
ra_test_fixture::m::inner::S
80098069
```
80108070
80118071
```rust

crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
4747
</style>
4848
<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>
49-
<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>
49+
<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>
5050
<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>
5151
<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>
5252
<span class="comment documentation">/// This is a doc comment</span>

0 commit comments

Comments
 (0)