aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/descriptors/module/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/descriptors/module/mod.rs')
-rw-r--r--crates/ra_analysis/src/descriptors/module/mod.rs46
1 files changed, 40 insertions, 6 deletions
diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs
index a6eaec178..78911d5d9 100644
--- a/crates/ra_analysis/src/descriptors/module/mod.rs
+++ b/crates/ra_analysis/src/descriptors/module/mod.rs
@@ -17,6 +17,7 @@ use crate::{
17 descriptors::{Path, PathKind, DescriptorDatabase}, 17 descriptors::{Path, PathKind, DescriptorDatabase},
18 input::SourceRootId, 18 input::SourceRootId,
19 arena::{Arena, Id}, 19 arena::{Arena, Id},
20 loc2id::{DefLoc, DefId},
20}; 21};
21 22
22pub(crate) use self::nameres::ModuleScope; 23pub(crate) use self::nameres::ModuleScope;
@@ -76,6 +77,20 @@ impl ModuleDescriptor {
76 Ok(res) 77 Ok(res)
77 } 78 }
78 79
80 pub(super) fn new(
81 db: &impl DescriptorDatabase,
82 source_root_id: SourceRootId,
83 module_id: ModuleId,
84 ) -> Cancelable<ModuleDescriptor> {
85 let module_tree = db._module_tree(source_root_id)?;
86 let res = ModuleDescriptor {
87 tree: module_tree,
88 source_root_id,
89 module_id,
90 };
91 Ok(res)
92 }
93
79 /// Returns `mod foo;` or `mod foo {}` node whihc declared this module. 94 /// Returns `mod foo;` or `mod foo {}` node whihc declared this module.
80 /// Returns `None` for the root module 95 /// Returns `None` for the root module
81 pub fn parent_link_source( 96 pub fn parent_link_source(
@@ -117,6 +132,14 @@ impl ModuleDescriptor {
117 Some(link.name(&self.tree)) 132 Some(link.name(&self.tree))
118 } 133 }
119 134
135 pub fn def_id(&self, db: &impl DescriptorDatabase) -> DefId {
136 let def_loc = DefLoc::Module {
137 id: self.module_id,
138 source_root: self.source_root_id,
139 };
140 db.id_maps().def_id(def_loc)
141 }
142
120 /// Finds a child module with the specified name. 143 /// Finds a child module with the specified name.
121 pub fn child(&self, name: &str) -> Option<ModuleDescriptor> { 144 pub fn child(&self, name: &str) -> Option<ModuleDescriptor> {
122 let child_id = self.module_id.child(&self.tree, name)?; 145 let child_id = self.module_id.child(&self.tree, name)?;
@@ -133,17 +156,28 @@ impl ModuleDescriptor {
133 Ok(res) 156 Ok(res)
134 } 157 }
135 158
136 pub(crate) fn resolve_path(&self, path: Path) -> Option<ModuleDescriptor> { 159 pub(crate) fn resolve_path(
160 &self,
161 db: &impl DescriptorDatabase,
162 path: Path,
163 ) -> Cancelable<Option<DefId>> {
137 let mut curr = match path.kind { 164 let mut curr = match path.kind {
138 PathKind::Crate => self.crate_root(), 165 PathKind::Crate => self.crate_root(),
139 PathKind::Self_ | PathKind::Plain => self.clone(), 166 PathKind::Self_ | PathKind::Plain => self.clone(),
140 PathKind::Super => self.parent()?, 167 PathKind::Super => ctry!(self.parent()),
141 }; 168 }
169 .def_id(db);
170
142 let segments = path.segments; 171 let segments = path.segments;
143 for name in segments { 172 for name in segments.iter() {
144 curr = curr.child(&name)?; 173 let module = match db.id_maps().def_loc(curr) {
174 DefLoc::Module { id, source_root } => ModuleDescriptor::new(db, source_root, id)?,
175 _ => return Ok(None),
176 };
177 let scope = module.scope(db)?;
178 curr = ctry!(ctry!(scope.get(&name)).def_id);
145 } 179 }
146 Some(curr) 180 Ok(Some(curr))
147 } 181 }
148 182
149 pub fn problems(&self, db: &impl DescriptorDatabase) -> Vec<(SyntaxNode, Problem)> { 183 pub fn problems(&self, db: &impl DescriptorDatabase) -> Vec<(SyntaxNode, Problem)> {