mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 11:20:54 +00:00
generalize boilerplate
This commit is contained in:
parent
1a5f32fcb5
commit
90215eb5a0
@ -16,7 +16,7 @@ use crate::{
|
||||
code_model_impl::def_id_to_ast,
|
||||
docs::{Documentation, Docs, docs_from_ast},
|
||||
module_tree::ModuleId,
|
||||
ids::{FunctionId, StructId, EnumId, EnumVariantId},
|
||||
ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef},
|
||||
};
|
||||
|
||||
/// hir::Crate describes a single crate. It's the main interface with which
|
||||
@ -197,8 +197,12 @@ pub struct Struct {
|
||||
}
|
||||
|
||||
impl Struct {
|
||||
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) {
|
||||
self.id.source(db)
|
||||
}
|
||||
|
||||
pub fn module(&self, db: &impl HirDatabase) -> Module {
|
||||
self.id.loc(db).module
|
||||
self.id.module(db)
|
||||
}
|
||||
|
||||
pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
|
||||
@ -217,10 +221,6 @@ impl Struct {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) {
|
||||
self.id.loc(db).source(db)
|
||||
}
|
||||
|
||||
pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> {
|
||||
db.generic_params((*self).into())
|
||||
}
|
||||
@ -238,8 +238,12 @@ pub struct Enum {
|
||||
}
|
||||
|
||||
impl Enum {
|
||||
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) {
|
||||
self.id.source(db)
|
||||
}
|
||||
|
||||
pub fn module(&self, db: &impl HirDatabase) -> Module {
|
||||
self.id.loc(db).module
|
||||
self.id.module(db)
|
||||
}
|
||||
|
||||
pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
|
||||
@ -250,10 +254,6 @@ impl Enum {
|
||||
db.enum_data(*self).variants.clone()
|
||||
}
|
||||
|
||||
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) {
|
||||
self.id.loc(db).source(db)
|
||||
}
|
||||
|
||||
pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> {
|
||||
db.generic_params((*self).into())
|
||||
}
|
||||
@ -271,8 +271,11 @@ pub struct EnumVariant {
|
||||
}
|
||||
|
||||
impl EnumVariant {
|
||||
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) {
|
||||
self.id.source(db)
|
||||
}
|
||||
pub fn module(&self, db: &impl HirDatabase) -> Module {
|
||||
self.id.loc(db).module
|
||||
self.id.module(db)
|
||||
}
|
||||
pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum {
|
||||
db.enum_variant_data(*self).parent_enum.clone()
|
||||
@ -296,10 +299,6 @@ impl EnumVariant {
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) {
|
||||
self.id.loc(db).source(db)
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for EnumVariant {
|
||||
@ -348,7 +347,11 @@ impl FnSignature {
|
||||
|
||||
impl Function {
|
||||
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
|
||||
self.id.loc(db).source(db)
|
||||
self.id.source(db)
|
||||
}
|
||||
|
||||
pub fn module(&self, db: &impl HirDatabase) -> Module {
|
||||
self.id.module(db)
|
||||
}
|
||||
|
||||
pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMapping> {
|
||||
|
@ -30,10 +30,6 @@ impl Function {
|
||||
db.body_hir(*self)
|
||||
}
|
||||
|
||||
pub(crate) fn module(&self, db: &impl HirDatabase) -> Module {
|
||||
self.id.loc(db).module
|
||||
}
|
||||
|
||||
/// The containing impl block, if this is a method.
|
||||
pub(crate) fn impl_block(&self, db: &impl HirDatabase) -> Option<ImplBlock> {
|
||||
let module_impls = db.impls_in_module(self.module(db));
|
||||
|
@ -1,8 +1,11 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::{
|
||||
marker::PhantomData,
|
||||
hash::Hash,
|
||||
};
|
||||
|
||||
use ra_db::{LocationIntener, FileId};
|
||||
use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, ast};
|
||||
use ra_arena::{Arena, RawId, impl_arena_id};
|
||||
use ra_arena::{Arena, RawId, ArenaId, impl_arena_id};
|
||||
|
||||
use crate::{
|
||||
HirDatabase, Def,
|
||||
@ -179,43 +182,53 @@ impl<N: AstNode> Clone for ItemLoc<N> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait AstItemDef<N: AstNode + Eq + Hash>: ArenaId + Clone {
|
||||
fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<N>, Self>;
|
||||
fn source(self, db: &impl HirDatabase) -> (HirFileId, TreeArc<N>) {
|
||||
let int = Self::interner(db.as_ref());
|
||||
let loc = int.id2loc(self);
|
||||
loc.source(db)
|
||||
}
|
||||
fn module(self, db: &impl HirDatabase) -> Module {
|
||||
let int = Self::interner(db.as_ref());
|
||||
let loc = int.id2loc(self);
|
||||
loc.module
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct FunctionId(RawId);
|
||||
impl_arena_id!(FunctionId);
|
||||
|
||||
impl FunctionId {
|
||||
pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> ItemLoc<ast::FnDef> {
|
||||
db.as_ref().fns.id2loc(self)
|
||||
impl AstItemDef<ast::FnDef> for FunctionId {
|
||||
fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::FnDef>, Self> {
|
||||
&interner.fns
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct StructId(RawId);
|
||||
impl_arena_id!(StructId);
|
||||
|
||||
impl StructId {
|
||||
pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> ItemLoc<ast::StructDef> {
|
||||
db.as_ref().structs.id2loc(self)
|
||||
impl AstItemDef<ast::StructDef> for StructId {
|
||||
fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::StructDef>, Self> {
|
||||
&interner.structs
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct EnumId(RawId);
|
||||
impl_arena_id!(EnumId);
|
||||
|
||||
impl EnumId {
|
||||
pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> ItemLoc<ast::EnumDef> {
|
||||
db.as_ref().enums.id2loc(self)
|
||||
impl AstItemDef<ast::EnumDef> for EnumId {
|
||||
fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::EnumDef>, Self> {
|
||||
&interner.enums
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct EnumVariantId(RawId);
|
||||
impl_arena_id!(EnumVariantId);
|
||||
|
||||
impl EnumVariantId {
|
||||
pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> ItemLoc<ast::EnumVariant> {
|
||||
db.as_ref().enum_variants.id2loc(self)
|
||||
impl AstItemDef<ast::EnumVariant> for EnumVariantId {
|
||||
fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::EnumVariant>, Self> {
|
||||
&interner.enum_variants
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user