diff options
Diffstat (limited to 'crates/ra_hir/src/module/mod.rs')
-rw-r--r-- | crates/ra_hir/src/module/mod.rs | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/crates/ra_hir/src/module/mod.rs b/crates/ra_hir/src/module/mod.rs index 81b9f948d..a011fd53e 100644 --- a/crates/ra_hir/src/module/mod.rs +++ b/crates/ra_hir/src/module/mod.rs | |||
@@ -18,15 +18,16 @@ use crate::{ | |||
18 | arena::{Arena, Id}, | 18 | arena::{Arena, Id}, |
19 | }; | 19 | }; |
20 | 20 | ||
21 | pub(crate) use self::nameres::ModuleScope; | 21 | pub use self::nameres::ModuleScope; |
22 | 22 | ||
23 | /// `Module` is API entry point to get all the information | 23 | /// `Module` is API entry point to get all the information |
24 | /// about a particular module. | 24 | /// about a particular module. |
25 | #[derive(Debug, Clone)] | 25 | #[derive(Debug, Clone)] |
26 | pub(crate) struct Module { | 26 | pub struct Module { |
27 | tree: Arc<ModuleTree>, | 27 | tree: Arc<ModuleTree>, |
28 | source_root_id: SourceRootId, | 28 | source_root_id: SourceRootId, |
29 | module_id: ModuleId, | 29 | //TODO: make private |
30 | pub module_id: ModuleId, | ||
30 | } | 31 | } |
31 | 32 | ||
32 | impl Module { | 33 | impl Module { |
@@ -145,17 +146,13 @@ impl Module { | |||
145 | } | 146 | } |
146 | 147 | ||
147 | /// Returns a `ModuleScope`: a set of items, visible in this module. | 148 | /// Returns a `ModuleScope`: a set of items, visible in this module. |
148 | pub(crate) fn scope(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> { | 149 | pub fn scope(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> { |
149 | let item_map = db.item_map(self.source_root_id)?; | 150 | let item_map = db.item_map(self.source_root_id)?; |
150 | let res = item_map.per_module[&self.module_id].clone(); | 151 | let res = item_map.per_module[&self.module_id].clone(); |
151 | Ok(res) | 152 | Ok(res) |
152 | } | 153 | } |
153 | 154 | ||
154 | pub(crate) fn resolve_path( | 155 | pub fn resolve_path(&self, db: &impl HirDatabase, path: Path) -> Cancelable<Option<DefId>> { |
155 | &self, | ||
156 | db: &impl HirDatabase, | ||
157 | path: Path, | ||
158 | ) -> Cancelable<Option<DefId>> { | ||
159 | let mut curr = match path.kind { | 156 | let mut curr = match path.kind { |
160 | PathKind::Crate => self.crate_root(), | 157 | PathKind::Crate => self.crate_root(), |
161 | PathKind::Self_ | PathKind::Plain => self.clone(), | 158 | PathKind::Self_ | PathKind::Plain => self.clone(), |
@@ -188,7 +185,7 @@ impl Module { | |||
188 | /// (which can have multiple parents) to the precise world of modules (which | 185 | /// (which can have multiple parents) to the precise world of modules (which |
189 | /// always have one parent). | 186 | /// always have one parent). |
190 | #[derive(Default, Debug, PartialEq, Eq)] | 187 | #[derive(Default, Debug, PartialEq, Eq)] |
191 | pub(crate) struct ModuleTree { | 188 | pub struct ModuleTree { |
192 | mods: Arena<ModuleData>, | 189 | mods: Arena<ModuleData>, |
193 | links: Arena<LinkData>, | 190 | links: Arena<LinkData>, |
194 | } | 191 | } |
@@ -214,19 +211,19 @@ impl ModuleTree { | |||
214 | /// `ModuleSource` is the syntax tree element that produced this module: | 211 | /// `ModuleSource` is the syntax tree element that produced this module: |
215 | /// either a file, or an inlinde module. | 212 | /// either a file, or an inlinde module. |
216 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 213 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
217 | pub(crate) enum ModuleSource { | 214 | pub enum ModuleSource { |
218 | SourceFile(FileId), | 215 | SourceFile(FileId), |
219 | Module(SourceItemId), | 216 | Module(SourceItemId), |
220 | } | 217 | } |
221 | 218 | ||
222 | /// An owned syntax node for a module. Unlike `ModuleSource`, | 219 | /// An owned syntax node for a module. Unlike `ModuleSource`, |
223 | /// this holds onto the AST for the whole file. | 220 | /// this holds onto the AST for the whole file. |
224 | pub(crate) enum ModuleSourceNode { | 221 | pub enum ModuleSourceNode { |
225 | SourceFile(ast::SourceFileNode), | 222 | SourceFile(ast::SourceFileNode), |
226 | Module(ast::ModuleNode), | 223 | Module(ast::ModuleNode), |
227 | } | 224 | } |
228 | 225 | ||
229 | pub(crate) type ModuleId = Id<ModuleData>; | 226 | pub type ModuleId = Id<ModuleData>; |
230 | type LinkId = Id<LinkData>; | 227 | type LinkId = Id<LinkData>; |
231 | 228 | ||
232 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] | 229 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] |
@@ -308,7 +305,7 @@ impl LinkId { | |||
308 | } | 305 | } |
309 | 306 | ||
310 | #[derive(Debug, PartialEq, Eq, Hash)] | 307 | #[derive(Debug, PartialEq, Eq, Hash)] |
311 | pub(crate) struct ModuleData { | 308 | pub struct ModuleData { |
312 | source: ModuleSource, | 309 | source: ModuleSource, |
313 | parent: Option<LinkId>, | 310 | parent: Option<LinkId>, |
314 | children: Vec<LinkId>, | 311 | children: Vec<LinkId>, |
@@ -327,21 +324,21 @@ impl ModuleSource { | |||
327 | ModuleSource::Module(id) | 324 | ModuleSource::Module(id) |
328 | } | 325 | } |
329 | 326 | ||
330 | pub(crate) fn as_file(self) -> Option<FileId> { | 327 | pub fn as_file(self) -> Option<FileId> { |
331 | match self { | 328 | match self { |
332 | ModuleSource::SourceFile(f) => Some(f), | 329 | ModuleSource::SourceFile(f) => Some(f), |
333 | ModuleSource::Module(..) => None, | 330 | ModuleSource::Module(..) => None, |
334 | } | 331 | } |
335 | } | 332 | } |
336 | 333 | ||
337 | pub(crate) fn file_id(self) -> FileId { | 334 | pub fn file_id(self) -> FileId { |
338 | match self { | 335 | match self { |
339 | ModuleSource::SourceFile(f) => f, | 336 | ModuleSource::SourceFile(f) => f, |
340 | ModuleSource::Module(source_item_id) => source_item_id.file_id, | 337 | ModuleSource::Module(source_item_id) => source_item_id.file_id, |
341 | } | 338 | } |
342 | } | 339 | } |
343 | 340 | ||
344 | pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { | 341 | pub fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { |
345 | match self { | 342 | match self { |
346 | ModuleSource::SourceFile(file_id) => { | 343 | ModuleSource::SourceFile(file_id) => { |
347 | let syntax = db.source_file(file_id); | 344 | let syntax = db.source_file(file_id); |