mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Switch type aliases to new sources
This commit is contained in:
parent
b7a36b5443
commit
64c21ed195
9 changed files with 162 additions and 73 deletions
|
@ -4,7 +4,7 @@ use hir_def::{ModuleId, StructId, StructOrUnionId, UnionId};
|
|||
use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode, NameOwner},
|
||||
match_ast, AstPtr,
|
||||
match_ast, AstPtr, SyntaxNode,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -52,48 +52,27 @@ impl FromSource for Trait {
|
|||
impl FromSource for Function {
|
||||
type Ast = ast::FnDef;
|
||||
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
||||
// FIXME: this doesn't try to handle nested declarations
|
||||
for container in src.value.syntax().ancestors() {
|
||||
let res = match_ast! {
|
||||
match container {
|
||||
ast::TraitDef(it) => {
|
||||
let c = Trait::from_source(db, src.with_value(it))?;
|
||||
c.items(db)
|
||||
.into_iter()
|
||||
.filter_map(|it| match it {
|
||||
AssocItem::Function(it) => Some(it),
|
||||
_ => None
|
||||
})
|
||||
.find(|it| same_source(&it.source(db), &src))?
|
||||
},
|
||||
ast::ImplBlock(it) => {
|
||||
let c = ImplBlock::from_source(db, src.with_value(it))?;
|
||||
c.items(db)
|
||||
.into_iter()
|
||||
.filter_map(|it| match it {
|
||||
AssocItem::Function(it) => Some(it),
|
||||
_ => None
|
||||
})
|
||||
.find(|it| same_source(&it.source(db), &src))?
|
||||
|
||||
},
|
||||
_ => { continue },
|
||||
}
|
||||
};
|
||||
return Some(res);
|
||||
}
|
||||
|
||||
let module_source = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
|
||||
let c = Module::from_definition(db, src.with_value(module_source))?;
|
||||
let res = c
|
||||
.declarations(db)
|
||||
let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
|
||||
Container::Trait(it) => it.items(db),
|
||||
Container::ImplBlock(it) => it.items(db),
|
||||
Container::Module(m) => {
|
||||
return m
|
||||
.declarations(db)
|
||||
.into_iter()
|
||||
.filter_map(|it| match it {
|
||||
ModuleDef::Function(it) => Some(it),
|
||||
_ => None,
|
||||
})
|
||||
.find(|it| same_source(&it.source(db), &src))
|
||||
}
|
||||
};
|
||||
items
|
||||
.into_iter()
|
||||
.filter_map(|it| match it {
|
||||
ModuleDef::Function(it) => Some(it),
|
||||
AssocItem::Function(it) => Some(it),
|
||||
_ => None,
|
||||
})
|
||||
.find(|it| same_source(&it.source(db), &src));
|
||||
res
|
||||
.find(|it| same_source(&it.source(db), &src))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,8 +93,27 @@ impl FromSource for Static {
|
|||
impl FromSource for TypeAlias {
|
||||
type Ast = ast::TypeAliasDef;
|
||||
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
|
||||
let id = from_source(db, src)?;
|
||||
Some(TypeAlias { id })
|
||||
let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
|
||||
Container::Trait(it) => it.items(db),
|
||||
Container::ImplBlock(it) => it.items(db),
|
||||
Container::Module(m) => {
|
||||
return m
|
||||
.declarations(db)
|
||||
.into_iter()
|
||||
.filter_map(|it| match it {
|
||||
ModuleDef::TypeAlias(it) => Some(it),
|
||||
_ => None,
|
||||
})
|
||||
.find(|it| same_source(&it.source(db), &src))
|
||||
}
|
||||
};
|
||||
items
|
||||
.into_iter()
|
||||
.filter_map(|it| match it {
|
||||
AssocItem::TypeAlias(it) => Some(it),
|
||||
_ => None,
|
||||
})
|
||||
.find(|it| same_source(&it.source(db), &src))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -258,6 +256,38 @@ where
|
|||
Some(DEF::from_ast(ctx, &src.value))
|
||||
}
|
||||
|
||||
enum Container {
|
||||
Trait(Trait),
|
||||
ImplBlock(ImplBlock),
|
||||
Module(Module),
|
||||
}
|
||||
|
||||
impl Container {
|
||||
fn find(db: &impl DefDatabase, src: Source<&SyntaxNode>) -> Option<Container> {
|
||||
// FIXME: this doesn't try to handle nested declarations
|
||||
for container in src.value.ancestors() {
|
||||
let res = match_ast! {
|
||||
match container {
|
||||
ast::TraitDef(it) => {
|
||||
let c = Trait::from_source(db, src.with_value(it))?;
|
||||
Container::Trait(c)
|
||||
},
|
||||
ast::ImplBlock(it) => {
|
||||
let c = ImplBlock::from_source(db, src.with_value(it))?;
|
||||
Container::ImplBlock(c)
|
||||
},
|
||||
_ => { continue },
|
||||
}
|
||||
};
|
||||
return Some(res);
|
||||
}
|
||||
|
||||
let module_source = ModuleSource::from_child_node(db, src);
|
||||
let c = Module::from_definition(db, src.with_value(module_source))?;
|
||||
Some(Container::Module(c))
|
||||
}
|
||||
}
|
||||
|
||||
/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
|
||||
/// equal if they point to exactly the same object.
|
||||
///
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue