mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 15:14:32 +00:00
Render assoc item owner in hover for items other than functions
This commit is contained in:
parent
a822291a02
commit
85203d9721
4 changed files with 135 additions and 28 deletions
|
@ -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)]
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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::{
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
|
@ -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())
|
||||
}
|
||||
|
|
|
@ -1202,7 +1202,7 @@ fn main() {
|
|||
*C*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::X
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -2277,7 +2277,7 @@ fn main() { let foo_test = unsafe { fo$0o(1, 2, 3); } }
|
|||
*foo*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::<extern>
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -4266,7 +4266,7 @@ fn main() {
|
|||
*B*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::T
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -4295,7 +4295,7 @@ fn main() {
|
|||
*B*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::T
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -4327,7 +4327,7 @@ fn main() {
|
|||
*B*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::T
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -4919,7 +4919,7 @@ fn test() {
|
|||
*FOO*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::S
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -5284,7 +5284,7 @@ impl T1 for Foo {
|
|||
*Bar*
|
||||
|
||||
```rust
|
||||
test::t2
|
||||
test::t2::T2
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -5306,7 +5306,7 @@ trait A {
|
|||
*Assoc*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::A
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -5327,7 +5327,7 @@ trait A {
|
|||
*Assoc*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::A
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -5346,7 +5346,7 @@ trait A where
|
|||
*Assoc*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::A
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -6632,7 +6632,7 @@ fn test() {
|
|||
*A*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::S
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -6661,7 +6661,7 @@ fn test() {
|
|||
*A*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::S
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -6691,7 +6691,7 @@ mod m {
|
|||
*A*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::S
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -7249,7 +7249,7 @@ extern "C" {
|
|||
*STATIC*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::<extern>
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -7267,7 +7267,7 @@ extern "C" {
|
|||
*fun*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::<extern>
|
||||
```
|
||||
|
||||
```rust
|
||||
|
@ -7285,7 +7285,7 @@ extern "C" {
|
|||
*Ty*
|
||||
|
||||
```rust
|
||||
test
|
||||
test::<extern>
|
||||
```
|
||||
|
||||
```rust
|
||||
|
|
Loading…
Reference in a new issue