Skip to content

Commit

Permalink
Render assoc item owner in hover for items other than functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Feb 20, 2024
1 parent a822291 commit 85203d9
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 28 deletions.
88 changes: 88 additions & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2653,6 +2653,37 @@ impl ItemInNs {
}
}

/// Invariant: `inner.as_extern_assoc_item(db).is_some()`
/// We do not actively enforce this invariant.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ExternAssocItem {
Function(Function),
Static(Static),
TypeAlias(TypeAlias),
}

pub trait AsExternAssocItem {
fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem>;
}

impl AsExternAssocItem for Function {
fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem> {
as_extern_assoc_item(db, ExternAssocItem::Function, self.id)
}
}

impl AsExternAssocItem for Static {
fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem> {
as_extern_assoc_item(db, ExternAssocItem::Static, self.id)
}
}

impl AsExternAssocItem for TypeAlias {
fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem> {
as_extern_assoc_item(db, ExternAssocItem::TypeAlias, self.id)
}
}

/// Invariant: `inner.as_assoc_item(db).is_some()`
/// We do not actively enforce this invariant.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -2727,6 +2758,63 @@ where
}
}

fn as_extern_assoc_item<'db, ID, DEF, LOC>(
db: &(dyn HirDatabase + 'db),
ctor: impl FnOnce(DEF) -> ExternAssocItem,
id: ID,
) -> Option<ExternAssocItem>
where
ID: Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<LOC>>,
DEF: From<ID>,
LOC: ItemTreeNode,
{
match id.lookup(db.upcast()).container {
ItemContainerId::ExternBlockId(_) => Some(ctor(DEF::from(id))),
ItemContainerId::TraitId(_) | ItemContainerId::ImplId(_) | ItemContainerId::ModuleId(_) => {
None
}
}
}

impl ExternAssocItem {
pub fn name(self, db: &dyn HirDatabase) -> Name {
match self {
Self::Function(it) => it.name(db),
Self::Static(it) => it.name(db),
Self::TypeAlias(it) => it.name(db),
}
}

pub fn module(self, db: &dyn HirDatabase) -> Module {
match self {
Self::Function(f) => f.module(db),
Self::Static(c) => c.module(db),
Self::TypeAlias(t) => t.module(db),
}
}

pub fn as_function(self) -> Option<Function> {
match self {
Self::Function(v) => Some(v),
_ => None,
}
}

pub fn as_static(self) -> Option<Static> {
match self {
Self::Static(v) => Some(v),
_ => None,
}
}

pub fn as_type_alias(self) -> Option<TypeAlias> {
match self {
Self::TypeAlias(v) => Some(v),
_ => None,
}
}
}

impl AssocItem {
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
match self {
Expand Down
21 changes: 16 additions & 5 deletions crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use arrayvec::ArrayVec;
use either::Either;
use hir::{
Adt, AsAssocItem, AssocItem, AttributeTemplate, BuiltinAttr, BuiltinType, Const, Crate,
DefWithBody, DeriveHelper, DocLinkDef, ExternCrateDecl, Field, Function, GenericParam,
HasVisibility, HirDisplay, Impl, Label, Local, Macro, Module, ModuleDef, Name, PathResolution,
Semantics, Static, ToolModule, Trait, TraitAlias, TupleField, TypeAlias, Variant, VariantDef,
Visibility,
Adt, AsAssocItem, AsExternAssocItem, AssocItem, AttributeTemplate, BuiltinAttr, BuiltinType,
Const, Crate, DefWithBody, DeriveHelper, DocLinkDef, ExternAssocItem, ExternCrateDecl, Field,
Function, GenericParam, HasVisibility, HirDisplay, Impl, Label, Local, Macro, Module,
ModuleDef, Name, PathResolution, Semantics, Static, ToolModule, Trait, TraitAlias, TupleField,
TypeAlias, Variant, VariantDef, Visibility,
};
use stdx::{format_to, impl_from};
use syntax::{
Expand Down Expand Up @@ -742,6 +742,17 @@ impl AsAssocItem for Definition {
}
}

impl AsExternAssocItem for Definition {
fn as_extern_assoc_item(self, db: &dyn hir::db::HirDatabase) -> Option<ExternAssocItem> {
match self {
Definition::Function(it) => it.as_extern_assoc_item(db),
Definition::Static(it) => it.as_extern_assoc_item(db),
Definition::TypeAlias(it) => it.as_extern_assoc_item(db),
_ => None,
}
}
}

impl From<AssocItem> for Definition {
fn from(assoc_item: AssocItem) -> Self {
match assoc_item {
Expand Down
22 changes: 15 additions & 7 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::{mem, ops::Not};

use either::Either;
use hir::{
Adt, AsAssocItem, CaptureKind, HasCrate, HasSource, HirDisplay, Layout, LayoutError, Name,
Semantics, Trait, Type, TypeInfo,
Adt, AsAssocItem, AsExternAssocItem, CaptureKind, HasCrate, HasSource, HirDisplay, Layout,
LayoutError, Name, Semantics, Trait, Type, TypeInfo,
};
use ide_db::{
base_db::SourceDatabase,
Expand Down Expand Up @@ -369,12 +369,20 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
match def {
Definition::Field(f) => Some(f.parent_def(db).name(db)),
Definition::Local(l) => l.parent(db).name(db),
Definition::Function(f) => match f.as_assoc_item(db)?.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)),
},
Definition::Variant(e) => Some(e.parent_enum(db).name(db)),
_ => None,

d => {
if let Some(assoc_item) = d.as_assoc_item(db) {
match assoc_item.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))
}
}
} else {
return d.as_extern_assoc_item(db).map(|_| "<extern>".to_owned());
}
}
}
.map(|name| name.display(db).to_string())
}
Expand Down
32 changes: 16 additions & 16 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ fn main() {
*C*
```rust
test
test::X
```
```rust
Expand Down Expand Up @@ -2277,7 +2277,7 @@ fn main() { let foo_test = unsafe { fo$0o(1, 2, 3); } }
*foo*
```rust
test
test::<extern>
```
```rust
Expand Down Expand Up @@ -4266,7 +4266,7 @@ fn main() {
*B*
```rust
test
test::T
```
```rust
Expand Down Expand Up @@ -4295,7 +4295,7 @@ fn main() {
*B*
```rust
test
test::T
```
```rust
Expand Down Expand Up @@ -4327,7 +4327,7 @@ fn main() {
*B*
```rust
test
test::T
```
```rust
Expand Down Expand Up @@ -4919,7 +4919,7 @@ fn test() {
*FOO*
```rust
test
test::S
```
```rust
Expand Down Expand Up @@ -5284,7 +5284,7 @@ impl T1 for Foo {
*Bar*
```rust
test::t2
test::t2::T2
```
```rust
Expand All @@ -5306,7 +5306,7 @@ trait A {
*Assoc*
```rust
test
test::A
```
```rust
Expand All @@ -5327,7 +5327,7 @@ trait A {
*Assoc*
```rust
test
test::A
```
```rust
Expand All @@ -5346,7 +5346,7 @@ trait A where
*Assoc*
```rust
test
test::A
```
```rust
Expand Down Expand Up @@ -6632,7 +6632,7 @@ fn test() {
*A*
```rust
test
test::S
```
```rust
Expand Down Expand Up @@ -6661,7 +6661,7 @@ fn test() {
*A*
```rust
test
test::S
```
```rust
Expand Down Expand Up @@ -6691,7 +6691,7 @@ mod m {
*A*
```rust
test
test::S
```
```rust
Expand Down Expand Up @@ -7249,7 +7249,7 @@ extern "C" {
*STATIC*
```rust
test
test::<extern>
```
```rust
Expand All @@ -7267,7 +7267,7 @@ extern "C" {
*fun*
```rust
test
test::<extern>
```
```rust
Expand All @@ -7285,7 +7285,7 @@ extern "C" {
*Ty*
```rust
test
test::<extern>
```
```rust
Expand Down

0 comments on commit 85203d9

Please sign in to comment.