aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r--crates/ra_hir/src/code_model_api.rs68
1 files changed, 61 insertions, 7 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 71123a698..92ab0f692 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -5,18 +5,19 @@ use ra_db::{CrateId, FileId};
5use ra_syntax::{ast::self, TreeArc, SyntaxNode}; 5use ra_syntax::{ast::self, TreeArc, SyntaxNode};
6 6
7use crate::{ 7use crate::{
8 Name, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId, 8 Name, ScopesWithSyntaxMapping, Ty, HirFileId,
9 type_ref::TypeRef, 9 type_ref::TypeRef,
10 nameres::{ModuleScope, lower::ImportId}, 10 nameres::{ModuleScope, lower::ImportId},
11 HirDatabase, PersistentHirDatabase, 11 HirDatabase, PersistentHirDatabase,
12 expr::BodySyntaxMapping, 12 expr::{Body, BodySyntaxMapping},
13 ty::{InferenceResult}, 13 ty::InferenceResult,
14 adt::{EnumVariantId, StructFieldId, VariantDef}, 14 adt::{EnumVariantId, StructFieldId, VariantDef},
15 generics::GenericParams, 15 generics::GenericParams,
16 docs::{Documentation, Docs, docs_from_ast}, 16 docs::{Documentation, Docs, docs_from_ast},
17 module_tree::ModuleId, 17 module_tree::ModuleId,
18 ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, 18 ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId},
19 impl_block::ImplId, 19 impl_block::ImplId,
20 resolve::Resolver,
20}; 21};
21 22
22/// hir::Crate describes a single crate. It's the main interface with which 23/// hir::Crate describes a single crate. It's the main interface with which
@@ -174,13 +175,14 @@ impl Module {
174 db.item_map(self.krate)[self.module_id].clone() 175 db.item_map(self.krate)[self.module_id].clone()
175 } 176 }
176 177
177 pub fn resolve_path(&self, db: &impl PersistentHirDatabase, path: &Path) -> PerNs<ModuleDef> {
178 db.item_map(self.krate).resolve_path(db, *self, path)
179 }
180
181 pub fn problems(&self, db: &impl HirDatabase) -> Vec<(TreeArc<SyntaxNode>, Problem)> { 178 pub fn problems(&self, db: &impl HirDatabase) -> Vec<(TreeArc<SyntaxNode>, Problem)> {
182 self.problems_impl(db) 179 self.problems_impl(db)
183 } 180 }
181
182 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
183 let item_map = db.item_map(self.krate);
184 Resolver::default().push_module_scope(item_map, *self)
185 }
184} 186}
185 187
186impl Docs for Module { 188impl Docs for Module {
@@ -282,6 +284,21 @@ impl Struct {
282 pub fn ty(&self, db: &impl HirDatabase) -> Ty { 284 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
283 db.type_for_def((*self).into()) 285 db.type_for_def((*self).into())
284 } 286 }
287
288 // TODO move to a more general type
289 /// Builds a resolver for type references inside this struct.
290 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
291 // take the outer scope...
292 let r = self.module(db).resolver(db);
293 // ...and add generic params, if present
294 let p = self.generic_params(db);
295 let r = if !p.params.is_empty() {
296 r.push_generic_params_scope(p)
297 } else {
298 r
299 };
300 r
301 }
285} 302}
286 303
287impl Docs for Struct { 304impl Docs for Struct {
@@ -331,6 +348,21 @@ impl Enum {
331 pub fn ty(&self, db: &impl HirDatabase) -> Ty { 348 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
332 db.type_for_def((*self).into()) 349 db.type_for_def((*self).into())
333 } 350 }
351
352 // TODO move to a more general type
353 /// Builds a resolver for type references inside this struct.
354 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
355 // take the outer scope...
356 let r = self.module(db).resolver(db);
357 // ...and add generic params, if present
358 let p = self.generic_params(db);
359 let r = if !p.params.is_empty() {
360 r.push_generic_params_scope(p)
361 } else {
362 r
363 };
364 r
365 }
334} 366}
335 367
336impl Docs for Enum { 368impl Docs for Enum {
@@ -449,6 +481,10 @@ impl Function {
449 db.body_syntax_mapping(*self) 481 db.body_syntax_mapping(*self)
450 } 482 }
451 483
484 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
485 db.body_hir(*self)
486 }
487
452 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping { 488 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping {
453 let scopes = db.expr_scopes(*self); 489 let scopes = db.expr_scopes(*self);
454 let syntax_mapping = db.body_syntax_mapping(*self); 490 let syntax_mapping = db.body_syntax_mapping(*self);
@@ -469,6 +505,24 @@ impl Function {
469 pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> { 505 pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> {
470 db.generic_params((*self).into()) 506 db.generic_params((*self).into())
471 } 507 }
508
509 // TODO move to a more general type for 'body-having' items
510 /// Builds a resolver for code inside this item.
511 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
512 // take the outer scope...
513 let r = self
514 .impl_block(db)
515 .map(|ib| ib.resolver(db))
516 .unwrap_or_else(|| self.module(db).resolver(db));
517 // ...and add generic params, if present
518 let p = self.generic_params(db);
519 let r = if !p.params.is_empty() {
520 r.push_generic_params_scope(p)
521 } else {
522 r
523 };
524 r
525 }
472} 526}
473 527
474impl Docs for Function { 528impl Docs for Function {