diff options
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 83 |
1 files changed, 37 insertions, 46 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 62301e4ab..3f2e14bc8 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -6,7 +6,7 @@ use ra_syntax::{ast::self, TreeArc, SyntaxNode}; | |||
6 | 6 | ||
7 | use crate::{ | 7 | use crate::{ |
8 | Name, ScopesWithSourceMap, Ty, HirFileId, | 8 | Name, ScopesWithSourceMap, Ty, HirFileId, |
9 | HirDatabase, PersistentHirDatabase, | 9 | HirDatabase, DefDatabase, |
10 | type_ref::TypeRef, | 10 | type_ref::TypeRef, |
11 | nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, | 11 | nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, |
12 | expr::{Body, BodySourceMap}, | 12 | expr::{Body, BodySourceMap}, |
@@ -38,24 +38,21 @@ impl Crate { | |||
38 | self.crate_id | 38 | self.crate_id |
39 | } | 39 | } |
40 | 40 | ||
41 | pub fn dependencies(&self, db: &impl PersistentHirDatabase) -> Vec<CrateDependency> { | 41 | pub fn dependencies(&self, db: &impl DefDatabase) -> Vec<CrateDependency> { |
42 | self.dependencies_impl(db) | 42 | self.dependencies_impl(db) |
43 | } | 43 | } |
44 | 44 | ||
45 | pub fn root_module(&self, db: &impl PersistentHirDatabase) -> Option<Module> { | 45 | pub fn root_module(&self, db: &impl DefDatabase) -> Option<Module> { |
46 | self.root_module_impl(db) | 46 | self.root_module_impl(db) |
47 | } | 47 | } |
48 | 48 | ||
49 | pub fn edition(&self, db: &impl PersistentHirDatabase) -> Edition { | 49 | pub fn edition(&self, db: &impl DefDatabase) -> Edition { |
50 | let crate_graph = db.crate_graph(); | 50 | let crate_graph = db.crate_graph(); |
51 | crate_graph.edition(self.crate_id) | 51 | crate_graph.edition(self.crate_id) |
52 | } | 52 | } |
53 | 53 | ||
54 | // FIXME: should this be in source_binder? | 54 | // FIXME: should this be in source_binder? |
55 | pub fn source_root_crates( | 55 | pub fn source_root_crates(db: &impl DefDatabase, source_root: SourceRootId) -> Vec<Crate> { |
56 | db: &impl PersistentHirDatabase, | ||
57 | source_root: SourceRootId, | ||
58 | ) -> Vec<Crate> { | ||
59 | let crate_ids = db.source_root_crates(source_root); | 56 | let crate_ids = db.source_root_crates(source_root); |
60 | crate_ids.iter().map(|&crate_id| Crate { crate_id }).collect() | 57 | crate_ids.iter().map(|&crate_id| Crate { crate_id }).collect() |
61 | } | 58 | } |
@@ -111,7 +108,7 @@ impl Module { | |||
111 | } | 108 | } |
112 | 109 | ||
113 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. | 110 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. |
114 | pub fn definition_source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, ModuleSource) { | 111 | pub fn definition_source(&self, db: &impl DefDatabase) -> (HirFileId, ModuleSource) { |
115 | self.definition_source_impl(db) | 112 | self.definition_source_impl(db) |
116 | } | 113 | } |
117 | 114 | ||
@@ -134,14 +131,14 @@ impl Module { | |||
134 | } | 131 | } |
135 | 132 | ||
136 | /// Returns the crate this module is part of. | 133 | /// Returns the crate this module is part of. |
137 | pub fn krate(&self, _db: &impl PersistentHirDatabase) -> Option<Crate> { | 134 | pub fn krate(&self, _db: &impl DefDatabase) -> Option<Crate> { |
138 | Some(self.krate) | 135 | Some(self.krate) |
139 | } | 136 | } |
140 | 137 | ||
141 | /// Topmost parent of this module. Every module has a `crate_root`, but some | 138 | /// Topmost parent of this module. Every module has a `crate_root`, but some |
142 | /// might be missing `krate`. This can happen if a module's file is not included | 139 | /// might be missing `krate`. This can happen if a module's file is not included |
143 | /// in the module tree of any target in `Cargo.toml`. | 140 | /// in the module tree of any target in `Cargo.toml`. |
144 | pub fn crate_root(&self, db: &impl PersistentHirDatabase) -> Module { | 141 | pub fn crate_root(&self, db: &impl DefDatabase) -> Module { |
145 | self.crate_root_impl(db) | 142 | self.crate_root_impl(db) |
146 | } | 143 | } |
147 | 144 | ||
@@ -151,12 +148,12 @@ impl Module { | |||
151 | } | 148 | } |
152 | 149 | ||
153 | /// Iterates over all child modules. | 150 | /// Iterates over all child modules. |
154 | pub fn children(&self, db: &impl PersistentHirDatabase) -> impl Iterator<Item = Module> { | 151 | pub fn children(&self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { |
155 | self.children_impl(db) | 152 | self.children_impl(db) |
156 | } | 153 | } |
157 | 154 | ||
158 | /// Finds a parent module. | 155 | /// Finds a parent module. |
159 | pub fn parent(&self, db: &impl PersistentHirDatabase) -> Option<Module> { | 156 | pub fn parent(&self, db: &impl DefDatabase) -> Option<Module> { |
160 | self.parent_impl(db) | 157 | self.parent_impl(db) |
161 | } | 158 | } |
162 | 159 | ||
@@ -229,7 +226,7 @@ impl StructField { | |||
229 | self.parent.variant_data(db).fields().unwrap()[self.id].name.clone() | 226 | self.parent.variant_data(db).fields().unwrap()[self.id].name.clone() |
230 | } | 227 | } |
231 | 228 | ||
232 | pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, FieldSource) { | 229 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, FieldSource) { |
233 | self.source_impl(db) | 230 | self.source_impl(db) |
234 | } | 231 | } |
235 | 232 | ||
@@ -257,7 +254,7 @@ pub struct Struct { | |||
257 | } | 254 | } |
258 | 255 | ||
259 | impl Struct { | 256 | impl Struct { |
260 | pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) { | 257 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StructDef>) { |
261 | self.id.source(db) | 258 | self.id.source(db) |
262 | } | 259 | } |
263 | 260 | ||
@@ -289,7 +286,7 @@ impl Struct { | |||
289 | .map(|(id, _)| StructField { parent: (*self).into(), id }) | 286 | .map(|(id, _)| StructField { parent: (*self).into(), id }) |
290 | } | 287 | } |
291 | 288 | ||
292 | pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> { | 289 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { |
293 | db.generic_params((*self).into()) | 290 | db.generic_params((*self).into()) |
294 | } | 291 | } |
295 | 292 | ||
@@ -325,7 +322,7 @@ pub struct Enum { | |||
325 | } | 322 | } |
326 | 323 | ||
327 | impl Enum { | 324 | impl Enum { |
328 | pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) { | 325 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) { |
329 | self.id.source(db) | 326 | self.id.source(db) |
330 | } | 327 | } |
331 | 328 | ||
@@ -337,7 +334,7 @@ impl Enum { | |||
337 | db.enum_data(*self).name.clone() | 334 | db.enum_data(*self).name.clone() |
338 | } | 335 | } |
339 | 336 | ||
340 | pub fn variants(&self, db: &impl PersistentHirDatabase) -> Vec<EnumVariant> { | 337 | pub fn variants(&self, db: &impl DefDatabase) -> Vec<EnumVariant> { |
341 | db.enum_data(*self) | 338 | db.enum_data(*self) |
342 | .variants | 339 | .variants |
343 | .iter() | 340 | .iter() |
@@ -345,7 +342,7 @@ impl Enum { | |||
345 | .collect() | 342 | .collect() |
346 | } | 343 | } |
347 | 344 | ||
348 | pub fn variant(&self, db: &impl PersistentHirDatabase, name: &Name) -> Option<EnumVariant> { | 345 | pub fn variant(&self, db: &impl DefDatabase, name: &Name) -> Option<EnumVariant> { |
349 | db.enum_data(*self) | 346 | db.enum_data(*self) |
350 | .variants | 347 | .variants |
351 | .iter() | 348 | .iter() |
@@ -353,7 +350,7 @@ impl Enum { | |||
353 | .map(|(id, _)| EnumVariant { parent: *self, id }) | 350 | .map(|(id, _)| EnumVariant { parent: *self, id }) |
354 | } | 351 | } |
355 | 352 | ||
356 | pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> { | 353 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { |
357 | db.generic_params((*self).into()) | 354 | db.generic_params((*self).into()) |
358 | } | 355 | } |
359 | 356 | ||
@@ -386,20 +383,17 @@ pub struct EnumVariant { | |||
386 | } | 383 | } |
387 | 384 | ||
388 | impl EnumVariant { | 385 | impl EnumVariant { |
389 | pub fn source( | 386 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) { |
390 | &self, | ||
391 | db: &impl PersistentHirDatabase, | ||
392 | ) -> (HirFileId, TreeArc<ast::EnumVariant>) { | ||
393 | self.source_impl(db) | 387 | self.source_impl(db) |
394 | } | 388 | } |
395 | pub fn module(&self, db: &impl HirDatabase) -> Module { | 389 | pub fn module(&self, db: &impl HirDatabase) -> Module { |
396 | self.parent.module(db) | 390 | self.parent.module(db) |
397 | } | 391 | } |
398 | pub fn parent_enum(&self, _db: &impl PersistentHirDatabase) -> Enum { | 392 | pub fn parent_enum(&self, _db: &impl DefDatabase) -> Enum { |
399 | self.parent | 393 | self.parent |
400 | } | 394 | } |
401 | 395 | ||
402 | pub fn name(&self, db: &impl PersistentHirDatabase) -> Option<Name> { | 396 | pub fn name(&self, db: &impl DefDatabase) -> Option<Name> { |
403 | db.enum_data(self.parent).variants[self.id].name.clone() | 397 | db.enum_data(self.parent).variants[self.id].name.clone() |
404 | } | 398 | } |
405 | 399 | ||
@@ -465,11 +459,11 @@ impl FnSignature { | |||
465 | } | 459 | } |
466 | 460 | ||
467 | impl Function { | 461 | impl Function { |
468 | pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) { | 462 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::FnDef>) { |
469 | self.id.source(db) | 463 | self.id.source(db) |
470 | } | 464 | } |
471 | 465 | ||
472 | pub fn module(&self, db: &impl PersistentHirDatabase) -> Module { | 466 | pub fn module(&self, db: &impl DefDatabase) -> Module { |
473 | self.id.module(db) | 467 | self.id.module(db) |
474 | } | 468 | } |
475 | 469 | ||
@@ -503,12 +497,12 @@ impl Function { | |||
503 | db.infer(*self) | 497 | db.infer(*self) |
504 | } | 498 | } |
505 | 499 | ||
506 | pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> { | 500 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { |
507 | db.generic_params((*self).into()) | 501 | db.generic_params((*self).into()) |
508 | } | 502 | } |
509 | 503 | ||
510 | /// The containing impl block, if this is a method. | 504 | /// The containing impl block, if this is a method. |
511 | pub fn impl_block(&self, db: &impl PersistentHirDatabase) -> Option<ImplBlock> { | 505 | pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { |
512 | let module_impls = db.impls_in_module(self.module(db)); | 506 | let module_impls = db.impls_in_module(self.module(db)); |
513 | ImplBlock::containing(module_impls, (*self).into()) | 507 | ImplBlock::containing(module_impls, (*self).into()) |
514 | } | 508 | } |
@@ -540,11 +534,11 @@ pub struct Const { | |||
540 | } | 534 | } |
541 | 535 | ||
542 | impl Const { | 536 | impl Const { |
543 | pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) { | 537 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) { |
544 | self.id.source(db) | 538 | self.id.source(db) |
545 | } | 539 | } |
546 | 540 | ||
547 | pub fn module(&self, db: &impl PersistentHirDatabase) -> Module { | 541 | pub fn module(&self, db: &impl DefDatabase) -> Module { |
548 | self.id.module(db) | 542 | self.id.module(db) |
549 | } | 543 | } |
550 | 544 | ||
@@ -553,7 +547,7 @@ impl Const { | |||
553 | } | 547 | } |
554 | 548 | ||
555 | /// The containing impl block, if this is a method. | 549 | /// The containing impl block, if this is a method. |
556 | pub fn impl_block(&self, db: &impl PersistentHirDatabase) -> Option<ImplBlock> { | 550 | pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { |
557 | let module_impls = db.impls_in_module(self.module(db)); | 551 | let module_impls = db.impls_in_module(self.module(db)); |
558 | ImplBlock::containing(module_impls, (*self).into()) | 552 | ImplBlock::containing(module_impls, (*self).into()) |
559 | } | 553 | } |
@@ -599,11 +593,11 @@ pub struct Static { | |||
599 | } | 593 | } |
600 | 594 | ||
601 | impl Static { | 595 | impl Static { |
602 | pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) { | 596 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) { |
603 | self.id.source(db) | 597 | self.id.source(db) |
604 | } | 598 | } |
605 | 599 | ||
606 | pub fn module(&self, db: &impl PersistentHirDatabase) -> Module { | 600 | pub fn module(&self, db: &impl DefDatabase) -> Module { |
607 | self.id.module(db) | 601 | self.id.module(db) |
608 | } | 602 | } |
609 | 603 | ||
@@ -630,15 +624,15 @@ pub struct Trait { | |||
630 | } | 624 | } |
631 | 625 | ||
632 | impl Trait { | 626 | impl Trait { |
633 | pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc<ast::TraitDef>) { | 627 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::TraitDef>) { |
634 | self.id.source(db) | 628 | self.id.source(db) |
635 | } | 629 | } |
636 | 630 | ||
637 | pub fn module(&self, db: &impl PersistentHirDatabase) -> Module { | 631 | pub fn module(&self, db: &impl DefDatabase) -> Module { |
638 | self.id.module(db) | 632 | self.id.module(db) |
639 | } | 633 | } |
640 | 634 | ||
641 | pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> { | 635 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { |
642 | db.generic_params((*self).into()) | 636 | db.generic_params((*self).into()) |
643 | } | 637 | } |
644 | } | 638 | } |
@@ -655,28 +649,25 @@ pub struct TypeAlias { | |||
655 | } | 649 | } |
656 | 650 | ||
657 | impl TypeAlias { | 651 | impl TypeAlias { |
658 | pub fn source( | 652 | pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::TypeAliasDef>) { |
659 | &self, | ||
660 | db: &impl PersistentHirDatabase, | ||
661 | ) -> (HirFileId, TreeArc<ast::TypeAliasDef>) { | ||
662 | self.id.source(db) | 653 | self.id.source(db) |
663 | } | 654 | } |
664 | 655 | ||
665 | pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc<GenericParams> { | 656 | pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { |
666 | db.generic_params((*self).into()) | 657 | db.generic_params((*self).into()) |
667 | } | 658 | } |
668 | 659 | ||
669 | pub fn module(&self, db: &impl PersistentHirDatabase) -> Module { | 660 | pub fn module(&self, db: &impl DefDatabase) -> Module { |
670 | self.id.module(db) | 661 | self.id.module(db) |
671 | } | 662 | } |
672 | 663 | ||
673 | /// The containing impl block, if this is a method. | 664 | /// The containing impl block, if this is a method. |
674 | pub fn impl_block(&self, db: &impl PersistentHirDatabase) -> Option<ImplBlock> { | 665 | pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { |
675 | let module_impls = db.impls_in_module(self.module(db)); | 666 | let module_impls = db.impls_in_module(self.module(db)); |
676 | ImplBlock::containing(module_impls, (*self).into()) | 667 | ImplBlock::containing(module_impls, (*self).into()) |
677 | } | 668 | } |
678 | 669 | ||
679 | pub fn type_ref(self, db: &impl PersistentHirDatabase) -> Arc<TypeRef> { | 670 | pub fn type_ref(self, db: &impl DefDatabase) -> Arc<TypeRef> { |
680 | db.type_alias_ref(self) | 671 | db.type_alias_ref(self) |
681 | } | 672 | } |
682 | 673 | ||