mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 06:03:58 +00:00
Reduce copy-paste
This commit is contained in:
parent
0c0ce1ae41
commit
1a567f5ca2
1 changed files with 30 additions and 20 deletions
|
@ -2,8 +2,8 @@
|
||||||
use either::Either;
|
use either::Either;
|
||||||
|
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
child_from_source::ChildFromSource, nameres::ModuleSource, AstItemDef, EnumVariantId,
|
child_from_source::ChildFromSource, nameres::ModuleSource, AstItemDef, EnumVariantId, ImplId,
|
||||||
LocationCtx, ModuleId, VariantId,
|
LocationCtx, ModuleId, TraitId, VariantId,
|
||||||
};
|
};
|
||||||
use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
|
use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
|
@ -53,24 +53,18 @@ impl FromSource for Trait {
|
||||||
impl FromSource for Function {
|
impl FromSource for Function {
|
||||||
type Ast = ast::FnDef;
|
type Ast = ast::FnDef;
|
||||||
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
||||||
match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
|
Container::find(db, src.as_ref().map(|it| it.syntax()))?
|
||||||
Container::Trait(it) => it.id.child_from_source(db, src),
|
.child_from_source(db, src)
|
||||||
Container::ImplBlock(it) => it.id.child_from_source(db, src),
|
.map(Function::from)
|
||||||
Container::Module(it) => it.id.child_from_source(db, src),
|
|
||||||
}
|
|
||||||
.map(Function::from)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromSource for Const {
|
impl FromSource for Const {
|
||||||
type Ast = ast::ConstDef;
|
type Ast = ast::ConstDef;
|
||||||
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
||||||
match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
|
Container::find(db, src.as_ref().map(|it| it.syntax()))?
|
||||||
Container::Trait(it) => it.id.child_from_source(db, src),
|
.child_from_source(db, src)
|
||||||
Container::ImplBlock(it) => it.id.child_from_source(db, src),
|
.map(Const::from)
|
||||||
Container::Module(it) => it.id.child_from_source(db, src),
|
|
||||||
}
|
|
||||||
.map(Const::from)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl FromSource for Static {
|
impl FromSource for Static {
|
||||||
|
@ -86,12 +80,9 @@ impl FromSource for Static {
|
||||||
impl FromSource for TypeAlias {
|
impl FromSource for TypeAlias {
|
||||||
type Ast = ast::TypeAliasDef;
|
type Ast = ast::TypeAliasDef;
|
||||||
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
||||||
match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
|
Container::find(db, src.as_ref().map(|it| it.syntax()))?
|
||||||
Container::Trait(it) => it.id.child_from_source(db, src),
|
.child_from_source(db, src)
|
||||||
Container::ImplBlock(it) => it.id.child_from_source(db, src),
|
.map(TypeAlias::from)
|
||||||
Container::Module(it) => it.id.child_from_source(db, src),
|
|
||||||
}
|
|
||||||
.map(TypeAlias::from)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,3 +254,22 @@ impl Container {
|
||||||
Some(Container::Module(c))
|
Some(Container::Module(c))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<CHILD, SOURCE> ChildFromSource<CHILD, SOURCE> for Container
|
||||||
|
where
|
||||||
|
TraitId: ChildFromSource<CHILD, SOURCE>,
|
||||||
|
ImplId: ChildFromSource<CHILD, SOURCE>,
|
||||||
|
ModuleId: ChildFromSource<CHILD, SOURCE>,
|
||||||
|
{
|
||||||
|
fn child_from_source(
|
||||||
|
&self,
|
||||||
|
db: &impl DefDatabase,
|
||||||
|
child_source: InFile<SOURCE>,
|
||||||
|
) -> Option<CHILD> {
|
||||||
|
match self {
|
||||||
|
Container::Trait(it) => it.id.child_from_source(db, child_source),
|
||||||
|
Container::ImplBlock(it) => it.id.child_from_source(db, child_source),
|
||||||
|
Container::Module(it) => it.id.child_from_source(db, child_source),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue