rust-analyzer/crates/ra_hir/src/from_source.rs

174 lines
6.2 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-10-30 14:19:30 +00:00
use hir_def::name::AsName;
2019-10-30 13:12:55 +00:00
use ra_syntax::ast::{self, AstNode, NameOwner};
2019-09-16 10:48:54 +00:00
use crate::{
db::{AstDatabase, DefDatabase, HirDatabase},
ids::{AstItemDef, LocationCtx},
2019-10-29 08:15:51 +00:00
AstId, Const, Crate, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, Module,
2019-10-12 17:30:53 +00:00
ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef,
2019-09-16 10:48:54 +00:00
};
pub trait FromSource: Sized {
type Ast;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self>;
}
impl FromSource for Struct {
type Ast = ast::StructDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Struct { id })
}
}
impl FromSource for Union {
type Ast = ast::StructDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Union { id })
}
}
impl FromSource for Enum {
type Ast = ast::EnumDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Enum { id })
}
}
impl FromSource for Trait {
type Ast = ast::TraitDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Trait { id })
}
}
impl FromSource for Function {
type Ast = ast::FnDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Function { id })
}
}
impl FromSource for Const {
type Ast = ast::ConstDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Const { id })
}
}
impl FromSource for Static {
type Ast = ast::StaticDef;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let id = from_source(db, src)?;
Some(Static { id })
}
}
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 })
}
}
// FIXME: add impl FromSource for MacroDef
impl FromSource for ImplBlock {
type Ast = ast::ImplBlock;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let module_src = crate::ModuleSource::from_child_node(
db,
src.file_id.original_file(db),
&src.ast.syntax(),
);
let module = Module::from_definition(db, Source { file_id: src.file_id, ast: module_src })?;
let impls = module.impl_blocks(db);
impls.into_iter().find(|b| b.source(db) == src)
}
}
impl FromSource for EnumVariant {
type Ast = ast::EnumVariant;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let parent_enum = src.ast.parent_enum();
let src_enum = Source { file_id: src.file_id, ast: parent_enum };
let variants = Enum::from_source(db, src_enum)?.variants(db);
variants.into_iter().find(|v| v.source(db) == src)
}
}
impl FromSource for StructField {
type Ast = FieldSource;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
let variant_def: VariantDef = match src.ast {
FieldSource::Named(ref field) => {
let ast = field.syntax().ancestors().find_map(ast::StructDef::cast)?;
let src = Source { file_id: src.file_id, ast };
let def = Struct::from_source(db, src)?;
VariantDef::from(def)
}
FieldSource::Pos(ref field) => {
let ast = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?;
let src = Source { file_id: src.file_id, ast };
let def = EnumVariant::from_source(db, src)?;
VariantDef::from(def)
}
};
variant_def
.variant_data(db)
.fields()
.into_iter()
.flat_map(|it| it.iter())
2019-09-23 18:31:30 +00:00
.map(|(id, _)| StructField { parent: variant_def, id })
2019-09-16 10:48:54 +00:00
.find(|f| f.source(db) == src)
}
}
impl Module {
pub fn from_declaration(db: &impl HirDatabase, src: Source<ast::Module>) -> Option<Self> {
let src_parent = Source {
file_id: src.file_id,
ast: ModuleSource::new(db, Some(src.file_id.original_file(db)), None),
};
let parent_module = Module::from_definition(db, src_parent)?;
let child_name = src.ast.name()?;
parent_module.child(db, &child_name.as_name())
}
pub fn from_definition(
db: &(impl DefDatabase + AstDatabase),
src: Source<ModuleSource>,
) -> Option<Self> {
let decl_id = match src.ast {
ModuleSource::Module(ref module) => {
2019-10-23 08:31:16 +00:00
assert!(!module.has_semi());
2019-09-16 10:48:54 +00:00
let ast_id_map = db.ast_id_map(src.file_id);
2019-10-29 08:15:51 +00:00
let item_id = AstId::new(src.file_id, ast_id_map.ast_id(module));
2019-09-16 10:48:54 +00:00
Some(item_id)
}
ModuleSource::SourceFile(_) => None,
};
db.relevant_crates(src.file_id.original_file(db))
.iter()
.map(|&crate_id| Crate { crate_id })
.find_map(|krate| {
2019-09-16 10:48:54 +00:00
let def_map = db.crate_def_map(krate);
let module_id = def_map.find_module_by_source(src.file_id, decl_id)?;
2019-10-30 09:27:54 +00:00
Some(Module::new(krate, module_id))
})
2019-09-16 10:48:54 +00:00
}
}
fn from_source<N, DEF>(db: &(impl DefDatabase + AstDatabase), src: Source<N>) -> Option<DEF>
where
N: AstNode,
DEF: AstItemDef<N>,
{
let module_src =
crate::ModuleSource::from_child_node(db, src.file_id.original_file(db), &src.ast.syntax());
let module = Module::from_definition(db, Source { file_id: src.file_id, ast: module_src })?;
2019-10-30 10:10:38 +00:00
let ctx = LocationCtx::new(db, module.id, src.file_id);
2019-09-16 10:48:54 +00:00
Some(DEF::from_ast(ctx, &src.ast))
}