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.rs31
1 files changed, 27 insertions, 4 deletions
diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs
index 302e3e81c..cbccdb2e2 100644
--- a/crates/ra_analysis/src/descriptors/module/mod.rs
+++ b/crates/ra_analysis/src/descriptors/module/mod.rs
@@ -11,6 +11,13 @@ use crate::FileId;
11 11
12pub(crate) use self::scope::ModuleScope; 12pub(crate) use self::scope::ModuleScope;
13 13
14/// Phisically, rust source is organized as a set of files, but logically it is
15/// organized as a tree of modules. Usually, a single file corresponds to a
16/// single module, but it is not nessary the case.
17///
18/// Module encapsulate the logic of transitioning from the fuzzy world of files
19/// (which can have multiple parents) to the precise world of modules (which
20/// always have one parent).
14#[derive(Debug, PartialEq, Eq, Hash)] 21#[derive(Debug, PartialEq, Eq, Hash)]
15pub(crate) struct ModuleTree { 22pub(crate) struct ModuleTree {
16 mods: Vec<ModuleData>, 23 mods: Vec<ModuleData>,
@@ -22,7 +29,7 @@ impl ModuleTree {
22 self.mods 29 self.mods
23 .iter() 30 .iter()
24 .enumerate() 31 .enumerate()
25 .filter(|(_idx, it)| it.file_id == file_id) 32 .filter(|(_idx, it)| it.source.is_file(file_id))
26 .map(|(idx, _)| ModuleId(idx as u32)) 33 .map(|(idx, _)| ModuleId(idx as u32))
27 .collect() 34 .collect()
28 } 35 }
@@ -50,8 +57,8 @@ pub enum Problem {
50} 57}
51 58
52impl ModuleId { 59impl ModuleId {
53 pub(crate) fn file_id(self, tree: &ModuleTree) -> FileId { 60 pub(crate) fn source(self, tree: &ModuleTree) -> ModuleSource {
54 tree.module(self).file_id 61 tree.module(self).source
55 } 62 }
56 pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> { 63 pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
57 tree.module(self).parent 64 tree.module(self).parent
@@ -110,11 +117,27 @@ impl LinkId {
110 117
111#[derive(Debug, PartialEq, Eq, Hash)] 118#[derive(Debug, PartialEq, Eq, Hash)]
112struct ModuleData { 119struct ModuleData {
113 file_id: FileId, 120 source: ModuleSource,
114 parent: Option<LinkId>, 121 parent: Option<LinkId>,
115 children: Vec<LinkId>, 122 children: Vec<LinkId>,
116} 123}
117 124
125/// `ModuleSource` is the syntax tree element that produced this module:
126/// either a file, or an inlinde module.
127#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
128pub(crate) enum ModuleSource {
129 File(FileId),
130 // Inline(SyntaxPtr),
131}
132
133impl ModuleSource {
134 fn is_file(self, file_id: FileId) -> bool {
135 match self {
136 ModuleSource::File(f) => f == file_id,
137 }
138 }
139}
140
118#[derive(Hash, Debug, PartialEq, Eq)] 141#[derive(Hash, Debug, PartialEq, Eq)]
119struct LinkData { 142struct LinkData {
120 owner: ModuleId, 143 owner: ModuleId,