mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
add module
methods
This commit is contained in:
parent
afc871199d
commit
aa2f58550a
2 changed files with 42 additions and 2 deletions
|
@ -9,7 +9,7 @@ use ra_syntax::ast::{self, NameOwner, StructKind, TypeAscriptionOwner};
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{AstDatabase, DefDatabase, HirDatabase},
|
db::{AstDatabase, DefDatabase, HirDatabase},
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
AsName, Enum, EnumVariant, FieldSource, HasSource, Name, Source, Struct, StructField,
|
AsName, Enum, EnumVariant, FieldSource, HasSource, Module, Name, Source, Struct, StructField,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl Struct {
|
impl Struct {
|
||||||
|
@ -170,12 +170,20 @@ impl VariantDef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
|
pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
|
||||||
match self {
|
match self {
|
||||||
VariantDef::Struct(it) => it.field(db, name),
|
VariantDef::Struct(it) => it.field(db, name),
|
||||||
VariantDef::EnumVariant(it) => it.field(db, name),
|
VariantDef::EnumVariant(it) => it.field(db, name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn module(self, db: &impl HirDatabase) -> Module {
|
||||||
|
match self {
|
||||||
|
VariantDef::Struct(it) => it.module(db),
|
||||||
|
VariantDef::EnumVariant(it) => it.module(db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
|
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
|
||||||
match self {
|
match self {
|
||||||
VariantDef::Struct(it) => it.variant_data(db),
|
VariantDef::Struct(it) => it.variant_data(db),
|
||||||
|
|
|
@ -569,6 +569,14 @@ impl DefWithBody {
|
||||||
DefWithBody::Static(s) => s.krate(db),
|
DefWithBody::Static(s) => s.krate(db),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn module(self, db: &impl HirDatabase) -> Module {
|
||||||
|
match self {
|
||||||
|
DefWithBody::Const(c) => c.module(db),
|
||||||
|
DefWithBody::Function(f) => f.module(db),
|
||||||
|
DefWithBody::Static(s) => s.module(db),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait HasBody: Copy {
|
pub trait HasBody: Copy {
|
||||||
|
@ -789,6 +797,20 @@ impl Const {
|
||||||
ImplBlock::containing(module_impls, self.into())
|
ImplBlock::containing(module_impls, self.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
|
||||||
|
db.trait_items_index(self.module(db)).get_parent_trait(self.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
|
||||||
|
if let Some(impl_block) = self.impl_block(db) {
|
||||||
|
Some(impl_block.into())
|
||||||
|
} else if let Some(trait_) = self.parent_trait(db) {
|
||||||
|
Some(trait_.into())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: move to a more general type for 'body-having' items
|
// FIXME: move to a more general type for 'body-having' items
|
||||||
/// Builds a resolver for code inside this item.
|
/// Builds a resolver for code inside this item.
|
||||||
pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
|
pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
|
||||||
|
@ -1075,3 +1097,13 @@ impl From<AssocItem> for crate::generics::GenericDef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AssocItem {
|
||||||
|
pub fn module(self, db: &impl DefDatabase) -> Module {
|
||||||
|
match self {
|
||||||
|
AssocItem::Function(f) => f.module(db),
|
||||||
|
AssocItem::Const(c) => c.module(db),
|
||||||
|
AssocItem::TypeAlias(t) => t.module(db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue