aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/descriptors/module
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-10-31 21:15:11 +0000
committerAleksey Kladov <[email protected]>2018-11-01 10:41:58 +0000
commit223fd2979c277cf402e4876d0e9a69257702f731 (patch)
treecf0dfb8abd3082f3a787a834184bb89b4d07f389 /crates/ra_analysis/src/descriptors/module
parentd685a9b564fe524865cda5713c527aaeb1ca6b1d (diff)
Introduce ModuleSource
Diffstat (limited to 'crates/ra_analysis/src/descriptors/module')
-rw-r--r--crates/ra_analysis/src/descriptors/module/imp.rs8
-rw-r--r--crates/ra_analysis/src/descriptors/module/mod.rs31
2 files changed, 32 insertions, 7 deletions
diff --git a/crates/ra_analysis/src/descriptors/module/imp.rs b/crates/ra_analysis/src/descriptors/module/imp.rs
index 1c102f4e5..22dbe7184 100644
--- a/crates/ra_analysis/src/descriptors/module/imp.rs
+++ b/crates/ra_analysis/src/descriptors/module/imp.rs
@@ -14,7 +14,9 @@ use crate::{
14 Cancelable, FileId, FileResolverImp, 14 Cancelable, FileId, FileResolverImp,
15}; 15};
16 16
17use super::{LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleTree, Problem}; 17use super::{
18 LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleSource, ModuleTree, Problem,
19};
18 20
19pub(crate) fn submodules( 21pub(crate) fn submodules(
20 db: &impl DescriptorDatabase, 22 db: &impl DescriptorDatabase,
@@ -43,7 +45,7 @@ pub(crate) fn module_scope(
43 module_id: ModuleId, 45 module_id: ModuleId,
44) -> Cancelable<Arc<ModuleScope>> { 46) -> Cancelable<Arc<ModuleScope>> {
45 let tree = db.module_tree(source_root_id)?; 47 let tree = db.module_tree(source_root_id)?;
46 let file_id = module_id.file_id(&tree); 48 let ModuleSource::File(file_id) = module_id.source(&tree);
47 let syntax = db.file_syntax(file_id); 49 let syntax = db.file_syntax(file_id);
48 let res = ModuleScope::new(&syntax); 50 let res = ModuleScope::new(&syntax);
49 Ok(Arc::new(res)) 51 Ok(Arc::new(res))
@@ -106,7 +108,7 @@ fn build_subtree(
106) -> Cancelable<ModuleId> { 108) -> Cancelable<ModuleId> {
107 visited.insert(file_id); 109 visited.insert(file_id);
108 let id = tree.push_mod(ModuleData { 110 let id = tree.push_mod(ModuleData {
109 file_id, 111 source: ModuleSource::File(file_id),
110 parent, 112 parent,
111 children: Vec::new(), 113 children: Vec::new(),
112 }); 114 });
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,