From 60a607d33f1c50acd0a4218da32abe35b2941e38 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 24 Jan 2019 17:54:18 +0300 Subject: new struct id --- crates/ra_hir/src/code_model_api.rs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'crates/ra_hir/src/code_model_api.rs') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 905615127..948718aa6 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -10,13 +10,13 @@ use crate::{ nameres::{ModuleScope, lower::ImportId}, db::HirDatabase, expr::BodySyntaxMapping, - ty::InferenceResult, + ty::{InferenceResult, VariantDef}, adt::VariantData, generics::GenericParams, code_model_impl::def_id_to_ast, docs::{Documentation, Docs, docs_from_ast}, module_tree::ModuleId, - ids::FunctionId, + ids::{FunctionId, StructId}, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -68,6 +68,7 @@ pub struct Module { pub enum ModuleDef { Module(Module), Function(Function), + Struct(Struct), Def(DefId), } @@ -83,6 +84,12 @@ impl Into for Function { } } +impl Into for Struct { + fn into(self) -> ModuleDef { + ModuleDef::Struct(self) + } +} + impl Into for DefId { fn into(self) -> ModuleDef { ModuleDef::Def(self) @@ -187,7 +194,7 @@ impl Module { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct StructField { - parent: DefId, + parent: VariantDef, name: Name, } @@ -201,38 +208,38 @@ impl StructField { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Struct { - pub(crate) def_id: DefId, + pub(crate) id: StructId, } impl Struct { - pub fn def_id(&self) -> DefId { - self.def_id + pub fn module(&self, db: &impl HirDatabase) -> Module { + self.id.loc(db).module } pub fn name(&self, db: &impl HirDatabase) -> Option { - db.struct_data(self.def_id).name.clone() + db.struct_data(*self).name.clone() } pub fn fields(&self, db: &impl HirDatabase) -> Vec { - db.struct_data(self.def_id) + db.struct_data(*self) .variant_data .fields() .iter() .map(|it| StructField { - parent: self.def_id, + parent: (*self).into(), name: it.name.clone(), }) .collect() } pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { - def_id_to_ast(db, self.def_id) + self.id.loc(db).source(db) } pub fn generic_params(&self, db: &impl HirDatabase) -> Arc { - db.generic_params(self.def_id.into()) + db.generic_params((*self).into()) } } @@ -310,7 +317,7 @@ impl EnumVariant { .fields() .iter() .map(|it| StructField { - parent: self.def_id, + parent: self.def_id.into(), name: it.name.clone(), }) .collect() -- cgit v1.2.3