aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model_api.rs29
-rw-r--r--crates/ra_hir/src/code_model_impl.rs15
-rw-r--r--crates/ra_hir/src/code_model_impl/function.rs22
3 files changed, 26 insertions, 40 deletions
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;
2 2
3use relative_path::RelativePathBuf; 3use relative_path::RelativePathBuf;
4use ra_db::{CrateId, Cancelable, FileId}; 4use ra_db::{CrateId, Cancelable, FileId};
5use ra_syntax::{ast, TreeArc, SyntaxNode, AstNode}; 5use ra_syntax::{ast, TreeArc, SyntaxNode};
6 6
7use crate::{ 7use crate::{
8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId, 8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
@@ -12,6 +12,7 @@ use crate::{
12 expr::BodySyntaxMapping, 12 expr::BodySyntaxMapping,
13 ty::InferenceResult, 13 ty::InferenceResult,
14 adt::VariantData, 14 adt::VariantData,
15 code_model_impl::def_id_to_ast,
15}; 16};
16 17
17/// hir::Crate describes a single crate. It's the main interface with which 18/// hir::Crate describes a single crate. It's the main interface with which
@@ -186,13 +187,7 @@ impl Struct {
186 &self, 187 &self,
187 db: &impl HirDatabase, 188 db: &impl HirDatabase,
188 ) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> { 189 ) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> {
189 let (file_id, syntax) = self.def_id.source(db); 190 Ok(def_id_to_ast(db, self.def_id))
190 Ok((
191 file_id,
192 ast::StructDef::cast(&syntax)
193 .expect("struct def should point to StructDef node")
194 .to_owned(),
195 ))
196 } 191 }
197} 192}
198 193
@@ -219,13 +214,7 @@ impl Enum {
219 } 214 }
220 215
221 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> { 216 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> {
222 let (file_id, syntax) = self.def_id.source(db); 217 Ok(def_id_to_ast(db, self.def_id))
223 Ok((
224 file_id,
225 ast::EnumDef::cast(&syntax)
226 .expect("enum def should point to EnumDef node")
227 .to_owned(),
228 ))
229 } 218 }
230} 219}
231 220
@@ -259,13 +248,7 @@ impl EnumVariant {
259 &self, 248 &self,
260 db: &impl HirDatabase, 249 db: &impl HirDatabase,
261 ) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> { 250 ) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> {
262 let (file_id, syntax) = self.def_id.source(db); 251 Ok(def_id_to_ast(db, self.def_id))
263 Ok((
264 file_id,
265 ast::EnumVariant::cast(&syntax)
266 .expect("variant def should point to EnumVariant node")
267 .to_owned(),
268 ))
269 } 252 }
270} 253}
271 254
@@ -304,7 +287,7 @@ impl Function {
304 } 287 }
305 288
306 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> { 289 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> {
307 Ok(self.source_impl(db)) 290 Ok(def_id_to_ast(db, self.def_id))
308 } 291 }
309 292
310 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> { 293 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
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 @@
1mod krate; // `crate` is invalid ident :( 1mod krate; // `crate` is invalid ident :(
2mod module; 2mod module;
3pub(crate) mod function; 3pub(crate) mod function;
4
5use ra_syntax::{AstNode, TreeArc};
6
7use crate::{HirDatabase, DefId, HirFileId};
8
9pub(crate) fn def_id_to_ast<N: AstNode>(
10 db: &impl HirDatabase,
11 def_id: DefId,
12) -> (HirFileId, TreeArc<N>) {
13 let (file_id, syntax) = def_id.source(db);
14 let ast = N::cast(&syntax)
15 .unwrap_or_else(|| panic!("def points to wrong source {:?} {:?}", def_id, syntax))
16 .to_owned();
17 (file_id, ast)
18}
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;
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use ra_db::Cancelable; 5use ra_db::Cancelable;
6use ra_syntax::{ 6use ra_syntax::{TreeArc, ast::{self, NameOwner}};
7 TreeArc,
8 ast::{self, AstNode, NameOwner},
9};
10 7
11use crate::{ 8use crate::{
12 DefId, DefKind, HirDatabase, Name, AsName, Function, FnSignature, Module, HirFileId, 9 DefId, HirDatabase, Name, AsName, Function, FnSignature, Module,
13 type_ref::{TypeRef, Mutability}, 10 type_ref::{TypeRef, Mutability},
14 expr::Body, 11 expr::Body,
15 impl_block::ImplBlock, 12 impl_block::ImplBlock,
13 code_model_impl::def_id_to_ast,
16}; 14};
17 15
18pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax}; 16pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
@@ -22,16 +20,6 @@ impl Function {
22 Function { def_id } 20 Function { def_id }
23 } 21 }
24 22
25 pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
26 let def_loc = self.def_id.loc(db);
27 assert!(def_loc.kind == DefKind::Function);
28 let syntax = db.file_item(def_loc.source_item_id);
29 (
30 def_loc.source_item_id.file_id,
31 ast::FnDef::cast(&syntax).unwrap().to_owned(),
32 )
33 }
34
35 pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable<Arc<Body>> { 23 pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable<Arc<Body>> {
36 db.body_hir(self.def_id) 24 db.body_hir(self.def_id)
37 } 25 }
@@ -48,8 +36,8 @@ impl Function {
48 36
49impl FnSignature { 37impl FnSignature {
50 pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> { 38 pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> {
51 let func = Function::new(def_id); 39 // FIXME: we're using def_id_to_ast here to avoid returning Cancelable... this is a bit hacky
52 let node = func.source_impl(db).1; // TODO we're using source_impl here to avoid returning Cancelable... this is a bit hacky 40 let node: TreeArc<ast::FnDef> = def_id_to_ast(db, def_id).1;
53 let name = node 41 let name = node
54 .name() 42 .name()
55 .map(|n| n.as_name()) 43 .map(|n| n.as_name())