Skip to content

Commit 85203d9

Browse files
committed
Render assoc item owner in hover for items other than functions
1 parent a822291 commit 85203d9

File tree

4 files changed

+135
-28
lines changed

4 files changed

+135
-28
lines changed

crates/hir/src/lib.rs

+88
Original file line numberDiff line numberDiff line change
@@ -2653,6 +2653,37 @@ impl ItemInNs {
26532653
}
26542654
}
26552655

2656+
/// Invariant: `inner.as_extern_assoc_item(db).is_some()`
2657+
/// We do not actively enforce this invariant.
2658+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2659+
pub enum ExternAssocItem {
2660+
Function(Function),
2661+
Static(Static),
2662+
TypeAlias(TypeAlias),
2663+
}
2664+
2665+
pub trait AsExternAssocItem {
2666+
fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem>;
2667+
}
2668+
2669+
impl AsExternAssocItem for Function {
2670+
fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem> {
2671+
as_extern_assoc_item(db, ExternAssocItem::Function, self.id)
2672+
}
2673+
}
2674+
2675+
impl AsExternAssocItem for Static {
2676+
fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem> {
2677+
as_extern_assoc_item(db, ExternAssocItem::Static, self.id)
2678+
}
2679+
}
2680+
2681+
impl AsExternAssocItem for TypeAlias {
2682+
fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem> {
2683+
as_extern_assoc_item(db, ExternAssocItem::TypeAlias, self.id)
2684+
}
2685+
}
2686+
26562687
/// Invariant: `inner.as_assoc_item(db).is_some()`
26572688
/// We do not actively enforce this invariant.
26582689
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
@@ -2727,6 +2758,63 @@ where
27272758
}
27282759
}
27292760

