From 19136cde000c2c227e79bf44febde990fe265cb7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 11 Jan 2019 20:43:10 +0300 Subject: uncopypaste def sources --- crates/ra_hir/src/code_model_api.rs | 29 ++++++--------------------- crates/ra_hir/src/code_model_impl.rs | 15 ++++++++++++++ crates/ra_hir/src/code_model_impl/function.rs | 22 +++++--------------- 3 files changed, 26 insertions(+), 40 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 3f882a736..5f61bb589 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use relative_path::RelativePathBuf; use ra_db::{CrateId, Cancelable, FileId}; -use ra_syntax::{ast, TreeArc, SyntaxNode, AstNode}; +use ra_syntax::{ast, TreeArc, SyntaxNode}; use crate::{ Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId, @@ -12,6 +12,7 @@ use crate::{ expr::BodySyntaxMapping, ty::InferenceResult, adt::VariantData, + code_model_impl::def_id_to_ast, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -186,13 +187,7 @@ impl Struct { &self, db: &impl HirDatabase, ) -> Cancelable<(HirFileId, TreeArc)> { - let (file_id, syntax) = self.def_id.source(db); - Ok(( - file_id, - ast::StructDef::cast(&syntax) - .expect("struct def should point to StructDef node") - .to_owned(), - )) + Ok(def_id_to_ast(db, self.def_id)) } } @@ -219,13 +214,7 @@ impl Enum { } pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc)> { - let (file_id, syntax) = self.def_id.source(db); - Ok(( - file_id, - ast::EnumDef::cast(&syntax) - .expect("enum def should point to EnumDef node") - .to_owned(), - )) + Ok(def_id_to_ast(db, self.def_id)) } } @@ -259,13 +248,7 @@ impl EnumVariant { &self, db: &impl HirDatabase, ) -> Cancelable<(HirFileId, TreeArc)> { - let (file_id, syntax) = self.def_id.source(db); - Ok(( - file_id, - ast::EnumVariant::cast(&syntax) - .expect("variant def should point to EnumVariant node") - .to_owned(), - )) + Ok(def_id_to_ast(db, self.def_id)) } } @@ -304,7 +287,7 @@ impl Function { } pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc)> { - Ok(self.source_impl(db)) + Ok(def_id_to_ast(db, self.def_id)) } pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable> { diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs index 1f28fab74..0cea9f7b6 100644 --- a/crates/ra_hir/src/code_model_impl.rs +++ b/crates/ra_hir/src/code_model_impl.rs @@ -1,3 +1,18 @@ mod krate; // `crate` is invalid ident :( mod module; pub(crate) mod function; + +use ra_syntax::{AstNode, TreeArc}; + +use crate::{HirDatabase, DefId, HirFileId}; + +pub(crate) fn def_id_to_ast( + db: &impl HirDatabase, + def_id: DefId, +) -> (HirFileId, TreeArc) { + let (file_id, syntax) = def_id.source(db); + let ast = N::cast(&syntax) + .unwrap_or_else(|| panic!("def points to wrong source {:?} {:?}", def_id, syntax)) + .to_owned(); + (file_id, ast) +} diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs index daf49e791..1ce939e05 100644 --- a/crates/ra_hir/src/code_model_impl/function.rs +++ b/crates/ra_hir/src/code_model_impl/function.rs @@ -3,16 +3,14 @@ mod scope; use std::sync::Arc; use ra_db::Cancelable; -use ra_syntax::{ - TreeArc, - ast::{self, AstNode, NameOwner}, -}; +use ra_syntax::{TreeArc, ast::{self, NameOwner}}; use crate::{ - DefId, DefKind, HirDatabase, Name, AsName, Function, FnSignature, Module, HirFileId, + DefId, HirDatabase, Name, AsName, Function, FnSignature, Module, type_ref::{TypeRef, Mutability}, expr::Body, impl_block::ImplBlock, + code_model_impl::def_id_to_ast, }; pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax}; @@ -22,16 +20,6 @@ impl Function { Function { def_id } } - pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { - let def_loc = self.def_id.loc(db); - assert!(def_loc.kind == DefKind::Function); - let syntax = db.file_item(def_loc.source_item_id); - ( - def_loc.source_item_id.file_id, - ast::FnDef::cast(&syntax).unwrap().to_owned(), - ) - } - pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable> { db.body_hir(self.def_id) } @@ -48,8 +36,8 @@ impl Function { impl FnSignature { pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc { - let func = Function::new(def_id); - let node = func.source_impl(db).1; // TODO we're using source_impl here to avoid returning Cancelable... this is a bit hacky + // FIXME: we're using def_id_to_ast here to avoid returning Cancelable... this is a bit hacky + let node: TreeArc = def_id_to_ast(db, def_id).1; let name = node .name() .map(|n| n.as_name()) -- cgit v1.2.3