diff options
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 92 |
1 files changed, 84 insertions, 8 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 2944926e6..41d4e2ed3 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -54,10 +54,11 @@ pub struct CrateDependency { | |||
54 | 54 | ||
55 | impl Crate { | 55 | impl Crate { |
56 | pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> { | 56 | pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> { |
57 | db.crate_graph() | 57 | db.crate_graph()[self.id] |
58 | .dependencies(self.id) | 58 | .dependencies |
59 | .iter() | ||
59 | .map(|dep| { | 60 | .map(|dep| { |
60 | let krate = Crate { id: dep.crate_id() }; | 61 | let krate = Crate { id: dep.crate_id }; |
61 | let name = dep.as_name(); | 62 | let name = dep.as_name(); |
62 | CrateDependency { krate, name } | 63 | CrateDependency { krate, name } |
63 | }) | 64 | }) |
@@ -69,7 +70,9 @@ impl Crate { | |||
69 | let crate_graph = db.crate_graph(); | 70 | let crate_graph = db.crate_graph(); |
70 | crate_graph | 71 | crate_graph |
71 | .iter() | 72 | .iter() |
72 | .filter(|&krate| crate_graph.dependencies(krate).any(|it| it.crate_id == self.id)) | 73 | .filter(|&krate| { |
74 | crate_graph[krate].dependencies.iter().any(|it| it.crate_id == self.id) | ||
75 | }) | ||
73 | .map(|id| Crate { id }) | 76 | .map(|id| Crate { id }) |
74 | .collect() | 77 | .collect() |
75 | } | 78 | } |
@@ -80,12 +83,11 @@ impl Crate { | |||
80 | } | 83 | } |
81 | 84 | ||
82 | pub fn root_file(self, db: &impl DefDatabase) -> FileId { | 85 | pub fn root_file(self, db: &impl DefDatabase) -> FileId { |
83 | db.crate_graph().crate_root(self.id) | 86 | db.crate_graph()[self.id].root_file_id |
84 | } | 87 | } |
85 | 88 | ||
86 | pub fn edition(self, db: &impl DefDatabase) -> Edition { | 89 | pub fn edition(self, db: &impl DefDatabase) -> Edition { |
87 | let crate_graph = db.crate_graph(); | 90 | db.crate_graph()[self.id].edition |
88 | crate_graph.edition(self.id) | ||
89 | } | 91 | } |
90 | 92 | ||
91 | pub fn all(db: &impl DefDatabase) -> Vec<Crate> { | 93 | pub fn all(db: &impl DefDatabase) -> Vec<Crate> { |
@@ -204,10 +206,26 @@ impl Module { | |||
204 | } | 206 | } |
205 | 207 | ||
206 | /// Returns a `ModuleScope`: a set of items, visible in this module. | 208 | /// Returns a `ModuleScope`: a set of items, visible in this module. |
207 | pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef)> { | 209 | pub fn scope( |
210 | self, | ||
211 | db: &impl HirDatabase, | ||
212 | visible_from: Option<Module>, | ||
213 | ) -> Vec<(Name, ScopeDef)> { | ||
208 | db.crate_def_map(self.id.krate)[self.id.local_id] | 214 | db.crate_def_map(self.id.krate)[self.id.local_id] |
209 | .scope | 215 | .scope |
210 | .entries() | 216 | .entries() |
217 | .filter_map(|(name, def)| { | ||
218 | if let Some(m) = visible_from { | ||
219 | let filtered = def.filter_visibility(|vis| vis.is_visible_from(db, m.id)); | ||
220 | if filtered.is_none() && !def.is_none() { | ||
221 | None | ||
222 | } else { | ||
223 | Some((name, filtered)) | ||
224 | } | ||
225 | } else { | ||
226 | Some((name, def)) | ||
227 | } | ||
228 | }) | ||
211 | .map(|(name, def)| (name.clone(), def.into())) | 229 | .map(|(name, def)| (name.clone(), def.into())) |
212 | .collect() | 230 | .collect() |
213 | } | 231 | } |
@@ -480,6 +498,14 @@ impl Adt { | |||
480 | pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> { | 498 | pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> { |
481 | Some(self.module(db).krate()) | 499 | Some(self.module(db).krate()) |
482 | } | 500 | } |
501 | |||
502 | pub fn name(&self, db: &impl HirDatabase) -> Name { | ||
503 | match self { | ||
504 | Adt::Struct(s) => s.name(db), | ||
505 | Adt::Union(u) => u.name(db), | ||
506 | Adt::Enum(e) => e.name(db), | ||
507 | } | ||
508 | } | ||
483 | } | 509 | } |
484 | 510 | ||
485 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 511 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
@@ -507,6 +533,14 @@ impl VariantDef { | |||
507 | } | 533 | } |
508 | } | 534 | } |
509 | 535 | ||
536 | pub fn name(&self, db: &impl HirDatabase) -> Name { | ||
537 | match self { | ||
538 | VariantDef::Struct(s) => s.name(db), | ||
539 | VariantDef::Union(u) => u.name(db), | ||
540 | VariantDef::EnumVariant(e) => e.name(db), | ||
541 | } | ||
542 | } | ||
543 | |||
510 | pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { | 544 | pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { |
511 | match self { | 545 | match self { |
512 | VariantDef::Struct(it) => it.variant_data(db), | 546 | VariantDef::Struct(it) => it.variant_data(db), |
@@ -534,6 +568,14 @@ impl DefWithBody { | |||
534 | DefWithBody::Static(s) => s.module(db), | 568 | DefWithBody::Static(s) => s.module(db), |
535 | } | 569 | } |
536 | } | 570 | } |
571 | |||
572 | pub fn name(self, db: &impl HirDatabase) -> Option<Name> { | ||
573 | match self { | ||
574 | DefWithBody::Function(f) => Some(f.name(db)), | ||
575 | DefWithBody::Static(s) => s.name(db), | ||
576 | DefWithBody::Const(c) => c.name(db), | ||
577 | } | ||
578 | } | ||
537 | } | 579 | } |
538 | 580 | ||
539 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 581 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -571,6 +613,14 @@ impl Function { | |||
571 | } | 613 | } |
572 | } | 614 | } |
573 | 615 | ||
616 | impl HasVisibility for Function { | ||
617 | fn visibility(&self, db: &impl HirDatabase) -> Visibility { | ||
618 | let function_data = db.function_data(self.id); | ||
619 | let visibility = &function_data.visibility; | ||
620 | visibility.resolve(db, &self.id.resolver(db)) | ||
621 | } | ||
622 | } | ||
623 | |||
574 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 624 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
575 | pub struct Const { | 625 | pub struct Const { |
576 | pub(crate) id: ConstId, | 626 | pub(crate) id: ConstId, |
@@ -590,6 +640,14 @@ impl Const { | |||
590 | } | 640 | } |
591 | } | 641 | } |
592 | 642 | ||
643 | impl HasVisibility for Const { | ||
644 | fn visibility(&self, db: &impl HirDatabase) -> Visibility { | ||
645 | let function_data = db.const_data(self.id); | ||
646 | let visibility = &function_data.visibility; | ||
647 | visibility.resolve(db, &self.id.resolver(db)) | ||
648 | } | ||
649 | } | ||
650 | |||
593 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 651 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
594 | pub struct Static { | 652 | pub struct Static { |
595 | pub(crate) id: StaticId, | 653 | pub(crate) id: StaticId, |
@@ -664,6 +722,14 @@ impl TypeAlias { | |||
664 | } | 722 | } |
665 | } | 723 | } |
666 | 724 | ||
725 | impl HasVisibility for TypeAlias { | ||
726 | fn visibility(&self, db: &impl HirDatabase) -> Visibility { | ||
727 | let function_data = db.type_alias_data(self.id); | ||
728 | let visibility = &function_data.visibility; | ||
729 | visibility.resolve(db, &self.id.resolver(db)) | ||
730 | } | ||
731 | } | ||
732 | |||
667 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 733 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
668 | pub struct MacroDef { | 734 | pub struct MacroDef { |
669 | pub(crate) id: MacroDefId, | 735 | pub(crate) id: MacroDefId, |
@@ -751,6 +817,16 @@ impl AssocItem { | |||
751 | } | 817 | } |
752 | } | 818 | } |
753 | 819 | ||
820 | impl HasVisibility for AssocItem { | ||
821 | fn visibility(&self, db: &impl HirDatabase) -> Visibility { | ||
822 | match self { | ||
823 | AssocItem::Function(f) => f.visibility(db), | ||
824 | AssocItem::Const(c) => c.visibility(db), | ||
825 | AssocItem::TypeAlias(t) => t.visibility(db), | ||
826 | } | ||
827 | } | ||
828 | } | ||
829 | |||
754 | #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] | 830 | #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] |
755 | pub enum GenericDef { | 831 | pub enum GenericDef { |
756 | Function(Function), | 832 | Function(Function), |