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.rs73
1 files changed, 62 insertions, 11 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 624c25c4d..9e6170440 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -4,7 +4,7 @@ use ra_db::{CrateId, SourceRootId, Edition};
4use ra_syntax::{ast::self, TreeArc}; 4use ra_syntax::{ast::self, TreeArc};
5 5
6use crate::{ 6use crate::{
7 Name, ScopesWithSourceMap, Ty, HirFileId, 7 Name, ScopesWithSourceMap, Ty, HirFileId, ImportSource,
8 HirDatabase, DefDatabase, 8 HirDatabase, DefDatabase,
9 type_ref::TypeRef, 9 type_ref::TypeRef,
10 nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, 10 nameres::{ModuleScope, Namespace, ImportId, CrateModuleId},
@@ -117,11 +117,7 @@ impl Module {
117 } 117 }
118 118
119 /// Returns the syntax of the last path segment corresponding to this import 119 /// Returns the syntax of the last path segment corresponding to this import
120 pub fn import_source( 120 pub fn import_source(&self, db: &impl HirDatabase, import: ImportId) -> ImportSource {
121 &self,
122 db: &impl HirDatabase,
123 import: ImportId,
124 ) -> TreeArc<ast::PathSegment> {
125 self.import_source_impl(db, import) 121 self.import_source_impl(db, import)
126 } 122 }
127 123
@@ -433,6 +429,45 @@ impl Docs for EnumVariant {
433 } 429 }
434} 430}
435 431
432/// The defs which have a body.
433#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
434pub enum DefWithBody {
435 Function(Function),
436 Const(Const),
437 Static(Static),
438}
439
440impl_froms!(DefWithBody: Function, Const, Static);
441
442impl DefWithBody {
443 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
444 db.infer(*self)
445 }
446
447 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
448 db.body_with_source_map(*self).1
449 }
450
451 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
452 db.body_hir(*self)
453 }
454
455 /// Builds a resolver for code inside this item.
456 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
457 match *self {
458 DefWithBody::Const(ref c) => c.resolver(db),
459 DefWithBody::Function(ref f) => f.resolver(db),
460 DefWithBody::Static(ref s) => s.resolver(db),
461 }
462 }
463
464 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
465 let scopes = db.expr_scopes(*self);
466 let source_map = db.body_with_source_map(*self).1;
467 ScopesWithSourceMap { scopes, source_map }
468 }
469}
470
436#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 471#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
437pub struct Function { 472pub struct Function {
438 pub(crate) id: FunctionId, 473 pub(crate) id: FunctionId,
@@ -483,11 +518,11 @@ impl Function {
483 } 518 }
484 519
485 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 520 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
486 db.body_with_source_map(*self).1 521 db.body_with_source_map((*self).into()).1
487 } 522 }
488 523
489 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { 524 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
490 db.body_hir(*self) 525 db.body_hir((*self).into())
491 } 526 }
492 527
493 pub fn ty(&self, db: &impl HirDatabase) -> Ty { 528 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
@@ -495,8 +530,8 @@ impl Function {
495 } 530 }
496 531
497 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { 532 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
498 let scopes = db.expr_scopes(*self); 533 let scopes = db.expr_scopes((*self).into());
499 let source_map = db.body_with_source_map(*self).1; 534 let source_map = db.body_with_source_map((*self).into()).1;
500 ScopesWithSourceMap { scopes, source_map } 535 ScopesWithSourceMap { scopes, source_map }
501 } 536 }
502 537
@@ -505,7 +540,7 @@ impl Function {
505 } 540 }
506 541
507 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { 542 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
508 db.infer(*self) 543 db.infer((*self).into())
509 } 544 }
510 545
511 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { 546 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
@@ -561,6 +596,14 @@ impl Const {
561 db.const_signature(*self) 596 db.const_signature(*self)
562 } 597 }
563 598
599 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
600 db.infer((*self).into())
601 }
602
603 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
604 db.body_with_source_map((*self).into()).1
605 }
606
564 /// The containing impl block, if this is a method. 607 /// The containing impl block, if this is a method.
565 pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { 608 pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> {
566 let module_impls = db.impls_in_module(self.module(db)); 609 let module_impls = db.impls_in_module(self.module(db));
@@ -625,6 +668,14 @@ impl Static {
625 // take the outer scope... 668 // take the outer scope...
626 self.module(db).resolver(db) 669 self.module(db).resolver(db)
627 } 670 }
671
672 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
673 db.infer((*self).into())
674 }
675
676 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
677 db.body_with_source_map((*self).into()).1
678 }
628} 679}
629 680
630impl Docs for Static { 681impl Docs for Static {