2761+
fn as_extern_assoc_item<'db, ID, DEF, LOC>(
2762+
db: &(dyn HirDatabase + 'db),
2763+
ctor: impl FnOnce(DEF) -> ExternAssocItem,
2764+
id: ID,
2765+
) -> Option<ExternAssocItem>
2766+
where
2767+
ID: Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<LOC>>,
2768+
DEF: From<ID>,
2769+
LOC: ItemTreeNode,
2770+
{
2771+
match id.lookup(db.upcast()).container {
2772+
ItemContainerId::ExternBlockId(_) => Some(ctor(DEF::from(id))),
2773+
ItemContainerId::TraitId(_) | ItemContainerId::ImplId(_) | ItemContainerId::ModuleId(_) => {
2774+
None
2775+
}
2776+
}
2777+
}
2778+
2779+
impl ExternAssocItem {
2780+
pub fn name(self, db: &dyn HirDatabase) -> Name {
2781+
match self {
2782+
Self::Function(it) => it.name(db),
2783+
Self::Static(it) => it.name(db),
2784+
Self::TypeAlias(it) => it.name(db),
2785+
}
2786+
}
2787+
2788+
pub fn module(self, db: &dyn HirDatabase) -> Module {
2789+
match self {
2790+
Self::Function(f) => f.module(db),
2791+
Self::Static(c) => c.module(db),
2792+
Self::TypeAlias(t) => t.module(db),
2793+
}
2794+
}
2795+
2796+
pub fn as_function(self) -> Option<Function> {
2797+
match self {
2798+
Self::Function(v) => Some(v),
2799+
_ => None,
2800+
}
2801+
}
2802+
2803+
pub fn as_static(self) -> Option<Static> {
2804+
match self {
2805+
Self::Static(v) => Some(v),
2806+
_ => None,
2807+
}
2808+
}
2809+
2810+
pub fn as_type_alias(self) -> Option<TypeAlias> {
2811+
match self {
2812+
Self::TypeAlias(v) => Some(v),
2813+
_ => None,
2814+
}
2815+
}
2816+
}
2817+
27302818
impl AssocItem {
27312819
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
27322820
match self {

crates/ide-db/src/defs.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
use arrayvec::ArrayVec;
99
use either::Either;
1010
use hir::{
11-
Adt, AsAssocItem, AssocItem, AttributeTemplate, BuiltinAttr, BuiltinType, Const, Crate,
12-
DefWithBody, DeriveHelper, DocLinkDef, ExternCrateDecl, Field, Function, GenericParam,
13-
HasVisibility, HirDisplay, Impl, Label, Local, Macro, Module, ModuleDef, Name, PathResolution,
14-
Semantics, Static, ToolModule, Trait, TraitAlias, TupleField, TypeAlias, Variant, VariantDef,
15-
Visibility,
11+
Adt, AsAssocItem, AsExternAssocItem, AssocItem, AttributeTemplate, BuiltinAttr, BuiltinType,
12+
Const, Crate, DefWithBody, DeriveHelper, DocLinkDef, ExternAssocItem, ExternCrateDecl, Field,
13+
Function, GenericParam, HasVisibility, HirDisplay, Impl, Label, Local, Macro, Module,
14+
ModuleDef, Name, PathResolution, Semantics, Static, ToolModule, Trait, TraitAlias, TupleField,
15+
TypeAlias, Variant, VariantDef, Visibility,
1616
};
1717
use stdx::{format_to, impl_from};
1818
use syntax::{
@@ -742,6 +742,17 @@ impl AsAssocItem for Definition {
742742
}
743743
}
744744

745+
impl AsExternAssocItem for Definition {
746+
fn as_extern_assoc_item(self, db: &dyn hir::db::HirDatabase) -> Option<ExternAssocItem> {
747+
match self {
748+
Definition::Function(it) => it.as_extern_assoc_item(db),
749+
Definition::Static(it) => it.as_extern_assoc_item(db),
750+
Definition::TypeAlias(it) => it.as_extern_assoc_item(db),
751+
_ => None,
752+
}
753+
}
754+
}
755+
745756
impl From<AssocItem> for Definition {
746757
fn from(assoc_item: AssocItem) -> Self {
747758
match assoc_item {

crates/ide/src/hover/render.rs

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

44
use either::Either;
55
use hir::{
6-
Adt, AsAssocItem, CaptureKind, HasCrate, HasSource, HirDisplay, Layout, LayoutError, Name,
7-
Semantics, Trait, Type, TypeInfo,
6+
Adt, AsAssocItem, AsExternAssocItem, CaptureKind, HasCrate, HasSource, HirDisplay, Layout,
7+
LayoutError, Name, Semantics, Trait, Type, TypeInfo,
88
};
99
use ide_db::{
1010
base_db::SourceDatabase,
@@ -369,12 +369,20 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
369369
match def {
370370
Definition::Field(f) => Some(f.parent_def(db).name(db)),
371371
Definition::Local(l) => l.parent(db).name(db),
372-
Definition::Function(f) => match f.as_assoc_item(db)?.container(db) {
373-
hir::AssocItemContainer::Trait(t) => Some(t.name(db)),
374-
hir::AssocItemContainer::Impl(i) => i.self_ty(db).as_adt().map(|adt| adt.name(db)),
375-
},
376372
Definition::Variant(e) => Some(e.parent_enum(db).name(db)),
377-
_ => None,
373+
374+
d => {
375+
if let Some(assoc_item) = d.as_assoc_item(db) {
376+
match assoc_item.container(db) {
377+
hir::AssocItemContainer::Trait(t) => Some(t.name(db)),
378+
hir::AssocItemContainer::Impl(i) => {
379+
i.self_ty(db).as_adt().map(|adt| adt.name(db))
380+
}
381+
}
382+
} else {
383+
return d.as_extern_assoc_item(db).map(|_| "<extern>".to_owned());
384+
}
385+
}
378386
}
379387
.map(|name| name.display(db).to_string())
380388
}

crates/ide/src/hover/tests.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ fn main() {
12021202
*C*
12031203
12041204
```rust
1205-
test
1205+
test::X
12061206
```
12071207
12081208
```rust
@@ -2277,7 +2277,7 @@ fn main() { let foo_test = unsafe { fo$0o(1, 2, 3); } }
22772277
*foo*
22782278
22792279
```rust
2280-
test
2280+
test::<extern>
22812281
```
22822282
22832283
```rust
@@ -4266,7 +4266,7 @@ fn main() {
42664266
*B*
42674267
42684268
```rust
4269-
test
4269+
test::T
42704270
```
42714271
42724272
```rust
@@ -4295,7 +4295,7 @@ fn main() {
42954295
*B*
42964296
42974297
```rust
4298-
test
4298+
test::T
42994299
```
43004300
43014301
```rust
@@ -4327,7 +4327,7 @@ fn main() {
43274327
*B*
43284328
43294329
```rust
4330-
test
4330+
test::T
43314331
```
43324332
43334333
```rust
@@ -4919,7 +4919,7 @@ fn test() {
49194919
*FOO*
49204920
49214921
```rust
4922-
test
4922+
test::S
49234923
```
49244924
49254925
```rust
@@ -5284,7 +5284,7 @@ impl T1 for Foo {
52845284
*Bar*
52855285
52865286
```rust
5287-
test::t2
5287+
test::t2::T2
52885288
```
52895289
52905290
```rust
@@ -5306,7 +5306,7 @@ trait A {
53065306
*Assoc*
53075307
53085308
```rust
5309-
test
5309+
test::A
53105310
```
53115311
53125312
```rust
@@ -5327,7 +5327,7 @@ trait A {
53275327
*Assoc*
53285328
53295329
```rust
5330-
test
5330+
test::A
53315331
```
53325332
53335333
```rust
@@ -5346,7 +5346,7 @@ trait A where
53465346
*Assoc*
53475347
53485348
```rust
5349-
test
5349+
test::A
53505350
```
53515351
53525352
```rust
@@ -6632,7 +6632,7 @@ fn test() {
66326632
*A*
66336633
66346634
```rust
6635-
test
6635+
test::S
66366636
```
66376637
66386638
```rust
@@ -6661,7 +6661,7 @@ fn test() {
66616661
*A*
66626662
66636663
```rust
6664-
test
6664+
test::S
66656665
```
66666666
66676667
```rust
@@ -6691,7 +6691,7 @@ mod m {
66916691
*A*
66926692
66936693
```rust
6694-
test
6694+
test::S
66956695
```
66966696
66976697
```rust
@@ -7249,7 +7249,7 @@ extern "C" {
72497249
*STATIC*
72507250
72517251
```rust
7252-
test
7252+
test::<extern>
72537253
```
72547254
72557255
```rust
@@ -7267,7 +7267,7 @@ extern "C" {
72677267
*fun*
72687268
72697269
```rust
7270-
test
7270+
test::<extern>
72717271
```
72727272
72737273
```rust
@@ -7285,7 +7285,7 @@ extern "C" {
72857285
*Ty*
72867286
72877287
```rust
7288-
test
7288+
test::<extern>
72897289
```
72907290
72917291
```rust

0 commit comments

Comments
 (0)