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.rs84
1 files changed, 78 insertions, 6 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 624c25c4d..db6e67d7f 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -433,6 +433,78 @@ impl Docs for EnumVariant {
433 } 433 }
434} 434}
435 435
436/// The defs which have a body.
437#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
438pub enum DefWithBody {
439 Func(Function),
440 Const(Const),
441 Static(Static),
442}
443
444impl DefWithBody {
445 pub fn get_funct(&self) -> &Function {
446 match *self {
447 DefWithBody::Func(ref f) => f,
448 _ => unreachable!()
449 }
450 }
451
452 pub fn const_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) {
453 match *self {
454 DefWithBody::Const(ref c) => c.source(db),
455 _ => unreachable!()
456 }
457 }
458
459 pub fn func_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
460 match *self {
461 DefWithBody::Func(ref f) => f.source(db),
462 _ => unreachable!()
463 }
464 }
465
466 pub fn static_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) {
467 match *self {
468 DefWithBody::Static(ref s) => s.source(db),
469 _ => unreachable!()
470 }
471 }
472
473 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
474 db.infer(*self)
475 }
476
477 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
478 db.body_hir(*self)
479 }
480
481 /// Builds a resolver for code inside this item.
482 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
483 // // take the outer scope...
484 // let r = self
485 // .impl_block(db)
486 // .map(|ib| ib.resolver(db))
487 // .unwrap_or_else(|| self.module(db).resolver(db));
488 // // ...and add generic params, if present
489 // let p = self.generic_params(db);
490 // let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
491 // r
492 unimplemented!()
493 }
494
495 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> {
496 // db.fn_signature(*self)
497 unimplemented!()
498 }
499
500 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
501 let scopes = db.expr_scopes(*self);
502 let source_map = db.body_with_source_map(*self).1;
503 ScopesWithSourceMap { scopes, source_map }
504 }
505
506}
507
436#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 508#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
437pub struct Function { 509pub struct Function {
438 pub(crate) id: FunctionId, 510 pub(crate) id: FunctionId,
@@ -483,11 +555,11 @@ impl Function {
483 } 555 }
484 556
485 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 557 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
486 db.body_with_source_map(*self).1 558 db.body_with_source_map(DefWithBody::Func(*self)).1
487 } 559 }
488 560
489 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { 561 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
490 db.body_hir(*self) 562 db.body_hir(DefWithBody::Func(*self))
491 } 563 }
492 564
493 pub fn ty(&self, db: &impl HirDatabase) -> Ty { 565 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
@@ -495,8 +567,8 @@ impl Function {
495 } 567 }
496 568
497 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { 569 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
498 let scopes = db.expr_scopes(*self); 570 let scopes = db.expr_scopes( DefWithBody::Func(*self));
499 let source_map = db.body_with_source_map(*self).1; 571 let source_map = db.body_with_source_map(DefWithBody::Func(*self)).1;
500 ScopesWithSourceMap { scopes, source_map } 572 ScopesWithSourceMap { scopes, source_map }
501 } 573 }
502 574
@@ -505,7 +577,7 @@ impl Function {
505 } 577 }
506 578
507 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { 579 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
508 db.infer(*self) 580 db.infer(DefWithBody::Func(*self))
509 } 581 }
510 582
511 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { 583 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
@@ -716,4 +788,4 @@ impl Docs for TypeAlias {
716 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { 788 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
717 docs_from_ast(&*self.source(db).1) 789 docs_from_ast(&*self.source(db).1)
718 } 790 }
719} 791} \ No newline at end of file